FUSION AUTOMATE
← All posts
2 min readmirrored from hackster.io

Create a Modbus TCP/IP Server with NodeMCU ESP8266

Build a Modbus TCP/IP server on a NodeMCU ESP8266 with the Arduino IDE and the ModbusIP_ESP8266 library. Register setup, code and client testing.

Create a Modbus TCP/IP Server with NodeMCU ESP8266

Originally published on Hackster.io — this is an authorized mirror; the original remains live there.

On this page (9)

If you're looking to integrate Modbus communication into your IoT project, you're in the right place. In this tutorial, we'll walk through the process of setting up a Modbus server using a NodeMCU ESP8266 microcontroller. Modbus is a widely used communication protocol in industrial automation and offers a straightforward way to exchange data between devices. By the end of this guide, you'll have a fully functional Modbus server running on your NodeMCU ESP8266, ready to communicate with Modbus clients.

Prerequisites

Before we begin, make sure you have the following:

  • NodeMCU ESP8266 board

  • Arduino IDE installed on your computer

  • Basic knowledge of C++ programming

  • Access to a Wi-Fi network for connecting the NodeMCU

Setting Up Your Environment

First, let's set up our development environment by installing the necessary libraries and configuring the Arduino IDE.

  • Open the Arduino IDE and navigate to Sketch > Include Library > Manage Libraries.

Search for and install the following libraries:

  • ESP8266WiFi (for Wi-Fi connectivity)

  • ModbusIP_ESP8266 (for Modbus server implementation)

  • Once the libraries are installed, we're ready to move on to the coding part.

Writing the Code

Copy and paste the following code into your Arduino IDE:

#include <ESP8266WiFi.h>
#include <ModbusIP_ESP8266.h>

const char* ssid = "YourWiFiSSID";      // Replace with your Wi-Fi SSID
const char* password = "YourWiFiPassword";  // Replace with your Wi-Fi 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
}

Understanding the Code

Let's break down what each part of the code does:

  • We include the necessary libraries for Wi-Fi connectivity and Modbus server implementation.
  • Replace "``YourWiFiSSID``" and "``YourWiFiPassword``" with your Wi-Fi credentials.
  • In the setup() function, we configure the Wi-Fi connection and set a fixed IP address for the NodeMCU.
  • The Modbus server is initialized on port 4444, and 20 holding registers are added, initialized to zero.
  • In the loop() function, we handle Modbus communication by calling the mb.task() function.

Uploading the Code

Connect your NodeMCU ESP8266 board to your computer via USB and select the correct board and port in the Arduino IDE. Then, click the upload button to compile and upload the code to your board.

Testing

Once the code is uploaded successfully, open the serial monitor in the Arduino IDE. You should see messages indicating the NodeMCU connecting to your Wi-Fi network and displaying the assigned IP address.

Now, your Modbus server is up and running, ready to communicate with Modbus clients.

Conclusion

In this tutorial, we've covered the process of creating a Modbus server using a NodeMCU ESP8266 board. You can now integrate this server into your IoT projects to exchange data with Modbus clients efficiently. Experiment with different configurations and functionalities to suit your specific requirements. Happy coding!

Code

sketch

#include <ESP8266WiFi.h>
#include <ModbusIP_ESP8266.h>

const char* ssid = "YourWiFiSSID";      // Replace with your Wi-Fi SSID
const char* password = "YourWiFiPassword";  // Replace with your Wi-Fi 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
}
Share

[ next in the queue ]