Ethernetshield intermittent connection

I’m using an Arduino Neo, Arduino 022, Ethernet Shield (SD? Not sure of version, bought very recently).

I’m using the Ethernet2 library to connect to a local http server, parse a few lines, and echo one of the lines back with Serial.

Problem is that client.connect() works about 1 time out of 5. The other times it just times out.

Code below, thanks for any help/advice. Server is apache running on windows if that makes any diff.


UPDATE: I’ve updated the code a bit now (I’ll leave the original code in this post for now), and have different (but worse) behaviour. The updated code loops making requests through the client object, waiting 2 seconds between requests. This updated code just hangs up at a random point, with different random behaviours. Very annoying, and hard to debug :frowning: Anyone have issues with Arduino/Ethernet shield related to heat or power?

#include <Ethernet2.h>

#include <aJSON.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 4, 17 };
byte gateway[] = { 192, 168, 4, 1 };
byte server[] = { 192,168,4,4 }; // Google
String lineBuffer ; 
String dataBuffer ; 
boolean inHeader ;
Client client(server, 80);

int heatingStatusPin = 2 ; 
int programRunningPin = 3 ; 
int programEndPin = 4 ; 

void setup()
  pinMode(heatingStatusPin, OUTPUT) ; 
  pinMode(programRunningPin, OUTPUT) ; 
  pinMode(programEndPin, OUTPUT) ; 

  digitalWrite(heatingStatusPin, LOW) ; 
  digitalWrite(programRunningPin, HIGH) ; 
  digitalWrite(programEndPin, LOW) ; 

  Ethernet.begin(mac, ip, gateway);



  if (client.connect()) {
    inHeader = true ; 
    client.println("GET /status.php HTTP/1.0");
  } else {
    Serial.println("connection failed");

void loop()
  char inChar ; 
  while (client.available()) {
    inChar =;
    if (inChar == '\n') {
     processLine(lineBuffer) ;
     lineBuffer="" ;
    } else {
     lineBuffer += inChar ; 
  if (!client.connected()) {
    Serial.print(dataBuffer) ; 

    digitalWrite(programRunningPin, LOW) ; 
    digitalWrite(programEndPin, HIGH) ; 

// processing each line in turn. must be setup beforehand with inHeader = true

void processLine(String fooBar) {

  int i = fooBar.indexOf("Content-Type: application/json") ;
  if (i == 0) {
    inHeader = false ;
    return ; 
  if (!inHeader) { 
    dataBuffer += fooBar ; 

I believe the Ethernet shield only allows 4 active sockets at a time. Is it possible you have other dormant/stale connections still attached to the Ethernet shield at the times you are getting timeouts?

Thanks for the reply. I don't think it was a socket issue, but perhaps some kind of resource issue.

I've commented out the JSON library, gone back to the original Ethernet library, made sure I'm doign cilent.stop() between each request, and it seems to be working OK right now.

The problem being that I need that functionality :-/

Any suggestions on how to use less memory here? I need a strcmp() type functiona and could only find that in the String object, which might be a bit heavy..?

For anyone else interested, this was probably a memory issue.

Make your own MemoryFree library using the header and source files here to see memory usage:

In my case, having < 200 bytes free seemed to be able to cause a crash.