[SOLVED] Sending position to ubidots with webhooks and particle electron


#1

Hello,

I’ve been working with Particle and Ubidots since last week and I wanted to send location over a Particle Electron to Ubidots using webhooks but I have problems with it.

I’ve found the way to send other kind of data like simple variables and position using tokens and through TCP/UDP/HTTP protocols and also I’ve tested with a Particle Photon using webhooks and it’s working very well.

The problem comes when I’m testing the same code with Particle Electron, is always sending 0.0 for latitude and longitude variables in the context. So I wanted to know if I can get some help from you guys. How can I send location using a Particle Electron instead of a Particle Photon? Is there something that am I missing?

Thank you so much!

// This #include statement was automatically added by the Particle IDE.
#include <Ubidots.h>
#include <google-maps-device-locator.h>


GoogleMapsDeviceLocator locator;

#define pingPin D5
#define readPin D0
FuelGauge fuel;

const char* WEBHOOK_NAME = "Ubidots";

Ubidots ubidots("webhook", UBI_PARTICLE);

float acc = 0, latitude = 0.0, longitude = 0.0;

void setup() {
    Serial.begin(9600);
}

void loop() {
    locator.withSubscribe(locationCallback).publishLocation();
    read_sensor();
    delay(20000);
}


void read_sensor() {
    
    uint32_t duration, inches, cm;
    
    // Reserves memory to store context key values, add as much as you need
    char str_lat[30];
    char str_lng[30];
      
    float batt = random(0,10), value = 1;
    
    /* Saves the coordinates as char */
    sprintf(str_lat, "%f", latitude);
    sprintf(str_lng, "%f", longitude);

    /* Reserves memory to store context array */
    char context[50];

    /* Adds context key-value pairs */
    ubidots.addContext("lat", str_lat);
    ubidots.addContext("lng", str_lng);

    /* Builds the context with the coordinates to send to Ubidots */
    ubidots.getContext(context);
    
    cm = duration/10; // converts the range to cm
    inches = random(0,50);//cm/2.54; // converts the range to inches
    
    ubidots.add("battery", batt);
    ubidots.add("inches", inches);
    ubidots.add("position", value, context); // Sends the position
    
    bool bufferSent = false;
    bufferSent = ubidots.send(WEBHOOK_NAME, PUBLIC);  // Will use particle webhooks to send data
    
    if(bufferSent){
      // Do something if values were sent properly
      Serial.println("Values sent by the device");
    }
}

void locationCallback(float lat, float lon, float accuracy) {
    // Handle the returned location data for the device. This method is passed three arguments:
    // - Latitude
    // - Longitude
    // - Accuracy of estimated location (in meters)
    
    latitude = lat;
    longitude = lon;
    acc = accuracy;
}

#2

Greetings, if you are sending as lat and lng 0.0 the issue seems to be related with the function to retrieve your coordinates, as zero is the default value for your location variables. Can you make sure that the locationCallback is updating the global location variables?


#3

Hello and thanks for your reply!

After made some few tests with the particle photon, I realize that it was required a delay in the code to wait to save the location data into the callback function, then I was able to send that data to the particle cloud + ubidots.

with a delay of 5 seconds or 6000 seconds was enough to be able to send data to ubidots in a clean way and since I plan to use some deep sleep modes in the particle, this was something required on each location reading. On my particle Electron was required to set a value between 5 - 10 seconds (I finally used 10 seconds) to be able to send location data.

Your comment help me to debug better the code, thanks!

// This example sends data to a variable to 
// Ubidots using Particle Webhooks.

/****************************************
 * Include Libraries
 ****************************************/

//#include "Ubidots.h"
#include <Ubidots.h>
#include <google-maps-device-locator.h>

/****************************************
 * Define Instances and Constants
 ****************************************/

const char* WEBHOOK_NAME = "Ubidots";

Ubidots ubidots("webhook", UBI_PARTICLE);
GoogleMapsDeviceLocator locator;

float latitude, longitude;
/****************************************
 * Auxiliar Functions
 ****************************************/

//Put here your auxiliar functions
void locationCallback(float lat, float lon, float accuracy) {
  // Handle the returned location data for the device. This method is passed three arguments:
  // - Latitude
  // - Longitude
  // - Accuracy of estimated location (in meters)
  latitude = lat, longitude = lon;
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  Serial.begin(115200);
  ubidots.setDebug(true);  // Comment this line for printing debug messages
}

void loop() {
    locator.withSubscribe(locationCallback).publishLocation();
    delay(6000);
    float value1 = 250.0, value = 1;
    char str_lat[10], str_lng[10], context[30];
  
    sprintf(str_lat, "%f", latitude);
    sprintf(str_lng, "%f", longitude);
  
    ubidots.addContext("lat", str_lat);
    ubidots.addContext("lng", str_lng);
    ubidots.getContext(context);
    ubidots.add("position", value, context);
    ubidots.add("Variable_Name_One", value1);  // Change for your variable name

    bool bufferSent = false;
    bufferSent = ubidots.send(WEBHOOK_NAME, PUBLIC);  // Will use particle webhooks to send data

    if(bufferSent){
      // Do something if values were sent properly
      Serial.println("Values sent by the device");
    }

    System.sleep(SLEEP_MODE_DEEP, 20);  // deep sleep for 20 seconds
}

#4

I am glad to know that you could solve your problem and thanks for sharing with us your solution. Please let us to know if we can give you any further assistance.

All the best