ESP32 as modbus tcp client

Asgeir
Posts: 1
Joined: Tue Mar 19, 2024 11:27 am

ESP32 as modbus tcp client

Postby Asgeir » Tue Mar 19, 2024 11:43 am

Hello,
I'm having some issues getting the esp32 running as a modbus tcp client.
I was able to get it running as a server, communicating with Siemens 1500 PLC as a client.

But when trying with esp as client and PLC as server, it's not working.
The PLC seems to be waiting for a connection, but for some reason the esp is not successful in establishing one.

Maybe someone here can help me find what I'm doing wrong.
Link to the modbus library I'm using: https://github.com/emelianov/modbus-esp8266

Please see attached screenshots of PLC and ESP Serial monitor output.


ESP code:

Code: Select all

// WIFI LIB
#include <WiFi.h>
const char* ssid = "RDAUT_AP";
const char* password = "";
#define CONNECTION_TIMEOUT 10
// Set static IP
IPAddress local_IP(10, 0, 0, 100);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

//Modbus LIB
#include <ModbusIP_ESP8266.h>

//ModbusIP object
ModbusIP mb;
IPAddress remote(10, 0, 0, 50);  //PLC address;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  //***************WIFI SETUP*************************
  WiFi.mode(WIFI_STA);  //Optional
  // Configures static IP address
  WiFi.config(local_IP, gateway, subnet);
  WiFi.begin(ssid, password);
  Serial.print("\nConnecting to: ");
  Serial.println(ssid);

  int timeout_counter = 0;

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
    timeout_counter++;

    if (timeout_counter >= CONNECTION_TIMEOUT * 5) {
      Serial.println("\nRebooting...");
      timeout_counter = 0;
      delay(500);
      ESP.restart();
    }
  }
  Serial.print("\nConnection established to: ");
  Serial.println(ssid);
  Serial.print("Local ESP32 IP: ");
  Serial.println(WiFi.localIP());

  //***************MODBUS SETUP *************************
  mb.client();  //Start Modbus IP
  Serial.println("Modbus client started");

  mb.disconnect(remote);  // Close connection to slave and
  mb.dropTransactions();  // Cancel all waiting transactions

  //***************MODBUS SETUP END*************************

  //**************SETUP DONE**********
  Serial.println("Setup done");
}



void loop() {
  // put your main code here, to run repeatedly:
  if (!mb.isConnected(remote)) {  // Check if connection to Modbus Slave is established
    mb.connect(remote);           // Try to connect if no connection
    Serial.print(".");
  }

  mb.task();  // Modbus task
  delay(50);  // Pushing interval
}
Attachments
Untitled1.png
Untitled1.png (8.8 KiB) Viewed 2231 times
Untitled.png
Untitled.png (111.12 KiB) Viewed 2231 times

Who is online

Users browsing this forum: brocliath, Corand and 113 guests