Ethernet2 error handling

Hi,

I'm making a weatherstation for my sailclub, which besides showing data on an LCD display also transfers it to a MySQL database so you can look at the data from home. Everything is working fine, I can gather all the data, show it on the LCD screen and transfer it successfully to the database.

Since the club is on a remote location, I started adding some error handling routines so it can recover from power loss and connectivity loss. The power loss got covered by using the watchdog routines so it will reset itself if the DHCP server is not responding yet.

Connectivity loss is not working at this moment. I'm trying to simulate all kind of failures, like just attaching it to an unconnected ethernet hub or pointing to an URL that does not exist and to my big surprise it doesn't complain at all and reports the connection is still working.

During my debugging I discovered that client.connect is always returning success even if it must fail. I made a second version of my sketch that I stripped down to the essentials.

int i = client.connect(server, 80);
// Serial.print("RC = ");
// Serial.println(i);
if (i == 1) {
//if (client.connect(server, 80)) {
client.println(datastring);
...

If I take the client.connect out of the if statement it kind of works. At least in the simple version it works, in the original version it's erratic. Bear in mind that I'm trying to improve the error handling, so when things go wrong. When there's no connectivity issue it works.

I read on this forum that ethernet2 SPI is kind of flakey, I followed the recommendation to set pin 4 high but it didn't help.

I'm using the official ethernet2 shield from arduino.org, the UNO board is a rev3 board from Ali Express, as for the LCD too. I switched to a REV3 board so the pins are compatible between both and can be easily stacked. On these boards SCL/SDA have their own pins.

I'm powering it externally over a 9V power supply, since the wind sensors require a higher voltage than 5 volt

The power rails are properly decoupled, I use a TSR 1-2450 5V/1A voltage regulator to power the Arduino from the 9V supply.

I added a couple of pictures + the two sketches

simple.ino (2.68 KB)

ws.ino (5.31 KB)

I just looked through the release notes of 1.8.5. According to this page Libraries - Arduino Reference the ethernet2 is now part of the standard libraries. I can't find it in a stock ide installation. There's just the Ethernet library

Try this. From the Menu bar:
Sketch -> Include Library -> Manage Libraries
In the "filter your search" box, enter "ethernet2".
Click on "More info", then "install".

Thanks, but this invalidates the statement on the release note that it's part of the standard libraries. I must be a typo inside their docs I guess.

Anyhow after some further testing I figured out the issue is linked to the fact I'm calling the ethernet library within an interrupt handler routine, which is clearly not a good idea. If you add some delayMicroseconds here and there it will eventually work.

For now I'll rewrite the interrupt handler to just set a flag when it hits the timer and I'll handle the rest in the main loop, sure the timing will be a little bit off but that doesn't matter for the application I'm writing

If you don't want to install the correct library for your ethernet controller, that is your bad.

I don't experience the error you describe if I use the correct library. connect returns 1 on success, and 0 or a negative value if fail.