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

 ESP8266-NodeMCU-V1

In this comprehensive tutorial, we will delve into the intricacies of controlling digital pins on a NodeMCU microcontroller 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, 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 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

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

Setting Up the Software

  1. Open the Arduino IDE.
  1. Install the required libraries by going to Sketch > Include Library > Manage Libraries and searching for:
    • ESP8266WiFi
    • ModbusIP_ESP8266
    Untitled
  1. 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);
    }
    
  1. Replace "YourWiFiSSID" and "YourWiFiPassword" with your Wi-Fi credentials.
  1. Upload the code to your NodeMCU ESP8266 board by clicking the Upload button in the Arduino IDE.
Untitled%201

Interacting with the Modbus Server

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

Untitled%202

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

  1. Open your preferred Modbus TCP client software (e.g., ModScan, Node-RED, etc.).
  1. Connect to the IP address of your NodeMCU ESP8266 board and the specified port (default is 502).
Untitled%204

Untitled%203
  1. Write a value of 0 or 1 to the designated coil address to control the digital pin connected to the LED.
Untitled%205
Untitled%206

Conclusion

In conclusion, this tutorial has provided you with a comprehensive guide on how to control digital pins of a NodeMCU ESP8266 microcontroller 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.

Comments

  1. The Explanation is very interesting and clear , is there a video for that tutorial on youtube channel ?

    ReplyDelete

Post a Comment