OpenSprinkler › Forums › Hardware Questions › OpenSprinkler Pi (OSPi) › Rain delay from Yahoo Weather › Re: Re: Rain delay from Yahoo Weather
Samer
@andrew I got the weather code incorporated into my web application. The only thing left is adding a configuration option to enable/disable the feature, add a way to edit the CSV file which contains the code to delays, and decide how often to run the function. My station watcher script (for logging) runs every minute and I intend to piggy back off of it. However, I think I will have it run the specific weather function less frequently for multiple reasons. The main being 1 minute is unnecessary I doubt the weather changes that often and I also don’t want to get IP’s banned from Yahoo’s API.
I added a regex to parse the data and have pulled all the current weather conditions not just the weather code. I want to incorporate the settings into a “current weather” page on my mobile app.
Here are my functions I am using so far:
#Weather functions
#Resolve location to WOEID
function get_woeid() {
$options = get_options();
$data = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.placefinder%20where%20text=%22".$options["loc"]."%22");
preg_match("/(d+) /", $data, $woeid);
return $woeid[1];
}
#Get the current weather code and temp
function get_weather_data() {
$woeid = get_woeid();
$data = file_get_contents("http://weather.yahooapis.com/forecastrss?w=".$woeid);
preg_match("/
$weather = array("text"=>$newdata[1],"code"=>$newdata[2],"temp"=>$newdata[3],"date"=>$newdata[4]);
return $weather;
}
#Lookup code and get the set delay
function code_to_delay($code) {
$codes = explode("n",file_get_contents("yahoo_weather.csv"));
foreach ($codes as $line) {
$data = explode(",", $line);
if (!isset($data[2])) continue;
if ($code == $data[0]) return $data[2];
}
return false;
}
The get_options function is something in my code to pull the options off the OpenSprinkler. I do this to prevent a WOEID configuration from the user. I am using options and not settings because the OSPi does not properly report the location in the settings. Here is an example of how I use these functions:
#Check the current weather for the devices location, and set the appropriate delay, if needed
function check_weather() {
$weather = get_weather_data();
$delay = code_to_delay($weather["code"]);
if ($delay === false) return;
send_to_os("/cv?pw=&rd=".$delay);
}
Just wanted to share what I have so far, thanks again!
Edit: I just saw your comment about WOEID update frequency! I will update my code to take that into consideration, thanks!
Edit 2: I just checked and the API limits the WOEID lookup to 2k per day. I am checking once every hour in the current implementation so I will not worry too much however it is still an HTTP request which increases latency. Since it is not something the user has to deal with I am keeping it a low priority fix, for now. In the future I might upgrade my web application to use a SQL database rather then files. At that point, I think I will switch the code to set the WOEID and poll the database instead.
Edit 3: I actually pushed this code into my web application. It is currently working but has no GUI options, yet.