netatmo api で温度取得

netatmo apiを利用してTemperatureを取得したので、自分の備忘として。

phpとpythonでデータ取得を試してみた。

phpでは、battery_vpの取得方法がわからん。

pythonでは、WeatherStationが複数ある場合、個々の取得方法がわからん。

今回は、1拠点のバッテリー数値の取得は、pythonで行い、温度はphpを使った。

netatmoのModule1が外部温度センサーに割り当てられbattery_vpがある。
この数値がイマイチよくわからない

/*for raingauge and outdoor module*/
class NABatteryLevelModule
{
/* Battery range: 6000 … 3600 */
const BATTERY_LEVEL_0 = 5500;/*full*/
const BATTERY_LEVEL_1 = 5000;/*high*/
const BATTERY_LEVEL_2 = 4500;/*medium*/
const BATTERY_LEVEL_3 = 4000;/*low*/
/* below 4000: very low */
}

のように書かれているので、cactiでThresholdsを設定する。
値は、Warnigを4100、Alertを4500とした。

Netatmo-API-PHP

netatmo.php

#!/usr/bin/php
<?php /** * Example of Weather station API * If you need more details, please take a glance at https://dev.netatmo.com/doc */ define('__ROOT__', dirname(dirname(__FILE__))); require_once (__ROOT__.'/src/Netatmo/autoload.php'); require_once 'Utils.php'; require_once 'Config.php'; /** * Prints a list of devices * */ function printDevices($devices, $title = NULL) { if(!is_null($devices) &amp;&amp; is_array($devices) &amp;&amp; !empty($devices)) { if(!is_null($title)) printMessageWithBorder($title); foreach($devices as $device) { printWSBasicInfo($device); } } } $scope = Netatmo\Common\NAScopes::SCOPE_READ_STATION; $config = array("client_id" =&gt; $client_id,
                "client_secret" =&gt; $client_secret,
                "username" =&gt; $test_username,
                "password" =&gt; $test_password);

$client = new Netatmo\Clients\NAWSApiClient($config);

//Authentication with Netatmo server (OAuth2)
try
{
    $tokens = $client-&gt;getAccessToken();
}
catch(Netatmo\Exceptions\NAClientException $ex)
{
    handleError("An error happened while trying to retrieve your tokens: " .$ex-&gt;getMessage()."\n", TRUE);
}

//Retrieve user's Weather Stations Information

try
{
    //retrieve all stations belonging to the user, and also his favorite ones
    $data = $client-&gt;getData(NULL, TRUE);
//    printMessageWithBorder("Weather Stations Basic Information");
}
catch(Netatmo\Exceptions\NAClientException $ex)
{
    handleError("An error occured while retrieving data: ". $ex-&gt;getMessage()."\n", TRUE);
}

if(empty($data['devices']))
{
    echo 'No devices affiliated to user';
}
else
{

    $users = array();
    $friends = array();
    $fav = array();
    $device = $data['devices'][0];
    $tz = isset($device['place']['timezone']) ? $device['place']['timezone'] : "GMT";

    //devices are already sorted in the following way: first weather stations owned by user, then "friend" WS, and finally favorites stations. Still let's store them in different arrays according to their type
    foreach($data['devices'] as $device)
    {

        //favorites have both "favorite" and "read_only" flag set to true, whereas friends only have read_only
        if(isset($device['favorite']) &amp;&amp; $device['favorite'])
            $fav[] = $device;
        else if(isset($device['read_only']) &amp;&amp; $device['read_only'])
            $friends[] = $device;
        else $users[] = $device;
    }

    //print first User's device Then friends, then favorite
    printDevices($users, "User's weather stations");
//    printDevices($friends, "User's friends weather stations");
//    printDevices($fav, "User's favorite weather stations");

}
?>;

バッテリーデータを取得するのは、こんな感じで調整した。

print ($module['battery_vp'])."\n";

netatmo.shでデータ取得
netatmo.sh

#!/bin/sh

cd /usr/share/cacti/site/scripts/netatmo-api/bin
/usr/bin/php ./netatmo.php &gt;/tmp/netatmo.data

indoor=`cat /tmp/netatmo.data |sed  -e '11,11!d'|cut -d' ' -f2`
outdoor=`cat /tmp/netatmo.data |sed  -e '29,29!d'|cut -d' ' -f2`
indoorh=`cat /tmp/netatmo.data |sed  -e '45,45!d'|cut -d' ' -f2`
outdoorh=`cat /tmp/netatmo.data |sed  -e '63,63!d'|cut -d' ' -f2`

printf ' Indoor:'$indoor
printf ' Outdoor:'$outdoor
printf ' Indoorh:'$indoorh
printf ' Outdoorh:'$outdoorh

rm /tmp/netatmo.data

exit 0

netatmo-api-python

netatmo.py

#!/usr/bin/python3
# encoding=utf-8

# 2013-01 : philippelt@users.sourceforge.net 

# Just connect to a Netatmo account, and print internal and external temperature of the default (or single) station
# In this case, sensors of the station and the external module have been named 'internal' and 'external' in the
# Account station settings.

import lnetatmo

authorization = lnetatmo.ClientAuth()
devList = lnetatmo.DeviceList(authorization)

#print ("Current temperature (inside/outside/battry): %s / %s °C" %
#        ( devList.lastData()['Indoor']['Temperature'],
#          devList.lastData()['Outdoor']['Temperature']),
#         devList.lastData()['Outdoor']['battery_vp']
#)

print   "Inndoor:",devList.lastData()['Indoor']['Temperature']
print   "Outdoor:",devList.lastData()['Outdoor']['Temperature']
print   "Battery:",devList.lastData()['Outdoor']['battery_vp']

#print('External module battery : ', "OK" if int(devList.lastData()['Outdoor']['battery_vp']) &gt; 5000 else "0" )

netatmo.sh

cd /usr/share/cacti/site/scripts/
python netatmo.py &gt; /tmp/netatmo

indoor=`cat /tmp/netatmo|grep Inndoor|cut -b10-`
outdoor=`cat /tmp/netatmo|grep Outdoor|cut -b10-`
batt=`cat /tmp/netatmo|grep Battery|cut -b10-`

printf ' Indoor:'$indoor
printf ' Outdoor:'$outdoor
printf ' Battery:'$batt

rm /tmp/netatmo

exit 0

上に書いたソースを元に試行錯誤の結果このようなグラフで妥協した。

graph_image

温度データは取得出来ているのだが、グラフが結構途切れてしまう。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です