Alexa - NodeMCU: WeMo Emulation Made Simple

14,767

85

18

Published

Introduction: Alexa - NodeMCU: WeMo Emulation Made Simple

About: Engineer, writer and forever student. Passionate to share knowledge of electronics with focus on IoT and robotics.
This Instructable is one of the winners on "Automation" contest 2017. Thanks a lot for all your votes! ;-)

A few months ago, I have explored here, how to use Alexa, an intelligent personal assistant made popular by the Amazon Echo and Echo-Dot, in Home Automation projects:

When IoT Meets AI: Home Automation With Alexa and NodeMCU

Alexa is capable of voice interaction, music playback, making to-do lists, setting alarms, streaming podcasts, playing audiobooks, and providing weather, traffic, and other real time information. Alexa can also control several smart devices using itself as a home automation hub. We will use on this project, the "Echo-Dot", that allows users to activate the device using a wake-word, such as "Alexa" or "Computer", as in "Star Trek!.

In the home automation space, Alexa can interact with several different devices as Philips Hue, Belkin Wemo, SmartThings, etc. In our case, we will emulate the WeMo, using the fauxmoESP library,

Here is the main difference between this tutorial and my previous one. The fauxmoESP library simplifies extremely the code needed to develop home automation projects.

WeMo is a series of products from Belkin International, Inc. that enables users to control home electronics from anywhere. The product suite includes a switch, motion sensor, Insight Switch, light switch, camera, and app. The WeMo Switch (our case here) can be plugged into any home outlet, which can then be controlled from an iOS or Android smartphone running the WeMo App, via home WiFi or mobile phone network.

The below block diagram shows what will be developed on our project:

The below video, shows how the project will look like at the end:

Step 1: Bill of Material (BoM)

(All values are referenced in USD)

  1. NodeMCU ESP8266-12E ($8.79)
  2. Echo Dot (2nd Generation) - Black ($49.99)
  3. 2 X Mini BreadBoard ($1.00)
  4. 4-Channel Relay Module ($8.49)
  5. Male-Female Dupont Cables ($1.00)
  6. External 5V power Supply or battery

Step 2: WeMo Emulation - the FauxmoESP Library

This tutorial is based on the great open source lib & example developed by Xosé Perez (Tinkerman). See his original post here: Emulate a WeMo device with ESP8266. Tinkerman based its work on Python example code developed by Maker Musings.

fauxmoESP is a library for ESP8266-based devices that emulates a Belkin WeMo device and thus allows you to control them using this protocol, in particular from Alexa-powered devices like the Amazon Echo or the Dot.

The library can be found on Xose Perez's BitBucket: fauxmoESP

1. fauxmoESP Library ZIP

In order to run it in your code, you will also need 2 more libraries by Hristo Gochkov:

2. ESPAsyncTCP

3. ESPAsyncWebServer

So, Go to your Arduino IDE and install the 3 above libraries under your Arduino/Library directory.


Some Technical stuff (optional):

If you are interested in understanding more deeply what's going on, read the article: HOW TO MAKE AMAZON ECHO CONTROL FAKE WEMO DEVICES, written by Rick Osgut where you can learn the basis of WeMo emulation.

The WeMo devices use UPnP to perform certain functions over the network. The device detection function starts out with the Echo or Dot (in our case here, the Dot) searching for WeMo devices using UPnP. The device then responds to the Dot with the device’s URL using HTTP over UDP. The Dot then requests the device’s description using that HTTP URL. The description is then returned as an HTTP response.

The actual “on/off” functionality of the WeMo devices is simpler since the Dot already knows about the device. The Dot simply connects to the WeMo over the HTTP interface and issues a “SetBinaryState” command. The WeMo then obliges and returns a confirmation via HTTP (see the Echo/Dot Communication diagram above).

Step 3: Creating Our WeMo Devices With NodeMCU

Let's connect a 4 Channel relay module to control 2 lamps and 2 outlets as shown in above diagram.

We will create 4 "single smart devices":

  • Light1 ==> Relay 1 ==> NodeMCU D5
  • Light2 ==> Relay 3 ==> NodeMCU D7
  • Outlet1 ==> Relay 2 ==> NodeMCU D6
  • Outlet2 ==> Relay 4 ==> NodeMCU D8

and 3 groups of combined devices:

  • All Devices (Light1, Light2, Outlet1 and Outlet2)
  • Living Room (Light1 and Outlet1)
  • Bed Room (Light2 and Outlet2)

Download and open the file NODEMCU_ALEXA_WeMos_4X_Fauxmo_EXT.ino from my GitHub and change the dummy wifi credentials, with your own:

/* Network credentials */
#define WIFI_SSID "YOUR SSID HERE"
#define WIFI_PASS "YOUR PASSWORD HERE"

Confirm that you have properly defined the pins where the relays are connected:

/* Set Relay Pins */
#define RELAY_1 D5
#define RELAY_2 D6
#define RELAY_3 D7
#define RELAY_4 D8

Now, we must define the "name" as our devices will be understood by Alexa:

// Device Names for Simulated Wemo switches
   fauxmo.addDevice("Light One");
   fauxmo.addDevice("Light Two");
   fauxmo.addDevice("Outlet One");
   fauxmo.addDevice("Outlet Two");
   fauxmo.addDevice("Bed Room");
   fauxmo.addDevice("Living Room");
   fauxmo.addDevice("All Devices");

And, we must at setup(), define the "callback" function:

fauxmo.onMessage(callback);

The loop() should be:

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

The callback function should be developed as below (this is only for Light1):

* ---------------------------------------------------------------------------
  Device Callback
 ----------------------------------------------------------------------------*/
void callback(uint8_t device_id, const char * device_name, bool state) 
{
  Serial.print("Device "); Serial.print(device_name); 
  Serial.print(" state: ");
  if (state) 
  {
    Serial.println("ON");
  } 
  else 
  {
    Serial.println("OFF");
  }
  
  //Switching action on detection of device name
  
  if ( (strcmp(device_name, "Light One") == 0) ) 
  {
    if (!state) 
    {
      digitalWrite(RELAY_1, HIGH);
    } 
    else 
    {
      digitalWrite(RELAY_1, LOW);
    }
  }
Note that I used "!state" inside the "if statement", because the 4 channel relays use reversed logic for activation.

On the Serial monitor you can see the messages exchanged:

[WIFI] Connecting to ROVAI TIMECAP 
............................. ==> CONNECTED!
[WIFI] STATION Mode, SSID: ROVAI TIMECAP, IP address: 10.0.1.36

Now, let's ask Alexa to find your device. There are 2 methods to do it:

  1. Using the Alexa App in your Smartphone
  2. Asking Alexa to do it directly using voice command, like: "Alexa (in our case "Computer"), Find connected devices" as shown in the video below:

Once Alexa has discovery your device, you can give her voice commands as shown below:

The above screenshot shows the Serial monitor response.

Step 4: Conclusion

As always, I hope this project can help others find their way in the exciting world of electronics and IoT!

Please visit my GitHub for updated files:

Home-Automation-with-Alexa-and-NodeMCU/tree/master/Version_FauxmoESP

I have a continuation of this tutorial, where more complex functions are explored. Take a visit:

"Computer, Firing all Weapons!"

For more projects, please visit my blog: MJRoBot.org

Saludos from the south of the world!

See you at my next instructable!

Thank you

Marcelo

Automation Contest 2017

Runner Up in the
Automation Contest 2017

Share

    Recommendations

    • Water Contest

      Water Contest
    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest

    18 Discussions

    0
    user
    ThisunS

    Question 3 months ago

    please help :'(

    what is this error

    Arduino: 1.8.5 (Windows 7), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

    Archiving built core (caching) in: C:\Users\Thisun\AppData\Local\Temp\arduino_cache_696658\core\core_esp8266_esp8266_nodemcuv2_CpuFrequency_80,FlashSize_4M1M,LwIPVariant_v2mss536,Debug_Disabled,DebugLevel_None____,FlashErase_none,UploadSpeed_115200_51a584696ca2b0fd6b18bc5de134f47a.a

    Sketch uses 279580 bytes (26%) of program storage space. Maximum is 1044464 bytes.

    Global variables use 35064 bytes (42%) of dynamic memory, leaving 46856 bytes for local variables. Maximum is 81920 bytes.

    warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08

    warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08

    warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08

    warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08

    warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08

    warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08

    warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08

    warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08

    warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08

    warning: espcomm_sync failed

    error: espcomm_open failed

    error: espcomm_upload_mem failed

    error: espcomm_upload_mem failed

    Hello,
    The first image in that tut includes a 4 wemo bridges. so what is the needs for them in case i used echo dot or my mobile phone app (reverb e.g)?
    and......
    I uploaded the code successfully but when i tried to discover the devices using reverb app. It says no discovered devices. sure, i didn't use any hardware bridges in that test. then what the problem. Thanks

    I have an echo. as soon as i burn the code on nodemcu and start discovering the devices. it says no devices found. Do i have to enable some skills for the same? Please suggest

    hey , very nice project, i followed your instructions and i now have voice control of three lights and my computer, i was wondering if there is a way to also control the lighs with my phone or my pc, i downloaded the wemo but it doesn't seem to recognize the nodemcu

    1 reply

    Please tell me,
    How can we encode on node mcu so that only one control our devide

    unfortunately, I haven't figured out how to work with Echo Dot in India.....:(

    1 reply

    I bought Echo dot2 in Chennai/India during 1st week of Nov2017. It worked nicely and i have made fake wemo device also. It also worked nicely, i was able to control 2 device in my home with Alexa Voice Command.

    Thank you for this article. I have found it is nicely working but it worked for home network(Known network) If i go outside where there is other network(Different SSID & PASSWORD) i unable to connect my nodeMCU either directly or through my Android phone in Wifi setting. Kindly share the arduino code for connecting Alexa, nodeMCU & Fake Wemo device to control 2 or 4 leds/plugs.

    Update: It appears to be a glitch within the Alexa App. Controlling the devices only toggle them off for a second. Voice commands with an Echo dot works as expected. There is no problem with the Arduino code; thanks again!

    Thank you for sharing your project. I have combined it with a project using servos to control blinds with an Arduino uno R3. I am having some troubles and could use some insight.

    I am using the NodeMCU 12E to drive pins high/low to control relays which are used to trigger servos on an Arduino Uno R3 controlling the blinds

    I am able to discover the devices in alexa but the relays don't hold their state. The relays turn OFF but only for about a second and then revert back to an ON STATE. It is as if the fauxmo returns a low in a heartbeat like fashion (Serial Monitor shows the same).

    In alexa... If I turn OFF device 1 and quickly switch to device two the first device will remain off. Device two can be turned OFF and then will turn back ON automatically.

    I am trying to have the relays hold their state until "told" by Alexa or voice commands. Any thoughts?

    Super, I have a question, how to connect it with DOMOTICZ? You can do it.

    Do I need to activate the Wemo skill on my Alexa App?

    0
    user
    BenP74

    10 months ago

    When I input the code, I get the error message "D5 was not declared in this scope"

    Screen Shot 2017-09-21 at 9.10.26 AM.png
    1 reply

    I can not see your photo. But anyway, check if you have all the libraries installed and if you do not have a typo on injurioso pin definitions:


    #include
    #include
    #include "fauxmoESP.h"

    /* Network credentials */
    #define WIFI_SSID "YOUR SSID HERE"
    #define WIFI_PASS "YOUR PASSWORD HERE"

    #define SERIAL_BAUDRATE 115200

    /* Belkin WeMo emulation */
    fauxmoESP fauxmo;

    /* Set Relay Pins */
    #define RELAY_1 D5
    #define RELAY_2 D6
    #define RELAY_3 D7
    #define RELAY_4 D8

    Could I emulate a motion wemo sensor with this library?

    1 reply

    Hi Nelson, I did not know, but I suppose, you can not. This library is designed to turn ON/OFF emulated WeMo devices. The WeMo motion sensor would be the opposite, sending data (ON/OFF) to Alexa.

    you should enter the cayenne mydevices 8266 contest