Hello All,
I am using an Arduino Uno with Ethernet Shield on ARDUINO1.0
. I am writing some logging software, which continuously sends HTTP requests, but after 4 connections errors (Errors appear to occur randomly; I believe these to be be network problems), the Arduino can no longer connect to the server at all (Infinite loop of connection errors).
I recall somewhere mentioning maximum open connections being 4, but I believe that I correctly close the connections each time. Please do correct me if I am wrong.
#include <SPI.h>
#include <Ethernet.h>
EthernetClient client;
char* testMessage = "{\"test\":\"test\"}";
byte mac[] = {0x00,0x23,0xDF,0x82,0xD4,0x01};
IPAddress server({redacted});
byte localIP[] = {155,198,114,4};
int port = {redacted};
void setup(){
Serial.begin(9600);
Ethernet.begin(mac, localIP);
delay(1000);
Serial.println("Arduino Started");
}
void loop(){
httpPut(testMessage, "/topic/:test/put?sessionId=SESpbn4u1g5k&expiry=30s", "201");
}
/* Does a HTTP PUT request to the server */
bool httpPut(const char* data, const char* url, char* expectedStatusCode){
if (client.connect(server, port)){
client.print("PUT");
client.print(" ");
client.print(url);
client.print(" ");
client.println("HTTP/1.0");
client.print("Content-length: ");
client.println(strlen(data));
client.println();
client.println(data);
client.println();
client.println();
while (!client.available()){
delay(1);
}
if (checkStatusCode(expectedStatusCode)){
while(client.available()){
char c = client.read();
}
}else{
Serial.println(F("Error in httpPut(...), returned unexpected error code"));
client.stop();
client.flush();
if (client.connected()){
Serial.println(F("Error: Client still connected after calling stop()"));
}
return false;
}
}else{
Serial.println(F("Error in httpPut(...), could not connect"));
client.stop();
client.flush();
if (client.connected()){
Serial.println(F("Error: Client still connected after calling styop()"));
}
return false;
}
client.stop();
client.flush();
if (client.connected()){
Serial.println(F("Error: Client still connected after calling styop()"));
}
return true;
}
/* Check response status code from server. Used by internal HTTP request functions. */
bool checkStatusCode(char* expected){
bool status = true;
// Read the HTTP version
for (int i=0;i<9;i++){
char c = client.read();
}
for (int i = 0;i<3;i++){
char c = client.read();
if (c != expected[i]){
return false;
}
}
return true;
}
The Serial output:
Arduino Started
Error in httpPut(...), could not connect
Error in httpPut(...), could not connect
Error in httpPut(...), could not connect
Error in httpPut(...), could not connect
← After this point, connection error loops infinity.
Error in httpPut(...), could not connect
Error in httpPut(...), could not connect
Error in httpPut(...), could not connect
Hopefully this is a trivial problem, but any help would be appreciated.