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;
 }

}

scott180
Posts: 2
Joined: Wed Jun 30, 2021 5:27 pm

Re: ESP32 Arduino, Ethernet connection TCP Client

Postby scott180 » Wed Jun 30, 2021 5:32 pm

Did you get an answer to your question? I have the same situation. I have an Arduino Mega with the ethernet shield and everything is working fine. I'm looking now to port that to the ESP32-POE-ISO and the different ethernet libraries are causing issues.

khoih-prog
Posts: 53
Joined: Sat Feb 22, 2020 8:16 pm

Re: ESP32 Arduino, Ethernet connection TCP Client

Postby khoih-prog » Thu Jul 29, 2021 12:13 am

You can try this code using WT32_ETH01 (ESP32 + LAN8720A Ethernet)

Code: Select all

/*
   Modbus master example

   Turns lock on MDC on and off every 1 sec

*/

#include <SPI.h>

#include <WebServer_WT32_ETH01.h>     // https://github.com/khoih-prog/WebServer_WT32_ETH01

WiFiClient 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

// Select the IP address according to your local network
IPAddress myIP(192, 168, 1, 232);
IPAddress myGW(192, 168, 1, 1);
IPAddress mySN(255, 255, 255, 0);

// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);

//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);
  while (!Serial);

  Serial.print("\nStarting ModbusClient on " + String(ARDUINO_BOARD));
  Serial.println(" with " + String(SHIELD_TYPE));
  Serial.println(WEBSERVER_WT32_ETH01_VERSION);

  //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
  //           eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
  //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
  ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);

  // Static IP, leave without this line to get IP via DHCP
  //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
  ETH.config(myIP, myGW, mySN, myDNS);

  WT32_ETH01_onEvent();

  WT32_ETH01_waitForConnect();

  Serial.println();
  Serial.print(F("Starting connection to ServerIp : ")); Serial.print(ServerIp);
  Serial.print(F(", Modbus Port : ")); Serial.println(modbusPort);

  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
  }  
  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 79 guests