Ubidots / Photon - Multiple Variables_ID

Hi All,

I am getting a little confused by how to add multiple Variables_ID’s to the Photon’s code for Ubidots.

I get that my photon has a Token and that each variable needs an ID but cant work out how to state it…my code at the mo just has one variable (see below) but do I just create a new variable headed with VARIABLE_ID2 or something?

Thanks a lot

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient/HttpClient.h"

// This code is based on the original Http Example by nmattisson, see https://github.com/nmattisson/HttpClient

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

    #define VARIABLE_ID "XXXXXXXXXXXXXXXXXXXXX"
    #define TOKEN "XXXXXXXXXXXXXXXXXXXXXX"

    HttpClient http;
    int switchPin = D2; // MicroSwitch is connected to D2
    int val; //variable for reading the pin status
    int buttonState; //variable to hold the button state
    unsigned int  counter = 0;
    char          msg[1024];
    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(switchPin, INPUT); // Initialize D2 pin as input
        buttonState = digitalRead (switchPin); // read the intial state
                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
       {
        val = digitalRead(switchPin); //read input value and store it in val
            if (val != buttonState); {       //the button state has changed!
                if (val == LOW) {              //check if the button is pressed
                    counter++;             //increment the button presses variable

        Serial.println("Sending data ...");

        request.body = "{\"value\":" + String(switchPin) + "}";

        // Post request
        http.post(request, response, headers);
        Serial.println(response.status);
        Serial.println(response.body);
        
        buttonState = val;               //save the new state in our variable

        nextTime = millis() + 1000;
    }
            }
       }
    }

@Nnpnew
To post another variable you first need to create a second variable for whatever you are trying to display on the Ubidots website. Then find the ID of the variable from ubidots and put this in your code as a second variable ID token such as VARIABLE_ID2 or whatever custom name you want to call it with the appropriate ID string following it.
You may need to move the section where you are setting the request.path for the variables to the loop where you are posting them. If you do not do this then the path will not be reset each time you try and upload the variables to Ubidots and you will be sending both of the values to the same ubidots variable.

Something similar to the following code may work for you, let me know if you are still having problems.

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

#define VARIABLE_ID "XXXXXXXXXXXXXXXXXXXXX"'
#define VARIABLE_ID2 "XXXXXXXXXXXXXXXXXXXXX"
#define TOKEN "XXXXXXXXXXXXXXXXXXXXXX"

HttpClient http;
int switchPin = D2; // MicroSwitch is connected to D2
int val; //variable for reading the pin status
int buttonState; //variable to hold the button state
unsigned int  counter = 0;
char          msg[1024];
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(switchPin, INPUT); // Initialize D2 pin as input
    buttonState = digitalRead (switchPin); // read the intial state
            request.hostname = "things.ubidots.com";
    request.port = 80;
    Serial.begin(9600);
}

void loop() {
    if (nextTime > millis()) {
        return;
    }
    // Read sensor value
    val = digitalRead(switchPin); //read input value and store it in val
        if (val != buttonState); {       //the button state has changed!
            if (val == LOW) {              //check if the button is pressed
                counter++;             //increment the button presses variable

    Serial.println("Sending data ...");
    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values?token="TOKEN;
    request.body = "{\"value\":" + String(switchPin) + "}";

    // Post request
    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
    
    // Send Second data point
    Serial.println("Sending data  2...");
    request.path = "/api/v1.6/variables/"VARIABLE_ID2"/values?token="TOKEN;
    request.body = "{\"value\":" + String(Second_Value) + "}";

    // Post request
    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
    buttonState = val;               //save the new state in our variable

    nextTime = millis() + 1000;
}
1 Like

Thanks for that will give it a try

hi @SailorDude.

Finally got around to testing and it worked great, thanks for the help. I have a door magnet connected to D0 on the Photon and have it setup to transmit to Ubidots (Variable ID 2) to say when its been triggered. Only problem is that I cant work out how to get Ubidots to just display the last time it was opened…its just keeps displaying the below:

I can see the raw data coming in though on a table…it just counts from the moment it was opened or triggered to when its closed again. This is fine but I would like the above pic to say opened at 12.53pm or something. Any help much appreciated.

Cheers

Are you trying to have the door sensor only notify you when you open the door or do you want a running indicator showing the current status of the door?
I think that you might be overwriting the data after the door has been opened but I don’t know for sure without seeing your code. Upload your code and I will probably be able to help you more.

If you only want to know when the door was last opened you should put an indicator in your program that does not let the photon upload any more data to ubidots until it is reset.
This could be done in a manner similar to what follows,

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

#define VARIABLE_ID "XXXXXXXXXXXXXXXXXXXXX"'
#define TOKEN "XXXXXXXXXXXXXXXXXXXXXX"
HttpClient http;

// 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;

int DoorPin = D0;
int val = 0;
int indicator = 0;
int time_indicator = 0;
reset_time = 10000 // reset time is currently set at 10 seconds


void setup(){
    pinMode(DoorPin, INPUT);
    request.hostname = "things.ubidots.com";
    request.port = 80;
}

void loop(){
    int val = digitalRead(DoorPin);   // reads the value from the door indicator

    if (val >= 200) {   // set this to whatever level is needed for the sensor to indicate that the door is open
        if (indicator == 0) {  //checks to see if the door has been opened recently
            // posts the value of the door if it has been opened, and only if it has been opened
            request.path = "/api/v1.6/variables/"VARIABLE_ID"/values?token="TOKEN;
            request.body = "{\"value\":" + String(val) + "}";

            // Post request
            http.post(request, response, headers);
            Serial.println(response.status);
            Serial.println(response.body);
            indicator = 1;   // sets indicator so that the code knows the door has been opened recently and that new 
                                    // data will not be uploaded until the indicator has been reset 
            time_indicator = millis()  // gets the current system time
        }
    }
    if (millis()-time_indicator >= reset_time){  // resets the indicator that that it will register another door opening after
                                                 // a set time interval defined at the top of the script by "reset_time"
        indicator = 0;
    }
}

Hope that this helps you with your project, happy hacking

1 Like

Thanks.

Heres my code below, I just want to register that the door has been opened and then have the system remember that the door was opened so not to keep telling the system its still open…

void ReadMagnet()
{  
  magnetValState = digitalRead(magnetPin);

  if(magnetValState == HIGH){      // Was motion detected
    digitalWrite(led, HIGH);     // Turn ON the LED
    
    Serial.println("Sending data  2...");
    request.path = "/api/v1.6/variables/"VARIABLE_ID_2"/values?token="TOKEN;
    request.body = "{\"value\":" + String(magnetPin) + "}";

    http.post(request, response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
                }
    
    else
        {
    digitalWrite(led, LOW);  // Turn the LED off
        }  
}

Then once the door has been closed, restart the program loop again with the other functions in the overall program.

Thanks!!

You’d use a global (or static) variable which holds the last state and only enter the publishing branch when you see a change.

1 Like