Voice Controlled Smart Home with Amazon Alexa Skill and iOS App
When you want to buy your house, you want it match perfectly your requirements right ? I think the same for home automation !
Did you never ever dream about controlling your house with your voice ?
As a science-fiction movies addict and in addition an IT Guy , that’s something that’s thrilling me ! .
Voice recognition is not something new, a lot of scientists around the world from the late 1960s as Fred Jelinek from IBM have researched and created multiples speech recognition software. Concept was more focus on helping people to speech to text rather than speech to action, that’s a crucial point .
Speech to text is something not yet fully working as we wish it could be, our expectations are high. I’m sure that everyone had tried to make text by speaking via Google or Siri but depending on noise around, your accent and multiples other factors if you don’t verify before sending you could get a lot of troubles :). So that’s far better now, but not yet 100 % efficiency and it will be harder to get there . Therefore in order to understand patterns (list of word) we don’t that expectations as we are not writing perfect sentences, we just need to understand which pattern it’s for ! That’s how home automation by voice are now so famous and works quite well.
With all the work that scientists have done during the past years with algorithms, language theories etc, led us now use it with a confident proficiency .
OK, so now how ?
Well depending of your goals, your needs, the time you want to allocate and the money you want to spend on, there is many providers below for each kind of home devices, with their own technologies, or their own apps, protocols etc.. you can be stuck with what they can’t do or even with what they can “just” do.
- Lightbulbs: Philips Hue, Lifx, Stack, GE Link, Lumen
- Smartplugs & Lamp Dimmers: Belkin WeMo, Zuli, iHome
- Door Locks: August Smart Lock, Schlage Sense, Yale
- Doorbells: Ring, Skybell, August Doorbell
- Thermostats/HVAC: Nest, Ecobee, Honeywell Lyric, Nuheat Signature
All are extremely good regarding quality, design and for some of them integration with voice devices (Home Kit, Amazon Echo). The only point I found is that you are limited by what they can do, and more other combined actions that relies each smart device together is very hard.
For example in order to light on and switch on TV you need to make two voice actions. Both cannot be linked if you wish too. OK I know sometimes you just want the light, but imagine it. Then in order to have some metrics about your specific home feature you need to have an app from each device provider.
Hence why my personal goal was to build myself a DIY Light On/Off system which I can switch off light just by saying “Alexa tell smart house Good Night”.
A simple “crazy” example would be that after turning on the light the heating goes on and your TV switch on on your news channel.. and the opposite for switching off the light.
Least but not last the big advantage of doing this yourself, is that you can handle statistics and data for your own collection purpose.
Regarding price, it can be costly depending who you are and what yours expectations are !.
So it’s time to go on the technical side now,
In order to achieve that I used theses hardware below :
- An Amazon Alexa Echo which will be my Voice hub command.
- An Arduino with an Ethernet Shield (you can use wifi shield as well if you want).
- An Radio Frequency Emitter
- A radio frequency power outlet plugin (Energenie : https://images-na.ssl-images-amazon.com/images/I/61ulhzHUDYL._SL1100_.jpg).
If you have good electronic skill sets the radio frequency emitter and the radio frequency power outlet plugin can be replaced by a simple relay switch. But I don’t like playing with real 230 V power !.
Then on the development side, I created :
- A Lambda function on AWS with NodeJs 4.3.
- An iOS Client .
- A website client in PHP with an MySQL Database for handle statistics.
- An Arduino script that handle MQTT Connection and communication with I/O Device.
Before digging inside, let’s take a look at the big picture below
So after asking Alexa what you want to do, it will trigger regarding your question the appropriate lambda function. Then will publish through an MQTT Broker a specific topic and a value.
Subscribers are the Arduino and the iOS Client (They can be as well publisher).
Then we have my website with handle statistics and build graphs after executions gone.
I made the choice to use a lambda function rather than a self hosted solution because the reliability and the integration is far better. I don’t want to be in the situation that I ask the light on and nothing happen because of server down, I will be to lazy to do it at that time !.
I’m sure you know what is a Lambda function already, but to make it short, It’s is an event-driven computing platform provided by Amazon as a part of the Amazon Web Services. Lambda functions are a self-contained functions that runs in isolation. So it’s not an classic API Hosting as they don’t share libraries or classes.
The trigger feature via Alexa is called an Alexa Skill. Amazon provide you a all Skill set SDK for playing musics, speeches and such more. Currently you can write a lambda function in Java, NodeJs and Python . I have chosen to use NodeJs as this project has to be a learning part also and I knew already both of the others.
To deploy your Alexa Skill there is many documentations provider by Amazon itself here , and you can find videos as well on YouTube .
To make it easily to understand , Alexa Skill works with a main code file, and two others files which are an Intent Scheme json file base and an utterance text file. The first one is a list of all your intents you want to implement in your skill, and the utterance is the patterns I explained you above that will trigger the skill regarding words inside.
here we have the IntentScheme
and here the utterances file
So now let’s see the Alexa Skill
Alexa Skill :
As you can see the part which handle your intent execution is Skill.prototype.intentHandler. All the other methods are use to log data in Cloudwatch, and make speech when the skill start or end.
The more important part here is the use of MQTT (MQ Telemetry Transport) inside the skill.
MQTT is a simple pub-sub messaging system which allows client’s existence independent of any other device to have a bidirectional communication channel through topics.
MQTT now become one of the highest standard for communicate with small devices and is now standardized under the wings of OASIS.
Main difference with HTTP are that MQTT is :
- data agnostic.
- It’s battery efficient and made for non stable bandwidth so very lightweight.
- Let’s chatter than HTTP. No need to handle response code and headers.
- In my opinion it will be also the future of mobile app communication because it’s can do the same as web sockets with a less chatter protocol and more other no need of an HTTP Server which can be cost effective for smart house if there is no need of it .
- Supporting one to one,one to many,one to zero distribution models whereas HTTP Layer is only a point to point connection.
- Support 3 QoS levels. So no need to put a huge logic to ensure message delivery, it’s safe.
So regarding theirs characteristics and where they might got used for, smart devices cannot work based on the old request / response model on many scenarios .
The main reason is that data are so important and we can’t afford data loss because of bandwidth issues (weather metric measurement on the top of 2G Network coverage house need to work as perfectly as your smartphone in 4G).
So that’s why Facebook messenger use it ! :).
Now move on to the embedded hardware side.
I used an Arduino rather than a Raspberry Pi, this choice was made in consideration of two parameters :
- No overkill.
Arduino is very efficient when we deal with I/O. It’s not good when we want to deal with process. the Arduino can do only one thing in a loop, for this matter it’s fine but if you intent to make multitasking features, you should go for a raspberry Pi. That’s where Raspberry Pi can be useful as it’s behave as a small computer with a lot of Ram , an OS , an UI etc..
In my case it will just connect to the MQTT Broker, subscribe to topics and wait until something happen !
So even if the raspberry pi can do what the arduino does , it’s less simpler and a bit overkill for this project.
Arduino scheme :
So we have here the 433 Mhz Emitter plugged on the Pin number 12 .
You might wondering why I need a radio frequency emitter ?
Well I bought radio frequency managed power outlet . So I have a radio frequency remote control with old buttons and battery which power on or off plugins ! I don’t want to use them because I want Alexa do it for me ! Then I need to replace it. In this case it’s more replicate it with an arduino based Radio Frequency Remote :).
You might not seeing it on that scheme but there is an Ethernet Board Extension that you need to plug in the Arduino board to get internet connection as below if you use ethernet.
Let’s see the source code that run inside the arduino :
As you can see we have two main functions here, the setup and the loop. Setup is use to make hardware and libraries initialization, the loop is your actual logic. Here the loop just make sure that client is still connected and we have a callback function declared that handle MQTT events.
Then now we are ready to make it run ! Let’s make a demo !
The last part of this project is more for fun and to fill a specific need ! . Where you are too far from Alexa or there is noise around it or you just don’t wanna talk !
iOS Companion App
I will just present the MQTT Part here, as the views and behaviors is not the matter here. I have used the very good pod library MQTTClient .
So the only thing you need to do, is declaring a client, connect and subscribe. Then you have some delegates to handle which topic is sent and what to do with values.
The connection and subscription part :
Then with a simple button with a small logic we just need to publish as Alexa skill do :
Then ! Here we are !
Now if we step back a little bit, we see that make your own home automation is not as hard as we can think before having your hand dirty !.
Of course my arduino device is not as shiny as Nest or big brand, sometimes connections could fail and there is a lot to do to make it a lot more stable (having retry policy, etc..) ! But the pleasure to make whatever you want to do in your house is just the best !.
More other all costs are reduced if we compare to major brand solutions. Here if you remove the cost of Amazon Alexa (149 £), you can expect 50 £ maximum for this, including the Arduino and the RF Power plugins ! . And if you are a crack on electronics you can go a lot more lower…. I’m sure without verify it that is less that Philips Light … and it’s doesn’t require you to change you home equipment so you can keep your old lamp and your old Lightbulbs !
Nowadays, you might have seen it but Mark Zuckerberg has created his own AI named Jarvis. It’s just a prototype but still !.
With all AI released this year as Jarvis, Google Home, Amazon echo, more ideas will comes and it’s time for you to choose the one you wanna use and build your own smart home !
Thank you for reading this story and hope this article has pleased you ! If you have any question further don’t hesitate !
You can find the Alexa Skill on my Github .