Modbus ethernet connection issue.

Hi. I am connecting an Arduino, ethrnet shield, xbee-shield with an xbee that reads reservoir levels from four reservoirs, and sends the data to a Logo!8 Siemens PLC through an ethernet Modbus connection. All devices is connected through a 5 port switch. Everything works 100%!
However, it only connects when my desktop PC is also connected in the network. It also keeps working for a while if I disconnect the desktop PC. But when I restart the Arduino without the desktop PC connection it would not connect at all. Do anyone by any change know why? The PC should not be part of the connection and it's only purpose is to load the program files to the individual devices. The Arduino, the Logo, and the HMI.
Even if my laptop is connected it would not connect.

Unit Comp.jpg

WinCC Setup.jpg

Unit Comp & Descrip.jpg

ArduinoUno_Level_Monitor_V2.ino (9.37 KB)

Unit Comp.jpg

WinCC Setup.jpg

Unit Comp & Descrip.jpg

with Ethernet.begin(mac, ip) the library uses defaults for the rest of the static IP configuration.
try to specify all static IP parameters

void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet)

Hi Juraj.
That did not work. Still the same problem. It’s like it’s using my PC as a router! It can’t start without it.

The system has been running now for about 24 hours without my PC. It looks to me like the system only needs my computer to start the modbus and ethernet connection and then it can run without it.

New discovery, it needs not the computer but the main Netgear router. The local ethernet is connected from my PC to the Netgear router via lan connections. I use it to connect to other devices like a printer and these projects. So when the Netgear router is connected the Arduino system starts up, but without it not.

Why do you have two Ethernet.begin lines?

Do you want a static IP address or one assigned via DHCP. You cannot have both.

There’s only one ethernet begin line!

Ethernet.begin(mac, ip); // subnet, gateway
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
}
// Serial.println(ip);

}

void loop() { // run over and over **********************************************************************

Although I have made lots of changes to try to make it work. I shall reload the latest copy.

My latest sketch. Still only starts when the router is connected.

ArduinoUno_Level_Monitor_V3.ino (10.3 KB)

you use DHCP service of the router with Ethernet.begin(mac).

don't call begin in every loop. it resets the Ethernet chip and requests an IP address from DHCP

Please explain. I don't know where I call the begin in every loop.

chrisvanwyk:
Please explain. I don't know where I call the begin in every loop.

void loop() { 
  if (Ethernet.begin(mac) == 0) {

Ok I see. So I’ve changed to this, and now it don’t start at all!

// Ethernet setup
#include <SPI.h>
#include <Ethernet.h>
#include “MgsModbus.h”

void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
byte mac = {0x90, 0xA2, 0xDA, 0x0F, 0x07, 0xD1};
byte ip = {192,168,0,4};
byte dns = {192,168,0,1};
byte gateway = {192,168,0,1};
byte subnet = {255,255,255,0};

EthernetServer server = EthernetServer(502);
// Ethernet setup end

String stringLev = “”;
String str-------------------------------------------------------------
cut out all variables…

int c = 0;
int receive1, receive2;
char a(Lev[15]);

MgsModbus Mb;

void setup() {

Serial.begin(9600);

}

void loop() {

v = ‘\0’; Lev[v] = ‘\0’;

Mb.MbsRun(); // Start Ethernet Modbus

Ok, ok I got it to work this way!! :roll_eyes:

Thanks a lot for your help, Juraj. I now understand what went wrong and how it works.

// Ethernet setup
#include <SPI.h>
#include <Ethernet.h>
#include “MgsModbus.h”

byte mac = {0x90, 0xA2, 0xDA, 0x0F, 0x07, 0xD1};
byte ip = {192,168,0,4};
byte dns = {192,168,0,1};
byte gateway = {192,168,0,1};
byte subnet = {255,255,255,0};
void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);

EthernetServer server = EthernetServer(502);
// Ethernet setup end

*variables… *

char a(Lev[15]);

MgsModbus Mb;

void setup() {

Ethernet.begin(mac, ip, dns, subnet, gateway);
Serial.begin(9600);

}

void loop() {

v = ‘\0’; Lev[v] = ‘\0’;

Mb.MbsRun(); // Start Ethernet Modbus

void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
is in Ethernet.h. you don't have to put it in the sketch

Thank you for your assistance as well. My project is installed and running 100%. There are a few other aspects at play to complete still. I shall in due course publish my project for everyone to view. I have seen a lot of questions, especially on the Siemens forums about the modbus connections between the Arduino and the Siemens PLC. The answer or fix was actually very simple yet is hidden under a misconception of how the ethernet instructions works!

The graphs on the display below is actual reservoir levels of four reservoirs, and is read with ultrasonic sensors feeding info int xbee radios, then transmitted to one receiving xbee radio, that is feeding an Arduino UNO using a xbee-shield, that in return sends this values to the LOGO!8 PLC via an Arduino ethernet shield. It happens in real-time, and is running 24/7. The PLC now determine according to these reservoir levels which reservoir to fill. This unit/system is in a water purifying plant with one 35KW pump and some control valves that will pump water where and when to fill a selected reservoir. No reservoir will be allowed to have less than 60% of water.
This system has so many advantages!
See Picture attached.
WP level control now fully functional on all reservoirs.
R1 shows empty, because it is empty for repairs!

Regards
Chris.

LOGO TDE.jpg

LOGO TDE.jpg

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.