PocketBeagle Alexa

PocketBeagle® can become your personal Amazon Alexa assistant. Follow these instructions and this tiny BeagleBone will do more than fetch!

PocketBeagle Alexa

Categories: Intermediate

The Goal

The goal of this project is to convert a Bluetooth speaker into an Amazon Alexa enabled smart speaker using
BeagleBoard.org®PocketBeagle
®. The project was inspired by and most of the code was borrowed from
Franklin Cooper’s
Hackster project:
BeagleAlexa
. This project will serve as more of a step by step guide to turning your Bluetooth speaker into a smart speaker.
PocketBeagle®
will receive spoken commands and perform ‘fetches’ from the internet and respond. We’ve added a set of LED lights as a notification strobe to give visual feedback to the user.
For more background on the project and the software used read our blog.

Things you need

Here is a list of hardware you will need to build a smart speaker

  • SD card (8GB or higher) to have the Linux image on
  • USB to Micro-USB cable
  • Some green through hole LEDs
  • 1K through hole resistors
  • Through-hole prototyping pcb
  • Jumper wires
  • Small plastic case to enclose electronics

*Many Bluetooth speakers come with built-in microphones. But, using the same device for playing and recording sound would require using
HSP
(Head-set Profile)/
HFP
(Hands-free Profile) Bluetooth modes, which have known to be unreliable and produce poor quality audio. Using separate devices is a fun way to quickly get started (we found this cool microphone that reminded us of BB-8), however, please look for updates to this project later.

Step 1: Putting the hardware together

Time to get your hands dirty by soldering the USB A breakout board to PocketBeagle. The following figure shows a Fritzing diagram providing the connections between the breakout board and PocketBeagle®.

PocketBeagle connected to USB-A breakout - Fritzing Diagram

PocketBeagle with soldered headers

Notification LED strobe

USB-A add on board

Connect the following devices to the USB hub (shown in the figure)

  • USB power brick to power the USB hub
  • USB cable to connect to PocketBeagle
  • USB cable for Microphone input
  • Bluetooth/Wi-Fi USB dongle

Powered USB hub connected to various peripherals

Connecting the LED module to PocketBeagle

PocketBeagle connected to USB hub

Initial setup

Power the USB hub by plugging in the power adapter for the USB hub

Step 2: Make a bootable microSD card

Latest Linux images for PocketBeagle® are available at
https://beagleboard.org/latest-images

The process of programming the microSD card with a Linux image listed in the page above is described in
https://beagleboard.org/getting-started

Step 3: Booting the board

After making placing the Linux image onto the microSD card, insert it into the microSD card slot of the PocketBeagle® and power up the PocketBeagle® by connecting it to your computer’s USB port by using the Micro-USB to USB cable. Make sure the USB hub is already powered using the brick adapter that comes with the USB hub. Open a web browser on the computer that is powering the PocketBeagle® and enter the URL:
http://192.168.7.2:3000/ide.html
This should bring up the Cloud9 IDE that allows you to develop software for the PocketBeagle®

Step 4: Connect to the internet

Insert the Wi-Fi + Bluetooth dongle into the host USB port soldered onto PocketBeagle®

Check for wlan interface by running ifconfig:

debian@beaglebone:/var/lib/cloud9$ ifconfig

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536

       inet 127.0.0.1  netmask 255.0.0.0

       inet6 ::1  prefixlen 128  scopeid 0x10<host>

       loop  txqueuelen 1  (Local Loopback)

       RX packets 3200  bytes 232640 (227.1 KiB)

       RX errors 0  dropped 0  overruns 0  frame 0

       TX packets 3200  bytes 232640 (227.1 KiB)

       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

       inet 192.168.7.2  netmask 255.255.255.252  broadcast 192.168.7.3

       inet6 fe80::6264:5ff:fe42:2972  prefixlen 64  scopeid 0x20<link>

       ether 60:64:05:42:29:72  txqueuelen 1000  (Ethernet)

       RX packets 581  bytes 109529 (106.9 KiB)

       RX errors 0  dropped 0  overruns 0  frame 0

       TX packets 405  bytes 112570 (109.9 KiB)

       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

usb1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500

       inet 192.168.6.2  netmask 255.255.255.252  broadcast 192.168.6.3

       ether 60:64:05:42:29:75  txqueuelen 1000  (Ethernet)

       RX packets 0  bytes 0 (0.0 B)

       RX errors 0  dropped 0  overruns 0  frame 0

       TX packets 0  bytes 0 (0.0 B)

       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=-28669<UP,BROADCAST,MULTICAST,DYNAMIC>  mtu 1500

       ether 08:bd:43:8a:2f:60  txqueuelen 1000  (Ethernet)

       RX packets 0  bytes 0 (0.0 B)

       RX errors 0  dropped 0  overruns 0  frame 0

       TX packets 0  bytes 0 (0.0 B)

       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


You should see a wlan(wlan0) wireless interface in the list of interfaces. Now run the connmanctl utility to connect to Wi-Fi

debian@beaglebone:/var/lib/cloud9$ connmanctl

connmanctl> enable wifi

Error wifi: Already enabled

connmanctl> scan wifi

Scan completed for wifi

connmanctl> agent on

Agent registered

connmanctl> services

   SCSL                 wifi_08bd438a2f60_5343534c_managed_psk

   octavo_wifi          wifi_08bd438a2f60_6f637461766f5f77696669_managed_psk

   SBG6900AC-C1D23      wifi_08bd438a2f60_5342473639303041432d4331443233_managed_psk

   octavo_guest         wifi_08bd438a2f60_6f637461766f5f6775657374_managed_psk

                        wifi_08bd438a2f60_hidden_managed_none

                        wifi_08bd438a2f60_hidden_managed_psk

connmanctl> connect wifi_08bd438a2f60_6f637461766f5f77696669_managed_psk

Agent RequestInput wifi_08bd438a2f60_6f637461766f5f77696669_managed_psk

 Passphrase = [ Type=psk, Requirement=mandatory ]

Passphrase? OSD3358!

Connected wifi_08bd438a2f60_6f637461766f5f77696669_managed_psk

connmanctl> exit

debian@beaglebone:/var/lib/cloud9


Step 6: Install required packages

  • Increase size of Linux partition on microSD card
cd /opt/scripts/tools

sudo ./grow_partition.sh

sudo reboot


  • Update repositories of Linux
sudo apt-get update


  • Install required Linux packages
sudo apt-get install swig python-alsaaudio sox espeak libcurl4-openssl-dev libsox-fmt-mp3


  • Install python packages
sudo pip install cherrypy requests


Step 5: Modify asound.conf file for proper pcm reference

Edit usr/share/alsa/alsa.conf to include the following change:

  • Change defaults.ctl.card and defaults.pcm.card variables to 1
defaults.ctl.card 1

defaults.pcm.card 1


Reboot the board to let the changes go into effect

sudo reboot


Step 6: Connect to Bluetooth Speaker

Edit /etc/pulse/daemon.conf to make the following changes to the variables listed in the file. Remove the semicolons ahead of each of the lines to uncomment the lines, then modify as shown below:

  • high-priority = yes
  • exit-idle-time = 86400

Start pulseaudio daemon:

debian@beaglebone:/var/lib/cloud9$ pulseaudio --start


Use bluetoothctl utility to connect to Bluetooth speaker:

debian@beaglebone:/var/lib/cloud9$ bluetoothctl

[NEW] Controller 38:D2:69:DA:F0:39 beaglebone [default]

[bluetooth]# power on

Changing power on succeeded

[bluetooth]# agent on

Agent registered

[bluetooth]# default-agent

Default agent request successful

[bluetooth]# scan on

Discovery started

[CHG] Controller 38:D2:69:DA:F0:39 Discovering: yes

[NEW] Device 04:52:C7:A7:10:5F Bose Mini II SoundLink

[NEW] Device 43:88:48:B8:62:1A BS-01

[bluetooth]# pair 43:88:48:B8:62:1A

Attempting to pair with 43:88:48:B8:62:1A

[CHG] Device 43:88:48:B8:62:1A Connected: yes

[CHG] Device 43:88:48:B8:62:1A UUIDs: 00001101-0000-1000-8000-00805f9b34fb

[CHG] Device 43:88:48:B8:62:1A UUIDs: 0000110b-0000-1000-8000-00805f9b34fb

[CHG] Device 43:88:48:B8:62:1A UUIDs: 0000110e-0000-1000-8000-00805f9b34fb

[CHG] Device 43:88:48:B8:62:1A UUIDs: 0000111e-0000-1000-8000-00805f9b34fb

[CHG] Device 43:88:48:B8:62:1A ServicesResolved: yes

[CHG] Device 43:88:48:B8:62:1A Paired: yes

Pairing successful

[CHG] Device 43:88:48:B8:62:1A ServicesResolved: no

[CHG] Device 43:88:48:B8:62:1A Connected: no

[bluetooth]# connect 43:88:48:B8:62:1A

Attempting to connect to 43:88:48:B8:62:1A

[CHG] Device 04:52:C7:A7:10:5F RSSI: -51

[CHG] Device 43:88:48:B8:62:1A Connected: yes

Connection successful

[BS-01]# scan off

[BS-01]# exit


Verify by using aplay. Executing the following command should play the audio file Front_Center.wav

debian@beaglebone:/var/lib/cloud9$ aplay /usr/share/sounds/alsa/Front_Center.wav

Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono


Ste 7: Add path variables

Edit /root/.bashrc file to include the following lines:

export LD_LIBRARY_PATH=/usr/local/lib

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

export PYTHONPATH=/usr/local/lib/python2.7/site-packages/


This will allow Python and Sphinx to find the various libraries we will be manually compiling soon. make sure to reboot the board after this step.

Step 8: Cloning repositories and building required utilities

Clone various repositories needed to /root/ folder:

cd /root/

git clone https://github.com/cmusphinx/sphinxbase.git

git clone https://github.com/cmusphinx/pocketsphinx.git 

git clone https://github.com/neerajdantu/beagleman.git


Build Sphinxbase (This might take a while to build):

sudo apt-get install bison

cd /root/sphinxbase

./autogen.sh

make

make install


Build PocketSphinx:

cd /root/pocketsphinx/

./autogen.sh

make

make install


Step 9: Setup Amazon Voice Service and get tokens

PocketAlexa product description

PocketAlexa security profile

Gather the following information from the process:

  • Product ID
  • Security Profile Description
  • Security Profile ID
  • Client ID
  • Client Secret
  • Make sure the allowed origins and allowed return URLs are the same as the ones shown in the above image
  • Input credentials gathered in this step to
    creds.py
    file
cd /root/beagleman

cp example_creds.py creds.py


  • Update python variables for ProductID, Security_Profile_Description, Security_Profile_ID, Client_ID and Client_Secret with values generated here. The updated file will look like the following(with the security variables blacked out).
  • Execute the following command
python auth_web.py &


  • Input url: ‘
    http://192.168.7.2:5000
    ‘ into a web browser of the computer the PocketBeagle® is connected to. If the PocketBeagle® has a different IP address, replace the IP address in the URL with PocketBeagle’s IP address.
  • Login to the Amazon account created for Amazon Voice Service, this will generate a token that will be displayed on screen
  • Update beagleman.py with the mac address of the Bluetooth speaker being used in line 23.
# Start pulseaudio daemon

os.system('sudo -H -u debian pulseaudio --start')

os.system("echo 'connect 43:88:48:B8:62:1A' | bluetoothctl")


Step 10: Execute the program

As root, execute the script alexa_start.sh

cd /root/beagleman

./alexa_start.sh 


The following figure shows the output of the shell program. When the terminal prompts the user to issue commands by printing ‘Debug: Ready to receive request’, Alexa can be activated by the key word
‘dog’
.

Step 11: Auto-execute on start-up

Make a logs directory:

cd /root/beagleman

mkdir logs 


Edit crontab to start a cronjob:

sudo crontab -e 


Add the following line to crontab file:

@reboot sh /root/beagleman/alexa_start.sh > /root/beagleman/logs/cronlog 2>&1


Step 12: Finishing up

PocketBeagle connected to USB hub for power

Finish setup

Final setup

Step 13: Your PocketBeagle® Alexa is ready to take commands!

Now that you have everything set up, we will test PocketBeagle® Alexa’s connection to the internet by asking it to tell us the weather in our city, tell us a random fact and a few surprises. At time of this writing, there are over 30,000 skills in the Alexa library. Let us know what you are able to do with your
BeagleBoard.org®
PocketBeagle® Alexa by entering it in the comments below!

Comments are not currently available for this post.