ESP32 Arduino, Ethernet connection TCP Client

Roking1
Posts: 1
Joined: Mon Oct 05, 2020 5:55 pm

ESP32 Arduino, Ethernet connection TCP Client

Postby Roking1 » Tue Oct 06, 2020 5:37 am

Hello Guys,

Description:

I like to transfer data from the Olimex Board to a controller for our screwdrivers (TCP Client). The controller for the screwdriver uses Modbus protocol to communicate (Ethernet port on the controller). With an Arduino Leonardo and the original Ethernet Shield 2 (controller W5500) it could get this to work. With the Arduino Leonardo I used the code Modbus_master (code below). This Code is copied form the Arduino "Chat Server" example and uses the Ethernet2.h library (very similar to the ethernet.h library). Since the Olimex uses another ethernet chip LAN8710A, I can not use the same library and code. For this board, the library ETH.h must be used. I could already successfully work with the Olimex "ETH_LAN8720" example and print out the local IP Address. Now I am looking for an example cold similar like the Arduino "Chat Server" which I can modify so I can get an ethernet to ethernet connection.

Arduion ChatServer: https://www.arduino.cc/en/Tutorial/Libr ... ChatServer

Hardware:
Board: Olimex ESP32-POE-ISO
IDE: Arduino IDE / PlatformIO
Computer OS: Windows 10

Code: Select all

/*
 * Modbus master example
 * 
 * Turns lock on MDC on and off every 1 sec
 * 
 */

#include <SPI.h>
#include <Ethernet.h>
EthernetClient MbmClient; // Modbus client
byte mbmsglock[] = {0x00,0x01,0x00,0x00,0x00,0x06,0x01,0x06,0x0F,0xA1,0x00,0x01}; // Modbus message 4001: MDC Lock 
IPAddress ServerIp(192, 168, 1, 100); // IP address of MDC
IPAddress ip(192, 168, 1, 99); // IP address of arduino ethernet shield
IPAddress gateway(192, 168, 1, 1); // Gateway IP
IPAddress subnet(255, 255, 255, 0); // Subnet mask
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x94, 0xB5 }; // MAC address of the ethernet shield
volatile long modbusPort = 502; //Modbus Port
bool lockbit = 0; // lock boolean

void setup() {
 Serial.begin(115200); // init serial connexion
 Ethernet.begin(mac, ip, gateway, subnet); // start ethernet interface
 MbmClient.connect(ServerIp,modbusPort); // connect ethernet device
}

void loop() {
 
  if (!MbmClient.connected()) { // If client not connected
  MbmClient.stop();             // Close connexion
  Serial.println("Reconnect"); 
  delay(50);
  MbmClient.connect(ServerIp,modbusPort); // Connect to MDC
  MbmClient.setConnectionTimeout(100); // set timeout to 100ms
  }  else {
   for(int i=0;i<11;i++) { // write modbus message to client byte by byte
      MbmClient.write(mbmsglock[i]);
    }
  MbmClient.write(byte(lockbit)); // write last byte: lock status
  delay(10);
  String reponse="";
  while (MbmClient.available()) { // while connection still alive
    char c = MbmClient.read();    // read client response 
    reponse += String(c, DEC);    // build the response in "reponse" string
    //Serial.print(c, DEC);
  }
  //MbmClient.stop();
  if (reponse.startsWith("0100061615-950")){ // if response looks like "0100061615-950"
      Serial.print("Modbus lock set to "); 
      Serial.println(lockbit);

  } else { // message does not comply with what expected
      Serial.println("Erreur Message");
  }
  }
 delay(1000); // wait 1sec
 if (lockbit) { // change lockbit
  lockbit = 0;
 }else {
  lockbit = 1;
 }

}

Who is online

Users browsing this forum: No registered users and 11 guests