お遊びTech

ITで遊んでみたり、ガジェットで遊んでみたり。その遊びは様々な効果があるかもしれません。

Nature RemoのセンサーデータをCactiでグラフ化

Nature Remoで取得できるセンサー情報をCactiでグラフ化します。

Nature Remoで取得可能なセンサー情報

Nature RemoではCloud API及び、Local APIが用意されており、Cloud APIにて以下のようなNature Remo本体のセンサー情報を取得することができます。

・hu : 湿度(%)
・il: 照度:独自の値(以下トリガーで定義されている値より)
 暗い:1-50
 薄暗い: 51-127
 薄明るい:128-205
 明るい:206-255
・mo: 人感(常に1。反応した際にcreated_atの日時が更新される)
・te: 温度(度)

 

このうち、hu, il, teをCactiにてグラフ化します。取得用のスクリプトpythonにて実施します。

その他、NatureRemoAPIの認証や制限などこちらをご参照ください。
https://developer.nature.global/

 

Nature Remo APIの取得

アクセストークンを取得します。
https://home.nature.global/
f:id:kleonids:20210828103610p:plain


アクセストークンは再度表示されないのでメモしておきます。
三者に家電など操作されてしまうため、外部に漏れないように注意してください。

curlにてテストしてみます。上記温度などのデータが取得できているはずです。

curl -X GET "https://api.nature.global/1/devices" -H "accept: application/json" -H "Authorization: Bearer 取得したアクセストークン"

 

Cactiスクリプトを実行
/usr/share/cacti/scriptsなどcactiスクリプトを置くディレクトリに以下のpythonコードを置きます。 

import requests
import json

def main():
  remoheaders = {
    'Accept' : 'application/json',
    'Authorization' : 'Bearer [トークン]'
  }
  try:
    res= requests.get('https://api.nature.global/1/devices', headers=remoheaders)
    res.raise_for_status()
    remodata = json.loads(res.text)
    #print(remodata[0])
    hu = remodata[0]['newest_events']['hu']['val']
    il = remodata[0]['newest_events']['il']['val']
    mo = remodata[0]['newest_events']['mo']['val']
    te = remodata[0]['newest_events']['te']['val']
    print('humidity:' + str(hu) + ' illminance:' + str(il) + ' motion:' + str(mo) + ' temperature:' + str(te))
  except requests.exceptions.RequestException as e:
    print("APIエラー: ",e)
    hu='nodata'
    il='nodata'
    mo='nodata'
    te='nodata'


if __name__ == "__main__":
  main()


スクリプトを配置します。

# cd /usr/share/cacti/scripts
# vi nature-remo-api.py
# chown cacti実行アカウント.  nature-remo-api.py
# chmod 755 nature-remo.api.py


テストしてみます。

# python3 nature-remo-api.py
humidity:59 illminance:245 motion:1 temperature:24.3

 

Cactiの設定

スクリプトを実行するデータ入力を作成します。
f:id:kleonids:20210828115015p:plain


データソースを作成します。アイテムは3つ作ります。
f:id:kleonids:20210828115207p:plain

f:id:kleonids:20210828115241p:plain

f:id:kleonids:20210828115256p:plain


NatureRemoのデバイスを作成します。
f:id:kleonids:20210828115344p:plain

f:id:kleonids:20210828115403p:plain


ツリーに配置します。
f:id:kleonids:20210828115513p:plain

グラフの表示

 一定時間待つと、無事グラフが作成されました。
f:id:kleonids:20210828115556p:plain


なお、NatureRemoとNatureRemo miniというものがありますが、Miniは温度のみしか取得できないため
ご注意ください。