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

Control NodeMCU ESP8266 Pins over Modbus TCP

Switch the digital pins of a NodeMCU ESP8266 on and off through Modbus TCP. Arduino sketch with a holding-register API plus Modscan testing steps.

Control NodeMCU ESP8266 Pins over Modbus TCP

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

On this page (8)

In this comprehensive tutorial, we will delve into the intricacies of controlling digital pins on a NodeMCU micro-controller using a built-in Modbus TCP server. Modbus, a widely-used communication protocol in industrial automation, facilitates the seamless interaction between devices, enabling efficient control and monitoring. By integrating Modbus functionality into your NodeMCU ESP8266, you gain the capability to remotely manage its digital pins from any Modbus client over a TCP/IP network.

Understanding the Components

Before we dive into the implementation, let's take a moment to understand the components involved:

  • NodeMCU: A popular development board based on the ESP8266 Wi-Fi module. It is widely used for IoT projects due to its low cost, built-in Wi-Fi capabilities, and compatibility with the Arduino IDE.
  • Modbus Protocol: A serial communication protocol used for transmitting data between electronic devices over a network. It is particularly prevalent in industrial automation systems for controlling and monitoring equipment.
  • Modbus TCP: An extension of the Modbus protocol that uses TCP/IP as its transport layer, allowing devices to communicate over Ethernet or Wi-Fi networks.
  • Modbus Client: A device or software application that initiates requests to a Modbus server for data acquisition or control purposes.
  • Modbus Server: A device or software application that responds to requests from Modbus clients, providing access to data registers for reading or writing.

Prerequisites

Before proceeding, ensure you have the following:

  • Hardware: NodeMCU ESP8266 board

  • Software: Arduino IDE installed on your computer

  • Knowledge: Basic understanding of Arduino programming

  • Network: Access to a Wi-Fi network

Setting Up the Hardware

  • Connect your NodeMCU ESP8266 board to your computer using a USB cable.
  • Open the Arduino IDE on your computer.

Setting Up the Software

  • Open the Arduino IDE.

  • Install the required libraries by going to Sketch > Include Library > Manage Libraries and searching for:

  • ESP8266WiFi

  • ModbusIP_ESP8266

  • Copy and paste the following code into your Arduino IDE:

#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);
}
  • Replace "YourWiFiSSID" and "YourWiFiPassword" with your Wi-Fi credentials.
  • Upload the code to your NodeMCU ESP8266 board by clicking the Upload button in the Arduino IDE.

Interacting with the Modbus Server

With the code uploaded and running on your NodeMCU ESP8266, it now functions as a Modbus TCP server.

You can utilize any Modbus TCP client software to interact with it. Here's how you can control the digital pins:

  • Open your preferred Modbus TCP client software (e.g., ModScan, Node-RED, etc.).

  • Connect to the IP address of your NodeMCU ESP8266 board and the specified port (default is 502).

  • Write a value of 0 or 1 to the designated coil address to control the digital pin connected to the LED.

Conclusion

In conclusion, this tutorial has provided you with a comprehensive guide on how to control digital pins of a NodeMCU ESP8266 micro-controller using a built-in Modbus TCP server. By integrating Modbus functionality into your NodeMCU-based projects, you can enhance their capabilities and enable remote control and monitoring over a network. Experiment further by incorporating additional sensors, actuators, or logic to tailor your NodeMCU ESP8266 system to specific automation or IoT applications. With the foundational knowledge gained from this tutorial, you're well-equipped to explore and innovate in the realm of IoT and industrial automation.

Code

Sketch

#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);
}
Share

[ next in the queue ]