-
-
Save CrazyHenk44/8a02fc0ea04e9db561d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$orig_tz = date_default_timezone_get(); | |
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'); | |
define('MEASURE_URL', 'https://api.netatmo.net/api/getmeasure'); | |
/** | |
* 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', 0); | |
/** | |
* Station id for openweathermap.org | |
*/ | |
define('O_URL', 'http://openweathermap.org/data/post'); | |
define('LAT', ''); | |
define('LONG', ''); | |
define('O_USER', ''); | |
define('O_PASSWORD', ''); | |
function get_data($url, $context = null) { | |
$response = file_get_contents($url, false, $context); | |
if ($response === false) { | |
fwrite(STDERR, "Couldn't get data for: $url\n"); | |
exit(1); | |
}; | |
if (DEBUG) { | |
print_r(json_decode($response)); | |
} | |
return json_decode($response); | |
} | |
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, | |
'scope' => 'read_station' | |
)) | |
) | |
); | |
if (DEBUG) print_r($opts); | |
$context = stream_context_create($opts); | |
$response_json = get_data( TOKEN_URL, $context); | |
if (DEBUG) print_r($response_json); | |
if (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 = get_data(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); | |
} | |
if ($device_list->body->devices[0]->type == "NAMain") { | |
$pressure = $device_list->body->devices[0]->dashboard_data->Pressure; | |
if (DEBUG) echo "Indoor found\n"; | |
} | |
$rain_1hour = 0; | |
$rain_today = 0; | |
foreach ($device_list->body->modules as $dev) { | |
if ($dev->type == "NAModule1") { | |
$temp = $dev->dashboard_data->Temperature; | |
$humid = $dev->dashboard_data->Humidity; | |
$time = $dev->dashboard_data->time_utc; | |
if (DEBUG) echo "Outdoor found\n"; | |
} else if ($dev->type == "NAModule3") { | |
if (DEBUG) echo "Rain found\n"; | |
$rain_1hour = $dev->dashboard_data->sum_rain_1; | |
$rain_today = $dev->dashboard_data->sum_rain_24; | |
} | |
} | |
// calculate dew point using temperature and humidity (optional) | |
// source: http://www.aprweather.com/pages/calc.htm | |
$dewpt = ($temp - (14.55 + 0.114 * $temp) * (1 - (0.01 * $humid)) - pow((2.5 + 0.007 * $temp) * (1 - (0.01 * $humid)), 3) - (15.9 + 0.117 * $temp) * pow(1 - (0.01 * $humid), 14)); | |
// Process it | |
$params = array( | |
'ID' => STATION_ID, | |
'PASSWORD' => STATION_PASSWORD, | |
'action' => 'updateraw', | |
'dateutc' => date("Y-m-d H:i:s",$time), | |
'humidity' => $humid, | |
'softwaretype' => 'Netatmo', | |
'rainin' => $rain_1hour * 0.039370, // inches | |
'dailyrainin' => $rain_today * 0.039370, // inches | |
'tempf' => $temp * 9 / 5 + 32, // Fahrenheid | |
'dewptf' => $dewpt * 9 / 5 + 32, // Fahrenheid | |
'baromin' => $pressure * 0.0295299830714 // Inches | |
); | |
ksort($params); | |
if (DEBUG) print_r($params); | |
$response = get_data(STATION_URL . '?' . http_build_query($params)); | |
if (DEBUG) print $response; | |
// Post to openweather, taken from http://openweathermap.org/blog/php-sample/ | |
// Adapted slightly. | |
$params_o = array ( | |
'temp' => $temp, | |
'humidity' => $humid, | |
'pressure' => $pressure, | |
'rain_1h' => number_format($rain_1hour,2), | |
'rain_today' => number_format($rain_today,2), | |
'lat' => LAT, | |
'long' => LONG, | |
'dewpoint' => number_format($dewpt + 273.15,1) // Kelvin... | |
); | |
if (DEBUG) print_r($params_o); | |
$fields_string=''; | |
foreach($params_o as $key=>$value) { | |
$fields_string .= $key.'='.$value.'&'; | |
} | |
$fields_string = rtrim($fields_string,'&'); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL,O_URL); | |
curl_setopt($ch, CURLOPT_USERPWD, O_USER . ":" . O_PASSWORD); | |
curl_setopt($ch, CURLOPT_POST,count($params_o)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
if (DEBUG) echo "$result\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment