How to Publish DHT11 Sensor Data From NodeMCU to Modbus TCP Server

by Fusion Atuomate in Circuits > Microcontrollers

20 Views, 0 Favorites, 0 Comments

How to Publish DHT11 Sensor Data From NodeMCU to Modbus TCP Server

Screenshot 2024-04-01 at 22-44-55 Publish DHT11 Sensor Data from NodeMCU to Modbus TCP Server.png

Modbus TCP is an open communication protocol used in industrial automation systems to connect devices over Ethernet. Modbus TCP utilizes the TCP/IP protocol to establish communication between devices. NodeMCU is an open-source development board that is built around the ESP8266 Wi-Fi module. The NodeMCU board is used to connect sensors and actuators with the Modbus TCP server. In this blog post, we will discuss how to publish DHT11 sensor data from NodeMCU to the Modbus TCP server.

NodeMCU and DHT11 Sensor

NodeMCU+DHT11.png

NodeMCU is a widely popular development board that is based on the ESP8266 Wi-Fi module. The board has built-in Wi-Fi capabilities and can be programmed using the Arduino IDE. DHT11 is a temperature and humidity sensor that is commonly used with NodeMCU. The sensor has four pins, VCC, GND, Data, and NC (Not Connected). The data pin of the sensor is connected to a digital pin of NodeMCU.

Publishing DHT11 Sensor Data to Modbus TCP Server

To publish DHT11 sensor data to the Modbus TCP server, we need to establish a connection between NodeMCU and Modbus TCP server. The following code demonstrates how to establish a connection between NodeMCU and Modbus TCP server and publish DHT11 sensor data.

#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);

Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}

delay(1000);
}
  • The above code establishes a connection to the Wi-Fi network using the WiFi.begin() function.
  • The mb.server() function initializes the Modbus TCP server.
  • The mb.addHreg() function adds Modbus registers for temperature and humidity.
  • The dht.begin() function initializes the DHT11 sensor.
  • The mb.task() function is used to handle Modbus TCP requests.
  • The dht.readTemperature() and dht.readHumidity() functions read the temperature and humidity values from the sensor.
  • The mb.Hreg() function saves the temperature and humidity values in the Modbus registers.

Conclusion

How to Publish DHT11 Sensor Data from NodeMCU to Modbus TCP Server | NodeMCU | Modbus TCP | DHT 11 |

In this blog post, we discussed how to publish DHT11 sensor data from NodeMCU to the Modbus TCP server. We learned how to establish a connection between NodeMCU and Modbus TCP server and how to read sensor data and save it to Modbus registers. Modbus TCP is a widely used protocol in industrial automation systems, and NodeMCU is a versatile development board that can be used to connect sensors and actuators to the Modbus TCP server.