LoginSignup
5
8

More than 5 years have passed since last update.

Nature Remo で温度超過時にアラート通知をする

Posted at

はじめまして。最近、チンチラを描い始めました。
猫ではなく、ネズミっぽいアレです。
可愛いですよね。めっちゃうんこするけど。

IMG_3254.jpg

ところで、チンチラは標高が高い山に原生している生き物なので、暑さに弱いのです。
温度が25度を超えるとしんどいらしいです。

これからの季節、基本冷房はつけっぱなしにする予定ですが、
うっかり野郎なので、エアコン付け忘れたまま外出してしまうかもしれません。
そのため、温度がしきい値を超えたときに通知が来るようなものを実装しようと考えました。

今回は、「Nature Remo」と、「Line Notify」を使って環境を作ります。
Nature Remoは、家電の操作をリモートでできるスマートなヤツです。
APIを使っていろいろ操作を行うことができます。
https://nature.global/jp/blog/2018/1/30/api

事前準備

・Nature Remoのアクセストークンを取得
以下URLからトークンを発行します。
https://home.nature.global/
トークンは一度しか表示されないので、メモします。

・Line Notifyのトークンを取得
https://notify-bot.line.me/
LINE@でアカウントを作ってもいいのですが、今回はPush通知だけ行いたいのでNotifyを選びました。

RemoのAPIを叩く

Swaggerにドキュメントが公開されています。
http://swagger.nature.global/#/default/get_1_devices

今回はRubyで書きたいので、Swagger上のサンプルコマンドをcurl-to-rubyでnet/httpに一発変換。
https://jhawthorn.github.io/curl-to-ruby/


require 'net/http'
require 'uri'

uri = URI.parse("https://api.nature.global/1/devices")
request = Net::HTTP::Get.new(uri)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer <アクセストークン>"

req_options = {
  use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end

puts response.body

以下のような結果が帰ってきます。


[
    {
        "created_at": "2018-04-02T09:33:23Z", 
        "firmware_version": "Remo/1.0.62-gabbf5bd", 
        "humidity_offset": 0, 
        "id": "<RemoのID>", 
        "name": "<Remoの名前>, 
        "newest_events": {
            "hu": {
                "created_at": "2018-04-03T06:44:31Z", 
                "val": 50
            }, 
            "te": {
                "created_at": "2018-04-03T04:50:11Z", 
                "val": 23
            }
        }, 
        "temperature_offset": 0, 
        "updated_at": "2018-04-02T14:14:41Z", 
        "users": [
            {
                "id": "<ユーザID>", 
                "nickname": "<自分の名前>", 
                "superuser": true
            }
        ]
    }
]

[newest_events]の[hu][val]が湿度、[te][val]が温度のようです。

必要な情報を抜き出す

温度と湿度の情報だけほしいので、抜き出します。


hash = JSON.parse(response.body)

temptation = hash[0]["newest_events"]["te"]["val"].to_f
humidity = hash[0]["newest_events"]["hu"]["val"].to_f

puts temptation
puts humidity

実行結果

23.0
50.0

いい感じの気温・湿度ですね。

Line Notifyからの通知

こちらを参考にしました。以下コードを追加。
https://qiita.com/pinori/items/7964b6c5c614ea783540

def LineNotify(msg)
    uri = URI.parse("https://notify-api.line.me/api/notify")
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true

    req = Net::HTTP::Post.new(uri.request_uri)
    req["Authorization"] = "Bearer <アクセストークン>"
    req["Content-Type"] = 'application/x-www-form-urlencoded;charset=UTF-8'
    req.set_form_data({message: msg})

    res = https.request(req)
end

upperTemp = 25.0
lowerTemp = 15.0

if temptation > upperTemp
    msg = "室温が" + temptation.to_s + "度になりました。\n" + "暑いよ~!(´˔`;^ )∫"
    LineNotify(msg)
elsif temptation < lowerTemp
    msg = "室温が" + temptation.to_s + "度になりました。\n" + "寒い…((-˔-^lll )∫)ガタガタ"
    LineNotify(msg)
end

実行する

IMG_9065.png

ちゃんと通知されました。
RemoのAPI制限は「5分以内に30回」なので、1分に1回程度なら回しても問題なさそうです。
それでは、よいチンチラライフを…

5
8
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
5
8