This tutorial demonstrates how to connect a DHT22 temperature and humidity sensor to an ESP32 running MicroPython. The sensor data is transmitted wirelessly to a Raspberry Pi using MQTT protocol and the results are displayed on I2C OLED display.
This tutorial builds on my last tutorial which shows how to load the MicroPython firmware on to an ESP32 and use Rshell to manipulate files and run python code in the REPL. I recommend you check it out first.
There’s been 1 breaking change since the last video with respect to loading the firmware which I’d like to address. It’s now necessary to specify hex address 0x1000 (instead of zero) when writing the MicroPython firmware to the ESP32.
MQTT is a very light weight connectivity protocol. It is very popular with IOT devices because it requires much less code and memory compared to other approaches such as a REST API.
It uses a publish subscribe model. A single broker acts as a server and manages messages among multiple clients. A client can publish a message to a topic and any client that subscribes to the topic will get the message.
For this video an ESP32 client connected to a DHT22 temperature and humidity sensor will publish the sensor data to a topic called temp humidity. A Raspberry Pi will be the broker and it will also be a client. A Python MQTT client running on the Pi will subscribe to the temp_humidity topic and show the results on an OLED display.
Topics are usually organized in a hierarchical order using slashes like the file system on a computer. They are then referred to as topic trees and it is possible to reference them with wild cards. + for single level and # for multilevel. For example you could have topics called sensors/temperature/attic and sensors/temperature/workshop. You could subscribe to both by using sensors/temperature/#. For this example there will be just 1 topic: temp_humidity.
Again this is a very simple example. You could have multiple ESP32’s publishing and subscribing to topics with all types of sensors such as solar power tracking, water pressure, motion detection, GPS, beacons, etc. And it doesn’t have to be limited to sensors, you could fire relays, activate home automation macros, run programs, etc.
Mosquitto will be used for the server. It is an open source message broker that implements the MQTT protocol. It can be installed on the Raspberry Pi using apt-get install. Mosquitto clients is also installed. It is a command line MQTT client that is helpful for debugging.
In addition to the Pi we’ll need a MicroPython MQTT client for the ESP32. A good one is umqtt.simple which is available from the Micropython-lib repository. After downloading the simple.py file, copy it into a folder called umqtt on the ESP32 using rshell.
The DHT-22 temperature/humidity sensor is easy to connect to the ESP32. Pin 1 Vcc on the DHT22 is connected to a 3.3V pin on the ESP32. Pin 2, the DHT-22 data line is connected to GPIO15. You can use any available GPIO pin because it will be specified int he code. It’s a good practice in terms of reliability to keep the data wire short, preferably under 15 cm. Pin 3 is left disconnected and pin 4 ground is connected to a ground on the ESP32. Normally you would connect a pull-up resistor between the data line and the 3.3V line, but it’s not necessary because the ESP32 GPIO pins have internal pull-ups that can be turned on from code.
Here is the MQTT client code that will run on the ESP32. It polls the DHT-22 and publishes the sensor data to a topic called temp_humidity.
Save the python to a file called dht_publish.py. Copy the file to the ESP32 root directory using rshell. Then start the MicroPython REPL.
Before running the client publishing code, it’s necessary to connect the ESP32 to a WiFi network.
Import dht_publish runs the client code.
The ESP32 should now be polling the DHT-22 and publishing the temperature and humidity data to the MQTT broker. We can test that data is actually be published by using the Mosquitto command line client to subscribe to the topic temp_humidity.
Ctrl-C is used to exit the Mosquitto client. The command line is great for debugging, but now let’s create a python client program that’s a little more useful. Install the Paho MQTT Python Client library using pip.
The python client will run on the same Rasbperry Pi that is also serving as the Mosquito MQTT broker and display the temperature and humidity readings on an SSD1306 OLED display. Adafruit provides a simple SSD1306 python library for driving OLED displays which can also be installed using pip.
The I2C SSD1306 OLED display is very easy to connect to the Pi. The display’s SDA pin is connected to the Pi’s SDA pin which is GPIO 2. The display’s SCL pin is connected the Pi’s SCL which is GPIO 3. The Vcc pin is connected to a 3.3V pin on the Pi and the grounds are connected.
Here is the Raspberry Pi Python MQTT client code that will subscribe to the temp_humidity topic and present the results to an OLED display.
A pic of the temperature and humidity being displayed.
If you having connection issues with the MicroPython Lib MQTT client, I recommend you take a look at the LoBo build of MicroPython which comes with an MQTT client that is more robust. It has an autoreconnect feature and it also allows you to poll the MQTT state to accommodate Wi-Fi disruptions. Here is an example:
There is more information on the LoBo build in Part 6 of this tutorial.
Part 3 of this tutorial demonstrates setting up a web server on an ESP32.