I'm using an Arduino Uno + ethernet shield W5100 with DHT22 sensor and RTC to create a connected temperature sensor. All is working fine, I can read temperature from local network and remote (with dyndns).
But after few hours, the device is unreachable (from local or remote network). I see in sd card that data are still logged ok but I suspect some network problem.
I use a fixed IP adress and the standard Ethernet lib.
Do you now if I need to take some precaution/best practice in code to ensure a reliable connection on long term ?
Thank you
Setup of ethernet in setup()
// Setup Ethernet device
Ethernet.begin(mac, ip);
if (Ethernet.linkStatus() == LinkOFF) { #ifdef serverDEBUG
Serial.println(F("Ethernet cable is not connected.")); #endif
}
server.begin();
And in loop() I check if still connected (but it seems to be not working) :
while (Ethernet.linkStatus() == LinkOFF) { #ifdef serverDEBUG
Serial.println(F("Ethernet cable is not connected.")); #endif
// Setup Ethernet device
Ethernet.begin(mac, ip);
server.begin();
delay(4000);
}
I would remove the code from setup. All Ethernet code should be in loop. If you want the application to run all other functions while the Ethernet is setup or down. Create a state variable and do only one Ethernet thing any one time you run trough loop. Do not use any delays.
Additionally, check whether you need to use any .end(), .close() or other functions for releasing resources used by the classes you are using. So, when you detect a disconnect, reset all state variables and objects for the ethernet link.
To test it, just unplug the Ethernet cable and see what happens when you plug it back in. Your app should just continue to run and connect to the network.
You might try putting a very simple web server code that just returns something back to a client like "I'm still here", then keep checking to see if the server stays responsive to client request. This might help identify if other components of your code are the issue, or a lan issue.
I've been trying that for the last few days and I got the same problem with a simple version of my code.
I will try asap the solution explained by klaus