Voice Control your devices with Alexa under $5 !!

Abhijeet Kamble
9 min readSep 27, 2020

--

Before starting this article, I would like to state the end result of my experiment/research.

End Result: I should be able to Voice control devices/appliances. Being lazy, I wanted to create a solution by which I would be able to control every appliances with Voice command or with an App.

Before starting to explain all things, I would like to explain about my research to achieve this, if interested, you can go through it or else you can skip this section and directly go to Actual Implementation.

Research

In this section, I would like to inform you about all the options that I explored to accomplish this. First I started with research about how microcontrollers works and started with understanding Arduino( I remember I worked on it in College Time for creating a line follower bot, but its an Advance version in current days)

Note: If you are familiar with C or C++ you can easily grasp the Arduino programming.

The first challenge for me was to search for an option which will allow me to connect through Internet since Arduino doesn’t have any wireless option available. After few search I came across Node MCU ESP 8266(will explain in next section) will help me to connect to internet and perform actions on it.

While most of the MicroControllers works with 3.3v or 5v and cannot handle the current provided from the electrical switches. For this thing to handle, We need a Relay(will explain in details in next section). The job of relay is just to act as a electromagnetic switch like on/off.

Now the most difficult task in front of me was to decide the technology with which I use to implement. There are many options like.

  • AWS IOT ( which I accomplished)
  • Google Assistant( Accomplished)
  • Alexa Custom Skills(Partially Accomplished)
  • Alexa Custom Smart Home Skills(Partially Accomplished)
  • Existing Alexa Smart Home Skills(Accomplished, Easy and Cost Saving)

I started off with understanding AWS IOT and its different services like AWS IOT Core and started registering devices, getting data and storing them to DynamoDB for further Operations, I also created AWS IOT Rules to accomplish some tasks and created Lambda(Python 3.8) to modify and change the actions on call of an API. I was quite happy till now as I was now able control the lights through Secured API Calls(For which you need to upload some certs on MCU),but what next. How should I integrate this with a Voice Controlled services ?

So I had 2 options in front of me Google Assistant and Amazon Alexa, I stated off with Google as its my favourite and use it on daily basis, However soon I realised that It would be difficult for me to integrate it with my setup. Though I continued my research and created multiple firebase functions to accomplish the functionality.

With the same time, I also started my research on Amazon Alexa and Alexa skills. Alexa skills is easy as compared to Google Assistant Actions and started with creating Alexa Custom Skills for my project, Initially it worked fine and created actions and it was working like a charm on the Emulator but the problems starts when I wanted to publish it and use it through my phone.

Please find below the Architectural Diagram of the following how it looks.

Architecture Diagram which I implemented

Now the most important thing and that is “$$MONEY$$”, Being an Indian, We usually first enquire about how much its gonna cost 😆 . I estimated the cost for this setup and it was too high like $5–15/mo. which is not acceptable to me and neither to anyone who want to implement it. So we are again at square one. 😉

My research stated again but this time for cheaper implementation and luckily I found one library which is created by Aircoookie who created a library name Espalexa which supports multiple devices like light, dimmable lights, controlling temperature and colour etc. ( There are alternatives as well, fauxmoESP library if you want to check, the maintainer has stopped maintaining the repo so I shifted to Aircookie’s library)

So, My final implementation goes with ESP8266, ESPAlexa library which costs me $0 . 😆

Actual Implementation

There are few hardware prerequisites for this functionality.

The actual implementation consist of ESP8266, ESPAlexa Library. Let’s start with ESP8266.

The ESP8266 is a low-cost Wi-Fi microchip, with a full TCP/IP stack and microcontroller capability. It consist of single chip CPU with GPIO, Analog channel, Serial channels, I2C, SPI, and most importantly on chip Wi-Fi.

Download Arduino IDE from Adruino website. Once done, we will install ESP8266 into Arduino.

If you have already installed the board to boards manager of Arduino IDE, skip this step else follow the steps

adding board to board manager.
  • Go to Tools > Boards > Boards Manager…
  • Search ESP8266 and Install it.
Esp8266 library installation
  • Close the Boards Manager window and select the Generic ESP8266 Module from board selection list as shown below.
select the board

And That’s all. Now we are ready for creating the circuit, so let’s just go through the circuit diagram.

So, take the jumper wires and actual wires and start creating the circuit.Here we used relay or switch. Relay is an electrically operated switch and like any other switch, it that can be turned on or off, letting the current go through or not. It can be controlled with low voltages, like the 3.3V provided by the ESP8266 GPIOs and allows us to control high voltages like 12V, 24V or mains voltage (230V in Europe and 120V in the US).

The relay module has three sockets: common (COM), normally closed (NC), and normally open (NO).

  • COM: connect the current you want to control (mains voltage).
  • NC (Normally Closed): the normally closed configuration is used when you want the relay to be closed by default. The NC are COM pins are connected, meaning the current is flowing unless you send a signal from the ESP8266 to the relay module to open the circuit and stop the current flow.
  • NO (Normally Open): the normally open configuration works the other way around: there is no connection between the NO and COM pins, so the circuit is broken unless you send a signal from the ESP8266 to close the circuit.

So, Now let’s move ahead and write some code which will help us to turn on the light. You can paste the same code in your editor and enter your wifi username and password.

#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>
// -------- WIFI INFORMATION -----
const char *WIFI_SSID = "---username";
const char *WIFI_PASSWORD = "----password---";
void bedroomLightChange(EspalexaDevice* dev);
Espalexa espalexa;
EspalexaDevice* bedroomLight;
// To Control Lights
int bedroomLightPin=5; // D1
void connectToWiFi(){

// This function will try to connect to WIFI
Serial.println(" Trying to connect to WiFi..");
Serial.println(WIFI_SSID);

WiFi.begin(WIFI_SSID,WIFI_PASSWORD);

// try only for 15 times and then take a pause.
int retries = 0;
while(WiFi.status() != WL_CONNECTED && retries < 15) {
delay(500);
Serial.print(".");
retries++;
}

Serial.println("WiFi Connected");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());

}
void bedroomLightChanged(EspalexaDevice* d){
if (d == nullptr) Serial.println("Sorry No device Found");
if(d->getValue()){
//Tune on the device
digitalWrite(bedroomLightPin,HIGH);
d->setValue(127);
Serial.println("Turned on the light");
}else{
// Turn off the device
digitalWrite(bedroomLightPin, LOW);
d->setValue(0);
Serial.println("Turned off the light");
}


}
void lightsInitalize(){// Setup the inbuilt LED Pin
pinMode(LED_BUILTIN, OUTPUT);
// Low BedroomLight_01
pinMode(bedroomLightPin,OUTPUT);
digitalWrite(bedroomLightPin,LOW);
}void setup() {
Serial.begin(9600);
lightsInitalize();
connectToWiFi();
bedroomLight = new EspalexaDevice("Bedroom Light", bedroomLightChanged,EspalexaDeviceType::dimmable);
espalexa.addDevice(bedroomLight);
espalexa.begin();
}void loop() {
// put your main code here, to run repeatedly:
espalexa.loop();
delay(1);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
}

Once copied this code and added in Arduino IDE, Save this file with some meaningful name and then make sure the ESP8266 is connected to the computer.

#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>

The above line of code just check if the Arduino architecture is ESP32 or not and depending upon that we are including appropriate library. Also, we are including the Espalexa.h library. ( Make sure you download the library and add it to Arduino library)

// -------- WIFI INFORMATION -----
const char *WIFI_SSID = "---username";
const char *WIFI_PASSWORD = "----password---";

Here, you will add your WIFI username and password.

void bedroomLightChange(EspalexaDevice* dev);
Espalexa espalexa;
EspalexaDevice* bedroomLight;
// TO Control Lights
int bedroomLightPin=5; // D1

In the above code snippet, we are first defining the action function which will get executed once the Alexa is active. Following that we are defining the variable of type Espalexa. We also defined a pin named D1 for controlling the relay named it as bedroomLightPin which operates on Pin no. 5.

void connectToWiFi(){

// This function will try to connect to WIFI
Serial.println(" Trying to connect to WiFi..");
Serial.println(WIFI_SSID);

WiFi.begin(WIFI_SSID,WIFI_PASSWORD);

// try only for 15 times and then take a pause.
int retries = 0;
while(WiFi.status() != WL_CONNECTED && retries < 15) {
delay(500);
Serial.print(".");
retries++;
}

Serial.println("WiFi Connected");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());

}

The above code snippet is a function to connect to wifi using the username and password defined in the program.Once connected, it will print the IP address to Serial Monitor.

void lightsInitalize(){// Setup the inbuilt LED Pin
pinMode(LED_BUILTIN, OUTPUT);
// Low BedroomLight_01
pinMode(bedroomLightPin,OUTPUT);
digitalWrite(bedroomLightPin,LOW);
}

The above code snippet used to initialise everything, it will set the pin mode as output and set the initial state as low.

void setup() {
Serial.begin(9600);
lightsInitalize();
connectToWiFi();
bedroomLight = new EspalexaDevice("Bedroom Light", bedroomLightChanged,EspalexaDeviceType::dimmable);
espalexa.addDevice(bedroomLight);
espalexa.begin();
}

The above code snippet shows the setup function, where we set the baud rate to 9600, then initialise the lights and connect to internet. Once done we define the Alexa device with a name and its type as dimmable. We add this device with the help of Alexa Echo(v3) to our amazon account(This will be taken care by the library).That’s all I guess the rest of the code is self explanatory.

So, Let’s Upload our Sketch(in Microcontrollers , program is referred as sketch).

To upload our sketch, just click on the button shown below and wait for few seconds.

uploading sketch

Once done, You should find a light blinking on your ESP8266 which means that the code is uploaded and hopefully it’s working. 😃

You can also check the same by opening serial monitor of your Arduino IDE.

Top right corner of Arduino IDE

The output should look similar like this. You will get an IP address assigned to your ESP8266 which you can use to browse and check the status of devices connected. Go to your browser and open http://IP-Address/espalexa to see all devices and their status.

Serial Monitor showing the IP acquired

If you are able to see the device in browser, let’s introduce Alexa ..!! 😉

Just say “ Alexa, Turn on Light 1” or “Alexa, Turn on Device 1

Here is the video of my setup which works like a charm.

CodeBase : https://github.com/abhijeetka/alexa-esp8266

Conclusion

So this is how I started the journey towards IOT, there are few more projects on which I am currently working. I will keep on posting my research. Thanks for reading 🙌

If any doubts, please comment.

--

--