/*Home
DHT11
Node-Red ESP8266 MQTT Publish Example
by
ee-diary
•
July 28, 2022
• 9 min read
Here it is shown how ESP8266 can be used to publish MQTT based message on the web using Node-Red as IoT platform. MQTT is a lightweight TCP protocol for building Internet of Things application. Here Mosqitto MQTT server/client application is used. Node-Red is open source graphical programming platform which can be used as Internet of Things platform.
Here ESP8266 based NodeMCU board is used to read temperature and humidity data from DHT11 sensor and send publish that over WiFi to the WiFi server so that it can be accessed by anyone over the internet.
To accomplish this we can divide the work in 3 steps involved as follows.
1. Interfacing ESP8266 with DHT11
2. Programming ESP8266
3. Set up and run MQTT server
4. Set up Node-Red for MQTT service
5. Publish MQTT service
The steps are described now below.
Contents
1. Interfacing ESP8266 with DHT11
The first step is to connect DHT11 to the ESP8266(NodeMCU) module. The interfacing diagram of DHT11 and NodeMCU ESP8266 is shown below.
NodeMCU Node Red Industrial Iot platform
The ESP8266 3.3V power pin is connected to the DHT11 Vcc pin and the ground pin of ESP8266 is connected to the ground of DHT11. The data pin of DHT11 is connected to the D7 pin of ESP8266 NodeMCU module. The following picture shows the hardware realized on actual breadboard.
DHT11 and NodeMCU
2. Programming ESP8266
Once the hardware connection between ESP8266 and the DHT11 is made, the next step is to write program for ESP8266 NodeMCU. The program will read the humidity and temperature data from the DHT11 module and send the data via WiFi to the MQTT server.*/
#include <WiFi.h>
#include "PubSubClient.h"
#include "DHT.h"
// DHT11
#define DHTTYPE DHT22
// Credentials for WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// MQTT Server(eg 192.168.1.4)
const char* mqtt_server = "m4.wqtt.ru";
// WiFi Client
WiFiClient nodeClient;
PubSubClient client(nodeClient);
// DHT Sensor pin at GPIO 13(D7)
const int DHTPin = 13;
// Initialize DHT sensor
DHT dht(DHTPin, DHTTYPE);
// Function to connect NodeMCU to WiFi router
void wifiConfig() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password,6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, NodeMCU IP address: ");
Serial.println(WiFi.localIP());
}
// Function to reconnect NodeMCU with MQTT broker
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("MQTTClient")) {
Serial.println("connected");
}
else {
Serial.print("failed, State: ");
Serial.print(client.state());
Serial.println("try again in 5 seconds...");
delay(5000);
}
}
}
void setup() {
dht.begin();
Serial.begin(115200);
wifiConfig();
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) {
reconnect();
}
if (!client.loop())
client.connect("MQTTClient");
// Read Humidity
float H = dht.readHumidity();
// Read temperature as Celsius (the default)
float T = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(H) || isnan(T)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//Convert float to string and store them in arrays
static char humidity[7];
dtostrf(H, 6, 2, humidity);
static char temperature[7];
dtostrf(T, 6, 2, temperature);
// Publishes Temperature and Humidity values
client.publish("dht11/temperature", temperature);
client.publish("dht11/humidity", humidity);
delay(5000);
}