A library for the yahoo weather api, made for spark core (http://spark.io).
Please read the yahoo Usage Information and Limits.
This library should be considered alpha tested software. It is not ready for use in production without testing and bugfixing.
Currently the library loads the high and low temperature for today, including a short description. example:
{
"high": "16",
"low": "1",
"text": "Mostly Cloudy"
}
(Here is a list of the possible "text" values: http://developer.yahoo.com/weather/#codes )
It can easily be extended, you can fork this project and send pull requests!
- open in your browser: http://developer.yahoo.com/yql/console/?q=select%20item%20from%20weather.forecast%20where%20u%3D'c'%20and%20woeid%3D%20781788
- press
Test
, enable "Tree View". - read the output and choose wich data you need. e.g. atmosphere-humidity
- limit your query to only select what you need. (it's a tree.) example:
select atmosphere.humidity, wind.speed from weather.forecast where woeid=2502265
- select "json" and copy the URL labeled "THE REST QUERY". Test it in your browser or wget/curl, keep the output as a reference for implementation.
- paste the URL into the
weather.cpp
torequest.path = "/v1/...
- adjust the method
Weather::parse()
to respect the other json parameters. Compare with the json output, watch out for spaces. Add more variables to theweather_response_t
struct. - make tests, deploy, leave feedback
- sometimes it does not connect to yahoo api on first try. after a few tries it should work.
It's using the HttpClient library for spark core. Before compiling, add those files to your workspace. If you are building locally add it to build.mk
.
To find your WOEID, browse or search for your city from the Yahoo Weather home page. The WOEID is in the URL for the forecast page for that city. (from the yahoo manual )
You have to call the init() method with your woeid, an instance of httpClient and a boolean for celsius/fahrenheit.
in setup():
weather.init("781788", httpClient,true);
weatherCache.init(&weather);
in loop():
the cache class caches the response for two hours:
weather_response_t resp = weatherCache.lazyUpdate();
if(resp.isSuccess) {
// print(resp.temp_high);
}
or you can access the update directly:
weather_response_t resp = weather->update();
This does not work yet, we first have to fix the httpClient. Use this only if C++ is your mother tongue.
#include "Adafruit_CharacterOLED.h"
#include "yahoo_weather.h"
#include "HttpClient.h"
unsigned int nextTime = 0; // next time to contact the server
Weather weather;
WeatherCache weatherCache;
HttpClient* httpClient;
void setup() {
httpClient = new HttpClient("query.yahooapis.com", 80);
lcd = new Adafruit_CharacterOLED(...);
// weather
weather.init("781788", httpClient);
weatherCache.init(&weather);
}
void loop() {
if (nextTime > millis()) {
// keep the same color while waiting
return;
}
// print weather
weather_response_t resp = weatherCache.lazyUpdate();
if (lcd != NULL && resp.isSuccess) {
lcd->setCursor(0, 1);
lcd->print(resp.temp_low);
lcd->print("-");
lcd->print(resp.temp_high);
lcd->print(" ");
lcd->print(resp.descr);
}
// check again in 5 seconds:
nextTime = millis() + 5000;
}
Have fun! Share!