Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9020

部屋が乾燥してきたらGoogleHomeで知らせてもらう

$
0
0

はじめに

今年の冬は、例年以上に湿度に注意という事で、湿度が低いときはGoogleHomeに注意してもらうことにした。

image.png

ハード

  • RaspberryPi 3B
  • GoogleHome (私のはminiです)
  • SwitchBot 温湿度計
  • 母艦(Windowsマシン。何でも良い)

モジュール

主なモジュールは以下の通り。
- Raspberry Pi OS:Buster(2020-05-27-raspios-buster-armhf)
- Node.js
- castv2-client
- VoiceText
- node-switchbot
- Ambient(予め準備しておく:Ambient)

Raspberry Pi OS

RaspberryPiのインストールは済んでいるもとします。

~ $ cat /proc/device-tree/model
Raspberry Pi 3 Model B Rev 1.2

~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:        10
Codename:       buster

Raspberry Pi OS:Buster(2020-05-27-raspios-buster-armhf)

castv2-googlehome

以前、投稿した「RaspberryPi で castv2-client を使ってGoogleHomeをしゃべらす」を使用するので、しゃべるところまでは完成させておくこと。

モジュールの取得

今回作成したモジュールをgithubに上げてあるので取得する。
https://github.com/nori-dev-akg/switchbotTH-googlehome

~ $ cd ~                                          #ホームに移動
~ $ git clone https://github.com/nori-dev-akg/switchbotTH-googlehome
~ $ cd switchbotTH-googlehome                     #カレントを移動しておく
~/switchbotTH-googlehome $ npm init --yes#作業用ディレクトリ初期化#以降、作業用ディレクトリで行う

Ambient

せっかくなので Ambient でログも取っておく
Ambientライブラリをインストール

~/switchbotTH-googlehome $ npm install ambient-lib

node-switchbot

Bluetooth用モジュールと node-switchbotライブラリをインストール

~/switchbotTH-googlehome $ sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev
~/switchbotTH-googlehome $ npm install @abandonware/noble
~/switchbotTH-googlehome $ npm install node-switchbot

npm install時にwarningが出るけどスルー

SwitchBot お試し

以下を実行すると、近くの SwitchBot のアドバタイズデータが取れます。何回か出ます。
"address" が SwitchBot の IPアドレス。

#sudoを付けること
~/switchbotTH-googlehome $ sudo node node-switchbot-sample.js
{"id": "xxxxxxxxxxxxxxxxx",
  "address": "xx:xx:xx:xx:xx:xx",
  "rssi": -87,
  "serviceData": {"model": "T",
    "modelName": "WoSensorTH",
    "temperature": {"c": 24.4,
      "f": 75.9
    },
    "fahrenheit": false,
    "humidity": 53,
    "battery": 100
  }}
:

部屋が乾燥してきたらGoogleHomeで知らせてもらう

いよいよ本題。以下、ソース部分を任意に変更すること

  • RaspberryPi のIPアドレス
  • GoogleHome のIPアドレス
  • VoiceText APIキー
  • SwitchBot IPアドレス
  • Ambient チャンネルID, ライトキー

ソースでは「20度以上で45%以下になったら通知」となっているので適宜。
メッセージも「乾燥しています」となっているので適宜。
※不在時対策:冬場不在時はエアコンを切っているので20度以上にはならないので

switchbotTH-googlehome.js

:constrapsberrypi_ip='192.168.0.31';constgooglehome_ip='192.168.0.200';constvoicetext_key='xxxxxxxxxxxxxxx';constswitchbot_ip='xx:xx:xx:xx:xx:xx';constambient_id=9999;// Ambient チャンネルIDconstambient_key='xxxxxxxxxxxxxx';// Ambient ライトキーconstmessage='乾燥しています';constspeaker='haruka';//'show', 'haruka', 'hikari', 'takeru', 'santa', 'bear'constmin_temp=20;// 20度以上constmin_humi=45;// 45%以下:

実行してみる

~/switchbotTH-googlehome $ sudo node switchbotTH-googlehome.js
24.3
44
ambient:200
乾燥しています
status broadcast playerState=IDLE content=http://192.168.0.31:8080/voicetext/voicetext.mp3
status broadcast playerState=PLAYING content=http://192.168.0.31:8080/voicetext/voicetext.mp3

上記のようになり、GoogleHomeから「乾燥しています」が聞こえればOK。

動かないときは

  • RaspberryPiのIPアドレスVoiceTextのAPIキーGoogleHomeのIPアドレスAmbient チャンネルID, ライトキーSwitchBot IPをもう一度確認する。

  • 特に RaspberryPiのIPアドレスGoogleHomeのIPアドレスが間違いやすい!!

  • node-switchbot で使用している noble モジュールが sudoを必要とするので sudoを必ず付ける!sudo node switchbotTH-googlehome.js

永続化 & 定期実行

RaspberryPiの再起動時にも実行されるようにする。
crontab は sudo で実行する!sudo crontab -e
必ず、フルパスで記述する!

#必ず sudo で実行すること! 
~/switchbotTH-googlehome $ sudo crontab -e#部屋が乾燥してきたらGoogleHomeで知らせてもらう*/5 **** /usr/local/bin/node /home/pi/switchbotTH-googlehome/switchbotTH-googlehome.js > /home/pi/switchbotTH-googlehome/log 2>&1
#/etc/rc.local の下の方に追加
~/switchbotTH-googlehome $ sudo nano /etc/rc.local

/etc/rc.local

:
# castv2-googlehome
forever start /home/pi/castv2-googlehome/api.js

exit 0

以上。


Viewing all articles
Browse latest Browse all 9020

Trending Articles