As I got further into the Smart Home game, I encountered a type of device I’ve never thought of using: microcontrollers. Reading into the possibilities I recognized these little things as a power and cost efficient way of handling different (light) tasks. Because I’ve already had a DHT22 lying around, which was used with a Raspberry Pi, I decided to purchase a microcontroller to build a little DIY thermometer leveraging MQTT. What better device is there, than (arguably) the king of it all: the ESP32. I decided on the Huzzah32 from Adafruit but regretted not getting a Sparkfun Thing as it seems to be to be better documented and, for reasons I don’t want to get into, I am not a big fan of Adafruit. Also Adafruit gimped the deep sleep possibilities, as the energy consumption is a lot higher with their ESP32 compared to other ESP32s. This post describes a DIY sensor with ESP32 and DHT22.
If you’re interested in recreating this, but you’re not sure where to start, I can highly recommend the ESP32 and Arduino playlist from Simply Explained, from which I will reference single videos in the course of this post.
How does it work?
When connected to a power source, the ESP32 turns on, connects to the WLAN specified and retrieves the temperature and humidity levels from the DHT22 sensor, which is attached by the GPIO pins. After receiving the readings from the sensor it crafts a InfluxDB-valid MQTT message and sends it to the MQTT server (mosquitto). Finally it goes to deep sleep, to save energy until it wakes up after the specified amount of time to repeat the process.
As MQTT is the defacto standard for Home Automation, it’s easy to subscribe to the topic and retrieve the sensor readings in most tools of your choice.
This project was mostly a proof-of-concept and a neat opportunity for me to learn about microcontrollers and development with those type of electronics. In the beginning I thought about crafting a few of those pairs, adding a USB powerbank to make it mobile and distribute them around my apartement to have temperature and humidty readings in every room. This turned out to be inefficient and more complex than anticipated. I go more in detail on this in chapter Issues and why I dismantled it again down below.
Connecting the ESP32 and DHT22
If you’re getting another ESP32 board, make sure to check it’s pinout diagram to connect the right pins to the DHT 22. For my Huzzah32 I connected the 3 pins of the DHT22 this way:
|+ pin (yellow)||3V pin|
|OUT pin (green)||GPIO pin 21|
|– pin (blue)||GND pin|
There are also DHT22 with 4 pins which are probably connected differently. Make sure to have a look at the pinout diagram of your microcontroller. The 3 volt pin and the ground pin are probably always very clear, but the data pin has to be fitted accordingly. I chose pin 21.
A great video on connecting ESP32 + DHT22 I can recommend is from the playlist of Simply Explained mentioned earlier in this post.
Creating the code for ESP32
As there are some great libraries to work with the DHT22 from Adafruit, I decided on using their libraries for this project. Doing some research before starting my first step into microcontroller-land, I came across a great framework to build applications and manage dependencies: platformIO. With an extensive Visual Studio Code addon it kickstarts development and supported me a great deal in my endeavour. There are also some (more than the linked one here) videos in the playlist from Simply Explained to install and work with platformIO.
The source code itself is not very complex and speaks mostly for itself. It’s necessary to define the DHT22 sensor, wifi and the mqtt broker. Because I wanted the ESP32 to go to deep sleep (to use less energy when not reporting data) the loop-function is not used. After waking from deep sleep the microcontroller will execute the setup-function again and lastly go back to deep sleep. There are also some great videos about deep sleep and various other power modes in the linked Youtube playlist.
The MQTT message payload is formatted like follows and is sent to a a topic called home/sensors.
I have uploaded my code (the entire platformIO project) to Github which can be found here:
Reading from MQTT
As the data is sent to a MQTT broker, many tools are able to read from MQTT to process this data. I have set up Telegraf which subscribes to the topic specified and writes the data to InfluxDB. I wrote a post about this topic some time ago, which was done for this exact use case, you can find it here. This is also the reason I’ve created the MQTT message payload as mentioned above, so that it can easily be written to InfluxDB.
Integration in Home Assistant
Having the data in InfluxDB allows for more integration with different applications. It’s possible to get the data straight from MQTT in Home Assistant, however I wanted to have the ability to persist data and to be able to make some historical analysis on the data in the future. For this reason I chose to write the data to InfluxDB and use the integration in Home Assistant to display this data from InfluxDB. I have explained this in more detail in a recent post here.
Issues and why I dismantled it again
So this looks all fine and amazing, right? No, unfortunately not. My idea was to attach some USB-powerbank to it and have it as a mobile sensor, which is further improved with the deep sleep mode, in which the ESP32 pulls a lot less power. However I found that most powerbanks actually have a safety switch which stops the powerbank from sending power when the receiving device is “gone”. Entering deep sleep, the power draw of the ESP32 is so low, that it actually triggers the powerbank to stop. So I noticed, as soon as the ESP32 goes into deep sleep, it will never wake up again, when connected to a powerbank, if there is no trigger (e.g. hitting the “start” button on the powerbank). Granted, there are LiPo batteries which can be attached to the ESP32 and will probably diminish this issue, but I simply didn’t want to invest more.
Especially not, when I stumbled across cheap Zigbee sensors, which last for a long time, are cheap and even look much prettier than the ESP32 (ok, debateable). So I got a Electrolama Zzh! USB dongle, installed Zigbee2Mqtt and bought some Sonoff SNZB-02 sensors. Integration in Home Assistant is fairly easy, they look good and work fine (although they report temperature at lower intervals, sometimes up to an hour).