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