Weather dashboard object

I have a script running on my Pi at  /home/pi/scripts/extdata.php  which contains a variable - $icon_link
$icon_link is the URL of an external image file, which I would like to display on one of my dashboards using a little html in the dashboard paragraph text;

<img src="the variable icon_link" alt="current status"> 

Could anyone please advise me how I can retrieve the value of the variable from the extdata.php script?

Paul

Robert Wall's picture

Re: Weather dashboard object

Paul, the only way I can think of is to write the variable into a file, then in the dashboard in PHP open and read the file.

(Would I be right in thinking you have a choice of many files, and one of the purposes of the script is to choose the correct one?  Where does the script get the information from to make that choice? - I'm wondering if there's another way.)

Paul Reed's picture

Re: Weather dashboard object

Yes, you are correct.

This is the script I am referring to, which uses an API to get local weather data from a website, which is parsed in the script to extract various data to variables. One aspect of the parsed data is a URL to an image file depicting the current weather - example, so the URL will change continuously depending upon the weather.
The script is run every 30 minutes via cron.

I'm not bothered about storing the URL, I just thought it would be a nice touch to add it to my weather dashboard, (using the dashboard editor instead of modding the oem modules).

Paul

Robert Wall's picture

Re: Weather dashboard object

Take a look at  php://memory and php://temp  I've never used these, but they look promising. Memory seems to do what you want, Temp seems to do the same up to a size limit, then it becomes a wrapper for disc access, so probably exactly the same as I suggested above but it's all done for you.

(I didn't know about these until I started sniffing around!)

Paul Reed's picture

Re: Weather dashboard object

I've never heard of them either, but I'll check them out tonight.

i thought retrieving the value would have been more straightforward, but I'm showing my ignorance now!

Paul

Mattia Rossi's picture

Re: Weather dashboard object

Paul, you need to have a script that retrieves the url info from weather underground (the first part of Martin's script is ok) but then you either pull the image and store it to a file, and then reference that file from your dashboard container, or you change the logic and use a script like:

<?php
//Change the weather underground API and city below
$json_string = file_get_contents("http://api.wunderground.com/api/***API***/geolookup/conditions/q/zmw:00000.115.16059.json");
$parsed_json = json_decode($json_string);
$image_url = $parsed_json->{'current_observation'}->{'image'}->{'url'};
header( 'Location: '.$image_url  ) ;

You can save this to /scripts/image.php and link it in the img src tag, it will retrieve the url from the json and then issue a redirect to the image file .. no need of storing it, no need to learn how to get an image and write it to the file system in php ;)

 

Paul Reed's picture

Re: Weather dashboard object

That works great Mattia, thank you.

The only change I made was to select the correct remote image source;

$image_url = $parsed_json->{'current_observation'}->{'icon_url'};

Otherwise it's great, and the weather icon changes to reflect the local  weather conditions (presently showing a 'light cloud'.
It's not finished yet, but the screenshot below shows the dashboard so far...

Paul

(click image for full screen view!)

Mattia Rossi's picture

Re: Weather dashboard object

I think we can still make you into a php progammer with some more effort ;)

Sorry for the wrong field .. was in a hurry and grabbed the first field that resembled a meaningful image ...

Nice Dashboard by the way :)

Paul Reed's picture

Re: Weather dashboard object

As a keen gardener, I want to be notified about overnight frosts to enable me to cover over tender plants & limit damage, especially in spring when there is new tender growth, so I've just modded the weather script  above to send a push message to my phone at midday, if the nightime temperature is predicted to fall below 3 degC.
A cron job runs the script each midday, which obtains a JSON weather data string from Wunderground.com (as above) and from which the predicted overnight temperature is parsed. If this is below 3 degC, then Curl is used to call the Boxcar script as described in this post about the event module.

It seems to work well, and can easily be changed to get other weather data, such as if wind gusts exceed certain speed, rainfall per hour more than... etc.

I've put the script in Github if anyone's interested.

Paul

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.