December 05, 2021

Davis weather station, WeatherLink, PHP

Updated 2021-12-05 !

In the harbour we are using a Davis weather station, Vantage Pro2, as our PWS(Private Weather Station).

If you like to display the data, online on the web, you can use the Davis service, Weatherlink(WL) where you register an account. 

Another way is to retrieve the weather data as XML or JSON, from the Weatherlink web-site, and use it in your own application (open source!).

Another approach with NodeRed could be find here.

Intro
We use the WeatherLinkIP to push weather data to WL. Unfortunately this hardware is discontinued and replaced with Weatherlink Live and uses the WL V2 API.

Code examples

Below You will find some PHP code which will do the work using the WL V1 API.

$xml = simplexml_load_file('http://api.weatherlink.com/v1/NoaaExt.xml?user=XXXX&pass=YYYY&apiToken=ZZZZ');

Dump the whole XML data to see what is available
var_dump($xml);

And loading some of the fields in an array
$cumulus[2] = $xml->{'temp_f'};
$cumulus[3] = $xml->{'relative_humidity'};
$cumulus[4] = $xml->{'dewpoint_f'};
$cumulus[5] = $xml->{'wind_mph'};   
$cumulus[22] = $xml->{'davis_current_observation'}->{'temp_in_f'}; 
$cumulus[23] = $xml->{'davis_current_observation'}->{'relative_humidity_in'};

Or get the JSON data with 
$service_url = 'http://api.weatherlink.com/v1/NoaaExt.json?user=XXXX&pass=YYYY&apiToken=ZZZZ';  
$curl_handle = curl_init($service_url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl_handle);

$wlJson = json_decode($curl_response);

And loading some of the fields in an array

$cumulus[2] = $wlJson->temp_f;
$cumulus[3] = $wlJson->relative_humidity;
$cumulus[4] = $wlJson->dewpoint_f;
$cumulus[5] = $wlJson->wind_mph;  

The name "cumulus" comes from a project where I update a "realtime.txt" file which is an input for the weather34 template. Code and install instructions are available here.

Remark
The latest API is V2, is used in this post.

No comments:

Post a Comment

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !