Amazon Echo or Alexa is a device and service by Amazon, which allows you to request news, weather information or even automate your home using voice control. People have succeeded in running the Echo software on the Raspberry Pi, making it a low cost and easy to install voice control solution.
Components
The components you’ll need for this build are:
- Raspberry Pi Zero (works with Pi 2 and 3 as well)
- USB soundcard with microphone input
- microphone
- speaker with amplifier
- momentary switch
- USB hub (if using Pi Zero)
- wifi dongle
- enclosure or box to store everything in
Software
Amazon Echo
To install Amazon Echo on the Pi, I followed the instructions from Novaspirit Tech:
The instructions in the video are clear and straightforward. Everything was installed in less than 10 minutes.
I did run into issues with Python reaching 100% CPU and some glitches in the audio, so I made modifications to the main script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
17c17 < button = 18 #GPIO Pin with button connected --- > button = 21 #GPIO Pin with button connected 97c97 < os.system('mpg123 -q {}1sec.mp3 {}response.mp3'.format(path, path)) --- > os.system('sudo mpg123 -q {}1sec.mp3 {}response.mp3 {}1sec.mp3'.format(path, path, path)) 138c138,140 < --- > # Avoid 100% CPU by Python > elif val == 1: > time.sleep(0.5) |
Default Sound
To have the Pi use the USB sound card by default, some minor changes are required. Edit or create the asound.conf file as follows:
1 2 3 4 5 6 7 8 9 10 11 |
pi@raspberrypi:~ $ sudo nano /etc/asound.conf pcm.!default { type hw card 1 } ctl.!default { type hw card 1 } |
Once the file has been edited or created, restart the process to apply the changes.
1 |
pi@raspberrypi:~ $ sudo /etc/init.d/alsa-utils restart |
Start / Restart
To have the application automatically start at boot and restart whenever it crashes, I created a helper script that check if the process is running, and if it is not, starts it.
1 2 3 4 5 6 7 |
pi@raspberrypi:~ $ sudo nano alexa.sh result=`ps aux | grep -i "main.py" | grep -v "grep" | wc -l` if [ $result -lt 1 ] then sudo /home/pi/AlexaPi/main.py & fi |
Using cron, the script is executed every minute to start/restart the process if needed. You can change the cron expression to check more or less frequently.
1 2 3 |
pi@raspberrypi:~ $ crontab -e * * * * * sh /home/pi/alexa.sh |
Hardware
Connections
The connections are rather straightforward, and can even be configured to be different, in the main.py script of AlexaPi.
In my case, the following connections were made:
- 5V / GND: power audio amplifier
- GPIO 25 / GND: arcade button LED
- GPIO 21 / GND: arcade button switch
Or as illustrated below:
Enclosure
For the enclosure, I reused an old box with magnetic lid, which I covered in duct tape to make it silver. I then cut a hole in the top lid for the arcade button to fit in, and a smaller hole at the back for the power cord. A Raspberry Pi sticker is used for decoration.
On the inside of the box are the components, with the speaker and microphone glued to the lid. The result may be messy on the inside, but it is clean on the outside 🙂
What can it do ?
Well, pretty much anything! You can ask Alexa about the weather, the news, or even to turn the lights on or off.
Alexa can also be linked to IFTTT, giving access to a bunch of channels using voice control, simply by saying “Alexa, trigger …”.
Try it, you’ll be surprised how easy it is!