Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
FUSION AUTOMATE
Published © GPL3+

Controlling Digital Pins of NodeMCU ESP8266 via Modbus

Controlling Digital Pins of NodeMCU ESP8266 via Built-in Modbus TCP/IP Server from Remote Modbus Client

IntermediateProtip1 hour333
Controlling Digital Pins of NodeMCU ESP8266 via Modbus

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

Arduino IDE
Arduino IDE
Windows 10
Microsoft Windows 10
Modscan - Modbus Client Software

Story

Read more

Code

Sketch

C/C++
#include <ESP8266WiFi.h>
#include <ModbusIP_ESP8266.h>

#define DIGITAL_REGISTER_ADDRESS 0

ModbusIP mb;

void setup() {
  Serial.begin(115200);
  WiFi.begin("YourWiFiSSID", "YourWiFiPassword"); // Replace with your WiFi credentials
  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.addCoil(DIGITAL_REGISTER_ADDRESS, false); // Initialize the coil with false (off)
  
  pinMode(LED_BUILTIN, OUTPUT); // Set LED_BUILTIN pin as output
}

void loop() {
  mb.task();

  // Read the value from Modbus register to control the LED
  bool ledStatus = mb.Coil(DIGITAL_REGISTER_ADDRESS);

  // Invert the LED status
  ledStatus = !ledStatus;

  // Control the LED based on inverted Modbus register value
  digitalWrite(LED_BUILTIN, ledStatus ? HIGH : LOW);

  delay(500);
}

Credits

FUSION AUTOMATE
25 projects • 3 followers
Contact

Comments

Please log in or sign up to comment.