How to Publish DHT11 Sensor Data from XIAO ESP32C3 to Built-in Modbus TCP/IP Server and Read it in Modbus Client
In this comprehensive tutorial, we will delve into the intricate process of integrating a DHT11 temperature and humidity sensor with the XIAO ESP32C3 microcontroller, enabling the publication of sensor data via a built-in Modbus TCP server. Furthermore, we'll explore how to access and interpret this data using a Modbus client application.
Introduction
The DHT11 sensor stands as a cornerstone in environmental monitoring, renowned for its accuracy and simplicity in measuring temperature and humidity. On the other hand, the XIAO ESP32C3 microcontroller offers a versatile platform with built-in Wi-Fi capabilities, making it an ideal candidate for IoT projects. By combining these two components and leveraging the power of the Modbus communication protocol, we can create a robust system for monitoring and managing environmental conditions remotely.
Requirements
Before embarking on this journey, ensure you have the following prerequisites:
- XIAO ESP32C3 microcontroller
- DHT11 temperature and humidity sensor
- Access to a Wi-Fi network
- Arduino IDE installed on your computer
Setting Up the Hardware
- Begin by connecting the DHT11 sensor to the XIAO ESP32C3 microcontroller. We'll utilize digital pin D6 for this purpose.
- Establish a physical connection between the XIAO ESP32C3 microcontroller and your computer using a USB cable.
Setting Up the Software
Let's navigate through the software setup process step by step:
- Launch the Arduino IDE on your computer.
- Install the necessary libraries by navigating to Sketch > Include Library > Manage Libraries. Search for and install the following libraries:
- DHT Sensor Library by Adafruit
- ModbusIP_ESP8266
- With the libraries successfully installed, copy and paste the provided code into the Arduino IDE.
- Replace
"Your_WiFi_SSID"
and"Your_WiFi_Password"
with your actual WiFi credentials.
- Upload the code to your XIAO ESP32C3.
Writing the Code
#include "DHT.h"
#include <WiFi.h>
#include <ModbusIP_ESP8266.h>
#define DHTPIN D6
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";
ModbusIP mb;
void setup() {
Serial.begin(115200);
// Set fixed IP address, subnet mask, gateway, and DNS server
IPAddress ip(192, 168, 1, 4);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(8, 8, 8, 8); // Google DNS
// Connect to WiFi with fixed IP configuration
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet, dns);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Print assigned IP address
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Initialize DHT sensor
dht.begin();
// Initialize Modbus server
mb.server();
mb.addCoil(0); // Dummy coil to indicate server running
mb.addHreg(0); // Register to hold temperature (16-bit integer)
mb.addHreg(1); // Register to hold humidity (16-bit integer)
}
void loop() {
// Read temperature and humidity
float temp = dht.readTemperature();
float hum = dht.readHumidity();
// Update Modbus registers
mb.Hreg(0, temp * 10); // Temperature (multiplied by 10 to avoid floating point)
mb.Hreg(1, hum * 10); // Humidity (multiplied by 10 to avoid floating point)
// Handle Modbus communication
mb.task();
// Print data to serial monitor
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("*C");
Serial.print(" Humidity: ");
Serial.print(hum);
Serial.println("%");
delay(500); // Delay before next reading
}
Let's dissect the key components of the code snippet provided:
- We include essential libraries such as the DHT sensor library, Wi-Fi library, and ModbusIP library.
- We define the pin connected to the DHT11 sensor (
DHTPIN
) and specify its type (DHTTYPE
).
- Within the
setup()
function:- Serial communication is initialized for debugging purposes.
- We configure the Wi-Fi connection using a fixed IP address.
- Initialization of the DHT sensor takes place.
- The Modbus server is initialized, and registers are added to store temperature and humidity data.
- In the
loop()
function:- Temperature and humidity readings are retrieved from the DHT11 sensor.
- Modbus registers are updated with the sensor data.
- Modbus communication tasks are handled.
- Sensor data is printed to the serial monitor for monitoring purposes.
Testing the Setup
Let's put our setup to the test:
- After uploading the code, open the serial monitor in the Arduino IDE (
Tools > Serial Monitor
).
- You should observe temperature and humidity readings being displayed in the serial monitor interface.
- To further validate our setup, utilize a Modbus client application to connect to the XIAO ESP32C3 microcontroller's IP address. From there, you can effortlessly retrieve and interpret temperature and humidity values stored within the Modbus registers.
Conclusion
In this extensive tutorial, we've meticulously explored the process of integrating a DHT11 sensor with the XIAO ESP32C3 microcontroller, thereby enabling the publication of sensor data via a built-in Modbus TCP server. This setup empowers users to remotely monitor temperature and humidity levels, offering invaluable insights into environmental conditions.
This tutorial merely scratches the surface of the vast possibilities afforded by IoT and Modbus communication. Feel free to experiment further with the provided code and explore additional functionalities to cater to diverse project requirements. Embrace the journey of exploration and innovation as you embark on your IoT endeavors.
Happy coding and may your projects flourish with boundless creativity!
Comments
Post a Comment