How to publish DHT11 sensor data from NodMCU to Modbus TCP Server and read it in Ignition SCADA
Introduction
Internet of Things (IoT) is a rapidly growing field with a wide variety of applications. One common use case is to collect data from sensors and transmit it over a network for further analysis or control. In this article, we will discuss how to publish data from a DHT11 temperature and humidity sensor connected to a NodeMCU to a Modbus TCP server and read it in Ignition SCADA.
Connecting DHT11 to NodeMCU
Before we can publish sensor data over the network, we first need to connect the sensor to the NodeMCU board. The DHT11 sensor has three pins: VCC, GND, and DATA. Connect VCC to the 3.3V pin on the NodeMCU, GND to GND, and DATA to any digital pin (e.g., GPIO-2 or D4).
Programming NodeMCU
To read data from the DHT11 sensor and publish it over Modbus TCP, we need to program the NodeMCU board. We can use the Arduino IDE with the ESP8266 board package to write the code. First, we need to install the DHT sensor library by Adafruit. Open the Arduino IDE and go to Sketch -> Include Library -> Manage Libraries... Search for "DHT sensor library by Adafruit" and install it.
Next, open a new sketch and copy the following code:
#include <ESP8266WiFi.h>
#include <ModbusIP_ESP8266.h>
#include <DHT.h>
#define DHTPIN 2 // D4 of NodeMCU is GPIO2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
ModbusIP mb;
// Modbus register addresses for temperature and humidity
const int TEMPERATURE_REGISTER_ADDRESS = 0;
const int HUMIDITY_REGISTER_ADDRESS = 1;
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
mb.server();
mb.addHreg(TEMPERATURE_REGISTER_ADDRESS, 0);
mb.addHreg(HUMIDITY_REGISTER_ADDRESS, 0);
dht.begin();
}
void loop() {
mb.task();
uint16_t temperature = dht.readTemperature();
uint16_t humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
mb.Hreg(TEMPERATURE_REGISTER_ADDRESS, temperature);
mb.Hreg(HUMIDITY_REGISTER_ADDRESS, humidity);
}
delay(1000);
}
Replace SSID
and PASSWORD
with your WiFi network
credentials. This code reads temperature and humidity from the DHT11
sensor and adds them as holding registers in the Modbus TCP server. It
also sets up a WiFi connection and starts the Modbus TCP server.
Upload the code to the NodeMCU board, and you should see temperature and humidity values being printed in the serial monitor.
Reading Data in Ignition SCADA
Finally, we can read the data in Ignition SCADA. Open the Ignition designer and create a new OPC-UA connection to the Modbus TCP server. Add two integer tags to represent temperature and humidity, and map them to the corresponding holding registers in the Modbus TCP server (addresses 0 and 1).
Now, we can create a new screen in the Ignition designer and add two numeric displays to show the temperature and humidity values. Bind the displays to the corresponding tags, and you should see real-time updates of the sensor data.
Conclusion
In this article, we have discussed how to publish data from a DHT11 sensor connected to a NodeMCU board to a Modbus TCP server and read it in Ignition SCADA. This is just one example of how IoT technologies can be used to collect and analyze sensor data. With the right tools and knowledge, the possibilities are endless.
Comments
Post a Comment