In a previous project, ESP-32 Temperature Reporting, I set up an ESP-32 microcontroller to capture temperature values and publish as MQTT messages. Using an MQTT broker (server software running on a Raspberry Pi) to capture and relay the messages which were then integrated with Home Assistant running on a local server. Home Assistant allows me to monitor such sensors and set up automations to take action.

Several months ago, given that summer was upon us and temperatures rising, I wanted to quickly set up monitoring of the ambient temperature in my loft room, as I run some network equipment in there and I wanted to make sure it wasn't getting too hot.

Implementation

Using an ESP-01S with a DHT11 temperature and humidity module that I had lying around seemed like an easy way to achieve this goal. The main issues were,

  1. Power: although ESP-01S usually takes 3.3V, this module takes 5V. There is no standard connector on the module
  2. This is temporary: I want to make a nicer version of this later so I want to use socketed components where possible. Dupont wires are acceptable because this room is mostly used for storage and it is unlikey that the circuit will be touched or moved.

Using stripboard, some SIL header sockets and a USB micro B breakout board I connected the simple circuit below.


Left: Stripboard and USB breakout, Right: ESP-01S and DHT11 module

Originally, I envisaged a version where the ESP-01S would be socketed on the stripboard in order to keep it away from the temperature module but that would need more wires and a lot more work on the stripboard.

Code

Using similar code to my previous ESP-32 project should have worked, however, occasionally the sensor went offline and didn't come back up. The problem was that if there were any fluctuations with power, the WiFi connection or the network connection with the MQTT broker, then the sensor would not reconnect. I expected this could happen but not as often as it happened in reality.

My code is based on the DHT example from Adafruit. The key changes required were to set up callbacks for disconnected WiFi and support for MQTT including callbacks for disconnected MQTT.

Source code is on github. Note: general.h must be edited to set the network configuration.

In this version, when WiFi is connected it automatically connects to the MQTT server in the onWifiConnect() callback. If WiFi connection is lost then in the onWifiDisconnect() callback it will re-connect WiFi which in turn will connect MQTT.

If MQTT connection is lost it will try to re-connect only if WiFi is up. If WiFi is down it will re-connect automatically and in turn, connect MQTT.

This version of code is much nicer and reconnects when necessary.

Integration

Following my previous project, I set up the integration with Home Assistant and added a gauge widget to my Homelab dashboard. This is a nice widget that is configurable to set ranges for when the gauge should be green, orange or red. Clicking on the widget opens a graph of historical data.


Home Assistant Temperature Widget

< Back