Amazon EchoからSynology DiskStationを起動する

f:id:siro_uma:20181021191153p:plain家でDiskStation DS216jを使っているのですが、保存の用途がメインで積極的にアクセスはしていません。なので、基本電源をオフにしていて、必要なタイミングでWake on Lan or 電源ボタンを使って起動していました。

ただ、これが微妙に面倒で、PCで作業ついでならWoLのコマンド一発ですが、携帯からだとちょっと扱いづらい。

ということで、あまり有効活用できていないAmazon Echoを使って、DS216jを立ち上げられるようにしたいと思います。

環境

設定手順

DiskStationの設定

DSMのコントロールパネルからWOLを有効にしましょう。DiskStation側の設定はこれだけ。

Raspberry Piの設定
nvmの設定

以下を参考にnvmを設定&nodejsをインストールしましょう。 なくても良い手順ですが、node環境で色々作業する際には便利です。 Raspberry Pi 3 Model BにnvmでNode.jsをインストールする - しらすの備忘録

Node-REDの設定

Node-REDはハードウェアデバイス/APIおよびオンラインサービスを接続するためのツールです。(日本ユーザ会より引用)

今回はこのNode-REDを使って、Amazon EchoとDS216jの橋渡しをします。

インストールは以下のコマンド1発です。

$ bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)

 
This script will remove versions of Node.js prior to version 6.x, and Node-RED and
if necessary replace them with Node.js 8.x LTS (carbon) 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


Running Node-RED update for user pi at /home/pi

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       ✔
  Update Node.js LTS                  ✔   Node v6.11.2   Npm 3.10.10
  Clean npm cache                     ✔
  Install Node-RED core               ✔   0.19.4 
  Move global nodes to local          -
  Install extra Pi nodes              -
  Npm rebuild existing nodes          ✔
  Add menu shortcut                   ✔
  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  Sat 20 Oct 06:14:59 UTC 2018  -  Finished  Sat 20 Oct 06:19:46 UTC 2018

ここでnode-red-startコマンドを使って、Serviceとして起動してみましょう。

$ node-red-start

Start Node-RED
 
Once Node-RED has started, point a browser at http://192.168.10.123:1880
On Pi Node-RED works better with the Firefox or Chrome browser
 
Use   node-red-stop                          to stop Node-RED
Use   node-red-start                         to start Node-RED again
Use   node-red-log                           to view the recent log output
Use   sudo systemctl enable nodered.service  to autostart Node-RED at every boot
Use   sudo systemctl disable nodered.service to disable autostart on boot
 
To find more nodes and example flows - go to http://flows.nodered.org
 
Starting as a systemd service.
Unit nodered.service entered failed state.
nodered.service holdoff time over, scheduling restart.
Stopping Node-RED graphical event wiring tool...
Starting Node-RED graphical event wiring tool...
Started Node-RED graphical event wiring tool.
/usr/bin/env: node-red-pi: No such file or directory
nodered.service: main process exited, code=exited, status=127/n/a
Unit nodered.service entered failed state.
nodered.service holdoff time over, scheduling restart.
Stopping Node-RED graphical event wiring tool...
Starting Node-RED graphical event wiring tool...
Started Node-RED graphical event wiring tool.
/usr/bin/env: node-red-pi: No such file or directory
nodered.service: main process exited, code=exited, status=127/n/a
Unit nodered.service entered failed state.
nodered.service holdoff time over, scheduling restart.
Stopping Node-RED graphical event wiring tool...
Starting Node-RED graphical event wiring tool...
Started Node-RED graphical event wiring tool.
nodered.service: main process exited, code=exited, status=127/n/a
Unit nodered.service entered failed state.
/usr/bin/env: node-red-pi: No such file or directory
nodered.service holdoff time over, scheduling restart.
Stopping Node-RED graphical event wiring tool...
Starting Node-RED graphical event wiring tool...
Started Node-RED graphical event wiring tool.
/usr/bin/env: node-red-pi: No such file or directory
nodered.service: main process exited, code=exited, status=127/n/a
Unit nodered.service entered failed state.
nodered.service holdoff time over, scheduling restart.
Stopping Node-RED graphical event wiring tool...
Starting Node-RED graphical event wiring tool...
nodered.service start request repeated too quickly, refusing to start.
Failed to start Node-RED graphical event wiring tool.
Unit nodered.service entered failed state.

nvmにインストールされたbinにpathが通っていないようなので、nodered.servicesのExecStartを以下のようにnvmに向けたものに編集しましょう。

ExecStart=/usr/local/nvm/versions/node/v6.11.2/bin/node $NODE_OPTIONS /usr/local/nvm/versions/node/v6.11.2/lib/node_modules/node-red/red.js $NODE_RED_OPTIONS

これで今度はちゃんと起動します。

node-red-start

Start Node-RED
 
Once Node-RED has started, point a browser at http://192.168.10.123:1880
On Pi Node-RED works better with the Firefox or Chrome browser
 
Use   node-red-stop                          to stop Node-RED
Use   node-red-start                         to start Node-RED again
Use   node-red-log                           to view the recent log output
Use   sudo systemctl enable nodered.service  to autostart Node-RED at every boot
Use   sudo systemctl disable nodered.service to disable autostart on boot
 
To find more nodes and example flows - go to http://flows.nodered.org
 
Starting as a systemd service.
Started Node-RED graphical event wiring tool.
Node-REDの構成

Wake on Lan側のnode redのWoL用パッケージが存在するのでそちらをインストールしましょう。

node-red-node-wol - Node-RED

Alexa側はこちらのブログを参考に設定を完了させました。

最終的には以下のようなフローを作成し、セットアップ完了。

f:id:siro_uma:20181021191153p:plain
Node-REDのフロー

これで、Alexaに「Alexa, NAS(ナス)をつけて」と言うとDS216jが起動するようになりました。

めでたしめでたし。