Getting external data into the photon


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:

rain throughout the
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 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);

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