-
-
Save mfr/4362912 to your computer and use it in GitHub Desktop.
#!/usr/bin/php | |
<?php | |
date_default_timezone_set('UTC'); | |
/** | |
* oAuth settings from http://dev.netatmo.com/dev/listapps | |
*/ | |
define('APP_ID', ''); | |
define('APP_SECRET', ''); | |
define('USERNAME', ''); | |
define('PASSWORD', ''); | |
define('TOKEN_URL', 'https://api.netatmo.net/oauth2/token'); | |
define('DEVICELIST_URL', 'https://api.netatmo.net/api/devicelist'); | |
/** | |
* Station ID from http://www.wunderground.com/wxstation/signup.html | |
*/ | |
define('STATION_URL', 'http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php'); | |
define('STATION_ID', ''); | |
define('STATION_PASSWORD', ''); | |
define('DEBUG', 1); | |
if (APP_ID == '' || APP_SECRET == '' || USERNAME == '' || PASSWORD == '' || STATION_ID == '' || STATION_PASSWORD == '') { | |
fwrite(STDERR, "APP_ID, APP_SECRET, USERNAME, PASSWORD, STATION_ID, STATION_PASSWORD cannot be empty.\n"); | |
exit(1); | |
} | |
$opts = array( | |
'http' => array( | |
'method' => 'POST', | |
'header' => 'Content-type: application/x-www-form-urlencoded', | |
'content' => http_build_query(array( | |
'grant_type' => "password", | |
'client_id' => APP_ID, | |
'client_secret' => APP_SECRET, | |
'username' => USERNAME, | |
'password' => PASSWORD | |
)) | |
) | |
); | |
if (DEBUG) print_r($opts); | |
$context = stream_context_create($opts); | |
$response = file_get_contents(TOKEN_URL, FALSE, $context); | |
$response_json = json_decode($response, TRUE); | |
if (DEBUG) print_r($response_json); | |
if (!$response || empty($response_json['access_token'])) { | |
fwrite(STDERR, "Couldn't retrieve the access_token. Please check your username and password.\n"); | |
exit(1); | |
} | |
$access_token = $response_json['access_token']; | |
$device_list = json_decode(file_get_contents(DEVICELIST_URL . '?access_token=' . $access_token)); | |
if (!$device_list || sizeof($device_list->body->devices) == 0) { | |
fwrite(STDERR, "Couldn't find any devices\n"); | |
exit(1); | |
} | |
if (sizeof($device_list->body->modules) == 0 || empty($device_list->body->modules[0]->_id)) { | |
fwrite(STDERR, "Couldn't find outdoor devices\n"); | |
exit(1); | |
} | |
$indoor_id = $device_list->body->devices[0]->_id; | |
$outdoor_id = $device_list->body->modules[0]->_id; | |
$last_data = $device_list->body->devices[0]->last_data_store; | |
if (DEBUG) print_r($last_data); | |
$params = array( | |
'ID' => STATION_ID, | |
'PASSWORD' => STATION_PASSWORD, | |
'action' => 'updateraw', | |
'dateutc' => date("Y-m-d H:i:s",$last_data->$outdoor_id->K), | |
'humidity' => $last_data->$outdoor_id->b, | |
'temp' => $last_data->$outdoor_id->a, | |
'baromhPa' => $last_data->$indoor_id->e, | |
'indoortemp' => $last_data->$indoor_id->a, | |
'indoorhumidity' => $last_data->$indoor_id->b, | |
'softwaretype' => 'Netatmo' | |
); | |
// calculate dew point using temperature and humidity (optional) | |
// source: http://www.aprweather.com/pages/calc.htm | |
$params['dewpt'] = ($params['temp'] - (14.55 + 0.114 * $params['temp']) * (1 - (0.01 * $params['humidity'])) - pow((2.5 + 0.007 * $params['temp']) * (1 - (0.01 * $params['humidity'])), 3) - (15.9 + 0.117 * $params['temp']) * pow(1 - (0.01 * $params['humidity']), 14)); | |
// conversion to F | |
$params['tempf'] = $params['temp'] * 9 / 5 + 32; | |
$params['dewptf'] = $params['dewpt'] * 9 / 5 + 32; | |
$params['indoortempf'] = $params['indoortemp'] * 9 / 5 + 32; | |
$params['baromin'] = $params['baromhPa'] * 0.0295299830714; | |
// remove SI after conversion | |
unset($params['temp'],$params['dewpt'],$params['indoortemp'],$params['baromhPa']); | |
ksort($params); | |
if (DEBUG) print_r($params); | |
$response = file_get_contents(STATION_URL . '?' . http_build_query($params)); | |
if (DEBUG) print $response; | |
?> |
Guys, sorry, this is just what I need, but how do I run it? I'm using an iPad only these days, although I do have a Vera3 home automation system which runs code similar to this and links to my Netatmo at present (Vera runs Linux).. All I have it doing is retrieving the data from Netatmo so I can do things like turn heaters on and off. I'm now adding a Hydrawise sprinkler controller and would like it to be able to see my Netatmo - it gets it's data from Weather Underground. So, how does this script run? I do have a Samsung Galaxy Note 2 if there's an Android way of doing it. Last choice is to integrate Hydrawise and Netatmo - beyond my programming skills although I do have the Hydrawise API. Yes, I know all the words but couldn't write the code for toffee! I guess I can see this is a php thing but have no idea how it runs. Lastly, I do have a web hosting service which is also Linux based if there's a smart way of publishing it there. Many thanks in advance!
hi mfr
which free cron service can i use for your script?
Seems like this script broke. I've used the official measure api which works. You can get the changes from my fork.
I also added support for a rain gauge as shipped by Netatmo.
Yep, line #67 - last_data_store is no longer available. I've updated this in my fork.
Excellent code. Thanks; very useful.
Quick question, do you know of a secure way of passing the passwords (or not at all) between Netatmo and WU? It appears, from the Weatherunderground wiki, that the API only has basic parameter passing via clear text http. Any ideas?