Introduction: An Echo Device Using the C.H.I.P. $9 Computer

These are instructions for building a box enclosure from a set of old USB speakers that will implement an Amazon Alexa Echo device using the C.H.I.P. $9 computer from Next Thing Co. (http://www.getchip.com/).

So what's an "Echo" device? An Amazon Echo is a wireless voice command product sold by Amazon.com. An Echo device can be a device built by you that uses Amazon's Alexa service. The device would be capable of voice interaction, and providing news, weather, and other real time information. Using the service your device would be limited compared to Amazon's product.

Limited by the software you create to interface with the service and also limited by the licensing restrictions set by Amazon to use the service. For example this type of device does not support setting a timer, or playing music like the Amazon Echo product. Another license restriction is not allowing the use of keyword detection. This means that your device would need a button to submit a question for example. The Amazon Echo product costs $179.99 and the Amazon Dot, a similar product, costs $89.99 at Amazon.com.

NOTE: this DIY device can also be developed using a Raspberry Pi as well but is not the focus of these instructions. For more info on how to do this with a Raspberry Pi see: https://github.com/amzn/alexa-avs-raspberry-pi,

I am a big fan of C.H.I.P. because it packs a lot of functionality in a small footprint, and since I just got mine in February I decided to use it on this project. I have also developed a home control application on Raspberry Pi using an iPhone, so the Raspberry Pi is a very similar platform.

Anyway..... I was tinkering around and got this working from Sam Machin’s “The $10 Echo” site (http://sammachin.com/the-10-echo/). I got it working with a USB thumb microphone and I had a set of old USB speakers lying around that I could not get rid of, so I decided to use them to create a box to contain “Alexa”.

I like reusing old equipment and gutting things like this rather than creating a new enclosure. My "Making" and handcrafting skills are not the best... In these instructions I will not go into too much detail on the specifics of the hardware modifications to the speakers I used, since you may choose to use other speakers or a completely different enclosure. I will just document my steps as an example and hopefully that will motivate you to create an even better enclosure and/or device. I will provide the configuration information, adding to Sam Machin's efforts, and recommendations from those on the C.H.I.P. forum site that were helpful to me.

Difficulty: Easy to medium software build, and medium hardware build effort with some soldering and disassembly & assembly

Limitations / Options / Alternative Approaches:

  • Amazon does not support using their service with keyword detection: This means implementing it on your own device with keyword detection is against Amazon’s service agreement. Therefore a push button is used to send commands and invoke the Alexa service. (Turns out I like this approach better anyway).
  • Microphone: I used the USB thumb mic because it was the simplest for me. Its low power and self contained requiring no amplification circuit or wiring or other additions.
  • Enclosure: I used the chosen USB speakers because I had them lying around, no extra costs for me. But also because they have built in audio amplification, power and volume adjust that I did not have to come up with. Alternatives: a more fancy box, your own amplification or fixed volume control or other enhancements.
  • Board modifications for C.H.I.P. not required: In the original article, Sam used a USB audio and mic combination device that also required him to cut a trace on the back of the C.H.I.P. board to enable audio input through the TRRS 3.5mm jack. I preferred not to do this and therefore, for my prototype, I used the TRRS 3.5mm jack for output only and a USB mic numb for voice input. For details on board modifications see README.MD on Sam’s github: http://tinyurl.com/jxy2xpu. I wanted to be able to re-use my C.H.I.P. at a later time and did not want to modify it. NOTE: later on you will see that for my final build I used the C.H.I.P. header connections U14 pins 4,6 & 8 for audio out rather than the TRRS jack because of space constraints in the enclosure.
  • Python code: ...and of course further optimization of the code and my steps for implementing the code could be developed and I welcome any comments.

Potential issues: software latency and wireless communications issues with the Alexa services and finding the right enclosure can be issues for you. There are frequent errors that can occur from communications with the Amazon service for multiple reasons. I tried to compensate for it by adding error checking that forces a restart of the code but they did not resolve these much. I am sure there may be further software optimization that can be done to improve on these issues. I think these happened when I got too impatient with it and was looking for faster responses... It can take 3 seconds or more for a response to a question. Be patient. Your mileage may vary on the 3 seconds depending on how fast your wireless & internet connections are.

NOTE: I suggest that you prototype (Steps 1 through Step 5) before investing a lot of time on a making or modifying a final enclosure, to better understand these issues and reconcile them with your expectations.

Materials list:

  • C.H.I.P $9 computer from Next Thing Co. @ http://docs.getchip.com/ ($9 + shipping = $15.47. They are not readily available as of this writing. You can preorder on the website for June 2016 delivery. )
  • Old set of USB speakers that will provide room for mounting C.H.I.P. and wiring, (~$10 or so on ebay)
  • USB Thumb Microphone: @Amazon: http://tinyurl.com/hoowvmn (~$2.55)
  • Momentary SPST Push Button Switch: @Amazon: http://tinyurl.com/hoowvmn (~$2.34)

Step 1: Setup & Pre-configure C.H.I.P.:

NOTE: depending on your C.H.I.P. build and any work you may have already done on it, there may be items on the list below, that you DO NOT need to include. For example: if you have already installed one of the software components such as "git".

    For an Initial setup of C.H.I.P.: See a great post on “Setting up C.H.I.P. as a headless server with minimal tools”: http://tinyurl.com/zw42n4q. Once that is done...

    I suggest you always upgrade C.H.I.P. as the first step:

    sudo apt-get update

    Install git:

    sudo apt-get install git

    Install some other software...:

    sudo apt-get install libasound2-dev memcached python-pip
    
    sudo apt-get install mpg321

    Get Sam's python code and set it up:

    sudo git clone  https://github.com/sammachin/AlexaCHIP

    cd AlexaCHIP

    sudo pip install -r requirements.txt

    And in case these are not installed yet:

    apt-get install --upgrade python
    apt-get install python-alsaaudio
    apt-get install python-pyalsa

    Step 2: Setup Alexa Service

    Use the following video: "Installing Alexa Voice Service to Raspberry Pi" (http://tinyurl.com/zzxywbt) to setup the Amazon Alexa device service. This setup allows you to use the Amazon Alexa service on the device and provides you a device ID, security codes and permission via your Amazon account to use the service on your device.

    This step involves going to "https://developer.amazon.com" per the video above to setup the service. I would recommend reviewing the whole video but the interesting parts to setup the service starts at 3:37 minutes where the actual service is setup.

    You will end up with

    • a Product ID, mine was: "EchoCHIP" - you can name it whatever you like,
    • a Security Profile Description "Echo CHIP" - I kept it short but you can give a longer description,
    • a Client ID (long id string provided by Amazon) ,
    • a Client Secret (long string provided by Amazon).

    A "refresh token" will be need to be retrieved in our next step to complete the Alexa Service config.

    Copy and maintain a record of all these items (although you can always also go back to the Amazon site to get these).

    Step 3: Update C.H.I.P. With Alexa & Python3 Settings:

    Using the “The $10 Echo” setup from http://sammachin.com/the-10-echo/ and the README.md file from his github files at: http://sammachin.com/the-10-echo/ do the following steps:

      Create the Alexa service credentials file and authorization files:

      1. Make a copy of example_creds.py to work with:
      cp example_creds.py creds.py

      Edit the creds.py to add the Alexa service device ID, security codes,etc and use your previous recorded details for the creds.py file:

      nano creds.py  

      Run the authorization request from the device :

       sudo python ./auth_web.py   
      

      On your computer go to your web browser using your C.H.I.P. local ip address: http://192.168.###.###:5000 and then you can get the refresh token from Amazon to add to cred.py. Copy this very long token into the cred.py.

      Edit the cred.py file and now add the "refresh token" you got from the Amazon site on your browser. This authenticates your device with your Amazon account:

      nano creds.py


      Ready to Test

      Install a momentary push button switch across: GPIO1 and GND (pins 14 and 1 on U14). See C.H.I.P. connector pinouts: http://docs.getchip.com/#pin-headers. NOTE: I suggest doing this for test and prototyping purposes now without final mounting it yet.

      At the C.H.I.P. :

      sudo apt-get install python3-pip     
      
      sudo pip3 install python3-memcached
      
      sudo pip3 install pyalsaaudio

      For the USB microphone setup: edit your /usr/share/alsa/alsa.conf file so that "pcm.front cards.pcm.front" is renamed "pcm.front cards.pcm.default" (for correct audio device selection and identification)

      nano /usr/share/alsa/alsa.conf

      For USB microphone setup: edit /etc/asound.conf and add the following code. This initializes the microphone and audio settings:

      pcm.usb {
          format S16_LE
          rate 44100
          type hw
          card 1
          device 0
      }
      pcm.!default {
          type asym
          playback.pcm {
            type plug
            slave.pcm "hw:0,0"
          }
          capture.pcm {
             type plug
             slave.pcm "usb"
          }
      }

      "reboot your C.H.I.P."

      sudo reboot

      At this point in this process, I wanted to verify and test the use of the USB mic and the audio output before running the Python code. With the microphone installed and a speaker plugged in I manually tested this by using...

      arecord -D plughw:1,0 -f cd test.wav

      ...to record some audio and...

      aplay test.wav

      ...to play that recording. This allowed me to make sure before running the Python code that input and record of audio with my chosen microphone and audio output were working. You may need to use "amixer" and/or "alsamixer" commands at the command lines to adjust mic and audio output settings. See the links below for more information.

      Here are some references to getting audio and recording working. Some of these are Raspberry Pi articles and some are general Linux articles but they provide some guidance on audio architectures:

      1. Raspberry Pi microphone setup with USB sound card | Linux Circle: http://www.linuxcircle.com/2013/05/08/raspberry-pi-microphone-setup-with-usb-sound-card/

      2. Using a USB Audio Device With a Raspberry Pi - Envato Tuts+ Computer Skills Article: http://computers.tutsplus.com/articles/using-a-usb-audio-device-with-a-raspberry-pi--mac-55876
      1. Advanced Linux Sound Architecture - ArchWiki: https://wiki.archlinux.org/index.php/Advanced_Linux_Sound_Architecture

      Step 4: Update to New Python Code & Try It Out

      IMPORTANT Update: replace the "main.py" in the AlexaCHIP folder with the following update from github: http://tinyurl.com/zrjaldy (Thanks to Justine Thomas). This update contains updates to the code to support the use of the USB Thumb Microphone. It uses "mplayer" to code to convert recordings to be compatible with the Alexa service. Unfortunately this adds some delay to the communication with the service. (A possible opportunity for optimization in the future).

      Let's now test the code. First let's enable the GPIO pins by running the setup script to enable pushbutton switch::

      sudo ./setup _gpio.sh

      At this point you should be able to test your configuration to communicate with the Alexa service. Run the python code:

      sudo python3 main.py

      Alexa should respond after a few seconds by saying "Hello". At this point you are ready to ask your first question:

      Press the pushbutton switch and keep it pressed while asking your question. For example:

      "Alexa, who are you?"

      After at least 3 seconds Alexa will then respond with a short explanation of who she is. Other related questions you can ask are:

      "When is your birthday?" or "Who made you?" or "Where were you born"

      Alexa will respond to each of these differently. Notice you did not need to use the name "Alexa" in front of these questions while hold down the pushbutton switch. This is because the switch replaces the "keyword detection" mechanism mentioned previously.

      Here are some other questions you can test:

        What time is it? or What day is it? 
        
        What is the weather? or What's new? or Who is JJ Watt? 

        If you setup Alexa with location settings it can respond with weather from your location. To do this you can setup the Alexa service settings using an iPhone app (Alexa app) or an Android app (Alexa app) or at this address: http://alexa.amazon.com. Here you can customize Alexa (i.e. setup your zip code & location etc).

        Now, it's just a matter of putting it all together in the hardware. But first let's button up the software automation a bit before that....

        Step 5: Automate the Software & Bootup

        In order to get the python code to run automatically you will need to set it up to run at bootup. I used a script in /etc/init.d to accomplish this.

        The following script allows you to stop, start and restart the Alexa service python application. See the script contents for the syntax for doing this.

        NOTE: using this script the Python code will start automatically when you apply power to the box and Alexa will be accessible without having to start it in any other way.

        Below is the code that should be copied to /etc/init.d as /etc/init.d/alexa-service.sh,

        #!/bin/sh 
        ### BEGIN INIT INFO
        # Provides:          alexa-service
        # Required-Start:    $remote_fs $syslog
        # Required-Stop:     $remote_fs $syslog
        # Default-Start:     2 3 4 5
        # Default-Stop:      0 1 6
        # Short-Description: Simple script to start a Alexa Service program at boot
        # Description:       A simple script which will start / stop a program a boot / shutdown.
        ### END INIT INFO
        #
        workdir=/home/chip/AlexaCHIP
        progname="/home/chip/AlexaCHIP/setup_alexa.sh"
        start() {
            echo "Starting service..."
            cd $workdir
            /home/chip/AlexaCHIP/setup_gpio.sh
            $progname
        }
        stop() {
            pid=`ps -ef | grep '[p]ython3 main.py' | awk '{ print $2 }'`
            echo $pid
            kill $pid
            sleep 2
            echo "Server killed."
        }
        case "$1" in
          start)
            start
            ;;
          stop)
            stop   
            ;;
          restart)
            stop
            start
            ;;
          *)
            echo "Usage: /etc/init.d/alexa-service {start|stop|restart}"
            exit 1
            ;;
        esac
        exit 0 

        After you copy the above script to /etc/init.d as /etc/init.d/alexa-service.sh, do the following:

          Make the script executable:

          sudo chmod 755/etc/init.d/alexa-service.sh

          Register script to be run at start-up:

          sudo update-rc.d alexa-service.sh defaults

          If you ever want to remove the script from start-up, run the following command: "sudo update-rc.d -f alexa-service.sh remove"

          The setup script: "setup_alexa.sh" script, used and refrerenced in the above startup script, is what I used to keep the python code running continuously. I first created it to invoke the "python3 main.py" command to troubleshoot the code. It helped me manage what I wanted to execute and do some troubleshooting. When I did not use it, there were too many times where there were errors that halted the code with no response from Alexa, even though it would be working correctly when it did respond. These errors tended to be communications or software latency issues I suspect, so the "setup_alexa.sh" script allowed me to keep it running. THis is not the best solution but it works, and keeps the service running for me. It just re-executes the code again if there was an error.

          Below is the contents of the "setup_alexa.sh" script:

          #!/bin/bash 
          PROGNAME='sudo python3 main.py'
          
          cd /home/chip/AlexaCHIP
          # Run the code until CTRL-C 
          while [ $? -ne 130 ]; do
              $PROGNAME
              sleep 5
          done
          exit $?

          Step 6: Modifying the Hardware

          Disassembly of the speaker box and modifications depend on the speaker or enclosure you decide to use. I used a set of HP Multimedia Speakers, Model no. UC236. These are USB powered speakers with a standard audio plug for audio input. For my purposes I eliminated the second speaker and kept the control speaker that had power, volume and tone controls. I also removed the audio plug since I intended on using it internally.

          The speaker board used in the control speaker has a power circuit to turn power to the speakers on/off and also has volume & tone controls.

          The control speaker PCB board had solder pads (connections for the audio plug which I removed). These I used to for input from the C.H.I.P. board so that you can hear Alexa's response.

          The control speaker enclosure also had to be modified to mount a pushbutton switch for activation of the Alexa service.

          Concerns: while the C.H.I.P computer does not usually get hot I realized after completing this that the control speaker enclosure does NOT have vents or ventilation of any kind. Before running it for too long I intend on making some vent holes in the enclosure to ensure it has some ventilation for the C.H.I.P.

          Step 7: Wiring the Power to the C.H.I.P. and the Speaker Board

          I did not want to take the chance of damaging my C.H.I.P. with power from the speaker PCB since I did not have a schematic and was not sure that switching the speaker board on / off would spike the C.H.I.P input.

          I also was not sure it drove enough current to power the C.H.I.P. So I wired power for the C.H.I.P. directly from the USB input power connection.

          So I have to live with the fact that as soon as I plug in the USB power connector the C.H.I.P would power on rather than using the control speaker power switch.

          The board has PCB pads for USB power input which I used to wire to the the C.H.I.P. board power pins on U13 header pins 1 (GND) & 3 (VCC-5V). These connections are not switched on by control speaker and therefore not susceptible to electrical noise from the power switch. Also, not having a schematic or knowing too much about this board I could not count on it providing enough power for the C.H.I.P.

          At later time I will look at this and see how I can improve on this.

          Step 8: Wiring the Audio Connections From the Speaker Board to the C.H.I.P.

          The speaker board had connections for audio input (from the audio plug I removed) and therefore I used these inputs to wire output audio from the C.H.I.P. You may ask why I did not use the TRRS 3.5mm jack. Using the jack would take up too much space and not allowed me to fit the C.H.I.P. board in the speaker enclosure.

          The USB Thumb Microphone: I used a USB Thumb Microphone (see Amazon: http://tinyurl.com/hoowvmn). This provided a better solution for me since it is low power, and does not require any amplification or other wiring. I also wanted to be able to re-use it, like the C.H.I.P. board, in case one day I wanted to disassemble this project and reuse the materials.

          Therefore I decided to cut a hole in the speaker box with my Dremel tool for the USB mic (see picture below). This allowed me to take it out and put it back in, in case I needed to use it with my Raspberry Pi units or other uses. Cutting this hole also worked out well since it allowed for more room in the speaker enclosure.

          Step 9: Wiring the Momentary Pushbutton Switch

          As mentioned above the pushbutton switch is used to initiate Alexa service and ask it questions. I mounted a large red switch (see Amazon: http://tinyurl.com/zr3ph8b) to the front panel and wired one side of the switch to U14 header connector pin 1 (GND) and the other to U14 header connector pin 14 (XIO-P1).

          Mounting the switch required me to drill out a hole for it with my Dremel tool to the front of the control speaker.

          Step 10: Mounting the C.H.I.P. Board Inside the Speaker Enclosure

          I wanted to be able to possibly reuse the C.H.I.P. board again. They are not readily available as of this writing. But you can preorder on the website for June 2016 delivery so I am waiting for my 2nd one.

          So I may reuse this one until I get another one. Therefore I decide to use velcro (handy man secret weapon #3, besides duck tape #1 and WD-40 #2). So I applied velcro to the back of the C.H.I.P. board and to the top of the inside of the speaker enclosure.

          NOTE: this is an important point, I had enough clearance for the board to fit behind the speaker and toward the top of the enclosure. I got lucky, since it can be hard to find room in most speaker boxes.

          I also plugged in the USB thumb mic.

          Step 11: Test and Reassemble the Whole Thing

          I then tested the unit with power without closing up the speakers to make sure everything was working correctly. Afterwards I closed it up and tested it again.

          As I mentioned above, while the C.H.I.P computer does not usually get hot I realized after completing this that the control speaker enclosure does NOT have vents or ventilation of any kind. Before running it for too long I intend on making some vent holes in the enclosure to ensure it has some ventilation for the C.H.I.P.

          Code Sources & Information References

          • Video on Alexa service setup: Use the following video on Installing Alexa Voice Service to Raspberry Pi and it is the same for C.H.I.P.: http://tinyurl.com/zzxywbt
          • Below are some questions you can try with the device:
          • Alexa, What time is it?
          • What day is it?
          • What's the weather? (make sure and set your location in Alexa app or @ http://alexa.amazon.com )
          • What's new?
          • What is 2 + 2?
          • How many days until September 1?
          • What are some movies playing nearby?
          • What movie won best picture in 1991?
          • Who are you?
          • Where do you live?
          • Who made you?
          • When is your birthday?
          • Where were you born?
          • How many cups are in a quart?
          • How many tablespoons are in a stick of butter?
          • Do aliens exist?
          • What is the answer to life, the universe, and everything?
          • What are you going to do today?
          • Tell me a joke.
          • Meow
          • Do you know Siri?
          • Can you rap?
          • Can you beatbox?
          • Who is the real slim shady?
          • Beam me up
          • Close the Pod Bay Doors
          • Tea. Earl Grey. Hot
          • What are the Laws of Robotics
          • What are the rules of fight club?
          • Do you have Prince Albert in a can?
          • Surely you can't be serious
          • Who's on first
          • To be or not to be...
          • Who is the fairest of them all? (ask this one twice)
          • What came first, the chicken or the egg?
          • How much wood would a wood chuck chuck if a wood chuck could chuck wood?\
          • How many licks does it take to get to the center of a tootsie pop?
          • Who loves ya baby?
          • What is love?.
          • How much is that doggie in the window?

          • Who let the dogs out?
          • Play a song. - NOT SUPPORTED FOR THIS DEVICE
          • Set a timer for 10 minutes. - NOT SUPPORTED FOR THIS DEVICE

          Although there are some differences & limitations to this device compared to the Amazon products, this link may be helpful also: http://blog.anythings.co/ultimate-guide-amazon-echo-alexa/

          I hope that making this will be as much fun for you as it has been for me. I take very little credit for originating any of this code or process. I take credit for compiling it all and for being too verbose here.... :-)

          Now go find a great way to build your own.

          Robotics Contest 2016

          Participated in the
          Robotics Contest 2016