How to Create Modbus TCP/IP Server with Seeed Studio XIAO ESP32C3

Introduction:

Modbus is a widely used communication protocol in industrial automation systems for transmitting data between electronic devices. In this tutorial, we will learn how to create a Modbus server using XIAO ESP32C3, a compact micro-controller board based on the ESP32-C3 chip. We will use the ModbusIP_ESP8266 library to facilitate Modbus communication.

Prerequisites:

efore we begin, ensure you have the following:

  • XIAO ESP32C3 board
  • USB cable for programming
  • Arduino IDE installed on your computer
  • Basic knowledge of Arduino programming

Setting up the Hardware:

Connect your XIAO ESP32C3 board to your computer using the USB cable.

Setting up the Software:

  • Open Arduino IDE on your computer.

  • Go to Sketch > Include Library > Manage Libraries… and search for “modbus-esp8266”. Click on Install to install the library.

Writing the Code:

				
					#include <WiFi.h>
#include <ModbusIP_ESP8266.h>

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, and gateway
  IPAddress ip(192, 168, 1, 4);
  IPAddress subnet(255, 255, 255, 0);
  IPAddress gateway(192, 168, 1, 1);

  // Connect to WiFi with fixed IP configuration
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  
  // Print assigned IP address
  Serial.println("Connected to WiFi");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Initialize Modbus server
  mb.server(4444);
  
  // Add 20 holding registers
  for (int i = 0; i < 20; i++) {
    mb.addHreg(i, 0); // Initialize all holding registers to zero
  }
}

void loop() {
  // Handle Modbus communication
  mb.task();
  delay(100); // Adjust delay as needed depending on your application requirements
}

				
			

Replace "Your_WiFi_SSID" and "Your_WiFi_Password" with your actual WiFi credentials.

Uploading the Code:

  • Select the correct board and port from the Tools menu in Arduino IDE.
  • Click on the Upload button to compile and upload the code to your XIAO ESP32C3 board.

Testing the Modbus Server:

  • After uploading the code successfully, open the serial monitor in Arduino IDE (Tools > Serial Monitor).

  • Ensure the baud rate is set to 115200.

  • You should see messages indicating the connection status and assigned IP address.

  • Now, you can connect Modbus client devices/software to your XIAO ESP32C3 board using the IP address and port 4444.

Conclusion:

In this tutorial, we’ve learned how to create a Modbus server using XIAO ESP32C3 and the ModbusIP_ESP8266 library. You can now integrate your XIAO ESP32C3 board into industrial automation systems and communicate with other Modbus devices seamlessly. Experiment with different configurations and functionalities to suit your specific application requirements.

Modbus is a powerful protocol for industrial automation, and by implementing a Modbus server on your XIAO ESP32C3 board, you can unlock a wide range of possibilities for controlling and monitoring your devices. Whether you’re building a small-scale automation system or a large industrial network, Modbus can help you achieve your goals efficiently and reliably. Happy coding!