Raspberry Pi & Linux Updating Cloud Data C++

I’m trying to integrate some weather monitoring services provided by Weather Underground into my application. I would like to have a device instance of the data as well as push the data to a dashboard or data bucket. The trouble i’m having is repeatedly calling either the lambda function or any regular function with a certain period of time between the calls. I understand the thinger.io dashboard provides a way to call the function, but I want the device to control the timeframe and when to retrieve & push data. The Arduino has a loop function by default, but no while loops or other loops work in the C++ environment on Raspberry Pi. Could someone provide the proper syntax to repeatedly call a function after some duration of time in C++? Here is what I have so far:


#include "thinger/thinger.h"
#include "wiringPi.h"
#include "wunderground/wunderground.hpp"
#include <string>

#define USER_ID             "******"
#define DEVICE_ID           "RPI_BPlus"
#define DEVICE_CREDENTIAL   "***********"

//Setup weather
std::string ApiKey = "***********";
wunderground::Conditions weather(ApiKey);
weather.loadData("City","State");


void intialize(){
    //Setup communications
    thinger_device RPI_BPlus(USER_ID, DEVICE_ID, DEVICE_CREDENTIAL);
    wiringPiSetup();
    
    //Setup pins
    pinMode(7, OUTPUT);
    
    //LED resource - turn led on/off
    RPI_BPlus["led"] << [](pson& in){
        if(in.is_empty())
            in = digitalRead(7) ? true : false;
        else{
            if(in)
                digitalWrite(7, HIGH);
            else
                digitalWrite(7, LOW);
        }
    };

    //wunderground resource - fetch weather data
    //How do I repeatedly call this from within this code?

    RPI_BPlus["wunderground"] >> [=weather](pson& out) 
    {
        out["temp"] = weather.getCondition("temperature");
    };
}


int main(int argc, char *argv[])
{
    intialize();
    RPI_BPlus.start();
    //Some sort of loop to call "wunderground" resource?
    
    return 0;
}

The wunderground c++ api fetches the data and the “wunderground” resource pushes this data to the thinger.io cloud, but only when I specify this from the dashboard. How can I automatically send data (coded on the device) to a data bucket or dashboard after a certain interval of time? This is my attempt using while loop, with no success:


int main() {
    double last_check = 10;
    double elapsed_time = 0;
    time_t someTime;
    RPI_BPlus.start(); //start services

    while(1)
    {
        RPI_BPlus.handle();  //not sure if this is necessary
        someTime = time(NULL);
        elapsed_time = double(someTime - last_check);
        if(elapsed_time >= 10)
        {
            last_check = double(someTime);
            updateweather();  //generic function that calls wunderground api
        }
    }
    
    return 0;
}

Hi, if you want to stream a resource at an specific moment, to store it in a bucket or to display it in a dashboard, you can call the thing.stream() method, as specified in the Arduino documentation (it is applicable to the Linux client):

http://docs.thinger.io/arduino/#coding-streaming-resources

Something like:

    while(1)
    {
        RPI_BPlus.handle();  //not sure if this is necessary (yes, it is necessary)
        someTime = time(NULL);
        elapsed_time = double(someTime - last_check);
        if(elapsed_time >= 10)
        {
            last_check = double(someTime);
            RPI_BPlus.stream(RPI_BPlus["wunderground"]);
        }
    }

Hope it works!

I figured out the issue after I looked through the source files. I was calling the “thing.start()” in main which redirects to a handling loop, never executing the while loop after. I simply omitted the “thing.start()” and then used the “thing.handle()” within the while loop.


int main(int argc, char *argv[])
{
    intialize();
        double last_check = 3;
        double elapsed_time = 0;
        //Don't put thing.start() if you have your own loop
        time_t end;
    
        while(1)
        {
            RPI_BPlus.handle();
            end = time(NULL);
            elapsed_time = double(end - last_check);
            if(elapsed_time >= 3)
            {
                last_check = double(end);
                RPI_BPlus.stream(RPI_BPlus["countUp"]);
            }
        }
    return 0;
}