LoginSignup
7
6

More than 5 years have passed since last update.

Node.jsで気象情報を取得してNatureRemoを操作する

Last updated at Posted at 2018-06-03

はじめに

Nature Remo のAPIを使ってみようということでやってみました。

毎朝気象情報を取得してその日の最高気温が一定以上ならば、エアコンをオンにするという仕組みを作ってみました。

単に決まった時間にエアコンをオンにするだけなら、Nature Remo の公式アプリから設定できます。
IFTTTと連携してもできます。

環境

・Raspberry Pi 3 ModelB(node.js実行環境として)
・Nature Remo(エアコン操作の赤外線登録済み)

使うもの

・Nature Remo のAPI
・livedoorのAPI(気象情報)
・node-schedule(決まった時間に定期的に実行させるライブラリ)

Nature Remo に命令する

公式ドキュメントはこちら
https://developer.nature.global/

アクセストークンの取得

下記ページにアクセスし、トークンを取得します。
https://home.nature.global/

命令のIDを取得する

まずはエアコンをオンにする命令のIDを取得します。
以下のコマンドで、登録済みの命令一覧が見れます。

$ curl -H 'Authorization: Bearer {$TOKEN}' -H "accept: application/json" -X GET "https://api.nature.global/1/appliances" | jq

{$TOKEN}のところは先程取得したトークンを入力してください。
jsonの表示のためにjqコマンドを追加しています。

実行したい命令を見つけたらidをメモしておきます。(以降signal_id)

試しに実行して確認してみます。

$ curl -H 'Authorization: Bearer {$TOKEN}' -H "accept: application/json" -X POST "https://api.nature.global/1/signals/{signal_id}/send"

次に気象情報を所得する

気象情報APIはいろいろあるらしいのですが、たまたま見つけたlivedoorのやつを利用します。
まずは公式ドキュメント
http://weather.livedoor.com/weather_hacks/webservice

東京の地域ID:130010の気象情報を取得します。

$ curl "http://weather.livedoor.com/forecast/webservice/json/v1?city=130010" | jq

決まった時間に実行する

時間を設定しその時間になったら処理を行う仕組みとして、node.jsのライブラリであるnode-scheduleを導入します。

まずは公式ドキュメント
https://github.com/node-schedule/node-schedule

インストールはnpmで行います。

$ npm install node-schedule

出来上がったコード

ターミナル上でcurlコマンドを実行していましたが、node-scheduleを利用するためnode.jsで実行できるように変換します。

curlコマンドをPythonやnode.jsのコードに変換する方法
https://qiita.com/tottu22/items/9112d30588f0339faf9b

そして出来上がったコードがこちら

var schedule = require('node-schedule');
var request = require('request');

var temperature_compare = 25;

var weather_options = {
    url: 'http://weather.livedoor.com/forecast/webservice/json/v1?city=130010'
};

function weather_callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        var json_obj = JSON.parse(body);
        var max_temperature_today = json_obj['forecasts'][0]['temperature']['max'];
        var max_temperature_tommorow = json_obj['forecasts'][1]['temperature']['max'];
        var max_temperature = max_temperature_today != null? max_temperature_today : max_temperature_tommorow;
        var max_celsius = max_temperature['celsius'];

        if(max_celsius > temperature_compare){
            var remo_headers = {
                'Authorization': 'Bearer {$TOKEN}',
                'accept': 'application/json',
            };

            var remo_options = {
                url: 'https://api.nature.global/1/signals/{signal_id}/send',
                method: 'POST',
                headers: remo_headers
            };

            function remo_callback(error, response, body) {
                if (!error && response.statusCode == 200) {
                    console.log(body);
                }
            }

            request(remo_options, remo_callback);
        }
    }
}

var timing = {
    hour: 06
    ,minute: 00
};

var job = schedule.scheduleJob(timing, 
    function(){
        request(weather_options, weather_callback);
    }
);

おわりに

livedoorの気象情報APIでは今日・明日・明後日の情報が取得できます。
午前中に実行したときは当日の最高気温が取得できたのですが、夕方ごろ実行するとnullになってました。
どのタイミングで切り替わるかわからなかったので、今日と明日両方取得するような動きにしてみました。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6