First project Homie ESP8266: publish in MQTT measurements of a DHT22 probe

Previously, we discovered the Homie library which simplifies the writing of Arduino code to develop connected objects ESP8266 which communicate by MQTT messages. In this article we will discuss in more detail how to use the library in an Arduino program. The library offers a very large number of functions which allow to personalize its code to the maximum. This is probably the most powerful library to write programs based on MQTT. Here, we will address only the essential functions that allow to write a program with the minimum of code. As it is always easier to start from an example, we will make a small temperature probe based on a DHT22.

Hardware used for this tutorial

esp8266 Wemos D1 mini Wemos D1 Mini
chargeur raspbery pi 3 5v 3000ma 5/3A micro-usb power supply


wemos d1 mini dht22 shield Shield DHT22 or DHT11 for Wemos D1 Mini

Probe wired on pin D4 (GPIO2)

Structure of a Homie program for ESP8266

Homie is a library that simplifies the writing of Arduino programs. It supports the MQTT communication protocol presented here. Homie supports the following technical aspects:

  • Connecting to the Wi-Fi network and the MQTT server
  • Homie is able to manage the re-connection to the Wi-Fi network / MQTT server
  • The structuring of MQTT messages
  • Wireless update by MQTT message

The minimum program

Here is the minimal program to run Homie.

The library must be included at the beginning of the program.

Then in the setup () loop of the Arduino code:

  • Since version 2, it is necessary to explicitly start the serial monitor with the function Serial.begin(115200) . Homie integrates a message handler (logger) that we will see later in the article
  • The Homie.setup () method initializes the library. Several parameters can be specified before calling this method. We will see them in detail in the next paragraph. The only required parameter and firmware name and version. In other words, the name of your program and its version number.

Finally, in the loop () of the Arduino code, the treatments are updated with the Homie.loop()  method.

Now we will add a DHT22 temperature probe and publish the temperature and humidity measurements.

Add a node (Node)

Each event (pressing a switch, a button, a limit sensor …) or measurement (temperature, humidity, CO2 rate, amount of particles in the air …) is called a Node. To define a Node, use the HomieNode method (documentation). All Nodes must be defined at the beginning of the program (before the Arduino code setup).

The constructor needs the following parameters:

  • Id: Node ID
  • Type: Node type. There is no imposed type
  • Handler (optional)

Which gives for a DHT22 which publishes two measures

Publish Data – MQTT Messages

To publish the measurements regularly, we will simply test a time interval. The Homie library does not use a Timer (the SimpleTimer library for example). Homie will be given the Handler function that it will execute on each cycle. The Handler is indicated at the time of the Arduino setup

Now, just test the elapsed time between two passes in the function.

To publish measurements, you must use the setProperty method (“generic_name”). send (value)

Structure of MQTT Homie messages

It’s time to look at what Homie’s messages will look like. In general, it takes the following form:

homie / device name / node name / property name value

Or more formally

homie / Device ID / Node ID / Property value

The homie prefix and the Device ID are defined at the time of configuration (see previous article)

The Node ID is assigned by the HomieNode method

Finally the property is assigned at the time of publication by the method setProperty ()

Here’s what we’ll get for example

Publishing data in JSON format

If you want to retrieve the data in JSON format, there is no function offered by Homie that allows to group and structure the data in JSON format. This does not pose any problem since it is enough to build its own chain. You can also use the ArduinoJSON library presented in this tutorial. Beware, however, of performance. Many treatments are performed asynchronously by the library. If the measurement frequency is too fast, sending MQTT messages may be slowed.

Here is a small piece of code that sends the temperature and the unit of measure to the JSON format

And this is what we get by subscribing to the topic homie/#  with the following command

If you have set up an authentication, complete and run this command

Full Project Code : Homie + DHT22

Here is the complete code of the project that you simply have to reverse on the ESP8266. Do not forget to change the pin on which the DHT22 is connected. Here it is plugged into the D4 pin because I used a shield DHT22 for Wemos d1 mini.

Here you can already realize a large number of projects with this code base.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.


DIY Projects