Hi @itayd100,
Last month we discussed in this thread about the stability of the Spark when sending continuous HTTP requests. Following the suggestions there, I re-wrote the code to connect the Spark to Ubidots (below). Basically, it uses the function “sprintf” instead of concatenating strings to setup the HTTP request body, which does a better handling of the internal memory allocation.
#include "HttpClient/HttpClient.h"
/**
* Declaring the variables.
*/
HttpClient http;
#define VARIABLE_ID "xxxxxxxxxx"
#define TOKEN "xxxxxxxxxxxxxxxxxxxxxx"
float voltage = 0.0;
char resultstr[64];
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
void setup() {
pinMode(A0, INPUT);
request.hostname = "things.ubidots.com";
request.port = 80;
//Serial.begin(9600);
}
void loop() {
// Get variable
voltage = analogRead(A0);
// Send to Ubidots
request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
sprintf(resultstr, "{\"value\":%.4f}",voltage);
request.body = resultstr;
http.post(request, response, headers);
//Serial.println(response.status);
//Serial.println(response.body);
delay(1000);
}