Hello,
System: Ethernet lib 1.1.2, ATMega 2560, Ethernet shield, arduino IDE 1.6.12
Description: client.connect() returns 0 after several successful POST requests
I am sending a small amount of data into an elasticsearch server using POST request every 2 seconds. After a while, could be 15 minutes, could be hours, Ethernet client does not manage to connect to my server ( Server up and running for sure ). At this step, The ATMega is not pingable anymore. The only way I have found to make it working again is to unplug/plug the power source.
Ive also attempted to add Ethernet.begin(mac); when it fails. Sometime it manage to recover and get a IP from the DHCP, sometime it does not.
Also, ive tried to soft reset, but it does not recover from it.
Ive made sure I have the following line in EthernetClient.cpp as mentioned in Ethernet fails connecting after a while -Freetronics Forum
int EthernetClient::connect(IPAddress ip, uint16_t port) {
if (_sock != MAX_SOCK_NUM)
return -1;
for (int i = 0; i < MAX_SOCK_NUM; i++) {
uint8_t s = W5100.readSnSR(i);
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
_sock = i;
break;
}
}
And you will find below the code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
#include <TimeLib.h>
void setup() {
Serial.begin(9600);
while (!Serial) {}
Ethernet.begin(mac);
Serial.println(Ethernet.localIP());
}
void loop() {
String dataES = "{\"location\": \"office\"}";
if(!postPage("192.168.1.68",9200,"/sensors/temperature",dataES))
Serial.print(F("Fail "));
else {
Serial.print(F("Pass "));
Serial.println(dataES);
}
delay(1000);
}
byte postPage(char* domainBuffer,int thisPort,char* page, String thisData)
{
int inChar;
char outBuf[64];
int TIMEOUT_REPLY = 5000;
Serial.print(F("connecting..."));
if(client.connect(domainBuffer,thisPort) == 1){
Serial.println(F("connected"));
sprintf(outBuf,"POST %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",domainBuffer);
client.println(outBuf);
client.println(F("Connection: close\r\nContent-Type: application/json"));
sprintf(outBuf,"Content-Length: %u\r\n",thisData.length());
client.println(outBuf);
client.print(thisData);
}
else {
Serial.println(F("failed"));
}
unsigned long ctime = millis() + TIMEOUT_REPLY;
while(client.connected()) {
if ( millis()>ctime ) {
Serial.println("NO RESPONSE WITHIN PRESCRIBED TIME");
break;
}
while(client.available()) {
inChar = client.read();
Serial.write(inChar);
delay(1);
}
delay(1);
}
Serial.println();
Serial.println(F("disconnecting."));
client.stop();
return 1;
}
Any idea ? I have been battling for months on this problem. I am bit clueless
Thank you very much