Hobby Science&Experiment

愛と工作の日々

趣味でやっている工作や勉強したことのメモ書きです。

Node-REDでAlexaに好きな言葉を喋らせる【Raspberry Pi3+】

Alexaは魅力的なツールだと思う一方、標準機能より更にカスタマイズした使い方をしたい時があります。例えば「部屋のCO2濃度が一定値を超えるとAlexaからアラートを出す。」「職場を出たタイミングを家族にAlexaから通知する。」等出来ればいいのにと思います。しかし通常の運用では好きな言葉をしゃべらせたり、任意のプログラムで操作するようなことが出来ません。

f:id:tara-chang:20200501163509p:plain
わが家のアレクサ
何か方法はないかと探したところ、以下の記事を発見しました。
dream-soft.mydns.jp
Node-REDのモジュールであるnode-red-contrib-alexa-remote2でAlexaをコントロールできるようです。Node-REDの実行サーバーとしてRaspberry pi3+を使用します。ラズパイ便利ですね。

この記事でやること

Raspberry pi3+へのNode-REDのインストール、node-red-contrib-alexa-remote2を使ってAlexaから任意の言葉を喋らせる。

Node-Redのインストール

Raspberry piでLXTerminalを開き、以下のコマンドでNode-REDをインストールします。

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

結果

This script will remove versions of Node.js prior to version 7.x, and Node-RED and
if necessary replace them with Node.js 10.x LTS (dubnium) and the latest Node-RED from Npm.

It also moves any Node-RED nodes that are globally installed into your user
~/.node-red/node_modules directory, and adds them to your package.json, so that
you can manage them with the palette manager.

It also tries to run 'npm rebuild' to refresh any extra nodes you have installed
that may have a native binary component. While this normally works ok, you need
to check that it succeeds for your combination of installed nodes.

To do all this it runs commands as root - please satisfy yourself that this will
not damage your Pi, or otherwise compromise your configuration.
If in doubt please backup your SD card first.

Are you really sure you want to do this ? [y/N] ? y

Would you like to install the Pi-specific nodes ? [y/N] ? y


Running Node-RED install for user pi at /home/pi on raspbian


This can take 20-30 minutes on the slower Pi versions - please wait.

Stop Node-RED ?
Remove old version of Node-RED ?
Remove old version of Node.js ?
Install Node.js LTS ? Node v12.16.2 Npm 6.14.4
Clean npm cache ?
Install Node-RED core ? 1.0.5
Move global nodes to local -
Install extra Pi nodes ?
Npm rebuild existing nodes -
Add shortcut commands ?
Update systemd script ?


Any errors will be logged to /var/log/nodered-install.log
All done.
You can now start Node-RED with the command node-red-start
or using the icon under Menu / Programming / Node-RED
Then point your browser to localhost:1880 or http://{your_pi_ip-address}:1880

Started 2020年 4月 19日 日曜日 14:00:56 JST - Finished 2020年 4月 19日 日 曜日 14:04:59 JST

上手くインストール出来たようです。
上のログに書いてあるように、Node-REDの起動は以下のコマンドで行います。

sudo nodes-red-start

ブラウザからhttp://{your_pi_ip-address}:1880にアクセスするとNode-REDのコンソールが開きます。

node-red-contrib-alexa-remote2を使う

ここからの工程は参考記事にかなり詳しく書かれているため、敢えて詳述する必要はないと思います。この記事の存在意義…(^^;)
Node-REDのコンソール画面を開きます。ノードの追加でnode-red-contrib-alexa-remote2を検索・インストールします。Alexa Routineというノードを選択し、連携したいAmazonアカウントを追加し、参考記事に従ってブランクを埋めました。

f:id:tara-chang:20200430193538p:plain
設定を保存したらTextに喋らせたい言葉、Deviceに喋らせるデバイスを設定します。

f:id:tara-chang:20200430194443p:plain
設定画面
Alexa Routineにトリガーを送るノードとして、タイムスタンプを接続します。デプロイした後、タイムスタンプを押すとAlexaが入力した言葉を喋るはずです。

ハマりポイント

最初タイムスタンプを押しても、Account not initialisedのエラーが頻発しました。
f:id:tara-chang:20200430195940p:plain
デプロイ後認証が済むまで数秒~10数秒程待ってやると、状態がinit with passwordからreadyに変わりAlexaと接続できました。
f:id:tara-chang:20200430195822p:plain

今後やってみたいこと

・CO2濃度が一定値を超えるとAlexaからアラートを出す。
・職場を出たタイミングを家族にAlexaから通知する。
・ゴミの日の前日にAlexeから通知する。

参考記事

dream-soft.mydns.jp