Ethernet Output pauses sketch

After identifying the delays in the ethernet card output operation while writing data to an online log file as the cause of problems I am experiencing, I created a simple test sketch.

The purpose for the test is to confirm that delays in the ethernet operation, is in fact causing problems (delays) with the rest of the sketch.

I believe that I have confirmed this, as running the test sketch, and then unplugging the ethernet cable from my router for a few seconds, I have simulated what would occur if there were internet delays or an internet connection failure.

The output in the Serial Monitor, which should change every 1 second, slowed down with delays of just over 2 seconds when the ethernet cable was unplugged.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,101);
IPAddress gateway(192,168,1,254);
IPAddress subnet(255,255,255,0);
IPAddress myserver(123,123,123,123);
EthernetServer server(82);
EthernetClient client;

long LastMil = 0;

void setup(){

  pinMode(4, OUTPUT);      //ETHERNET SETUP
  Ethernet.begin(mac, ip, subnet, gateway); 
  server.begin();
  Serial.begin(9600);  
  LogItWebC(1000);
  Serial.println("Send an get1 in serial monitor to test client."); // what to do to test client
  pinMode(16, OUTPUT);      
  pinMode(17, OUTPUT);      
  pinMode(18, OUTPUT);      
  LogItWebC(1001);

}

void loop(){

  unsigned long currentMillis = millis();
  if (currentMillis - LastMil >= 1000){
    LogItWebC(currentMillis);
    LastMil = currentMillis;
    Serial.print("----------");
    Serial.println(currentMillis);
  }

}  //end of LOOP


void LogItWebC(long LogTxt){

  if (client.connect(myserver, 80)) {
    client.print("GET http://www.xxx.com/serveittest.php?data=");
    client.print("1----");
    client.print(LogTxt);
    client.println(" HTTP/1.1");
    client.println("Host: www.xxx.com");
    client.println();
    delay(250);
    client.stop();
    Serial.print("1----");
    Serial.println(LogTxt);
  } 

} // end of void LogItWebC()

The output in the Serial Monitor, which should change every 1 second, slowed down with delays of just over 2 seconds when the ethernet cable was unplugged.

Why does that surprise you? You are trying to connect to a server each time through loop. The Arduino talks to the ethernet shield, which assumes that it can talk to the internet. It takes a while to determine that it can't.

PaulS: Why does that surprise you? You are trying to connect to a server each time through loop. The Arduino talks to the ethernet shield, which assumes that it can talk to the internet. It takes a while to determine that it can't.

Hi PaulS

It does not surprise me, as I already suspected that would be the problem when I coded the test sketch.

However, it does create a grey area / blind spot in the code.

Let me give you an example :

I have an outdoor PIR sensor which detects motion - let's say there is an intruder on the premises ( as opposed to the daily visit of monkeys ).

The sketch receives the input from the PIR, and activates a warning siren on an output pin. It then tries to write the event to the online log file.

At the same time, I see the intruder on my CCTV system, and promptly press the panic button to set off alarms and summon the armed security reaction team.

However, the sketch is still trying to write the PIR event to the online log file (delayed by any number of internet problems), and therefore does not 'see' the signal on the input pin from the panic button.

However, the sketch is still trying to write the PIR event to the online log file (delayed by any number of internet problems), and therefore does not 'see' the signal on the input pin from the panic button.

Mission critical devices do not try to talk to the internet. They log locally, and offload uploading that data to some other device.

OK. Makes sense.

So what’s the ideal solution ?

2 Arduinos - 1 for the main sketch and the other with the ethernet board just to upload the log data ?

Communication between them ?
A common SD card ? Is that possible, or are we again getting into the possibilities for conflicts arena ?
Or a Serial connection to pass the log data to the second board ?

Would appreciate suggestions.

So what's the ideal solution ?

2 Arduinos - 1 for the main sketch and the other with the ethernet board just to upload the log data ?

Yes.

Communication between them ?

Yes. SoftwareSerial and two pins (plus ground) on each one.

A common SD card ? Is that possible

How? The hardware is connected to one Arduino. The other one can't possibly write to it.

Or a Serial connection to pass the log data to the second board ?

Yes. Or I2C if they are close enough. Serial is easier, though (for me, at least).

DaveO: OK. Makes sense.

So what's the ideal solution ?

2 Arduinos - 1 for the main sketch and the other with the ethernet board just to upload the log data ?

Communication between them ? A common SD card ? Is that possible, or are we again getting into the possibilities for conflicts arena ? Or a Serial connection to pass the log data to the second board ?

Would appreciate suggestions.

Use UDP, http and tcp in general are pretty bad at blocking bits and do not work well on unreliable networks. There are a real lot of reasons SNMP uses UDP in a one packet one response format it can work even when the network is nearly down. Worst is your waiting for an arp timeout before spitting the packet out. This means that some will not make it and you can determine if you need to implement some method to resend that data.

For a security bit I would want something polling that device that can send a potential alert if it fails to get a response. A second arduino wont make it better if your sharing the same data path to send that alarm. You could us one on the various INT's to handle always seeing that panic button and reacting to it immediately. If your going to connect two arduio's I would suggest i2c it's faster than serial though it can be hard to get it to go long distances.

Any reason you have a delay in your http outbound connection?

silasmoeckel: Any reason you have a delay in your http outbound connection?

Thanks for the advice.

Yes, a few reasons :

  1. it was in the sample code that I found when I was making the sketch.
  2. with my level understanding, I have no good reason to delete it ( nor any compelling reason to keep it ).
  3. it worked, so I didn't try to fix something that wasn't broken.
  4. monkey-see, monkey-do. :)