HTTP client inestability problem


I am testing a sketch which do some measures and them composed a String wich is sended by http client.

It works well but about once a day, the Photon restarts.

I have read some people has the same problem using HTTP Client library (Photon Problem - http.get(request, response, headers) Causing reset) and the problem could be produced by something related with the dynamic memory and the inneficient use of strings. I am using 0.4.7 firmware in the photon.

Here is the part of the code where I prepare the data before sending:

request.path = "/emoncmsmonitor/input/post.json?&apikey="APIKEY"&node="+String(NODE)+"&json={voltage:"+String(emontx.Vrms)+",iload:"+String(ct1.Irms)+",pload:"+String(ct1.realPower)+",PACKETS:"+String(packets)+",RESTARTS:"+String(restart)+"}";    

http.get(request, response, headers);

APIKEY is defined as:

#define APIKEY "1d3d56982b3754ad1a2ccf0cf98a1327"                  

The rest of variables and constants are:

const int NODE = 7;
float emontx.Vrms;
float ct1.Irms;
float ct1.realPower;
int packets;
int restart;    

I think it could be improved with the use of sprintf but I cannot achieve to work, it compiles but the data doesn’t arrived.

Here is my try with the sprintf:

char* dataStringToSend;
    sprintf(dataStringToSend, "/emoncmsmonitor/input/post.json?&apikey=%s&node=%i&json={ct1Vrms:%1f,ct1Irms:%1f,ct1realpower:%1f,PACKETS:%i}",APIKEY,NODE,emontx.Vrms,ct1.Irms,ct1.realPower,packets);
request.path = dataStringToSend;
http.get(request, response, headers);

In this code APIKEY is define as: const char APIKEY[33] = "1d3d56982b3754ad1a2ccf0cf98a1327";

With the delays I have better stability results.

When it fails it restarts send some messages, and then restarts again some messages, etc.

Any help will be apreciated. Thanks

If you want to use sprintf() you need a char dataStringToSend[MAXLEN+1]; buffer otherwise you won’t have any RAM allocated for the string and sprintf() will just push your string to the arbitrary (possibly NULL) location to where your pointer happens to point.

What format do you expect to get with "%1f"?


I did other try with TCP client with similar results, in that case I use this:

sprintf(dataStringToSend, “GET /emoncmsmonitor/input/post.json?node=%i&json={power1:%f,voltaje:%f,irms1:%f,counter:%i}&apikey=%s”,

where I defined dataStringToSend as: char* dataStringToSend;

Sorry, in the copy paste I forgot to add the line:

dataStringToSend = new char[256];

This example sends two packets then:


  • Those variables are double. Examples values could be:
    • realPower: XXX.YY
    • apparentPower: XXX.YY
    • powerFactor: 0.9 (values between 0 and 1)
    • Vrms: XXX.Y
    • Irms; XXX.YY