Getting external data into the photon

Hi,

For my project, I need to compute various data from my sensors, which is not a problem. But, I need to import an external value.

To be more clear, I have various sensors connected to my photon and I get all the input values as needed. I need to compute some of those values against the external temperature that will interact with some output of the Photon. Unfortunately, for physical reason, it is not possible (for now) to connect an external probe on the photon.I can get the external temperature, using the darksky API but I get a whole bunch of data I don’t need. I can process the JSON answer from darksky, on another hardware, to extract JUST the external temperature and make it available to my photon but I don’t know how.

Many thanks

By the way, I have tried to use my MQTT broker (… another hardware …) but could never manage to suscribe to 2 differents topics.

You can use webhooks to filter the data you are interrested in. There is a tutorial in the documentation, or on the forum.

I don’t know about webhooks. I will have a look and let you know.

Many thanks

I just checked about webhooks and it seems to be very interesting and what I need. What I just did not get is how to filter the JSON answer to just get the temparature.

Here is the the first part of the JSON supposed to be received by the webhook :smile:

{“latitude”:46.7424,“longitude”:7.0964,“timezone”:“Europe/Zurich”,“offset”:1,“currently”:{“time”:1489077037,“summary”:"Light
Rain",“icon”:“rain”,“precipIntensity”:0.5867,“precipProbability”:0.63,“precipType”:“rain”,“temperature”:8.14,“apparentTemperature”:7.37,“dewPoint”:4.29,“humidity”:0.77,“windSpeed”:1.65,“windBearing”:261,“visibility”:9.99,“cloudCover”:1,“pressure”:1023.94,“ozone”:307.14},“hourly”:{“summary”:"Light
rain throughout the
day.",“icon”:“rain”,“data”:[{“time”:1489075200,“summary”:"Light
Rain",“icon”:“rain”,“precipIntensity”:0.5309,“precipProbability”:0.61,“precipType”:“rain”,“temperature”:10.38,“apparentTemperature”:10.38,“dewPoint”:6.37,“humidity”:0.76,“windSpeed”:1.73,“windBearing”:264,“visibility”:9.99,“cloudCover”:1,“pressure”:1023.82,“ozone”:307.71},{“time”:1489078800,“summary”:"Light
Rain",“icon”:“rain”,“precipIntensity”:0.6401,“precipProbability”:0.64,“precipType”:“rain”,“temperature”:5.99,“apparentTemperature”:5,“dewPoint”:2.28,“humidity”:0.77,“windSpeed”:1.56,“windBearing”:258,“visibility”:9.99,“cloudCover”:1,“pressure”:1024.05,“ozone”:306.59},{“time”:1489082400,“summary”:"Light
Rain",“icon”:“rain”,“precipIntensity”:0.7493,“precipProbability”:0.66,“precipType”:“rain”,“temperature”:5.19,“apparentTemperature”:4.24,“dewPoint”:1.79,“humidity”:0.79,“windSpeed”:1.44,“windBearing”:254,“visibility”:9.99,“cloudCover”:1,“pressure”:1024.34,“ozone”:306.17},{“time”:1489086000,“summary”:"Light
Rain",“icon”:“rain”,“precipIntensity”:0.8814,“precipProbability”:0.68,“precipType”:“rain”,“temperature”:4.88,“apparentTemperature”:4.01,“dewPoint”:1.99,“humidity”:0.82,“windSpeed”:1.36,“windBearing”:253,“visibility”:9.99,“cloudCover”:1,“pressure”:1024.74,“ozone”:306.91},{“time”:1489089600,“summary”:"Light
Rain" …

I just need the 8.14 on line 3 !!!

The result is a hierarchic JSON

{... ,"currently":{... ,"temperature":8.14, ...}, ...}

So you’d put {{currently.temperature}} in your response template to get the temperature “child” of the currently “parent” field.

1 Like

Hi ScruffR,

I did understood that I had to put the JSON filter to get the temperature, but where I am lost is I don’t get where to put that filter. In the Webhook or directly on the Photon. Since the JSON message is quiet big, and JSON parsing is quiet resources intesive I would have prefered to filter on the Webhook side to reduce network load and preserve CPU power.

The particle cloud will do the heavy lifting for you.
Your device publishes an event that will trigger the webhook to request the data from your providing server, this server will send the big raw response back to the Particle cloud where a worker thread will parse that and filter out the data you told it to look for via the response template.
After that the filtered data will be sent back to your device via a webhook-response event to which you can subscribe and which will only carry your desired values in its data field.

If you are building your webhook via https://console.particle.io/integrations you’ll probably find the whole thing quite straight forward.

In fact, I’m on the webhook documentation since a while but this is very confusing !!!

This is very clear how to publish something to a webhook that will POST that somewhere else, but not clear at all when you want a piece of a GET answer.

I created a webhook called darksky that send a GET with a darksky URL (I use the same URL I use with CURL on a bash shell).

I need to trigger the webhook using “Particle.publish(“darksky”, yyyy, PRIVATE);” darksky is the name of the webhook (the event) but I have no idea what to put in yyyy . The webhook ask me to do “String data = String(10);” and put data in place of yyyy . Since I just want the answer I don’t really get that, what does that do ?

Then I understand that to get the answer, I need to create a function for the Particle.subscribe in the style of : “*void getTemp(const char *event, const char data)” and in that case, I get the whole answer from the webhook.

It is not clear how to get those data (I suppose they are in the data variable) and it is not clear how to do a response template to filter the JSON answer to just get the temperature.

yyyy would be what you want the Particle cloud to “inject” into the request (POST or GET or what not).
For a GET request you’d probably want to use “Advanced Settings” to create a query request.
If your GET request does not need a query, you can just use Particle.publish("darsky", PRIVATE) or Particle.publish("darsky", "", PRIVATE)

More info about your needs would help to get a bit more specific :wink:

For that you also need to open the “Advanced Settings” page.

See here

Again, many, many, many thanks. It works like a charm. I do get exactly the temp.

But just a question :
I have my Particle.subscribe that runs a function. In that function, I just and only pass the data variable to the global variable of the sketch. Is ther a way, ti get the value directly on the sketch, not using a function.

void setup() {

Particle.subscribe(“hook-response/darksky”, getTemp, MY_DEVICES);

}
void Loop() {

}
void getTemp(const char *event, const char *data) {

exTemp = data;

}

exTemp = data;

this will only pass the const char* pointer out of the subscribe handler, but the string this pointer points to will soon after that be gone.
You need to copy the string itself into a global char[] or if you are expecting a number you can do the translation and store that globally.

char responseString[64];
double responseValue;

void getTemp(const char *event, const char *data) {
  // store the response string globally
  strncpy(responseString, data, sizeof(responseString)-1));

  // store a numeric value represented by the response data globally
  responseValue = atof(data);
}

No!
But what’s the problem? That question is not far off asking: “Can I have my device do what I want it to do without programming it?”

1 Like