はじめに
キッカケはこのツイート。そもそもAlexa Gadgets Toolkit
の存在を忘れていたので、いまさらながら、Alexa Gadgets Toolkit
をやってみようと思った。
Alexa Gadgets Toolkitを使えば可能かも🤔 :ESP32をAlexa Gadgets Toolkitデバイスにしよう https://t.co/pdU67zmSZV
— robo8080 (@robo8080) June 12, 2021
Alexa Gadgets Toolkit とは
Bluetooth経由で互換性のあるAmazon Echoデバイスと対話するAlexa接続のアクセサリ。
Alexa Gadgets Toolkit
を使用することで、独自のAlexaガジェットを作成することができる。
- Alexaスキルからガジェットにアクセスする。
- Alexaのネイティブ機能に反応してガジェットの動作をトリガーする。
- Speechmark data
- Notifications
- Timers, alarms, and reminders
- Wake word detection
- Current time
- Tempo data of songs from Amazon Music
Alexaとドムヘッドを連携させるために必要な部品
今回は、Alexa-Gadgets-Raspberry-Pi-Samplesの Wake Word LED Gadget Example
をそのまま使う。
部品
- DOM HEAD 2
- Echo Show 10
- Raspberry Pi 3 Model B+
- Speaker ( Seria )
- Breadboard
- Red LED
- Resistor(330 ohm)
- Jump wire x 4
※Echoデバイスから音を出したかったが、Alexa Gadgets Toolkit
でBluetoothを使用してしまったため、スピーカーはイヤホンジャックに接続した。
Amazon IDとAlexa Gadget Secretを生成する
Alexa Voice Service Developer Console にログインして、製品
をクリックし、新しい商品を作成することで、Amazon ID
と Alexa Gadget Secret
が生成できる。
Amazon ID
と Alexa ガジェットのシークレット
を使用する。
サンプルコードを入手する
Raspberry Pi
用のサンプルコードを github から clone して、Raspberry Pi
に転送する。何も考えずに、/home/pi/Alexa-Gadgets-Raspberry-Pi-Samples
に配置した。
git clone https://github.com/alexa-samples/Alexa-Gadgets-Raspberry-Pi-Samples.git
セットアップする
基本的には、サンプルのREADME.mdに書かれている通りに進めていけばセットアップができる。
pi@raspberrypi:~ $ cd Alexa-Gadgets-Raspberry-Pi-Samples/
pi@raspberrypi:~/Alexa-Gadgets-Raspberry-Pi-Samples $ pwd
/home/pi/Alexa-Gadgets-Raspberry-Pi-Samples
pi@raspberrypi:~/Alexa-Gadgets-Raspberry-Pi-Samples $ sudo python3 launch.py --setup
Amazon ID
と Alexa ガジェットのシークレット
の入力を求められるので、上記で取得した文字列を入力する。
+--------------------------------------------------------------------+
| .oooooooo. 888 |
| d8P' 'Y8b .oooo. 888 .ooooo. oooo ooo .oooo. |
| 888 888 'P )88b 888 d88' '88b '88b..8P' 'P )88b |
| 888 888 .oP'888 888 888ooo888 Y888' .oP'888 |
| '88bb dd88' d8( 888 888 888 .o .o8''88b d8( 888 |
| 'Y8bb,ood8P' 'Y888888o 888o 'Y8bod8P' o88' 888o 'Y888888o |
+--------------------------------------------------------------------+
Do you want to configure all examples with your Alexa Gadget credentials (y/n)? y
Enter the Amazon ID for your gadget: ← <Amazon ID>
Enter the Alexa Gadget Secret for your gadget: ← <Alexa ガジェットのシークレット>
:
:
※このあと、けっこう時間がかかる。
サンプルプログラムを修正する
サンプルコードに音を出す部分を追加している。デフォルトでインストールされている pygame
を使ってドムの起動音を鳴らすようにした。ウェイクワードが発話された時、state.value == 'active
時に音を出力するようにした。
#
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# These materials are licensed under the Amazon Software License in connection with the Alexa Gadgets Program.
# The Agreement is available at https://aws.amazon.com/asl/.
# See the Agreement for the specific terms and conditions of the Agreement.
# Capitalized terms not defined in this file have the meanings given to them in the Agreement.
#
import pygame.mixer
import time
import logging
import sys
from gpiozero import LED
from agt import AlexaGadget
# mixer init
pygame.mixer.init()
pygame.mixer.music.load("/home/pi/launch.mp3")
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(__name__)
GPIO_PIN = 14
LED = LED(GPIO_PIN)
class WakewordGadget(AlexaGadget):
"""
An Alexa Gadget that turns an LED on and off in sync with the detection
of the wake word
"""
def __init__(self):
super().__init__()
def on_alexa_gadget_statelistener_stateupdate(self, directive):
for state in directive.payload.states:
if state.name == 'wakeword':
if state.value == 'active':
logger.info('Wake word active - turn on LED')
LED.on()
# play
pygame.mixer.music.play(1)
time.sleep(8)
pygame.mixer.music.stop()
elif state.value == 'cleared':
logger.info('Wake word cleared - turn off LED')
LED.off()
if __name__ == '__main__':
try:
WakewordGadget().main()
finally:
logger.debug('Cleaning up GPIO')
LED.close()
配線
Raspberry PiとLEDの 配線はサンプルそのまま。この赤いLEDをドムのモノアイ部分に埋め込んでいるだけ。
LEDを埋め込むためにパーツを削り、ジャンパーピンの通り道を穴をドリルで開ける。
動作確認
Wake Word LED Gadget Example
を起動し、Alexaをウェイクする。
pi@raspberrypi:~/Alexa-Gadgets-Raspberry-Pi-Samples $ sudo python3 launch.py --example wakeword
おわりに
Alexaの雰囲気を100%壊してしまう点ではイマイチなガジェットかもしれないけど、個人的にはとても満足した。どちらかというと製作にかかった時間のほとんどは、ドムヘッドの塗装と、LEDを埋め込むための改造だった...