Smart Home tutorial: ESP8266, Alexa and Amazon Echo

Francesco Azzola
6 min readJul 5, 2019

--

This Smart home tutorial describes how to build a smart home project using ESP8266, Alexa and Amazon Echo . In more detail, in this project, we will integrate ESP8266 with Alexa through Amazon Echo. In this way, you can control devices connected to ESP8266 using Alexa voice commands. As a result of this project, you can turn on and off several connected devices using voice commands. As you may already know, voice-enabled devices are gaining popularity. The voice interaction between humans and remote devices is evolving. This opens new opportunities for home automation.

The picture below shows the final result where the ESP8266 is controlled by Alexa:

How to control ESP8266 using Alexa

This is the first step of our project. Before using an IoT device with our voice through Alexa, it is necessary to register the device so that Amazon Echo can handle it.

In this tutorial, we will use LEDs connected to ESP8266. The first step then is adding ESP8266 to Amazon Echo in the device list, so that Alexa can control the ESP8266. To this purpose, this smart home project uses fauxmoESP library that simplifies the project. This library helps us to connect ESP8266 with Alexa. Let us suppose to use Arduino IDE to develop this project, then it is necessary to add the library. You have to download the .zip file and add it to Arduino IDE. This library requires two more libraries:

Anyway, there are several ways we can use to control an ESP8266 not only using Alexa. We can implement a system that uses an ESP8266 MQTT client to receive and send data.

How to connect ESP8266 with Alexa

You can install these two libraries in the same way you did for fauxmoESP. Once the libraries are installed correctly, it is time to build the ESP8266 sketch so that it is able to manage the interaction with Amazon Echo. The code is shown below:

#include <ESP8266WiFi.h> 
#include <ESPAsyncTCP.h>
#include <fauxmoESP.h>
fauxmoESP fauxmo; void setup() {
Serial.begin(115200);
if (connectWifi()) { // Setup fauxmo
Serial.println("Adding LED device");
fauxmo.setPort(80);
fauxmo.enable(true);
fauxmo.addDevice("Led");
}
}
void loop() {
fauxmo.handle();
}
boolean connectWifi() { // Let us connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(".......");
Serial.println("WiFi Connected....IP Address:");
Serial.println(WiFi.localIP());
return true;
}

In this example, the ESP8266 registers a device named Led. Now, we can use the Alexa app (in our smartphone) to discover the ESP8266, as shown below:

Now, you have to click on “Add new device” and then:

Finally, Alexa starts looking for new connected devices::

That’s all. The device now is connected to Amazon Echo. As you have noticed, using a few lines of code we have connected our ESP8266 to Alexa through Amazon Echo.

How to listen to voice command through Alexa

Once the ESP8266 is connected to Amazon Echo, it is possible to send commands using voice through Amazon Alexa. To get notified when the user interacts with their voice with our ESP8266, it is necessary to add a method to the sketch shown above:

... fauxmo.onSetState([](unsigned char device_id, 
const char * device_name,
bool state, unsigned char value) {
Serial.print("Device name:");
Serial.println(device_name);
// Here we handle the command received
} ...

Using the state value, the ESP8266 knows if it is necessary to turn on or off the LEDs connected to it. Now, we are able to send commands to Amazon Echo:

Alexa, turn on the <device_name>Alexa, turn off the <device name>

These two commands turn on and off the devices.

Do you know you can integrate ESP8266 with Firebase through the realtime database?

Building a Smart Home project to control LEDs using ESP8266 integrated with Alexa

Now we know how to interact with our ESP8266 using voice commands and exploiting the Amazon Alexa capabilities to understand our language. Now we can build a more complex Smart Home project supposing we want to control two different LED strips:

  • A Neopixel Ring Led
  • A Neopixel Strip Led

In this project, we want to control them independently, so it is necessary to register two different devices with Amazon Echo.

ESP8266 Schematic

Before diving into the details about how to control these two different devices using Amazon Alexa, ESP8266 and Amazon Echo, it is useful to have an overview of how to connect these devices with ESP8266. The schematic below shows how to do it:

ESP8266 with Alexa to turn on or off LEDs

The connections are quite simple, the only interesting aspect is that ESP8266 uses D1 and D2 to control the LEDs.

To control these two LED devices we use the Neopixel library. The code below shows how to do it:

Adafruit_NeoPixel pixelsRing(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel pixelsStrip(NUM_LEDS_STRIP, PIN_STRIP, 
NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
pixelsRing.setBrightness(200);
pixelsRing.begin();
pixelsStrip.begin(); ...
}
void turnOffRing() {
pixelsRing.fill();
pixelsRing.show();
}
void turnOffStrip() {
pixelsStrip.fill();
pixelsStrip.show();
}
void setRingColor(int r, int g, int b) {
for (int i=0; i < NUM_LEDS; i++)
pixelsRing.setPixelColor(i, pixelsRing.Color(r,g,b));
pixelsRing.show();
}
void setStripColor(int r, int g, int b) {
pixelsStrip.fill(pixelsStrip.Color(r,g,b));
pixelsStrip.show();
}

In this code, we simply assign two different colors to these two devices when they are turned on.

Controlling LEDs using Amazon Echo and Alexa

The last step of this Smart home project is registering two different devices and control their status using voice commands. To do it, we will register two different devices, as shown at the beginning of this article:

fauxmo.addDevice(“Ring”);
fauxmo.addDevice(“Strip”);

Let us start the discovering process again:

Turn LEDs on or off using Alexa

Finally, we can control them using voice commands:

fauxmo.onSetState([](unsigned char device_id, 
const char * device_name,
bool state,
unsigned char value) {
Serial.print("Device name:");
Serial.println(device_name);
if (strcmp(device_name, "Ring") == 0) {
Serial.print("Ring status:");
Serial.print(state);
Serial.println("");
if (state)
setRingColor(10,100,40);
else
turnOffRing();
}
else if (strcmp(device_name, "Strip") == 0) {
Serial.print("Strip status:");
Serial.print(state);
Serial.println("");
if (state)
setStripColor(180,30,40);
else
turnOffStrip();
}
});
}

The code above, simply, checks the device name and changes its state according to the Alexa voice command.

Now we can use these commands with Alexa:

Alexa, turn on the ring

If you want to turn on the strip led, then the command to use is:

Alexa, turn on the strip

The picture below shows both devices turned on or off:

In the picture below, all the LEDs strips are turned on:

As you have seen, it is very simple to control ESP8266 using Alexa. There are more interesting projects you can build:

Wrapping up…

This post described how to integrate ESP8266 with Alexa using Amazon echo. Once we know how to control ESP8266 using Alexa, it is possible to control some LEDs using voice commands. The project demonstrated how easy it is to build a smart home project based on ESP8266, Alexa and Amazon echo.

If you liked this post you can visit my blog or follow me on Twitter.

Originally published at https://www.survivingwithandroid.com on July 5, 2019.

--

--

Francesco Azzola

I’m an electronic engineer with a passion for IoT and Machine Learning. I’m one of the top 100 most important influencers in IIoT in 2020 (Onalytica).