Smart Home tutorial: ESP8266, Alexa and Amazon Echo

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:

Alexa ESP8266 how to integrate

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 way 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;
}Code language: C++ (cpp)

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:

Alexa add ESP8266 device using Amazon Echo

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

Add a new device to Amazon Echo

Finally, Alexa starts looking for new connected devices::

How to add a device to Amazon Echo

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
}
...Code language: PHP (php)

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:

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:

#include <Adafruit_NeoPixel.h>
#define PIN D2
#define PIN_STRIP D1
#define NUM_LEDS 12
#define NUM_LEDS_STRIP 8
// LEDS
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();
}Code language: PHP (php)

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");Code language: JavaScript (javascript)

Let us start the discovering process again:


Amazon Echo with devices (ESP8266) connected

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("");
          // Set RGB Color
          if (state)
             setRingColor(10,100,40);
          else
             turnOffRing();         
       }
       else if (strcmp(device_name, "Strip") == 0) {
         Serial.print("Strip status:");
          Serial.print(state);
          Serial.println("");
          // Set RGB Color
          if (state)
             setStripColor(180,30,40);
          else
             turnOffStrip();                 
       }
     });
   }Code language: PHP (php)

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

Alexa turns on LED using Amazon echo and ESP8266/ESP32

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

Alexa, turn on the strip

 How to connect ESP8266 to alexa using Amazon Echo and send commands using voice

The picture below shows both devices turned on or off:

Integrate Alexa (ECHO) with ESP32

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.

    1. Anonymous January 22, 2020
      • Francesco Azzola January 22, 2020
    2. Gilles C. August 4, 2020
      • Francesco Azzola August 4, 2020
    3. Gilles C. August 5, 2020

    Add Your Comment