Getting just temperature from any weather service?

Hey guys I am working on a weather lamp (Turns a RGB led strip) depending on the temperature of the city where I live.

I started based on parsing a JSON from openweathermap. I checked this thread, seems a bit deprecated, mostly the JSON library. Anyhow I manged to make it work, but sparkcore crashes due to lack of memory I think.

I was wondering is there any other API I can just ping for the Temperature instead of a big JSON and then parse it?

Thanks in advance.

1 Like

you can try something like this webhook from the open-api Weather Underground:

{
"event": "get_my_weather",
"url": "http://api.wunderground.com/api/<getYourOwnApiKeyAndInsertHere>/conditions/q/NJ/Princeton.json",
"requestType": "POST",
"headers": null,
"query": null,
"responseTemplate": "{{#response}}{{#current_observation}}{{#estimated}}{{weather}}~{{temp_f}}~{{relative_humidity}}~{{wind_dir}}~{{wind_gust_mph}}~{{dewpoint_f}}~{{/estimated}}{{/current_observation}}{{/response}}",
"json": null,
"auth": null,
"mydevices": true
}

returns:

""Partly Cloudy~76.6~94%~South~0~75~"

strip out what you don't want in your response template; something like this will return just your temperature:

{
"event": "get_my_weather",
"url": "http://api.wunderground.com/api/<getYourOwnApiKeyAndInsertHere>/conditions/q/NJ/Princeton.json",
"requestType": "POST",
"headers": null,
"query": null,
"responseTemplate": "{{#response}}{{#current_observation}}{{#estimated}}{{temp_f}}~{{/estimated}}{{/current_observation}}{{/response}}",
"json": null,
"auth": null,
"mydevices": true
}

You should be able to easily parse that return.

4 Likes

Hey thank you so much for the reply, I’ve been reading and testing in order to make a good reply.

I read about webhooks, and thats the way to go. I have some doubts regarding defining the reponse template.

This is my JSON, http://api.openweathermap.org/data/2.5/weather?q=Buenos%20Aires,ar&units=metric&lang=es_ar

On the response template I should add the complete description of the JSON I am using? or just the fields I want to retrieve?

Regarding the code on the spark core, I am ok there, Im a bit of a JR when it come to web and json, so sorry if some of my questions are really basic.

Also, should I nano a local json, using the format your provided??

like:

   {
"event": "get_my_weather",
"url": "my own JSON",
"requestType": "POST",
"headers": null,
"query": null,
"responseTemplate": //my own response template,
"json": null,
"auth": null,
"mydevices": true
}

when I add a new json using the client, I get this kind of definition:

particle webhook GET get_weather2 http://api.openweathermap.org/data/2.5/weather?q=Buenos%20Aires,ar&units=metric&lang=es_aSending webhook request  { uri: 'https://api.particle.io/v1/webhooks',
  method: 'POST',
  json: 
   { event: 'get_weather',
     url: 'http://api.openweathermap.org/data/2.5/weather?q=Buenos%20Aires,ar',
     deviceid: undefined,
     requestType: 'GET',
     mydevices: true },
  headers: { Authorization: 'Bearer 75ee4042b0e4ec052387ef6900d4b342511XXXX' } }
Successfully created webhook with ID 5608591cbcf35fab63XXXXXX

Thanks in advance.

Just include the fields you want in your response template. Look at this tutorial for ways to help you limit the response to what you want.

1 Like