AIY Voice Kit V2 電源入れたらGoogleアシスタントを実行

とりあえず、サンプルプログラムが動くようになったので、電源をいれたら、実行されるようにしたいと思います。

OSのバージョン

念のため確認。

1
2
3
4
5
6
pi@raspberrypi:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.1 (stretch)
Release: 9.1
Codename: stretch

どうやるの?

こちらの記事(Google Voice Kit をオートスタートさせる)を参考に、/home/pi/AIY-projects-python/HACKING.mdのRunning automaticallyの項に従うことにしました。

最新のイメージファイルだと、HACKING.mdに記載がないらしいのですが、V2でデフォルトの状態では、記載がありました。

1
2
3
4
5
If you want the voice recognizer service to run automatically when the Pi
boots, you need to have a file in the `src` directory named `main.py`. You can
make a copy of one of the example scripts and rename it. Then run this command:

sudo systemctl enable voice-recognizer.service

実行したいPythonのソースファイルをsrc/main.pyとして保存して、voice-recognizer.serviceを有効にすればよいと。

というわけで、とりあえずコピー。

1
pi@raspberrypi:~ $ cp ./AIY-voice-kit-python/src/examples/voice/assistant_grpc_demo.py ./AIY-voice-kit-python/src/main.py

参考にした記事にはvoice-recognizer.serviceに不備があるとありますが、とりあえず、このままいってみます。
voice-recognizer.serviceを有効化。

1
2
pi@raspberrypi:~ $ sudo systemctl enable voice-recognizer.service
Created symlink /etc/systemd/system/multi-user.target.wants/voice-recognizer.service → /lib/systemd/system/voice-recognizer.service.

起動後、ステータスを確認。残念な結果のようです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pi@raspberrypi:~ $ systemctl status voice-recognizer.service
● voice-recognizer.service - voice recognizer
Loaded: loaded (/lib/systemd/system/voice-recognizer.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2018-12-01 11:49:32 UTC; 1min 54s ago
Process: 750 ExecStart=/home/pi/AIY-projects-python/env/bin/python3 -u src/main.py (code=exited, status=203/EXEC)
Main PID: 750 (code=exited, status=203/EXEC)

Dec 01 11:49:32 raspberrypi systemd[1]: voice-recognizer.service: Failed with result 'exit-code'.
Dec 01 11:49:32 raspberrypi systemd[1]: voice-recognizer.service: Service hold-off time over, scheduling restart.
Dec 01 11:49:32 raspberrypi systemd[1]: Stopped voice recognizer.
Dec 01 11:49:32 raspberrypi systemd[1]: voice-recognizer.service: Start request repeated too quickly.
Dec 01 11:49:32 raspberrypi systemd[1]: Failed to start voice recognizer.
Dec 01 11:49:32 raspberrypi systemd[1]: voice-recognizer.service: Unit entered failed state.
Dec 01 11:49:32 raspberrypi systemd[1]: voice-recognizer.service: Failed with result 'exit-code'.
pi@raspberrypi:~ $ /home/pi/AIY-projects-python/env/bin/python3 -u src/main.py

しかたがないので、/lib/systemd/system/voice-recognizer.service を編集することに。
参考にしたサイトからコピペしていいのかよくわからなかったので、とりあえずExecStartのパスが怪しいと思って書き直しました。

前:ExecStart=/home/pi/AIY-projects-python/env/bin/python3 -u src/main.py
後:ExecStart=/usr/bin/python3 -u /home/pi/AIY-projects-python/src/main.py

全体はこちら。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# This service can be used to run your code automatically on startup. Look in
# HACKING.md for instructions on creating main.py and enabling it.

[Unit]
Description=voice recognizer
After=network.target ntpdate.service

[Service]
Environment=VIRTUAL_ENV=/home/pi/AIY-projects-python/env
Environment=PATH=/home/pi/AIY-projects-python/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ExecStart=/usr/bin/python3 -u /home/pi/AIY-projects-python/src/main.py
WorkingDirectory=/home/pi/AIY-projects-python
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

そしてリブート後、再確認。
スクリプトの実行はうまくいったようです。

1
2
3
4
5
6
7
8
9
10
11
pi@raspberrypi:~ $ systemctl status voice-recognizer.service
● voice-recognizer.service - voice recognizer
Loaded: loaded (/lib/systemd/system/voice-recognizer.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-12-01 12:10:45 UTC; 2min 26s ago
Main PID: 556 (python3)
CGroup: /system.slice/voice-recognizer.service
└─556 /usr/bin/python3 -u /home/pi/AIY-projects-python/src/main.py

Dec 01 12:10:45 raspberrypi systemd[1]: Started voice recognizer.
Dec 01 12:12:00 raspberrypi python3[556]: Press the button and speak
Dec 01 12:12:00 raspberrypi python3[556]: [2018-12-01 12:12:00,698] INFO:recorder:started recording

1度シャットダウンして、電源を入れなおしてみます。(ターミナル接続なし)
ボタンを押して話しかけたら答えてくれました。

これで。

シンプルなAIスピーカとしては使えますが。

起動してるんだか。
ちゃんと聞いてくれてるんだか。
問い合わせ中なんだか。

全くわからない。

ので。

何らかの形でわかるようにしたい。

と思うのでありました。

やれるのかな。やれるよね。きっと。

いや。

その前にOSの時間がずれているの直したいような気がする。

なお。

OSの時間がずれていても、今の時間はちゃんと正しいのを答えてくれます。
Googleアシスタントは、毎回APIで聞きにいっているからだと思われます。