It is possible to set up a do-it-yourself home automation system with an ESP board and Raspberry Pi (RPi). Both offer Wi-Fi connectivity and can communicate easily over a home network. However, a communication protocol is required for wireless communication of devices operating on a wider network.
Since conventional Internet protocols like HTTP are often resource-intensive to implement with microcontroller boards, IoT protocols like MQTT offer an option. MQTT is a standard messaging protocol for the Internet of Things.
In this project, we will design an ESP-based weather monitor and configure it as an MQTT editor. An MQTT broker (i.e. Mosquitto) will be configured on the RPi to communicate with the ESP device. An MQTT subscriber will be configured on the same RPi to read and record temperature and humidity readings.
The sensor used to read temperature and humidity on the ESP32/ESP8266 is the DHT11. DHT22 can also be used if preferred.
Required components
1. Raspberry Pi x1
2. Display Monitor x1
3. Micro-USB Cable and Power Adapter for Raspberry Pi x1
4. HDMI cable x1
5. Keyboard and mouse x1
6. ESP8266/ESP32 x1
7. DHT11/DHT22 x1
8. Micro USB cable for ESP8266/ESP32 x1
Configuring RPi as an MQTT broker
Mosquitto, the broker we use, is responsible for all communication in the form of topic-bound messages between MQTT clients. The ESP32/ESP8266 is configured as an MQTT editor. The subscriber is configured on the Raspberry Pi.
Make sure the RPi is configured as a desktop computer . Then install Mosquitto on it. To do this, open the terminal and run this command:
sudo apt-get install mosquito
Then install the Mosquitto command-line client by running the following command in the terminal:
sudo apt-get install mosquito-clients -y
After installing the MQTT broker, we will need to make changes to its configuration files. The default configuration file is stored in /etc/mosquitto/mosquitto.conf. Open the default configuration file in the nano editor by running the following command in the terminal:
sudo nano /etc/mosquitto/mosquitto.conf
The configuration file should look similar to this…
In the configuration file, we will disable the default settings with this line:
include_dir /etc/mosquitto/conf.d
for
# include_dir /etc/mosquitto/conf.d
Next, we will not allow anonymous users to connect to the broker by adding this line:
allow_anonymous false
We will have to define a file to save the password by adding this line:
password_file /etc/mosquitto/pwfile
Then we will set the listening port to 1883 by adding this line:
listener 1883
Save changes by pressing Ctrl+S and exit by pressing Ctrl+X. The configuration file will look like this after you complete these edits…
Now let's set the username and password so that MQTT clients can access the MQTT broker. To create a username or reset an existing user's password, run the following command in the terminal:
sudo mosquitto_passwd -c /etc/mosquitto/pwfile
For this project, we set the username to “neo” and the password to “eglabs”. Make sure you choose a strong password. Note that when you enter the password, nothing is visible in the window.
You can remove an existing user by running this command in the terminal:
sudo mosquitto_passwd -d /etc/mosquitto/pwfile
After generating a username and password, restart your Raspberry Pi for these changes to take effect. To do this, run this command in the terminal:
sudo restart
The Mosquitto broker can be launched by running the following command in the terminal:
sudo systemctl start mosquito
Let's make sure the Mosquitto broker is working by running this command in the terminal:
sudo systemctl status mosquito
The Mosquitto broker can be stopped by running this command in the terminal:
sudo systemctl stop the mosquito
The Mosquitto broker can be restarted by running this command in the terminal:
sudo systemctl restart mosquito
The Mosquitto broker can be enabled at startup by running this command in the terminal:
sudo systemctl enable mosquito
The Mosquitto broker can run in the background by running this command in the terminal:
mosquito -d
Configuring the weather monitor
We will configure the weather station using ESP32/ESP8266. To do this, you will need to connect DHT11/DHT22 with ESP32/ESP8266

DHT11 interfaced with ESP32 to create the weather station.
Configuring ESP32/ESP8266 as MQTT editor
Load the following sketch into the ESP32/ESP8266 to run the ESP as a weather station. It will publish the temperature and humidity values to the MQTT broker.
#include “DHT.h”
#include “PubSubClient.h”
#include “WiFi.h”
#define DHTPIN4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// WiFi
const char*ssid = “SSID”;
const char* wifi_password = “PASSWORD”;
//MQTT
const char* mqtt_server = “192.168.*.*”;
const char* humidity_topic = “humidity”;
const char* temperature_topic = “temperature”;
const char* mqtt_username = “neo”; //MQTT username
const char* mqtt_password = “eglabs”; // MQTT password
const char* clientID = “Weather_Reporter”; //MQTT client ID
// Initialize the WiFi and MQTT Client objects
WiFiClient wifiClient;
// 1883 is the Broker listener port
PubSubClient Client(mqtt_server, 1883, wifiClient);
// Custom function to connect to MQTT broker via WiFi
void connect_MQTT {
Serial.print(“Connecting to “);
Serial.println(ssid);
//Connect to Wi-Fi
WiFi.begin(ssid, wifi_password);
//Wait until the connection is confirmed
while (WiFi.status != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
// Debug – Generates the IP address of the ESP8266
Serial.println(“WiFi connected”);
Serial.print(“IP Address: “);
Serial.println(WiFi.localIP );
//Connect to MQTT broker
if (client.connect(clientID, mqtt_username, mqtt_password)) {
Serial.println(“Connected to MQTT Broker!”);
}
other {
Serial.println(“Failed to connect to the MQTT Broker…”);
}
}
empty configuration {
Serial.begin(9600);
dht.begin;
}
empty loop {
connect_MQTT;
Serial.setTimeout(2000);
float h = dht.readHumidity;
float t = dht.readTemperature;
Serial.print(“Humidity: “);
Serial.print(h);
Serial.println(”%”);
Serial.print(“Temperature: “);
Serial.print
Serial.println(”*C”);
// MQTT can only transmit strings
String hs=”Hum: “+String((float)h)+” % “;
String ts=”Temp: “+String((float)t)+”C “;
// PUBLISH to Broker MQTT (topic = Temperature)
if (client.publish(temperature_topic, String
Serial.println(“Temperature sent!”);
}
other {
Serial.println(“Failed to send temperature. Reconnecting to MQTT Broker and trying again”);
client.connect(clientID, mqtt_username, mqtt_password);
client.publish(temperature_topic, String
}
// PUBLISH to MQTT Broker (topic = Moisture)
if (client.publish(humidity_topic, String(h).c_str )) {
Serial.println(“Moisture sent!”);
}
other {
Serial.println(“Failed to send moisture. Reconnecting to MQTT Broker and trying again”);
client.connect(clientID, mqtt_username, mqtt_password);
delay(10); // This delay ensures that client.publish does not conflict with the client.connect call
}
client.disconnect; // disconnect from MQTT broker
delay(1000*60); // print new values after 1 minute
}
How ESP-based MQTT editor works
ESP32 is configured as an MQTT editor. To work with DHT11/DHT22, DHT.h must first be imported. The PubSubClient.h library is also imported to configure the ESP32 as an MQTT client. The library was written by Nick O'Leary. It can be installed in the Arduino IDE by navigating to Tools->Manage Libraries and searching for PubSubClient.
Observation:
- The library must be installed before importing it into the sketch. WiFi.h must be imported to connect the ESP32/ESP8266 to the WiFi network.
- The output pin of DHT11 connects to pin D04.
- The DHT object is instantiated with DHTTYPE for DHT11 and DHTPIN for 4.
- The WiFi SSID and password are stored in the SSID and wifi_password variables. Make sure to replace “SSID” and “PASSWORD” with the SSID and WiFi network key of your own WiFi network.
- Variables are declared to store the MQTT server IP, topic names, MQTT username and password, and client ID.
- The IP address of the MQTT broker is the same as the IP address of the Raspberry Pi on the network.
The IP address of the Raspberry Pi can be obtained by running the following command in the terminal:
hostname -I
The first string obtained when executing the above command is the IPv4 address, which must be used in the sketch. Replace the MQTT server IP (“192.168.*.*”) with the IP address of your own Raspberry Pi.
A WiFiClient object is instantiated and used to instantiate the MQTT client object. The connect_MQTT function is defined to connect to the WiFi network and the MQTT broker.
In the setup function, the baud rate for recording information in the Arduino IDE's Serial Monitor is set to 9600 and the DHT is initialized by calling the DHT.begin method.
In the loop function, the DH11's temperature and humidity values are retrieved by calling the dht.readHumidity and dht.readTemperature methods. The retrieved values are wrapped as strings and passed as messages to the 'humidity' and 'temperature' MQTT topics respectively by calling the client.publish method.
Lastly, the client is disconnected by calling the client.disconnect method. A one minute delay is provided.
Configuring MQTT subscriber on RPi
Install the paho-mqtt library to configure the MQTT client on the Raspberry Pi. Run the following command in the terminal to install this library:
sudo pip install paho-mqtt
Open the Thonny IDE and copy the following code into the editor:
import paho.mqtt.client as mqtt
MQTT_ADDRESS = '192.168.*.*'
MQTT_USER = 'neo'
MQTT_PASSWORD = 'eglabs'
MQTT_TOPIC_TEMP = 'Temperature'
MQTT_TOPIC_HUMD = 'Humidity'
def on_connect(client, userdata, flags, rc):
print('Connected with result code ' + str(rc))
client.subscribe(MQTT_TOPIC_TEMP)
client.subscribe(MQTT_TOPIC_HUMD)
def on_message(client, user data, message):
print(msg.topic + ' ' + str(msg.payload))
main def:
mqtt_client = mqtt.Client
mqtt_client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
mqtt_client.on_connect=on_connect
mqtt_client.on_message=on_message
mqtt_client.connect(MQTT_ADDRESS, 1883)
mqtt_client.loop_forever
if __name__ == '__main__':
print('MQTT Bridge for InfluxDB')
main
Save the script as RPI-Sub.py and run the script.
How MQTT subscriber works
The MQTT subscriber on the RPi is run with the help of a Python script. The script imports the paho.mqtt.client library. Variables are declared to store the MQTT broker IP address (which is the same as the RPi IP address), the MQTT username, the MQTT password, and the MQTT topics.
The customer subscribes to two topics, 'temperature' and 'humidity', when connecting with the broker. To do this, the on_connect function is defined and called in the main loop. Messages published from the ESP-based MQTT editor are printed to the console by calling the user-defined on_message function.
In the main loop, the MQTT client is started by calling the mqtt.Client method. The MQTT username and password are set by calling the mqtt_client.username_pw_set method. The client is connected to the broker by calling the mqtt_client.connect method and enters an infinite loop by calling the mqtt_client.loop_forever method.
When connecting to the MQTT broker, the user-defined on_connect function is called, subscribing to the respective topics. When receiving messages from the ESP-based editor, the user-defined on_message function is called, which prints the messages to the console.
Results