[SOLVED] Unable to use Collections API to post 2+ variables with Particle Electron

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();
    }
    
}

Hello mm510.

you can try with sprintf like this:

char resultstr[150];
sprintf(resultstr,"[{\"variable\": \"%s\",\"value\":%d}, {\"variable\": \"%s\",\"value\":%d}]",VARIABLE_ID_1, VARIABLE_ID_2, g_f1, g_f2 );

Or you could try with the ubidots library for photon, maybe with your electron can run.
Search it in contrib libraries

Hey @mm510 our Electron should arrive soon, we’ll give it a try and let you know how it behaves with our community library for Particle.

I tried the sprintf command previously without success. Something else is wrong (see a later message in this thread for details).

The Ubidots library, on the other hand, is actually working (finally some 201s! Three variables in one POST!). It was shockingly easy to implement. Thanks for the tip.

However, something weird happened. The data went to the wrong place. I started with two Ubidots data sources: “Particle” and “Electron.” Electron has the two variables I’m interested in (f1 and f2). I copied the variable ID code from f1 and f2 into my project. But when I ran the program, the data went into the Particle data source, not the Electron source. So I trashed the Particle data source, erased all of my variables, deleted my tokens, then created a new token and two new variables (f1_e and f2_e). So now I had only the Electron data source and two variables.

When I rebooted my Electron and started transferring data, the data went into the Particle data source with two newly created variables. But I just deleted Particle!

So I looked carefully at the diagnostics messages and then at the library files. I noticed that that there is a “name” : “Particle” statement hardwired into the library that seems to be pointing at the Particle data source, so when I pulled it off the shelf I got the Particle data source. The Web IDE doesn’t let me edit this library, so if I want to continue with the Ubidots library I will need to get it from github and learn how to include it (probably very easy).

The diagnostic messages from the Ubidots library for Particle (see other message in this thread) gave me something new to try in my original project that uses the HTTPCLIENT library. Thanks to an addition of some quotation marks, I am successfully using the “Collections” API with my Electron to send multiple variables in a single POST operation.

This is an example of the payload that I had been sending originally:

[{"variable": "56d...e12","value":208.0}, {"variable": "56d...442","value":45.0}]

When I add quotation marks around the values, like the line below, the POST operation works:

[{"variable": "56d...e12","value":"208.0"}, {"variable": "56d...442","value":"45.0"}]

Two items of note: 1) the API documentation for Collections does not recommend putting quotes around actual value, 2) I get a response status of -1. I don’t know if that is an artifact of the HTTPLCLIENT library or an actual response from Ubidots.

Since making the change I have been running my program for more than 1 hour sending three variables in a single POST. So far, so good…

You’re right; the “name”:“particle” is hardwired as default into the library. This is meant to be changed later on for your custom name from the Ubidots site. The real ID to identify the data source is the core id, which is written as a tag:

So you can do either one of these two things:

  1. change the name of the new datasource :

  1. Go to your existing Electron datasource and add a tag = coreid:

Thanks for your remark; we’ll check if we can let the user specify the Data source name from the Particle code - it does makes more sense.

Hope this helps