Here it is illustrated how ESP8266 NodeMCU and Node Red can be used as Industrial IoT platform. IoT is abbreviation for Internet of Things and Industrial IoT is also often abbreviated as IIoT. The concept of IoT is to use internet as the communication medium to control hardware and sensors. These hardware are called things in IoT world. The IIoT means using IoT for industrial application for controlling and monitoring environmental data and sensors.
NodeMCU is a WiFi module which can be connected to PC/Laptop via WiFi. The NodeMCU has I/O ports to which sensors and other hardware can be connected. The sensor data can be monitored and the data can be sent via WiFi to the PC/Laptop.
Node Red is open source application which can be used as IoT connectivity platform. Node Red uses graphical flow programming language. Here Node Red is used to build application that reads the sensor data coming from NodeMCU and display the data on the web.
NodeMCU & DHT11
The following shows DHT11 with nodeMCU connected using breadbaord.
The DHT11 has 3 pins, Vcc, data and ground pins. The DHT11 Vcc pin is connected to Vin pin of nodeMCU and the ground pin is connected to the ground pin. The DHT11 data pin is connected pin 13 of NodeMCU. The following shows the circuit schematic of connecting DHT11 and NodeMCU.
Programming NodeMCU
The NodeMCU is programmed using Arduino IDE. The following is the code for NodeMCU.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
// DHT11
#define DHTTYPE DHT11
// Credentials for WiFi
const char* ssid = "Your_WiFI_SSID";
const char* password = "Your_WiFi_Password";
// MQTT Server(eg 192.168.1.4)
const char* mqtt_server = "Your MQTT IP address";
// 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);
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 DHT11 sensor humidity & temperature values
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);
}
In the above code, the have used three libraries, the ESP8266WiFi.h, the PubSubClient.h and the DHT.h. All these libraries can be installed via the library manager in the Arduino IDE. To know how to install the ESP8266WiFi.h see the tutorial LED Blink using ESP8266 & Arduino IDE. The DHT library download and its basic use is explained in the tutorial DHT11 Humidity and Temperature Sensor with Arduino.
The ESP8266WiFi.h library is used to connect to the local WiFi network. For this we have set up the ssid and password for the local wifi. Here users should put the SSID name and password for the local WiFi network. The wifiConfig() function is used to connect to the WiFi network. It tries to connect to the WiFi network with the credentials provided and when the connection between the WiFi router and the NodeMCU is successful, the IP address of the NodeMCU is printed on the serial monitor. This function is called in the setup() function.
The PubSubClient.h library is used to transfer data and control messages between MQTT server and the NodeMCU. To do this first a client called nodeClient is created. This client is used to publish humidity and temperature data from the DHT11/NodeMCU to the MQTT server using MQTT protocol. Also this client is used to perform functions other than publish. In the reconnect(), the client is used to check connection and if there is no connection or connection is lost, client methods such as connect() and connected() are used perform checks and try connection to the MQTT broker. Also the method state() is used to check the state of the connection.
The DHT.h library is used to for using the DHT11 sensor module explained in greater details in the tutorial LCD Display of Temperature and Humidity using DHT11 & Arduino with Code. The library makes easy to use the DHT sensor module as it provides methods such as readHumidity() and readTemperature().
In the setup() function we start reading the DHT11 sensor data using dht.begin(). The we start the serial port with baud rate of 115200bps. This is used for debug purpose. Then we call the wifiConfig() function which sets up the WiFi connection to the NodeMCU. Finally we use the client object and its setServer() method to connect to the MQTT server at the mqtt_server IP address which is set up at the beginning and at the port 1883.
The local internal IP address for the MQTT server can be found out with ipconfig command in the cmd command terminal in windows as shown below.
In the loop() function we check whether the client is connected using the connected() method which returns boolean true or false. If the client is not connected then the reconnect() is called to make re-connection between MQTT client and the MQTT server. Similarly the loop() method is to make regular call and transfer MQTT message between the server and client.
After this we use the readHumidity() and readTemperature() methods of the dht object to read the DHT11 sensor humidity and temperature and save them in float variables H and T respectively. Then we check whether the values H and T are valid or not and continue if the values received for H and T are valid. The float variables H and T are converted to strings and stored in char array humidity[] and temperature[].
Finally the value stored in the humidity and temperature array are published by the MQTT client using the publish methods in the topics dht11/humidity and dht11/temperature.
A delay of 5 seconds is used to publish the MQTT message. A longer delay may be better because humidity and temperature do not usually change drastically.
MQTT Server
MQTT(MQ Telemetry Transport) is a lightweight messaging protocol that is used for passing messages in IoT applications. The next step is to setup the MQTT server and Node Red application that reads in the MQTT message coming from the Node MCU.
Here Mosquitto MQTT is used for the MQTT server. You can download and install it from the following link:
The installation is straight forward. Once you install it you will find various files in a folder like the one shown below.
Among the various application files, the mosquito.exe is used to start the MQTT server. This is done by using command terminal and using the mosquitto -v command as illustrated below.
Node Red
The next step is to create MQTT web application in Node Red that reads the incoming MQTT message and displays the humidity and temperature on user interface on the web.
The nodes labelled dht11/humidity and dht11/temperature are mqtt in nodes which can be found in the network library.
The setting for the dht11/humidity and dht11/temperature node are shown below.
The Server field is localhost:1883 for both nodes, the Action field is Subscribe to single topic, the Topic filed are dht11/humidity and dht11/temperature respectively. The quality of service QoS for both are 1.
The two nodes labelled Humidity and Temperature are gauge node which can found in the dashboard library.
The dashboard library must be installed from the menu>manage palette or via npm. The tutorial Node Red IoT with Arduino DHT11 explains how to install the dashboard library.
The dashboard nodes such as gauge used here requires tab and group. So first we have to create a tab and group names and then configure the gauge nodes to use the tab/group names. To create a new tab go to dashboard and then create a new tab. Name the tab DHT11. Then from the new tab, create a new group and name it NodeMCU as shown below.
Again from the group click on edit as shown below.
Now set the width to 10 and uncheck the Display group name.
Next double click the Humidity and Temperature gauge nodes and configure them as shown in the picture below.
In the Group field select the [DHT11]NodeMCU for both the gauge nodes. Set 5x5 for Size for both gauge nodes. The label field are Humidity and Temperature. In the value format for humidity add % symbol and for temperature value format add ℃.
Finally click on the deploy and then start.
On the browser open the url https://localhost:1880/ui to see the dashboard display as shown below.
As shown above, the url used here is http://app.ee-diary.ga/ui. So the data from the MQTT service is available on the public web or internet and therefore accessible to all. As such this is example of simple IoT solutions to create industrial IoT applications. To learn how to host and display application on custom domain name see the tutorial Deploy Arduino Red Node App on Web with Cloudflare Tunnel.
The following is the node red flow code.
[
{
"id": "6e8e647f55f19c65",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": "",
"env": []
},
{
"id": "5afcb2f70fb4032f",
"type": "mqtt in",
"z": "6e8e647f55f19c65",
"name": "",
"topic": "dht11/humidity",
"qos": "1",
"datatype": "auto-detect",
"broker": "62942e11fc49d33e",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 320,
"y": 180,
"wires": [
[
"f0c5c71cb90a0d4b"
]
]
},
{
"id": "f0c5c71cb90a0d4b",
"type": "ui_gauge",
"z": "6e8e647f55f19c65",
"name": "",
"group": "fecb1abb8b6d2678",
"order": 1,
"width": "5",
"height": "5",
"gtype": "gage",
"title": "Humidity",
"label": "",
"format": "{{value}}%",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "",
"seg2": "",
"className": "",
"x": 540,
"y": 180,
"wires": []
},
{
"id": "bf3fce1725291ee7",
"type": "mqtt in",
"z": "6e8e647f55f19c65",
"name": "",
"topic": "dht11/temperature",
"qos": "1",
"datatype": "auto-detect",
"broker": "62942e11fc49d33e",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 330,
"y": 260,
"wires": [
[
"6f2e240f9a0c13c8"
]
]
},
{
"id": "6f2e240f9a0c13c8",
"type": "ui_gauge",
"z": "6e8e647f55f19c65",
"name": "",
"group": "fecb1abb8b6d2678",
"order": 1,
"width": "5",
"height": "5",
"gtype": "gage",
"title": "Temperature",
"label": "",
"format": "{{value}}℃",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "",
"seg2": "",
"className": "",
"x": 550,
"y": 260,
"wires": []
},
{
"id": "62942e11fc49d33e",
"type": "mqtt-broker",
"name": "",
"broker": "localhost",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "fecb1abb8b6d2678",
"type": "ui_group",
"name": "NodeMCU",
"tab": "886288b874f81978",
"order": 1,
"disp": false,
"width": "10",
"collapse": false,
"className": ""
},
{
"id": "886288b874f81978",
"type": "ui_tab",
"name": "DHT11",
"icon": "dashboard",
"order": 8,
"disabled": false,
"hidden": false
}
]
So in this way we can create simple IoT application using Node Red as IoT platform. We can add to this MySQL database to store the humidity and temperature data and thus create a IoT monitoring system. To use MySQLdatabase in node red see the tutorial Read and Store Sensor Data into Database with Node Red.