Home automation with Arduino and Home Assistant - Thomas More

Explore how Jonas Claes builds a home automation controller using Arduino Mega, ESP32, and Home Assistant, focusing on Ethernet for reliability and security, and creating a modular codebase in C++ for different devices​.

Home automation with Arduino and Home Assistant - Thomas More
Thomas More

Home automation with Arduino and Home Assistant

The purpose of this project is to build a home automation controller using small microcontrollers. This should be easy to program, should work when there is no internet or certain services do not work and should be able to communicate with other devices.

So the choice of Home Assistant speaks for itself. At the time of writing, this is 1 of the most popular home automation platforms with thousands of open-source developers working on it.

For the hardware side, we use an Arduino Mega and an Espressif ESP32, among others. The Arduino Mega does not have onboard Wi-Fi, so we are using an ENC28J60, which is an Ethernet controller. The ESP32 does have onboard Wi-Fi, but again we are using an ENC28J60 because the ESP32 only has 2.4GHz Wi-Fi. This in itself is not a big deal, but when building out a smarthome, it is still always wise to connect as few devices via Wi-Fi as possible because more devices equals slower speed. In addition, Ethernet is more reliable and predominantly more secure.

Implementation

I got to work with the Arduino Mega by setting up a development environment in Platform.io, which is an extension for Visual Studio Code that allows you to easily write C++ code for microcontrollers. With this environment, we can also easily set up different build targets, with which we can basically compile the same code for an ESP32 or other microcontrollers. This way you can design a fairly simple and modular codebase that can run on different devices.

After all this was done I started working on the physical hardware. I built everything with C++, working object-oriented as much as possible. That means that, for example, a switch is one class and a push button is another class. With this principle I made it possible to define an output with a class, which takes as inputs multiple class instances, this way you can easily reuse and program switches.

After this was finished, I started writing the network functionality. For the Arduino Mega this was quite easy, but initially the ESP32 does not support ENC28J60. For this I searched for a library that could, and after much searching I actually found one. The README stated that this was not possible, but with a small modification to the library I made it possible anyway.

Once I had the network up and running I started building the MQTT connection. This went very smoothly with the PubSubClient library and the ENC28J60 network controller.

Jochen's request was also to run this on an Arduino Uno. Unfortunately, it has too little memory to support all these functions and thus will not be within the scope of this project.