I have shiny new Particle Electron that I am trying to connect to Ubidots (Electron firmware 0.4.8). Ideally, I’d like to start sending batches of variables, presumably using the “collections” part of the API.
Using the HTTPCLIENT (V 0.0.5) in the Particle Library, I am able to send two variables using two independent POST commands (POST api/v1.6/variables/{variable_id}/values, “Writes a new value to a variable”), but when I try to up my game to posting “collections” (POST api/v1.6/collections/values, “Send values to several variables in a single request.”), I get a response status of “-1”. (I vaguely recall having the exact same problem with the Particle Core many months ago.)
This is how I seem to be populating the elements of the HTTP request (middle of variable ID replaced by dots):
Hostname: things.ubidots.com
Path: /api/v1.6/collections/values
Body: [{"variable": "56c...d16","value":119.000000}, {"variable": "56c...a30","value":134.000000}]
From this request, I get a response status of -1 (sometimes it’s 0, usually the first request after reboot).
Below is a skeleton of my Particle code showing the key elements of the procedure. Does anything look wrong? My basic connectivity is fine, as demonstrated by the ability to use POST for one variable at a time. Perhaps a bracket or quote is out of place, or could there be an issue with the HTTP library?
Thanks.
#include "HttpClient/HttpClient.h"
HttpClient http;
#define VARIABLE_ID_1 "56c...d16"
#define VARIABLE_ID_2 "56c...a30"
#define TOKEN "rVU...dyG"
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL }
};
http_request_t request_1;
http_response_t response_1;
#define CLOUD_UPDATETIMERINTERVALSEC 30
String resultstr;
double g_f1 = 0.0;
double g_f2 = 0.0;
unsigned long runTime;
unsigned long runTimeSec;
// Initialize
void setup()
{
request_1.hostname = "things.ubidots.com";
request_1.port = 80;
request_1.path = "/api/v1.6/collections/values";
Serial1.begin(115200); // communication between Electron and my device
Serial.begin(9600); // diagnostic feed
runTime = millis();
}
// Main loop
void loop()
{
static unsigned long cloud_UpdateTimer = millis(); //cloud_ update timer
//do sensor readings (populate the g_f1 and g_f2 variables)
sensorUpdate();
// Periodically send data to cloud
if (millis()-cloud_UpdateTimer > 1000*CLOUD_UPDATETIMERINTERVALSEC)
{
// Convert g_f1 and g_f2 into strings
String f1str = String(g_f1);
String f2str = String(g_f2);
resultstr = "[{\"variable\": \""VARIABLE_ID_1"\",\"value\":" + f1str + "}, {\"variable\": \""VARIABLE_ID_2"\",\"value\":" + f2str + "}]";
request_1.body = resultstr;
http.post(request_1, response_1, headers);
//reset update timer
cloud_UpdateTimer = millis();
}
}