The AlexaPhone is a 1970s-style trimphone that I've converted to house a Raspberry Pi running Amazon's Alexa voice service.
To use it you just lift the handset, speak your query, hang up and Alexa's response is delivered via a built-in speaker. It has a colour-changing LED to show what it's up to and plays an authentic trimphone sound to indicate when Alexa is ready to receive your instructions.
The GPIO Challenge is an Alexa Skill that I've created to test your knowledge of everyone's favourite 40-pin connection - a multiple choice quiz for Raspberry Pi noobs and gurus alike!
Check out the AlexaPhone GPIO Challenge Video to see it in action!
Background and ResearchIt was exciting to read earlier this year that Amazon had released some new code enabling Raspberry Pi tinkerers to connect their projects to the Alexa voice service, and especially to see it covered in the mainstream news a few days later.
I experimented with voice search a while back with my Google Voice Search-O-Matic project, but being able to do something similar with a Pi opened up a whole range of new possibilities. I had a project in mind straightaway, to convert a now-obsolete piece of tech to work with this new service. I began by looking through the Amazon Alexa documentation on GitHub, and my first reaction was that it seemed like an awful lot of complicated hoops to jump through!
The first stage is to register for an Amazon Developer account, then set up a device profile on the Amazon website - this process provides you with the security codes and ID numbers that are passed from your project to the Alexa service. I found it easiest to set this up on my laptop and drop all the codes into a self-addressed email so that I could later retrieve them on the Pi via Claws Mail - the IDs are very long strings of characters and you definitely don't want to be typing them in manually!
At this point I looked a bit more closely into the Amazon instructions and found that my excitement may have been a bit premature - the official instructions do get Alexa workiing on the Pi, but they're based on having a network cable and monitor attached, activating the voice search by clicking a software button on screen. This wasn't really the code I was looking for - I was interested in building a standalone wireless enabled "headless" device, with no monitor and some kind of hardware switch to activate the voice search. After a bit of searching around I came across Sam Machin's excellent AlexaPi code on GitHub. This seemed a lot simpler than the published Amazon version, and was written in Python, so felt much more accessible. After a few evenings of fiddling and a dig through the Open & Closed Issues on GitHub I got it working on my Raspberry Pi 3, using a hardware push-button to get Alexa to "Listen Up!", an old ipod dock to play back the sound and a near-obsolete USB VOIP Phone (kindly donated by my Mother-in-law) to capture the sound.
Getting the Pi Alexa-readyI won't go into all the detail of the code here as it's well documented on GitHub and seems (from all the Watch notifications I get) to be in active development so may change often. With it working on my Pi 3 I had the idea to build the AlexaPhone in parallel with my original project, starting from scratch with my Pi 2 (unfortunately meaning no RetroPie temporarily). Having set it up once already I managed to complete the Pi & code configuration in a single evening - this is the process I followed:
First I set up a new device called AlexaPhone on the Amazon website, following the process in Section 3.4 of the ReadMe. The only change needed to support the AlexaPi code was to replace amazon's suggested https://localhost:3000 with https://localhost:5000 in the "Web Settings" section of the Security Profile. We'll come back to the web settings a bit later on. With the device set up I sent myself all the codes by email and turned my attention to the Pi.
I started by downloading & writing a fresh installation of Raspbian to an 8gb SD card - you could also use NOOBS, this worked for me on the Pi 3. With a monitor & network cable attached I did the following after the first boot:
- Expand the filesystem (open a terminal session, type raspi-config, select option 1)
- Still in raspi-config set the Audio to force 3.5mm output instead of HDMI (Advanced Options - Audio)
- Still in raspi-config change the Hostname to AlexaPhone - if you have multiple Pis on the wireless network this makes it easier to tell them apart
- Still in the terminal type lsusb - listing the USB devices to make sure the USB phone was detected
- From the desktop set up the Microphone level to maximum (Preferences > Audio Device Settings)
- From the desktop set up the WiFi by clicking on the network icon in the taskbar
From here I rebooted the Pi and then switched back to the laptop temporarily to...
- Log into the WiFi router and set the Pi to have a "Reserved IP Address" - the MAC address of the Pi's WiFi adaptor should be obvious in your router's list of Attached Devices, especially if you changed the hostname.
- Make a note of the Pi's IP address, log back into the Amazon website and add two new lines to the Web Settings of the Security Profile, so that in Allowed Origins there's one line that says https://localhost:5000 and below it a new one that says https://YourPiIpAddress:5000. The same for Allowed Return URLs so there's https://localhost:5000/authresponse and https://YourPiIpAddress:5000/authresponse showing.
Now to set up the AlexaPi code! I followed the instructions from GitHub which were well-written, I'll just add my personal experiences:
- Make sure you're logged in as root (sudo su in terminal) before you do the Git Clone bit
- Make sure you're in the Root folder before you do the Git Clone bit (cd /root/ in terminal)
- After the Git Clone bit is done you'll need to edit the file setup.sh if want to use wi-fi - switch to the AlexaPi directory (cd AlexaPi) then manually edit (sudo nano setup.sh) the file - replacing ip=ifconfig eth0 with ip=ifconfig wlan0
- Now you can run setup.sh, which prompts you for all the long codes that were generated on the Amazon website while setting up the device profile. I found it easiest to copy & paste these from Claws Mail.
- For me the setup.sh script gave an error at the end, though it may well work fine for you - all I needed to do was manually edit the creds.py file in the AlexaPi folder, pasting in the security IDs again.
- From here I manually ran auth_web.py (sudo python auth_web.py), then opened a web browser window and typed https://localhost:5000 - this brought up an Amazon login page to complete the process
I'm not certain that all 100% of the processes above were necessary, but that was what worked for me. After a reboot the AlexaPi service started automatically when the Pi booted, making me jump when it gave its "Hello" greeting.
The main part of the code is appropriately called main.py, and this contains the instructions for capturing the audio query, uploading it to Alexa and retrieving/playing back the response, as well as defining the GPIO pins for the switch and indicator LEDs. The code is designed for a three-legged Tri Colour LED, which was perfect for me as I wanted to keep the phone exterior as uncluttered as possible. The only thing I amended for this project was the filename of the .mp3 file played on startup, replacing it with the name of the trimphone ringtone that I'd recorded. With the code thankfully working perfectly I left it well alone, disconnected the monitor and network cable and moved on to the construction.
Button and MicrophoneWhen I was experimenting with the first Alexa build I was really impressed at the hardware support of the Pi - the old USB VOIP phone worked straightaway, as did an equally old webcam with built-in mic, I had fully expected to need to wrestle with extra code or buy a modern USB microphone so it was great to be able to re-use what I had lying around.
I was trying to decide whether to crack open the USB phone's case when I noticed it had 3.5mm sockets in the side, presumably to attach an external microphone and speaker. At this point I had a Eureka moment and wondered whether an old phone handset could be plugged in here - I'd done something similar once before, converting a rotary phone into a bluetooth headset, so was intrigued to try it! I butchered a 3.5mm audio cable, plugged it into the USB phone and experimented with test leads connecting it to the various cables inside the phone, having taped up the USB phone's internal microphone. It worked after trying a few different combinations, and I found that using the phone handset I could practically whisper my search query and it was recognised.
Next came the button - the AlexaPi code needs the hardware button to be continually pressed while speaking, which I found a little bit counter-intuitive in testing. I wanted to use the phone's receiver switch to trigger the code, so that lifting the handset would start Alexa listening and replacing it would tell her to go fetch the response. I was very glad at this point that I'd decided to use a reproduction trimphone rather than one of my other "original" rotary phones - inside the case a lovely modern ribbon cable was poking out directly from the receiver switch. By experimenting with jumper cables, breadboard and an LED I hit upon a combination of connections that closed the switch when the handset was lifted and opened it again when it was replaced. The mic and button had gone really well - now it was time to sort out the speaker.
Pirate ContentWhen I was kicking around my Alexa ideas I knew that it would be important to have a small and decently loud battery powered speaker. I didn't want to have an extra power supply coming from the phone, just the power lead for the Pi.
I stumbled across a solution at the local electronics shop, a KitSound Mini Buddy - this seemed ideal as it was small, had a micro-usb-rechargeable battery and was cheap at under £5. The packaging claimed it was surprisingly loud which I took with a pinch of salt, but honestly I was impressed at the volume. As we'd bought the "pirate" version my son asked if that would make Alexa talk like a pirate - I think that'd be a great development for the future!
The speaker worked well in testing with the Pi, so the next thing was to take it apart - no way was the whole speaker case going to fit inside the phone body. After removing four screws the case split open in two pieces (and his ears fell off), One side held the speaker, the other the circuit board and tiny pillow-like battery. If you want to do the same thing I'd say take it really slowly, the wires are super thin in there!
The speaker was securely stuck to the grille, so I scored carefully around the outside edge with a little screwdriver and craft knife to remove the glue, narrowly avoiding poking a hole through the speaker cone. The circuit board was a bit more tricky, it was held in by a screw and hot glue, which I removed a tiny bit at a time with a scalpel under a magnifying lamp.
Eventually the delicate circuit, battery and speaker were free of the case and ready to add to the phone (after testing to make sure they still worked). Initially I'd planned to have the speaker "always on" and constantly connected to the Pi for charging, but started to worry how this might interfere with the Pi's power and whether constant charging would make the weeny battery go pop. I decided to mount the circuit board in the phone case, cutting out a slot to make the charging port and switch accessible without dismantling the phone. I drilled a 25mm hole in the base of the case for the speaker and then hot-glued the board in place.
Phone Box ChallengeWith everything working while strewn across the bench the next step was to fit all of the components into the phone body - a bit daunting as the space was really tight and the mechanical receiver switch needed a lot of space to move freely up & down. As the speaker was already in place I was left with just the Pi, USB Phone and connecting cables to fit in. After inspecting the voip handset I was sure I couldn't crack it open without breaking it, so spent a good while trying different layouts.
It was tricky working around all the left-over plastic posts and clips in the base so to make things easier I took a rotary tool cutting wheel to any protruding bits of plastic, leaving an almost flat surface to work with. The case is held closed by a single screw, so everything but its post could be removed. This made all the difference, so after double-checking the layout I drilled holes to fit the Pi's bolts and the cable ties that would hold everything in place.
Final TouchesIt was a satisfying moment to finally get the phone sitting there on its own, with all the components tucked away inside. One of the last jobs was to fit the colour-changing LED into the hole in the side of the body. In the trimphone's previous life as a bluetooth headset this hole had been filled by a 3.5mm audio socket, so it was a bit bigger than the 5mm LED. I didn't want to just hot-glue the LED in place, so I secured it with a rubber washer on either side of the case.
After that I printed out a question mark logo for the dial insert, cutting it out with the help of a conveniently-sized coffee pod, which finished it off nicely.
Ready Alexa?This is definitely one of my favourite ever projects, it has just the right combination of old and new tech that I love and (unlike normally) it's practical, useful and I like the final look of it. It's great fun testing it with the kids and hearing the kind of things they ask Alexa. I have a pretty good idea of what "she" will and won't be able to cope with, but they approach it with more open minds, (when is my Birthday?) and are surprised when she's stumped by something like "how do I build a Redstone clock".
The AlexaPi code is developing all the time, to include new features like reminders and music playback, so it's worth keeping an eye on GitHub for the latest releases.
What IFTTT?It's a lot of fun asking Alexa for information, facts and riddles but I wanted the AlexaPhone to do much more. Bring on the IFTTT integrationl!
If you've not used IFTTT (IF This Then That) yet it's an online service, and once you're signed up you can connect up all of your web services like Gmail, Facebook and Twitter. There's a total smorgasbord of services to choose from, also including control options for smart appliances like lightbulbs, thermostats and sockets.
The IFTTT rules are set up in "recipes" - kind of like an Outlook rule or an IF statement in SQL or Visual Basic, for example I have a recipe that says "IF a new photo is added to my Google Drive THEN download it to the "My Pictures/2016" folder on my Personal Cloud"
IFTTT works especially well with the Alexa voice service - for the IF part of a recipe you can set up a trigger phrase, for example "the quiz lights" (as seen in the video) and if you then say to Alexa "Trigger the quiz lights" she will pass the request to IFTTT, which will fire the THEN part of the recipe, in this case activating a remote socket connected to an LED disco light.
It even goes beyond websites and "smart things" - if you have IFTTT installed on your phone (mine is the Android version) then you can interact with it in both directions to play a specific music track or receive an alert when the battery is low.
Skilling UpIFTTT is a fantastic service, but saying "Trigger" before each request makes you feel a bit like Roy Rogers or Del Boy after a while - I wanted to create a smoother Alexa experience without needing to rely on IFTTT to do the heavy lifting. Alexa's "native" apps are called skills, and the process for creating these is reasonably straightforward and well documented, with an ever- growing repository of available skills. If you've already created a developer account to make your homebrew Echo then the same account can be used for the other web services, which is handy.
This was new territory so I decided to follow the "Build an Alexa Trivia Skill in an Hour" tutorial. I was inspired to create a quiz about the GPIO connections - I've been tinkering with the Pi for a year or two and even so I'm constantly discovering new capabilities, and thought these would make good "did you know" type questions. I know my way around the GPIO pins fairly well now (I can usually find a ground pin without looking it up) but I thought a quiz would be a fun way to help memorise some more pins, and a good challenge for Pi gurus who already know it blindfolded.
Once I'd downloaded the skill examples from the Github repository it was straightforward to follow the tutorial steps - I used the "Reindeer Games" multiple choice quiz template in this case. The hardest part was coming up with enough questions! I wanted them to have a range of difficulties so that everyone would be able to get at least one right.
With the questions completed (and having learned a lot about reindeer in passing) it was time to submit the skill for certification, the process by which it would be approved by Amazon and made available to Echo owners. The certification process itself works well, I received detailed and personalised feedback via email the day after submission and after a couple of tweaks, some help from the developer forums and a bit of copying & pasting I resubmitted and was notified that the skill had been approved and was live - so exciting!
I hope to bring the AlexaPhone to an upcoming Raspberry Pi event to test the GPIO Challenge on the community - maybe even get my kids to keep a leaderboard of top scorers. Overall I found the skill creation process like many other things I've done with the Pi - seemingly impossible at first glance, but surprisingly accessible and straightforward with a little guidance and perseverance. Now that I know the ropes my next plan is to create a skill to look up resistor values by speaking the colours (or vice versa) - personally I'd find that incredibly useful!
If you have an Alexa-enabled device why not take the GPIO challenge for yourself? You can find it in the skill section of the Alexa app or the web app, just search for "GPIO Challenge".
Comments