FUSION AUTOMATE
Published © GPL3+

Create Modbus TCP/IP Server using NodeMCU ESP8266

How to Create Modbus TCP/IP Server with NodeMCU ESP8266

IntermediateProtip1 hour2,369
Create Modbus TCP/IP Server using NodeMCU ESP8266

Things used in this project

Story

Read more

Code

sketch

C/C++
#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
}

Credits

FUSION AUTOMATE
25 projects • 3 followers
Contact

Comments

Please log in or sign up to comment.