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.
JL
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 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);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect()) {
inHeader = true ;
Serial.println("connected");
client.println("GET /status.php HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
char inChar ;
while (client.available()) {
inChar = client.read();
if (inChar == '\n') {
processLine(lineBuffer) ;
lineBuffer="" ;
} else {
lineBuffer += inChar ;
}
}
if (!client.connected()) {
Serial.println();
Serial.println("Done.");
client.stop();
Serial.print(dataBuffer) ;
digitalWrite(programRunningPin, LOW) ;
digitalWrite(programEndPin, HIGH) ;
for(;;)
;
}
}
// 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 ;
}
}