ESP8266 + DHT22 + MQTT: make a connected object IoT and include it in Home Assistant

ESP8266 + DHT22 + MQTT, here’s how we will learn how to make our first connected object (a temperature probe) and connect it to the Home Assistant server. To realize this connected object we will need a WiFi module ESP8266, a DHT22 temperature and humidity sensor and an LED (to simulate the control of a lamp). For the home automation part, we will use the Home Assistant software.

Configuration required

You will need a computer with the following software installed:

  • A Broker (server) MQTT. I recommend Mosquitto already presented in this article
  • A home automation server. The easiest thing if you’re new to home automation and that English does not put you off too much, is to install Home-Assistant described in this article.

To make this article, I used a Raspberry Pi 3 with an active SSH connection (if you need to know more about SSH, read this article first).

Necessary material

To complete this project, you will need the following equipment

Last update was on: 21 January 2018 5 h 05 min

Circuit

The Wemos D1 mini is cable like a classic Arduino. When the Wemos D1 Mini is connected to the USB port of the computer, the power supply is recovered on the Pin + 5V. To operate the Wemos on battery LiPo or batteries it will suffice to connect to the Pin 5V and G.

In the program, the DHT22 is connected to the Pin D4, the Led on the Pin D2.

IOT object connecté domotique home-assistant ESP8266+DHT22+MQTT_bb

 

Code

The ESP8266 (ESP-12) can be programmed in Lua or using the Arduino IDE. Once again it’s about taste. I have a preference for IDE by habit but especially ESP8266Wifi and PubSub libraries really simplify the lives of developers.

To publish the measurements on the MQTT Mosquitto Broket, we will need to integrate the following 3 libraries into our Arduino project:

  • ESP8266WiFi.h: this library and the ideal toolbox to connect (and reconnect) an ESP8266 to the internet, to a server …
  • PubSubClient.h: This library allows you to send and receive MQTT messages and manage QoS
    DHT.h: this library makes it possible to easily recover the measurements of the DHT11 or DHT22 sensor

Create a new project and paste the following code to fit your configuration.

The project code is also available on GitHub here


If you discover the ESP8266 modules, read this article first to learn how to program and upload a program using the Arduino IDE.

Integration with a home automation server Home Assistant

If you do not know Home-Assistant, I invite you to read this article that explains how to install it and configure your box to access it from the internet.

The great thing about Home-Assistant is that there is only one script to edit to display the temperature and humidity published by the ESP8266 on Mosquitto. Go to the installation directory of Home-Assistant.io

then open the configuration file

We add a section mqtt

Now we add a new sensor (sensor) and we get the temperature on the topic sensor / temperature. The value is in the payload

We do the same for humidity by adding a sensor 2

Save the configuration (Ctrl + X then O) and launch the server with the hass command. Refresh the page in your browser to see the temperature and humidity measurement.

home assistant mqtt sensor esp8266

Turn on, turn off an LED from Home-Assistant

Now add a switch that will allow us to turn on or off an LED. This is a general example. We just activate an Arduino output. We could very simply replace the Led with a relay.

Stop Home-Assistant and reopen the configuration.yaml file where we will add a switch block

esp8266 wemos d1 mini home assistant switch interrupteur mqtt

Restart Home-Assistant. You now have a new widget named “Switch” in which is located the kitchen switch. Press the lightning bolt to turn on the LED.

If you have configured your box to make the Home-Assistant server accessible from the internet, you can even switch the LED on and off from your smartphone.

 

I hope you enjoyed this little project of discovering ESP8266 modules integrated into a home automation server. In a future article we will integrate it with Jeedom.

Subscribe to the weekly newsletter

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

Tags:

DIY Projects