Ubidots and the Particle Photon

Hey guys, I was able to send data to Ubidots using the Photon, you can find the code below. As @bko suggests, it’s a good idea to test your Ubidots credentials beforehand, for example pasting this into your browser:

http://things.ubidots.com/api/v1.6/variables/?token=YOUR-TOKEN-HERE

This should list all the variables in your account. Once you’ve verified your token works, you can check this code which just worked for me (based on the original example in the http repo but simplified):

#include "HttpClient/HttpClient.h"
#include "application.h"

#define VARIABLE_ID "YOUR-VARIABLE-ID"
#define TOKEN "YOUR-UBIDOTS-TOKEN"

HttpClient http;
int lightLevel = 0;
unsigned int nextTime = 0;    // Next time to contact the server

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    { "Content-Type", "application/json" },
    { 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;
    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values?token="TOKEN;
    Serial.begin(9600);
}

void loop() {
    if (nextTime > millis()) {
        return;
    }
    // Read sensor value
    lightLevel = analogRead(A0);

    Serial.println("Sending data ...");
    
    request.body = "{\"value\":" + String(lightLevel) + "}";

    // Post request
    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);

    nextTime = millis() + 1000;
}

Feel free to post an entry in our community in case you think it’s an issue with your account.