Last
month I visited a tech friend of mine who has integrated Alexa and Philips Hue light
strips in his hobby room. He demonstrated to me how easy and convenient it is to
manage light colors using Amazon’s speech recognition Alexa. I was impressed
with the systems’s integration and the new lighting experience. So, I decided
to develop my own system to control lights using Raspberry PI.
As
far as I could understand, Amazon Echo works in this scenario through a bridge which
communicates with the Philips Hue system using ZigBee. ZigBee operates on the
IEEE 802.15.4 specification, which makes it possible to transmit data between
devices through the wireless network.
In
this blog post, I’ll demonstrate to you how to create a voice assistant using
Azure Bing Speech and Raspberry PI, which manages the Philips Hue light bulb.
IMPORTANT:
Always consider safety aspects when dealing with electricity!
To dos:
- Get your Raspberry PI ready
- Configure the Philips Hue light bulb
- Develop the logic to control the light bulb using the RaspBee API
- Create an Azure Bing Speech service
- Connect a microphone (Optional)
- Develop the logic to communicate with the Azure Bing Speech API
- Moment of truth (Run all components together)
Hardware/Components used:
- Raspberry PI 2
- RaspBee Premium
- Philips Hue White E27
- Mini Microphone
- Micro SD with 64 GB
Software used:
1. Get your Raspberry ready
A
Raspberry PI is a little computer which can be used in electronic projects and as a normal computer as well since it supports operating systems with
graphic interface. In this project I’ll work with an operating system image
called Raspbian Jessie RaspBee which has already been configured to work with
the RaspBee component. You can download the image from this website which also offers an installation guide. In the
paragraphs below, I added extra information which can help you during this
step.
After
downloading the image, you must restore the Raspbian Jessie RaspBee image into
a freshly formatted micro SD card. For this step I’d recommend the USB Image
Tool (http://www.alexpage.de/usb-image-tool/download/) which has a user friendly interface.
After
restoring the image and turning your Raspberry on, I’d suggest you update your operating system. Open the terminal and run the following commands:
- sudo apt-get update # Fetches the list of available updates
- sudo apt-get upgrade # Installs the newest versions of all packages
Now that you’ve finished the installation guide for the operating system, it’s time to
connect the RaspBee module to the Raspberry PI. This module brings the Zigbee connectivity
to your Raspberry PI allowing direct communication between the Raspberry PI and
the Philips Hue light bulb.
To connect
the RaspBee module to Raspberry PI, make sure your Raspberry PI is not powered
and plug your RaspBee module into the first 12 GPIO pins. Here is what it looks
like:
Your
Raspberry PI is now ready!
2. Configure the Philips Hue light bulb
The
Philips Hue ecosystem includes a line of smart lamps which use the Zigbee
protocol to communicate. In this project I’ll use the Philips Hue White E27
light bulb. Here is how it looks:
Getting
started with the Philips Hue is really easy since you just need to screw the
bulb into a matching light socket considering safety aspects.
After
the light bulb is installed, you can turn on your configured Raspberry PI and
set up the system. The Raspbian Jessie RaspBee operating system automatically
starts the RaspBee tool which you can use to manage the devices connected to it.
In order to find your Philips Hue light bulb, connect the Rapsberry PI to the
internet and run the discover feature (www.dresden-elektronik.de/discover) in your browser. You’ll be asked to select
your gateway and to enter the login credentials. You find the login credentials
in the RaspBee’s quick start guide.
On
the Wireless Light home page click menu, then settings to navigate to the
settings page. Once on the settings page, find and click Open Network button to
allow Zigbee devices to join the network, then find and click the Scan for
Devices button to find your Philips Hue light bulb. Here is what it looks like:
By clicking
the “blink” button you can test the connectivity.
3. Develop the logic to control the light bulb using the RaspBee REST API
The
python code below demonstrates all steps necessary to control the light bulb
using the RaspBee REST API. In order to have authorized access to the REST API,
you need an API key which can be acquired through a POST request to /api. The
next API request changes the light bulb’s state to on.
The last API request deletes the created API key
since it doesn’t expire and I want to keep things organized. That’s it! I
added a couple of comments to the code, so it is easier to understand.
4. Create an Azure Bing Speech service
I
want to manage the light bulb state using real-time speech recognition turning
my Raspberry PI into a voice assistant. So, I chose the Azure Bing Speech
service for a reliable and accurate speech recognition. This step is optional
since you can also use an offline speech recognition system explained in
step 6.
Creating
a Bing Speech service is very simple. First off, navigate to the Azure Portal (https://portal.azure.com). After logging in, click new then search for Bing Speech API and request a new Speech API
service. After filling in the required fields, your form should look similar to
this:
When
the creation process has finished, find the Speech API keys since they’re
necessary for authentication with the Bing Speech service. Here you can find
them:
5. Connect a microphone (Optional)
You
can run the python code from different kinds of devices. For instance, if you
already have a laptop with an integrated microphone, then you could use it to run
the program above and manage the Philips Hue light bulb without configuring an
extra system.
I
want my Raspberry to work as a voice assistant, so I’ll demonstrate to you how to
get a microphone working with the Raspberry. Mini Microphone is a plug and play
device. You only need to connect it to your Raspberry and change the audio device
settings to get it working properly. The pictures below show what my audio
preferences look like. Since the audio input quality was low, I adjusted the
gain of the microphone to very high:
That’s
it! You can run the code below in the terminal to test if the microphone was
set up correctly. It records the audio for three seconds, then it plays it.
arecord
-D plughw:1,0 -d 3.0 AudioTest.wav && aplay AudioTest.wav
6. Develop the logic to communicate with the Azure Bing Speech API
In
order to communicate with the Azure Bing Speech API, I decided to use the
friendly python library SpeechRecognition since it offers you all the necessary
interfaces to communicate with the different kinds of speech recognition
systems.
Here
how to install the SpeechRecognition library and its dependencies:
- pip install pyaudio
- pip install pocketsphinx (Optional – offline speech recognition system)
- pip install SpeechRecognition
I used
the code below to test the Bing Speech API, the PocketSphinx and the SpeechRecognition
library. I added a couple of comments to the code, so it is easier to
understand.
7. Moment of truth (Run all components together)
Finally!
It’s time to try all the components together. I developed the python code below
which turns your Raspberry into a voice assistant to control the Philips Hue
light bulb. The code consists of the code snippets above and a loop which
repeats the process of audio input, voice recognition and text interpretation.
- “Turn the light on”
- “Turn the light off”
- “Exit”: Stop running the program!
The
picture below shows my Raspberry with all components and the Philips Hue light
bulb.
Summary:
I’m
very satisfied with the project’s result because it helped me to take a look
under the hood of a voice assistant device. As you can see, voice assistant
devices’ basic usage isn’t that complicated.
I
found the SpeechRecognition library which gives support to different voice
recognition systems such as PocketSphinx, another very interesting project
which supports offline voice recognition. I’m also very satisfied with the accuracy
and quality of the Azure Bing Speech API. I became a big fan of RaspBee
which provides easy communication with the Philips Hue.
Playing
around with all the components was once again very fun! I hope you enjoyed it also J
Links:
- RaspBee