Alexa Controlled IoT Home Automation by Emulating a WeMo Device using NodeMCU

Alexa controlled IoT Home Automation by Emulating a WeMo Device using NodeMCU

It’s been almost three years since Amazon released the Amazon ECHO voice controlled speakers and the popularity of the speaker has continued to soar because of astounding performance of the Alexa Voice Service, and the fact that the platform was opened up to developers has led to the development of Alexa compatible devices by top electronics manufacturers and the birth of several Alexa/Amazon echo based tech start-ups.

 

There are many Alexa enabled smart lightings are available in the market but they are a little bit costly, so here in this tutorial we learn to build our own Alexa controlled Light using ESP12E NodeMCU. In this Alexa controlled home automation project, we will emulate the WeMo switch by using ESP module.

 

WeMo is the name of series of IoT products developed by Belkin company, it mainly include WeMo switch which can be controlled from anywhere over internet. So connecting any AC appliance with WeMo switch makes that device IoT enabled. IoT based Home automation is very popular nowadays and we have previously done many IoT Home Automation Projects using different controllers like Raspberry Pi, ESP8266, Arduino etc.

 

Materials Required

  1. NodeMCU ESP-12E (ESP8266 can also be used)
  2. Relay Modules
  3. AC Bulb
  4. Jumper Wires

 

Amazon Echo controlled Home Automation

 

Circuit Diagram

Connections for this Amazon Echo controlled Home Automation is shown below:

Circuit Diagram for Amazon Echo controlled Home Automation

 

Here two relay modules are connected to NodeMCU to control two Home appliances. A 5v power supply from an adaptor or 5v AC to DC converter can be used to power the circuit. I am using HiLink 5v SMPS to provide power.

The whole setup is assembled in a 3D printed box to give it a smart appliance board look. There is a bulb holder and a socket to plug an appliance like TV. The assembled box is shown in the image below.

Circuit-Hardware for-Amazon Echo-controlled Home Automation

 

Ways to control NodeMCU with Amazon Echo Dot

There are several methods from which we can control our ESP. They are listed below with features

  1. Using Amazon Alexa Skills: This method is for developers who knows how to create an skill and it requires knowledge of AWS services. It’s a complex and time consuming method.
  2. Using Third Party Services: This method is very popular and we have used this many times. Using third party services like IFTTT we can trigger any action whenever we receive commands from Alexa. This method is easy but requires integration of two services like Amazon Alexa + Webhooks.
  3. Using already built skills: There are some Smart home skills which are already available in Alexa skill store. Sinric is one of them which is used to make any appliance a custom Alexa enabled Smart home device. Library for the ESP boards are available on github and with some configuration on Sinric website we can control our appliances using Alexa. But the code for ESP boards is little difficult to understand for the beginners but it is easy to use.  
  4. Using virtual switch emulation libraries: Alexa has built in support for some home appliances like Phillips Hue and Belkin WeMo. So some developers emulated these platforms and developed their own version of Phillips hue or WeMo by spoofing the response to behave like a supported device such as the WeMo.

Here in this tutorial we are using this virtual switch emulation technique so lets see this method in detail.

 

WeMo Switch Emulation using Amazon Alexa Echo Dot

The WeMo devices use UPnP (Universal Plug and Play protocol) to send and receive data over the network. We can easily track this communication between device and Wi-Fi network using Wireshark network tool. Wireshark is used to collect packets while the communication takes place between WeMo device and Echo dot speaker. Developer found that Device Detection Function starts with the Echo searching for WeMo devices using UPnP. Then the device responds to the Echo with the device’s URL using HTTP over UDP. Echo requests the device to send the description of itself in HTTP format.

 

Now, Echo detects the device and establishes a connection. The Echo and the WeMo connects over the HTTP and issues a “SetBinaryState” (On/OFF) command. Then WeMo takes that command and sends a confirmation via HTTP. The complete flow chart emulating a WeMo switch using Alexa is given below

Flow Chart Emulating a WeMo Switch using Alexa

 

Now, this information can be used to set up our own WeMo virtual cloud. So, a script was made in the same way to emulate the Phillips or WeMo devices by developers and can be used with any ESP device.

FauxmoESP library is one of them which is easy to use and emulates Phillips devices. Using this library we can make many virtual devices and control our appliances using Alexa.

So here we are using virtual switch emulation technique as it is easy to implement and need less coding.

 

Downloading and Installing Required Libraries for WeMo Emulation

As we will create multiple virtual connections environment on ESP so we need to install fauxmoESP along with Asynchronous TCP library.

1. For ESP8266 download the Asynchronous TCP library from this link and for ESP32 download it from this link.

2. Then download fauxmoESP library from this link .

3. Now, unzip these files in libraries folder in your Arduino directory which can be found in Documents folder. Also, rename these folders as oseperez-fauxmoesp-50cbcf3087f to xoseperez_fauxmoesp and ESPAsyncTCP-master to ESPAsyncTCP.

 

4. There is an example code in fauxmoESP for controlling appliances we have to modify this example. Open Arduino IDE and go to Examples -> FauxmoESp -> fauxmoESP_Basic.

Downloading and Installing Required Libraries for WeMo Emulation

 

Before starting with the coding part, make sure you have installed ESP board files. If you don’t have board files then follow our previous tutorials on getting started with ESP using Arduino IDE.

 

Code and working Explanation

Complete code with a working video for this Alexa controlled Home appliances is given at the end of this tutorial, here we are explaining the complete program to understand the working of the project.

First, include important header files for ESP board and fauxmoESP. There different header files for ESP8266 and ESP32 but in this example code both libraries are included so this code will work for both the boards. Also, define pin number for the relays.

#include <Arduino.h>
#ifdef ESP32
  #include <WiFi.h>
//  #define RELAY_PIN_1 12
//  #define RELAY_PIN_2 14
#else
  #include <ESP8266WiFi.h>
  #define RELAY_PIN_1 4
  #define RELAY_PIN_2 14
#endif
#include "fauxmoESP.h"

 

Define baud rate of 115200 and Wi-Fi-SSID and Password. Also, make an instance for fauxmoESP as fauxmo so that we can use this in our code.

#define SERIAL_BAUDRATE 115200
#define WIFI_SSID "*******"
#define WIFI_PASS "*******"
fauxmoESP fauxmo;

 

Make a separate function for Wi-Fi setup so that it can be called in void setup function. Make the WiFi mode as station mode and pass the SSID and Password in WiFi.begin() function. Wait for the connection to establish and display the IP of ESP.

void wifiSetup() {
  WiFi.mode(WIFI_STA);
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
..
..
}

 

In void setup() function, pass the baud rate to serial.begin function and call the wifisetup function.

void setup() {
  Serial.begin(SERIAL_BAUDRATE);
  Serial.println();
  wifiSetup();

 

Make relay pins high or low by default.

  pinMode(RELAY_PIN_1, OUTPUT);
  digitalWrite(RELAY_PIN_1, HIGH);
  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, HIGH);

 

Now, fauxmoESP has to create its own webserver, for this pass true in createserver function, enable function and set port number as 80. If you make false in enable function then it will prevent the devices from being discovered and switched.

  fauxmo.createServer(true);
  fauxmo.setPort(80);
fauxmo.enable(true);

 

Add devices using fauxmo.addDevice() function. The argument will be the name of your device that you will used to ask Alexa to switch it on/off.

  fauxmo.addDevice(bedroom_light);
  fauxmo.addDevice(tv);

 

Now, make a function when a command is received from Alexa. In this function we will compare the string with the device name when it is matched then change the state of  AC appliance according to the command given.

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
    if ( (strcmp(device_name, bedroom_light) == 0) ) {
      if (state) {
        digitalWrite(RELAY_PIN_1, LOW);
      } else {
        digitalWrite(RELAY_PIN_1, HIGH);
      }
    }
}

 

Similarly, do for the second AC Appliance.

 

In void loop() function, just check for the incoming packets from Alexa server using fauxmo.handle function and it will take the actions using onSetstate() function .

void loop() {
  fauxmo.handle();
..
}

 

That’s it.

Finally upload the complete code (given at the end) in NodeMCU after connecting the circuit as per the circuit diagram shown above. Also, note that the Wi-Fi network should remain same for both the NodeMCU and Amazon echo dot.

Select the correct board and port number from Tools menu and hit the upload button. You can open serial monitor in Arduino IDE to see whats happening inside the code. Make the baud rate 115200 in serial monitor.

 

Testing the Alexa Home Automation System

Now, try saying Alexa, Discover Devices. Alexa will respond like Starting discovering… I found two devices, try saying “Alexa, turn on Bedroom light”.

Testing the Alexa Home Automation System

 

Alternatively, you can discover these devices in your Alexa app. Tap on + sign and then discover devices. You should see two devices namely Bedroom light and TV.

Testing IoT Alexa Home Automation System

 

Now we are ready to test our IoT Alexa Home Automation System. So just try saying Alexa, turn on Bedroom light and the Relay one should be turned on.

Now say Alexa, turn off Bedroom light and relay one should turned off. Try giving commands to turn on/off the TV.

You can see the response and state of appliances in serial monitor.

IoT based Alexa controlled Home Automation System

 

So this is how a IoT based Alexa controlled Home Automation System can be made by emulating WeMo Switch using ESP12E NodeMCU.

Find the complete program and demonstration Video are below. Also check other Iot based Home Automation Projects.

Code

#include <Arduino.h>
#ifdef ESP32
  #include <WiFi.h>
  #define RELAY_PIN_1 12
  #define RELAY_PIN_2 14
#else
  #include <ESP8266WiFi.h>
  //#define RF_RECEIVER 5
  #define RELAY_PIN_1 4
  #define RELAY_PIN_2 14
#endif
#include "fauxmoESP.h"
#define SERIAL_BAUDRATE 115200

#define WIFI_SSID "*******"
#define WIFI_PASS "*********"
#define app1 "bedroom light"
#define app2 "tv"
fauxmoESP fauxmo;

void wifiSetup() {
  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}

void setup() {
  // Init serial port and clean garbage
  Serial.begin(SERIAL_BAUDRATE);
  Serial.println();

  // Wi-Fi connection
  wifiSetup();

  pinMode(RELAY_PIN_1, OUTPUT);
  digitalWrite(RELAY_PIN_1, HIGH);

  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, HIGH);
  
  fauxmo.createServer(true); 
  fauxmo.setPort(80); 
  fauxmo.enable(true);
  fauxmo.addDevice(app1);
  fauxmo.addDevice(app2);

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {    
    Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
    if ( (strcmp(device_name, app1) == 0) ) {
      Serial.println("RELAY 1 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_1, LOW);
      } else {
        digitalWrite(RELAY_PIN_1, HIGH);
      }
    }
    if ( (strcmp(device_name, app2) == 0) ) {
      Serial.println("RELAY 2 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_2, LOW);
      } else {
        digitalWrite(RELAY_PIN_2, HIGH);
      }
    }
  });

}

void loop() {
  fauxmo.handle();

  static unsigned long last = millis();
  if (millis() - last > 5000) {
    last = millis();
    Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
  }
}
 

Video

19 Comments

hello,
can we add two more devices with this code?
alexa discovers 2 devices correctly.but when i add two more devices in code,alexa doesn't find it
please tell me how to control 4 devices with alexa

Hi Shankar,

You just need to define your input relay pins in the program and then use digitalWrite() to control them. For example we only defined one pin in this program but you can define all the pins for 4 channel relay like:

 pinMode(RELAY_PIN_1, OUTPUT); 

pinMode(RELAY_PIN_2, OUTPUT); 

pinMode(RELAY_PIN_3, OUTPUT); 

and then use :

digitalWrite(RELAY_PIN_1, HIGH);

digitalWrite(RELAY_PIN_2, HIGH);

digitalWrite(RELAY_PIN_3, HIGH);

Hello,

First of all thank you so much for giving this tutorial.

Sir, i want to know how to use Fauxmoesp library with webserver.
For example, i want to control appliances using html web page( using Webserver library) and also with alexa( using Fauxmoesp library). Please help me to know how we can use both in one sketch.
Your help will be highly appreciable.

Thanks,
Joy Kumar.

Ricardo August…

11 February 2021

I Have problem using the auxmoESP.h library. I received the error "device is not compatible with the requested value".

To solve this error, e used other library named ESPALEXA.h Some minimal change was necessary in code..

I believe need some update in library fauxmoesp in function "callback" is necessary.

THanks

I must point out my appreciation for your kindness supporting men and women that need assistance with the question. Your personal commitment to passing the solution around turned out to be pretty powerful and have continuously permitted others much like me to realize their aims. Your new helpful tips and hints signifies a lot a person like me and a whole lot more to my colleagues. Thank you; from all of us.

My husband and i felt so delighted that John managed to finish off his web research out of the precious recommendations he was given through your web site. It is now and again perplexing to just continually be giving out concepts people today have been selling. So we figure out we have the blog owner to thank for that. The most important explanations you've made, the simple web site navigation, the relationships you aid to foster - it's got most excellent, and it's aiding our son in addition to the family recognize that the content is pleasurable, which is certainly extraordinarily mandatory. Thank you for all the pieces!

Thank you a lot for giving everyone such a splendid opportunity to read in detail from this site. It is always very nice and jam-packed with a great time for me personally and my office fellow workers to visit your blog particularly thrice in one week to learn the latest stuff you will have. And definitely, I'm just actually fulfilled considering the striking concepts you give. Certain two facts on this page are indeed the most efficient I've ever had.

I just wanted to write down a brief message to be able to say thanks to you for all the fantastic tricks you are posting at this website. My rather long internet investigation has finally been rewarded with really good facts and techniques to talk about with my good friends. I would declare that most of us site visitors actually are truly endowed to live in a fabulous community with very many lovely professionals with very helpful points. I feel very much blessed to have used the website page and look forward to so many more brilliant times reading here. Thanks once again for all the details.

I have to voice my respect for your kindness supporting individuals who absolutely need guidance on your idea. Your very own dedication to getting the message up and down had been especially significant and have without exception made many people like me to attain their pursuits. Your useful guide denotes so much a person like me and additionally to my office colleagues. Thanks a lot; from each one of us.

I must point out my love for your kindness in support of visitors who have the need for guidance on this particular subject. Your personal commitment to getting the solution throughout had become rather useful and have in most cases made employees just like me to reach their objectives. Your new interesting instruction entails a great deal to me and far more to my office workers. Thanks a ton; from all of us.

I happen to be writing to let you be aware of what a wonderful experience my cousin's daughter experienced checking your site. She came to find plenty of details, not to mention what it is like to have an incredible helping mood to get others quite simply grasp various complicated matters. You truly did more than people's expected results. Thanks for rendering the beneficial, safe, educational and also unique tips about your topic to Ethel.

Thank you so much for providing individuals with an exceptionally brilliant possiblity to read in detail from this site. It is always very nice and as well , full of a lot of fun for me personally and my office acquaintances to search your blog nearly thrice in a week to read through the fresh items you have. And lastly, I am always happy with all the brilliant creative ideas you serve. Some 3 areas in this article are absolutely the simplest I have had.

I am just commenting to let you know what a great experience our princess had checking your web site. She came to understand such a lot of things, which include what it's like to possess a great coaching spirit to make folks really easily know precisely selected problematic issues. You actually exceeded visitors' desires. Thank you for showing those effective, trustworthy, revealing not to mention easy thoughts on this topic to Evelyn.

I and also my friends were looking through the best items from your website and then suddenly developed a terrible suspicion I never thanked the site owner for those tips. All of the ladies are actually as a result very interested to see all of them and already have definitely been taking pleasure in these things. Thank you for actually being simply thoughtful as well as for getting this form of quality themes most people are really needing to be informed on. My very own sincere apologies for not expressing appreciation to you sooner.

I have to express some thanks to the writer for bailing me out of this condition. As a result of researching throughout the the net and getting things which are not pleasant, I believed my entire life was well over. Living minus the solutions to the problems you've fixed through your good review is a critical case, as well as those that would have in a wrong way affected my entire career if I hadn't encountered the website. Your good expertise and kindness in controlling all things was vital. I'm not sure what I would have done if I had not encountered such a solution like this. I'm able to now look forward to my future. Thanks for your time very much for the impressive and amazing guide. I won't hesitate to recommend your site to any person who desires guidance about this topic.

I simply desired to thank you very much once again. I am not sure what I would have sorted out in the absence of the actual tactics revealed by you on this theme. It absolutely was a very difficult difficulty in my circumstances, nevertheless looking at the very professional strategy you solved that made me to jump with delight. Extremely happier for this work and in addition wish you realize what a great job you were accomplishing educating people through the use of your web blog. More than likely you've never encountered any of us.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.