Hi everyone, for my end of degree project i have to use my ESP32 WeMos Board with Modbus over ethernet. I know that ESP32 is usually used with wifi but i made the suitable modifications to adapt ethernet library for ESP32, therefore, currently, it works for sending data through ethernet. Now i want to use ethernet to develop MODBUS TCP over it. I saw that last year our mate WVESP32 made more or less the same but due to that conversation is old i’m going to post this for everybody as well.
What modifications do i have to do to implement that? I append my declaration code and also setup code.
I also add the warnings that i get.
I hope you can help me. Thank you!
Code:
#include <Ethernet.h>
#include <ArduinoModbus.h>
//#include "local_config.h"
#include <ModbusTCPServer.h>
bool _1s;
unsigned long TimeAct, TimePrev, HodingResult, InputResult, HeartBeat, i, StartingAddr;
long Cmd;
EthernetServer EthServer(502);
ModbusTCPServer modbusTCPServer;
void setup() {
// Ethernet Settings
/*
byte mac[] = { 0x4E, 0xA0, 0xBE, 0x3F, 0xFE, 0x0F }; // Define MAc address
byte ip[] = { 192, 168, 1, 23 }; // Define IP address
byte subnet[] = { 255, 255, 255, 0 }; // Define SubNEt mask
*/
uint8_t eth_MAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
/*
* Define the static network settings for this gateway's ETHERNET connection
* on your LAN. These values must match YOUR SPECIFIC LAN. The "eth_IP"
* is the IP address for this gateway's ETHERNET port.
*/
IPAddress eth_IP(192, 168, 1, 120); // *** CHANGE THIS to something relevant for YOUR LAN. ***
IPAddress eth_MASK(255, 255, 255, 0); // Subnet mask.
IPAddress eth_DNS(192, 168, 1, 2); // *** CHANGE THIS to match YOUR DNS server. ***
IPAddress eth_GW(192, 168, 1, 0); // *** CHANGE THIS to match YOUR Gateway (router). ***
// initialize the ethernet device
Ethernet.begin(eth_MAC, eth_IP, eth_MASK); // Assign MAC, IP, and subnet mask
Serial.begin(9600);
EthServer.begin(); // start listening for clients
modbusTCPServer.begin(); // start listening for clients
// Define Holding register:
HodingResult = modbusTCPServer.configureHoldingRegisters(0, 100);
InputResult = modbusTCPServer.configureInputRegisters(0, 100);
Serial.print("Holding Reg init result =");
Serial.print(HodingResult);
Serial.print("\n");
Serial.print("Input Reg init result =");
Serial.print(InputResult);
Serial.print("\n");
Serial.print("Modbus server address=");
Serial.println(Ethernet.localIP());
Serial.print("\n");
}
Warnings: