Arduino Bad request on HTTP after working well for a while

Hi folks!

i’m using an arduino uno to send analog data from 5 SCT-013-100 (Current Sensors) to a webserver, trough ENC28j60. It works fine for several hours, and there was one time it worked for 44hours straight, but suddenly it stops sending the correct message, and HTTP log on my server returns error code 400 (Bad Request).

I’ve tried to troubleshoot the error by forcing the network failure, power loss and the re-establish it again, and it returned sending data correctly and then presenting the same error afte a few hours.

My friend who’s developing the backend on server said it can be caused by an exception that the code is generating, and the error is not being treated, but i have no idea how to correct it.

Here follows my code:

#include <EmonLib.h>
#include <SPI.h>
#include <EtherCard.h>

EnergyMonitor emon1, emon2, emon3, emon4, emon5;

//Variaveis Ethernet
static byte mymac = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[850];
const char website PROGMEM = “”;
uint32_t timer;

void setup() {

Serial.begin(9600);

double CalibIrms = 60;

//Pino, calibracao - Cur Const= Ratio/BurdenR. 2000/33 = 60
emon1.current(A5, CalibIrms);
emon2.current(A5, 70);
emon3.current(A3, 90);
emon4.current(A3, 112);
emon5.current(A3, 75);

Serial.println(F("\n[webClient]"));

//Starts DHCP communication
if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0) {
Serial.println(F(“Failed to access Ethernet controller”));
}
if (!ether.dhcpSetup()) {
Serial.println(F(“DHCP failed”));
}

ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);

// // if website is a string containing an IP address instead of a domain name,
// // then use it directly. Note: the string can not be in PROGMEM.
char websiteIP = “52.67.190.99”;
ether.parseIp(ether.hisip, websiteIP);

ether.printIp("SRV: ", ether.hisip);

}

void loop() {

//Comunicacao Ethernet
ether.packetLoop(ether.packetReceive());

//Le os dados dos sensores e os transmite
if (millis() > timer) {
timer = millis() + 10000;
leDados();
}

}

void leDados(){

double Irms1,Irms2,Irms3,Irms4,Irms5;
Stash stash;
static byte session;

uint16_t Cal = 1480 ;

//Calcula a corrente
Irms1 = emon1.calcIrms(Cal);
Irms2 = emon2.calcIrms(Cal);
Irms3 = emon3.calcIrms(Cal);
Irms4 = emon4.calcIrms(Cal);
Irms5 = emon5.calcIrms(Cal);

Serial.print(Irms1);
Serial.println();
Serial.print(Irms2);
Serial.println();
Serial.print(Irms3);
Serial.println();
Serial.print(Irms4);
Serial.println();
Serial.print(Irms5);
Serial.println();

byte dadosSensor = stash.create();

// stash.print(F("[{“id”:null,“connectorId”:“16”,“timestamp”:“1”,“amperage”:“1”,“voltage”:“1”},"));
// stash.print(F("{“id”:null,“connectorId”:“17”,“timestamp”:“2”,“amperage”:“2”,“voltage”:“2”},"));
// stash.print(F("{“id”:null,“connectorId”:“18”,“timestamp”:“3”,“amperage”:“3”,“voltage”:“3”}]"));

stash.print(F("[{“id”:null,“connectorId”:“16”,“timestamp”:“21”,“amperage”:""));
stash.print(Irms1);
stash.print(F("" }, “));
stash.print(F(”{“id”:null,“connectorId”:“17”,“timestamp”:“21”,“amperage”:""));
stash.print(Irms2);
stash.print(F("" }, “));
stash.print(F(”{“id”:null,“connectorId”:“18”,“timestamp”:“21”,“amperage”:""));
stash.print(Irms3);
stash.print(F("" }, “));
stash.print(F(”{“id”:null,“connectorId”:“19”,“timestamp”:“21”,“amperage”:""));
stash.print(Irms4);
stash.print(F("" }, “));
stash.print(F(”{“id”:null,“connectorId”:“20”,“timestamp”:“21”,“amperage”:""));
stash.print(Irms5);
stash.print(F("" }] "));
stash.println();
stash.save();
int stash_size = stash.size();

// Compose the http POST request, taking the headers below and appending
// previously created stash in the sd holder.
Stash::prepare(PSTR(“POST http://52.67.190.99/dashservice/rest/public/connectordata/savelist/ HTTP/1.1 200 OK” “\r\n”
“Host:52.67.190.99/dashservice/rest/public/connectordata/savelist/” “\r\n”
“Content-Length: $D” “\r\n”
“Content-Type: application/json;charset=utf-8” “\r\n”
“\r\n”
“$H”),
stash_size,dadosSensor);

Serial.println();
Serial.print(F(“Dados Enviados”));

// send the packet - this also releases all stash buffers once done
// Save the session ID so we can watch for it in the main loop.
session = ether.tcpSend();

}

Thanks!

You neither check the return value of the packetLoop() call nor did you check what the server returns. In the example code for xively you can see what you should do with the return value of tcpSend() and how to react in the loop.

If you still experience the same problem after fixing above, please post what the server receives when it thinks it’s a bad request.