Home Automation using Alexa- Part 2
Hi again,
In the previous part of this article series, we created custom Alexa Skills for Echo using different methods.
If you have reached here, then I hope you have already completed Part 1 of the article. If not, then read that article first as this is a continuation of it and it will make more sense.
In this part, we will set up our IoT device and configure it to interact with our custom Alexa skill built here.
In order to set-up our IoT configuration we require the following hardware component:
1. Esp8266 IoT KIT: The ESP8266 is a low-cost Wi-Fi microchip with a full TCP/IP stack and microcontroller capability. It's easily available in any electronic shop or you can buy it online.
This is how it looks.
2. Relay: A relay is an electrically operated switch. It consists of a set of input terminals for a single or multiple control signals and a set of operating contact terminals. In simple words, you can understand it as a device that acts as a bridge between your electronic and electrical devices. It’s also very easily available in any electronic or online store and very affordable in price.
I hope you are ready with these devices. Let’s start the process.
First, we need to set up and configure our IoT device. This broadly requires three steps:
Building Firmware:
Before building any firmware for our device, let’s know what a firmware is.
Firmware is a software program programmed on a hardware device. It provides the necessary instructions on how the device communicates with the other computer hardware.
Now, for firmware build, you need to check-out this Link.
Here, you were asked to select modules you want to ship to your firmware. Basically, modules here are the capabilities you want to enable in your chipset.
A list of modules is already checked by default.
Apart from it, you need to select the websocket
module also.
Once done, provide your email-Id and click on Start your Build
Button at the bottom of the page.
You will receive the build directly in your mail in some time.
The mail you received will have two links. In that, you have to select the link with a float label. Click on the link to download the generated build in your system.
Flashing Firmware into the Kit
Before flashing the build into our IO kit, we need to install the USB driver to your system which allows our system to connect with our IoT device via USB.
For this, you need to hit the below command.
To know more about this cask-driver check the link below:
https://github.com/Homebrew/homebrew-cask-drivers
Now to flash the build in your kit, we have to take help of a special Python tool named esptool
.
I hope your system is configured with python setup and pip
tool.If not, refer to this link below for the same.
https://docs.python.org/3/installing/index.html
Now run the following command from your terminal in order to install esptool in your system.
To know more about this toll refer to this link.
Once the tool is installed, connect your IoT kit with your system via USB.
and run this command from your terminal. Don't forget to put the path of your downloaded build at the end of the command.
Executing the above command will flash our build into the kit and it will become ready to execute codes.
Compiling and shipping the codes into the kit:
Now in order to interact with our skills, we need to compile and run some functionalities in our kit.
The language we use to write our functional logic will be Lua
. Lua is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded use in applications.
It’s very easy to understand and fits best in the embedded system.
To know more about it refer its official link.
https://www.lua.org/docs.html
Our code logic can be broadly divided into three-parts:
But in order to ship this logic very easily, it will be better to use an IDE for the same. For this, I would recommend ESPlorer.
Click on the link below to download it in your system. Since this is a Java tool, it will work in all OS.
http://esp8266.ru/esplorer-latest/?f=ESPlorer.zip
Once downloaded, extract the zip folder and click on the Esplorer.jar file in order to run the application in your system.
This is how it looks like when opened for the first time.
Now connect your IoT device via USB, click on refresh
button and usbtoUart
option from the connection dropdown option.
Now select the port as 115200 and click on Open
Button.
The port number may be different for different systems and devices. In case that doesn’t work, then try with other ports.
In order to verify and establish a connection with the application, you need to press the small reset button present just behind the USB port of the IoT device.
Now, your IoT device is connected & the application is ready to ship the code into the device.
The first task to do here is to establish the WiFi connection in the device.
Paste the following line under the editor section of the application, but be sure to update the WiFi name
and password
in the below code.
Now press Send To ESP Button. If you got True
in the output section, it means your WiFi set-up is completed successfully and if you have provided the right credentials, your device will be automatically connected with the WiFi whenever it is available.
In order to test your connection execute the following line.
If it returns some kind of IP address, it means your device is connected with the network.
Now, let's move to the next step i.e to set-up the WebSocket module in our IoT Kit.
But what is a WebSocket?
WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. So, we are going to use this to interact with the backend of our Alexa-skill.
It consists of two parts: Server and Clients. For this, our Alexa skill backend will serve the purpose of the server and our IoT will be the client to this server that keeps listening for broadcast messages.
Let us set up our server first. If you are using a node server for your Alexa Skill backend, then implementation becomes quite easy.
What you need to do is to install the ws module
in your project via npm
command.
Now the WebSocket module is available in the project. What you have to do is to copy and paste following codes in your index.js
file:
Now our WebSocket server setup is done, Let's set its client to an IoT device in order to start the communication between the two.
In order to do so, paste the following line in the code editor section of Esplore. Remove all the previous code from it before pasting it there:
Now you need to save this code in your system so that you can access it later. For this, click on the file menu present at the top of the application and select the Save to disk option. Give it any meaningful name and then click on Save Button.
Now in order to ship this code into the IoT device, click on the Send to ESP button. After clicking on it, your file name will appear at the right-most navigation panel.
Now, click on the file and your code logic will start running. If you got the Connected message in the output console, it means your connection is established and if not try to reset the kit by clicking on RTS Button and following the steps again.
So, now our connection is established and our Alexa skill backend and IoT are handshaking each other via WebSocket protocol.
Whenever our intent gets hit in our backend server, we need to notify our IoT kit by sending the message via WebSocket, and on the IoT side, this message is mapped with some functionalities that get triggered whenever it is received.
For doing so in the backend server, you just need to use the send method to broadcast the message to the client.
Now, on the IoT side, we need to map this incoming message with some functionalities, like setting the pin state to On or Off, so that our connected Relay also gets turned On or OFF with the appliances accordingly.
This can be done by simply adding the below code in our file, that we have created just above:
Here we are setting our pin 1 to low(0) configuration, whenever it receives the turn on the light
message from the connection.
For this, we have used the gpio
module.
You must be wondering why we are configuring our pin to ‘low’ as we have to turn on our light. We do it because the Relay works in the opposite way.
If it receives low signal(0) from the pin, it will be switched On and if it receives the high signal it will be switched off.
Let's connect our Relay for the final setup:
Our IoT operates on 3–5v of DC electric supply, whereas most of our electrical appliances operate on 220V or 110V of AC electric supply. In order to fill this large gap, a Relay is used.
To connect our relay to our IoT device, we need to interconnect three pins.
The first two will be the ground and 3vcc/5vcc pins. This will switch on the relay circuit.
The third will be the ‘In’ pin of the relay with our Configured Pin on which we are changing state i.e pin1.
Now, our IoT kit is connected with the IoT and we just have had to connect our appliances in a switch.
To know how to do this refer to this below.
Congratulations. If you have connected your appliances with the Relay successfully. This was the last part of the setup.
It’s time to test your whole set-up.
You can do this from either the Alexa test simulation present in Alexa console or you can directly invoke via your Echo device.
Finally, I rest my case.
Thank you so much for reading. I hope you found this article series helpful.
Don’t forget to follow me on Twitter if you want to know more about the project or just want to talk.
Book a Discovery Call.