esp8266_inside

Emulate a WeMo device with ESP8266

My daughters love to talk to (or with) my Amazon Dot [Amazon US] in their funny English: “Alexa, hello!”, “Alexa,  li-on!” (actually “light on”). It’s so easy to use it to switch on/off things at home using the fauxmo python script by Maker Musings. In his post about Amazon Echo and Home Automation more than a year ago he explains how he reverse-engineered the protocol of the WeMo switches that Alexa (Amazon Echo [Amazon US] or Amazon Dot [Amazon US]) supports.

I also have a server running the fauxmo script with an MQTT handler to control some of the Sonoffs I have at home, but this morning I woke up thinking: why should I use an external script to control my devices if I can code it in the firmware?

The fauxmoESP library

You will never be the first. Aruna Tennakoon had already done it a few months ago. But his approach did not suit me. I wanted something that could be easily embedded into an existing solution (like my ESPurna firmware). So I’ve coded it into a library I could include in my other projects and have it run in 3 lines of code. Literally.

Lately I have been migrating ESPurna to the state-of-the-art ESPAsyncWebServer by core developer Hristo Gochkov (@me-no-dev). So I decided to go for his suit of asynchronous TCP and UDP libraries for this project (note: finally the ESPAsyncUDP library has been removed and its using the WiFiUdp instead). The result is a fast, sturdy library that is amazingly easy to use.

The result is the fauxmoESP library, named after Musings’ code.

The fauxmoESP library for ESP8266 is released as free open software and can be checked out at my fauxmoESP repository on Bitbucket.

Installing and compiling it

Instructions here have been updated for version 2.0 of the library.

Using PlatformIO

As I said, the fauxmoESP library depends on ESPAsyncTCP and ESPAsyncUDP libraries by Gochkov. You will need those first in order to compile it. You will also need the latest Arduino ESP8266 Core installation, at least from after July 11 2016. This is required to join the multicast group where the WeMo app (or Alexa) broadcast a message when searching for compatible devices.

You will need to install ESPAsyncTCP via the lib_deps option in the platformio.ini file or by typing:

pio lib install ESPAsyncTCP

Using Arduino IDE

Same applies to the Arduino IDE. You will need to use the development version of the ESP8266 Arduino Core. Steps to use the library are:

  1. Install the latest ESP8266 Arduino Core using the instructions here: https://github.com/esp8266/Arduino#using-git-version (remove before the stable version from your Boards Manager if any).
  2. Copy or checkout the ESPAsyncTCP and ESPAsyncUDP libraries in your arduino/libraries folder, it should be under “My Documents/Arduino/libraries” in Windows or “Documents/Arduino/libraries” in Mac or Linux unless you have placed it somewhere else.
  3. Same for the fauxmoESP library, check it out in the arduino/libraries folder.
  4. Restart your Arduino IDE
  5. Look for the fauxmoESP_Basic example under File > Examples > fauxmoESP > …
  6. Choose your board and compile.

Using it

Include the library, instantiate an object, set the device name define your devices and the message callback and you are done. It can’t be easier. Since all the networking stuff is asynchronous there is no need to manually poll for new messages. If you are using Amazon Echo [Amazon US] or Dot [Amazon US], once the device is running click on “Discover devices” from the “Smart Home” tab and your device name will be added to the list, now just say “Alexa, turn on/off” and enjoy!

You can change the device name on the fly and re-scan for new devices, the old one will be replaced.

The library allows you to define more than one device to be discovered and then perform different actions depending on which one was triggered. This was a suggestion by user Dave Myers in the comments below and has meant a change in the library API. Version 2.0 includes some discovery optimizations and has some API changes. The example below is taken from the 2.0.0 examples. As you can see the library requires a minimum setup.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include "credentials.h"

#define SERIAL_BAUDRATE                 115200

fauxmoESP fauxmo;

// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------

void wifiSetup() {

    // Set WIFI module to STA mode
    WiFi.mode(WIFI_STA);

    // Connect
    Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
    WiFi.begin(WIFI_SSID, WIFI_PASS);

    // Wait
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(100);
    }
    Serial.println();

    // Connected!
    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();
    Serial.println();

    // Wifi
    wifiSetup();

    // Fauxmo v2.0
    fauxmo.addDevice("light one");
    fauxmo.addDevice("light two");
    fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
        Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
    });

}

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

Control your ESPurna device with Alexa

I’ve also worked on the integration of the fauxmoESP library in ESPurna. The code is there and it works but I have decided to disable support by default. Basically because of the required staging environment for the espressif8266 platform in PlatformIO. Right now to compile it with WeMo emulation support you have to change the platform to “espressif8266_stage” and add “-DENABLE_FAUXMO=1” to the build flags. Check my platformio.ini file above. Once PlatformIO updates the espressif platform I will probably enable it by default. As of version 1.4.1 of ESPurna it includes fauxmoESP 2.0 which does not require any special development environment and is included by default in every ESPurna built.

CC BY-SA 4.0 Emulate a WeMo device with ESP8266 by Tinkerman is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

143 thoughts on “Emulate a WeMo device with ESP8266

  1. Pingback: “Alexa, Make My ESP8266 Do Something” | Hackaday

  2. Dave Myers

    Great code! I’ve used the other wemo-emulation code for the esp8266 that you mention, but I like the library approach used here. One feature I’m trying to add to your code is ability to instantiate multiple instances of fauxmoESP within a single sketch / on a single ESP8266 device. (I.e., I want to advertise two separate faux WeMo devices in order to have separate control of GPIO pins.

    I’ve successfully modified the code to incorporate the TCP port passed to the constructor within the WeMo uuid. However, I’m stuck with the UDP bind to the multicast address (within the ESPAsyncUDP class). Once the first instantiation binds to the multicast address, the second instantiation fails to bind – and hence only the first object responds to any of the Echo’s search requests.

    Any ideas on how to modify to allow multiple binds or multiple callbacks when a specific multicast address is opened? And this is all complicated by each fauxmoESP object having its own _udp object….

    Perhaps consider this a feature request. Having a single esp8266 be able to emulate multiple WeMos would be very handy if – for instance – the esp has several gpio outputs connected to relays.

    Thanks in advance.

    Reply
    1. Xose Pérez Post author

      Actually it’s a really good idea. I’d rather implement it as a feature in the class instead of instantiate different objects of it. The UDP binding has to be unique and it can answer to a request providing different device addresses. Then there should be an ESPAsyncTCP instance for each device, listening to different ports. The API could be something like:

      fauxmo.addDevice("switch1");
      fauxmo.addDevice("switch2");
      fauxmo.begin();

      Will give it a try.

      Reply
        1. Dave Myers

          Excellent! Tested and working.

          I ran through some tests of extreme situations (18 devices) — Alexa has trouble discovering all – and is inconsistent about which of the 18 are discovered each time. The most I’ve been able to have discovered is 10 but many tests only found 8. I’ve read in other places that this is a limitation in the way Alexa’s discovery code works – likely too short of a timeout while waiting for UDP responses.

          Since a practical limit of number of devices per ESP is likely less than 5, I think this should work.

          Thanks again.

          Reply
          1. Xose Pérez Post author

            Great!
            I had not run any tests with so many devices, 4 was the maximum I thought of since I recall having seen 4 relay ESP8266 boards…

          2. Dave Myers

            Thanks again. The only reason I can think of for a higher number of devices (and actually an idea I’m working on implementing) is a 433MHz remote outlet WeMo bridge. Ie, wemo like control over (much) cheaper 433MHz outlets. Since the ESP module won’t be constrained by gpio pins (as it will only be connected to a 433 transmitter), the number of outlets for individual control is only limited by range and the number of outlets allowed by the RF protocol.

            I’ll play with the debug logs and see if I can trace what is happening with the device search responses.

            Thanks again.

          3. DMS

            I was also not able to get more than 8 devices discovered. If you figure out what’s going on I’d be grateful! Looking at logs it appeared that while all devices were returned in the UPnP request (2 of them in a row actually) only 6-8 returned with HTTP calls.

        2. DMS

          I did copy and compile the multidevice branch, added some lookup code to allow a string table to hold 16 some odd devices, increased TCP_MAX_CLIENTS to 16 and interfaced via rest APIs to a Chronos I RS232 bridge (another esp device). I was only able to get between 6 and 8 devices registered – the UPnP call returned all 16 but only 6-8 got HTTP call backs for information. Ran out of time to debug but wonder if the return of 16 caused a timeout on ECHO.

          Reply
          1. Beautifulsmall

            Using the main branch, I have been trying several numbers of fauxmo.addDevice(“light one”);
            I used light 0-16, Alexa reported finding 4 but responded correctly to 1-8, said 11 and 13 had multiple devices , 14 worked, 15,16 not recognised.

          2. Xose Pérez Post author

            Maybe it would be a good idea to add a delay between UDP responses. Will try it next week, I’m off for the weekend.

  3. Andrew

    I am able to upload the example sketch, and successfully connect to my local WiFi, but the device is not discoverable. I even tried to manual ping the IP address assigned to the esp8226 but I am not getting a response. Any ideas?

    Reply
    1. Xose Pérez Post author

      I guess you are already monitoring the console output, don’t you? Check if there is any activity enabling the DEBUG_FAUXMO build flag.

      Reply
  4. Bobonion

    Great code, thanks. I use the esp8266 to send its control over to an arduino nano which sends TV and Sky IR remote controls, works ok but needs one device per remore button hence the need for over 16. Need to implement “jump advert” and other code combinations, “Channel BBC1”. Not sure if devices is the best way but not come across any others.

    Reply
    1. Xose Pérez Post author

      Thanks! Wow, 16 devices are a lot of devices. Did your Amazon Echo/Dot find them all?
      I have to investigate on how to add more options, right now it only supports on/off kind of commands, but I’d like to add colors, intensities,… like ins your case it could be “Alexa, set device channel to 8”.

      Reply
      1. bobonion

        No I can’t get more than six just now. It would be better to have the state pattern as the second word instead of on/off. I’ll try to look into this also. I’ll have to do a instructables if It all comes together. If I don’t use the number buttons still need 20 options ! .

        Reply
  5. Jez Merchant-Locke

    Hi… I have just had an emotional few hours getting this to work (first time using the arduino and esp) I have the terminal stating light one on and off but how do i toggle a pin on the ESP?

    Reply
    1. Xose Pérez Post author

      Cool! Congrats and welcome to the group 🙂
      Now that you have the basics going on you can just use digitalWrite to toggle any pin:


      fauxmo.onMessage([](const char * device_name, bool state) {

      Serial.printf("[MAIN] %s state: %s\n", device_name, state ? "ON" : "OFF");

      // GPIO2 is attached to the onboard LED in the ESP12 module:
      digitalWrite(2, state);

      });

      Reply
  6. Bobonion.

    Ive ended up deviating slightly and used the skills kit and lambda to catch my TV remote voice requests and just a url call with data to the esp http webserver from Lambda. Im getting ~ 1 second round trip with the Ireland AWS server. Not much in it between this and devices although ive an open port with no security. I can get all the commands I want though.
    hope your having luck.

    Reply
    1. Xose Pérez Post author

      I gave it a try the other day without luck. A skill looks like being the right path, but I’d like to know if there is a way to do something general, like an Alexa-to-MQTT skill…

      Reply
      1. Jesus

        Already tested. Now I can switch on/off my light relays.
        But I can’t get recognized more than 8 WeMo devices. I want to send IR commands to my Tv and air heating system, so getting more than 8 would be great.
        Have you checked it?
        Ha-Bridge could do this but I don’t want to have a Pi or Pc running all the time the java script.

        Reply
        1. Xose Pérez Post author

          Can you checkout the v2 branch of the project and see if it works better for you? You will have to install ESPAsyncWebServer too and mind there is a change in the callback signature. Check the example for that branch.

          Reply
          1. Jesus

            With V2 I got up to eleven devices detected !!!, some times are three or four devices marked “offline” but they work, the same as in first version.

            It’s important to name the devices in groups of no more than eight units, I mean: “lamp one, lamp two, …, lamp eight” if you put more then Echo only recognizes the first eight ones. Seems that Alexa groups devices with the same starting name in table of no more that eight.
            You can say : “Alexa lamp on” and she said “I found several devices that matches this name, which one did you want” then you must say “one” or “two”, etc.

            I checked again your first version taking care of this syntax but the maximum number of devices was eight again.

            When I “discover devices” in the Alexa web app some times I got a crash in ESP8266 unit printing the stack in the console and resets. Usually it happened after pressing “discover devices” four or five times.

          2. Xose Pérez Post author

            Good! I’ve also noticed that v2 is slightly more responsive, but the discovery process is very noisy so no wonder why not all of them get discovered the first time. The “offline” flag only means it was not seen during the last discovery process. But since it is already in you list, chances are it will work when invoked on it’s own, without so much noise. So, after all, it’s a matter of running the discovery process 2 or 3 times until all your devices have been added to the list.

            Does the crash you mention happen with the first or second version?

          3. Jesus

            The crash happened with V2 and after pressing discovering devices many times.

            Have you tested with more than one ESP8266 boards running your sketch ?

            Will the limit of 8-11 devices for each ESP board or for the total ?

          4. Xose Pérez Post author

            That’s a good question. I don’t know… yet. Will do some tests. I don’t think there is a hard limit in the number of devices, the problems arise when there are too many requests, then some might get lost. If that’s the case the jam will be localised on every single ESP board.

  7. Jeff Bacon

    this is an excellent project and I’ve already built several small projects around it. One thing I notice though, only Alexa can recognize these devices. What would it take to get some other smart hub (such as Samsung SmartThings) to also recognize these?

    Also, is there a mechanism where we could query the status of one of these devices (rather than just send commands, instead have it return the state of the device through a callback)?

    Reply
    1. Xose Pérez Post author

      Hi, Thanks! It’s a matter of knowing the protocol. I don’t know much about SmartThings but it looks like it uses a simple HTTP protocol so it might not be hard to do. I’m sure someone has already done that. The WeMo support for Alexa is quite limited. That’s great since it doesn’t even checks whether it’s a real WeMo device but on the other hand it’s (afaik) only one direction.

      Reply
  8. Disneysw

    I have just ordered some sonoff devices with the intention of re-flashing them to work with Alexa. For those who do not know sonoff produce a range of low cost smarthome devices based on the ESP8266 chip. The hardware gets resonable reviews and its hard to bet the price

    Will let you know how I get on!

    Reply
  9. Paulo Cunha

    Hello all im getting this errors when i try to compile fauxmoESP_Basic in arduino ide:

    Arduino: 1.6.13 (Windows 10), Board: “Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 1M (64K SPIFFS), ck, Disabled, None”

    C:\Users\Cunha\Documents\Arduino\libraries\ESPAsyncUDP-master\src\AsyncUDP.cpp: In member function ‘bool AsyncUDP::listenMulticast(ip_addr_t*, uint16_t, uint8_t)’:

    C:\Users\Cunha\Documents\Arduino\libraries\ESPAsyncUDP-master\src\AsyncUDP.cpp:248:57: error: ‘udp_set_multicast_netif_addr’ was not declared in this scope

    udp_set_multicast_netif_addr(_pcb, multicast_if_addr);

    ^

    C:\Users\Cunha\Documents\Arduino\libraries\ESPAsyncUDP-master\src\AsyncUDP.cpp:249:36: error: ‘udp_set_multicast_ttl’ was not declared in this scope

    udp_set_multicast_ttl(_pcb, ttl);

    ^

    exit status 1
    Error compiling for board Generic ESP8266 Module.

    Any help would be great.
    Thanks.

    Reply
    1. Xose Pérez Post author

      Hi
      No, that’s the current stable version. Please check the instructions in the link I provided.

      Reply
    1. Xose Pérez Post author

      Really, you should have read the link I posted, the same link in the documentation of the library. It’s the same solution you have just linked.

      Reply
  10. John Melling

    Great project and thanks for all your hard work – much appreciated.
    Just received my Amazon Echo for Xmas and have reprogrammed a Sonoff device with added 433MHz transmitter to emulate a cheap Energenie remote control of 4 mains sockets. Echo found and recognised the devices by their names e.g. Lounge lights and turns them on/off as commanded. Wife very impressed! Keep up the good work. Thanks again.

    Reply
    1. Xose Pérez Post author

      Hi
      Thank you and good work you too. A great idea to hack the Sonoff as a bridge. I hope you will share your project.

      Reply
      1. John Melling

        I’ll try and put some details together and send it to you probably early in the new year. I’m also looking at WEMO emulating an IR remote control to get Alexa to control the TV. Using the Sonoff gives the 3v3 power supply, ESP8266, and a suitable case all in one. I’ve upgraded the flash memory device so I can do OTA programming and removed the relay to give space for the 433MHz transmitter module.

        Reply
        1. Xose Pérez Post author

          The thing with the Wemo emulation is that, so far, it seems to support only on/off commands. I have it controlling my xmas tree with commands line “rainbow mode on”, “turn on twinkle mode” and so on. It’d be cool to be able to use attributes like “Alexa, set xmas tree mode rainbow”, or in you case: “Alexa, change TV channel to…”

          Reply
          1. John Melling

            Yes I agree it is rather a limitation but I’ll start with TV on/off,mute on/off, volume on/off to increase/decrease volume level and a few select channels e.g. BBC 1 HD on, ITV 1 HD on etc.and see how it goes from there. Will probably end up having an ‘Alexa shouting match’with my wife trying to pick channels but it will make a change from fighting over who should have the remote control!

  11. Paulo Cunha

    Xose i need your help.
    Im using AlexaPi and Alexa cant discover any devices. I have flashed my sonoff with arduino ide by selecting “generic esp8266”.
    Help

    Reply
  12. pdev

    Happy new year! Can this be integrated with Google Home? Based on Wemo’s announcement, the Wemo firmware version should be 10885 or higher for both iOS and Android. I am not sure if that would affect anything here. Anyway, your comments would be very much appreciated in this regard. Thanks!

    Reply
    1. Xose Pérez Post author

      The fauxmo emulator is not 100% compatible with Wemo. Actually your device using fauxmoESP library won’t be visible from the WeMo app. But it happens that Alexa does not care about details like authentication, it just uses the minimum set of API to interact with the device and that’s just what the library exposes. So it will depend on the actual integration between Google Home and a WeMo device…

      Reply
    2. jzizmor

      pdev, have you tried this with a google home yet? I’m considering replacing my echo dot with a google home but wanted to check on this same issue as well.

      Reply
    1. Xose Pérez Post author

      The ESPurna firmware version 1.4.1 includes the required changes to use fauxmoESP 2.0, please update your ESPurna copy too.

      Reply
  13. George Ioakimedes

    I just followed your install directions and I started with the example code from adafruit, https://learn.adafruit.com/easy-alexa-or-echo-control-of-your-esp8266-huzzah/software-setup

    I get a compile error message:

    Arduino: 1.8.0 (Windows 7), Board: "Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 512K (64K SPIFFS), ck, Disabled, None"
    
    C:\Users\George\Documents\Arduino\ESP8266_Alexa\ESP8266_Alexa.ino: In function 'void setup()':
    
    ESP8266_Alexa:57: error: no matching function for call to 'fauxmoESP::onMessage(void (&)(const char*, bool))'
    
         fauxmo.onMessage(callback);
    
                                  ^
    
    C:\Users\George\Documents\Arduino\ESP8266_Alexa\ESP8266_Alexa.ino:57:30: note: candidate is:
    
    In file included from C:\Users\George\Documents\Arduino\ESP8266_Alexa\ESP8266_Alexa.ino:3:0:
    
    C:\Users\George\Documents\Arduino\libraries\fauxmoESP\src/fauxmoESP.h:104:14: note: void fauxmoESP::onMessage(TStateFunction)
    
             void onMessage(TStateFunction fn) { _callback = fn; }
    
                  ^
    
    C:\Users\George\Documents\Arduino\libraries\fauxmoESP\src/fauxmoESP.h:104:14: note:   no known conversion for argument 1 from 'void(const char*, bool)' to 'TStateFunction {aka std::function<void(unsigned char, const char*, bool)>}'
    
    exit status 1
    no matching function for call to 'fauxmoESP::onMessage(void (&)(const char*, bool))'
    
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.
    

    Would you mind taking a look and helping me determine how I could fix this error?

    Reply
  14. George Ioakimedes

    I found the reason for the error. As explained in your v2.0 notes, the callback function was changed. It added device_id so the adafruit example code function “callback” needs to be changed to this:

    void callback(unsigned char device_id, const char * device_name, bool state) {
      Serial.print("Device ID "); Serial.print(device_id);
      Serial.print("Device "); Serial.print(device_name); 
      Serial.print(" state: ");
      if (state) {
        Serial.println("ON");
      } else {
        Serial.println("OFF");
      }
    }
    

    Your example code should also be updated to version 2.0 support since for me that is what I downloaded following your instructions. At this point I have only compiled the code successfully, I have not actually tested it yet.

    Reply
    1. Xose Pérez Post author

      The example code in the repo was updated with the version bump. I guess you mean the code in this post. You are right, I have just updated it to reflect version changes. Thank you!

      Reply
  15. Jesus

    Hi, again Xose.

    I’ve been using V2 FauxmoESP since two weeks ago and all is ok. Now I’ve seen that V2 has some changes, now don’t use AsyncUDP, is it?

    So my question is if it’s worth to recompile and install this new version?

    Now I just get 8 to 11 devices discovered, will this new version improve it?

    Visc a Premià de Mar, crec que molt a prop teu, no?

    Thanks for your support !!!

    Reply
    1. Xose Pérez Post author

      Well, they say “if it’s working, don’t change it”, right? I have had better discovery experience with v2. Testing with 16 devices it very often discovers all of them in the first round. Aside from that the other major “improvement” is that staging version of the Arduino Core for ESP8266 is no longer required. As a drawback now you have to poll for UDP messages manually calling the handle() method in your loop. I’d say it’s a matter of taste.

      I tant! Jo estic a Sant Pol.

      You are welcome!

      Reply
      1. Jesus

        in my loop() there are loops with long delays that could take up to 15 seconds to finish due to my blinds program scenes, so, would be a problem to call the handle() function every, lets say 15 seconds?
        Are there any way to have both libraries v1 and v2 at arduino libraries folder? now I’m replacing it each time I make some tests.

        Thanks company !!!

        Reply
        1. Xose Pérez Post author

          The handle() is only needed to pull UDP requests, so only when you are discovering devices. Something that will not happen very often and that’s under your control. On the other hand I would try to find a different solution for your loop. 15 second delays will break you WIFI connection for sure.

          Reply
  16. jzizmor

    Have you tried this with multiple Arduinos? I have multiple light switches I want to control that are too far apart to be convenient.

    Reply
    1. Xose Pérez Post author

      First thing the library is meant to work on an ESP8266 microcontroller. Not Arduino hardware. Second, yes I have several devices at home that I manage with voice commands but they are all in the range of my WiFi network. I don’t know what you mean by “too far apart”.

      Reply
      1. jzizmor

        It was my understanding that this was meant to run on an Arduino with the ESP8266 attached to it.

        I am unclear then where to do the “pio lib install”.

        Also, how do I get the firmware for the ESP8266 on the Arduino IDE.

        Reply
  17. Pingback: Controlling an IoT Yunshan ESP8266 WIFI Network Relay via an Amazon Echo/Alexa | µC eXperiment

  18. TimK

    I purchased a Sonoff and Sonoff Dual in the hopes to get it working with Alexa. I read in a few places online that Sonoff has an effort to support Alexa sometime this year (around the Jan-Feb-March timeframe)… I’m afraid if I flash to espurna and I run into issues, the device would be useless to me. Is there any way to backup and restore the original ewelink firmware? I did find this interesting article that appears to have the original firmware; I’m new to all of this and of course I’m just worried: https://wiki.almeroth.com/doku.php?id=projects:sonoff.

    Reply
    1. Xose Pérez Post author

      Wow! Great article. I did not know about it, thank you for pointing me to it.

      Of course if you ever want to use the original firmware again you will need a way to back it up (if not the binary itself). I have not tested it. This forum thread could be of some help.

      Reply
  19. Doug Carr

    Hi,

    First thank you for your hard work! This is a fantastic contribution to the maker community.

    I don’t have much experience programming or with electronics but I’d like to think I fan follow directions and Google my brains out when something goes wrong. I had the first version working very well. No problems at all. Then I saw that a new version of fauxmoESP was released and I decided to give it a go. I went back to the release of the ESP8266 core and installed the new fauxmoESP library. I then compiled the sample code and successfully uploaded to same Adafruit Feather HUZZAH that the old code was running on. All well and good so far. I opened the serial monitor and then asked Alexa to discover devices. This is the first time I’ve even seen a controller crash so I was very surprised.

    Needless to say I have Googled my brains out on this one and I’m at a loss to solve this. Please see the crash dump below and offer any advise you can on how I can resolve this. Thank you.

    ===== Begin Crash Dump =====

    Exception (3):
    epc1=0x4010010e epc2=0x00000000 epc3=0x00000000 excvaddr=0x40070001 depc=0x00000000

    ctx: cont
    sp: 3ffef440 end: 3ffef830 offset: 01a0

    >>>stack>>>
    3ffef5e0: 00000000 3fff076c 3ffee5c0 40204636
    3ffef5f0: 3ffee71c 0000fff8 0000fff8 4010020c
    3ffef600: 00000000 3fff076c 3ffef788 4010068c
    3ffef610: 3fff076c 3fff076c 2e333032 402041b4
    3ffef620: 00000008 00000000 3ffee5c0 4020319a
    3ffef630: 50545448 312e312f 30303220 0d4b4f20
    3ffef640: 4341430a 432d4548 52544e4f 203a4c4f
    3ffef650: 2d78616d 3d656761 30343638 440a0d30
    3ffef660: 3a455441 6e755320 3032202c 766f4e20
    3ffef670: 31303220 30302036 3a30303a 47203030
    3ffef680: 0a0d544d 3a545845 4f4c0a0d 49544143
    3ffef690: 203a4e4f 70747468 322f2f3a 312e3430
    3ffef6a0: 322e3534 312e3330 353a3631 31303032
    3ffef6b0: 7465732f 782e7075 0a0d6c6d 3a54504f
    3ffef6c0: 74682220 2f3a7074 6863732f 73616d65
    3ffef6d0: 6e70752e 726f2e70 70752f67 312f706e
    3ffef6e0: 222f302f 736e203b 0d31303d 2d31300a
    3ffef6f0: 3a534c4e 34343420 44363535 45383034
    3ffef700: 0d313037 5245530a 3a524556 736e5520
    3ffef710: 69636570 64656966 5055202c 312f506e
    3ffef720: 202c302e 70736e55 66696365 0d646569
    3ffef730: 3a54530a 6e727520 6c65423a 3a6e696b
    3ffef740: 69766564 2a3a6563 550a0d2a 203a4e53
    3ffef750: 64697575 636f533a 2d74656b 2d305f31
    3ffef760: 35343434 34443635 37453830 3a3a3130
    3ffef770: 3a6e7275 6b6c6542 643a6e69 63697665
    3ffef780: 2a2a3a65 0a0d0a0d 3fff0000 0000000f
    3ffef790: 0000000f 322e3535 313a3035 0d303039
    3ffef7a0: 3ffe8858 74cb91cc 3a706473 3ffef7d0
    3ffef7b0: 3ffee5d4 00000000 3ffee5c0 40203248
    3ffef7c0: 3fffdad0 3ffee5d4 3ffee5c0 402032f5
    3ffef7d0: 3ffe8858 14cb91cc 3ffe8858 14cb91cc
    3ffef7e0: 0000dd3c 3ffef7d0 00000000 00000000
    3ffef7f0: 3fff07ec feefeffe 40201c5c 3ffee7fc
    3ffef800: 3fffdad0 00000000 3ffee7f5 40201e0c
    3ffef810: 3fffdad0 00000000 3ffee7f5 40204470
    3ffef820: feefeffe feefeffe 3ffee810 40100718
    <<<stack<<<

    ets Jan 8 2013,rst cause:2, boot mode:(3,6)

    load 0x4010f000, len 1384, room 16
    tail 8
    chksum 0x2d
    csum 0x2d
    v6000001c

    Reply
    1. Xose Pérez Post author

      This is with the fauxmoESP_Basic code crashing? Does it say anything else before the crash?

      Reply
      1. Doug Carr

        No, Other than the WIFI connection statement that’s the entire crash dump.

        I have tried going back to the GIT version of the ESP8266 Core and get the same crash.

        I Googled and Googled and found this which is similar but beyond my programming skills.

        https://forums.adafruit.com/viewtopic.php?f=8&t=103753&hilit=+PROGMEM#p519530

        If you have any advice I’d greatly appreciate it but for now I’m going to try to get back to
        the old version that worked for me.

        Again I do thank you for your effort on this.

        Doug

        Reply
  20. Doug Carr

    Hi,

    Yes this is with the sample code. No changes other than the credentials.h file. It will crash just as soon as I tell Alexa to scan for new devices. I crashes and then tries to rejoin the WiFi. It will sometimes find one or two devices though. As I said I’m at a loss here.

    I’m at work now and can’t tell you if anything is displayed on the serial monitor before the crash but I will run it tonight and reply.

    Thank you for your time.

    Doug

    Reply
  21. modmanpaul

    First off thank you a million times. I tried to donate a small amount but the paypal is not in english or looks like normal so I might guess fields. can you give me a USA donate link?

    So what I am trying to do is have “pin A” turn on for 3 seconds then off when “ON” command is received and “pin B” turn on for 3 seconds and then off when “OFF” command received. I like others have never used a usb-ttl or esp8266 but I got pretty far. I can get the esp-12e onboard led to turn on or off as step one but my research and attempts further are stuck.

    thank you for anything you or anyone else here might suggest.
    I haven’t coded anything for OFF until I had ON working obviously so its missing from the code below

    Here’s my code:

    #include 
    #include 
    #include 
    #include 
    #define SERIAL_BAUDRATE                 115200
    
    fauxmoESP fauxmo;
     
    void wifiSetup() {
     
        // Set WIFI module to STA mode
        WiFi.mode(WIFI_STA);
     
        // Connect
        Serial.printf("[WIFI] Connecting to %s ", "FiOS-SGVFL");
        WiFi.begin("FiOS-SGVFL", "jib7584rows3699tan");
        // Wait
        while (WiFi.status() != WL_CONNECTED) {
            Serial.print(".");
            delay(100);
        }
        Serial.println();
     
        // Connected!
        Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
     
    }
    void setup() {
      // put your setup code here, to run once:
        pinMode(2, OUTPUT);
        digitalWrite(2, HIGH); //because led seems to work in reverse and we want off to start
        // Init serial port and clean garbage
        Serial.begin(SERIAL_BAUDRATE);
        Serial.println();
        Serial.println();
     
        // Wifi
        wifiSetup();
     
        // Fauxmo v2.0
        fauxmo.addDevice("device one"); // 
        fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
            Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
          digitalWrite(2, !state);
          delay(3000);                  // waits for 3 second
          digitalWrite(2, state);
        });
             
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
        fauxmo.handle();
    }
    
    
    
    I also tried this code for the pin:
    
    	  if (state2Val != "ON") {
          digitalWrite(2, HIGH);
          delay(9000);                  // waits for a second
          digitalWrite(2, LOW); 
    }
    if (state2Val != "OFF") {
          digitalWrite(2, LOW);
          delay(9000);                  // waits for a second
          digitalWrite(2, HIGH);
    }
    
    
    with declaring the variable state2Val in the head and making the value match the state with these codes respectively:
    
    char state2Val;
    
    String state2Val = String(state ? "ON" : "OFF");
    
    Reply
    1. Xose Pérez Post author

      Hi
      I wasn’t aware the Paypal page was not localised. My bad. I have lost a fortune!!! 🙂
      I switched to a different Paypal extension that lets me define the locale and it should be american English now. Please do try again. Thank you!

      About your code. First: do not do many things in the callback or it will complain. A long delay like the one you are using is almost always a bad idea (anywhere in the code). There is a handy library called “Ticker” than is included in the Arduino Core for ESP8266 (so you already have it) and it basically lets you defer actions or execute them at a given interval.

      First you include it and instantiate the object:

      #include <Ticker.h>
      Ticker defer;
      

      Next you define you callback methods (the ones you will call after 3 seconds), I’m doing it easy here passing the pin number:

      void turnON(int pin) {
        digitalWrite(pin, HIGH);
      }
      void turnOFF(int pin) {
        digitalWrite(pin, LOW);
      }
      

      And then code inside the fauxmo callback would be like this:

      if (state) {
        digitalWrite(2, HIGH);
        defer.once_ms(3000, turnOFF, 2);
      }
      

      Like in your example this is only for pin 2: turning it on when an ON message is received and off after 3000 milliseconds. Note that the pin parameter is passed as the third argument in the once_ms method.
      Also note I’ve just written the code here. It’s untested.

      Hope it helps!

      Reply
      1. modmanpaul

        Paypal worked perfectly thank you. I’m glad to be able to give a what I can considering the contribution you made to people.

        The code also worked great. I still had an issue in development of the led not turning back off but then it hit me. For convience I was using the onboaqrd led for developement, but that pin does more than what I want lol. Sure enough when I switched to another pin (12) and an external led setup, all worked perfect.

        Sadly my project was to control a device called a vent-miser and I failed to take into account the power requirements of an always on wifi device. It’s a project killer as the idea was to have about 12 vents in my home grouped in alexa smart home device groups and if they sleep all hell will break loose when it comes time to work haha

        Reply
        1. Xose Pérez Post author

          Thank you too! It is great to know people give credit to things you do 🙂

          Maybe you could use a different approach and sleep them in short intervals. It will be almost real time. Another option would be to use a less power hungry radio device. Check this post about using Moteinos as low power sensors: Moteino Door Monitor.

          Reply
      2. Bill

        I’ve used delay before in a sketch to control sprinklers, but found using it with Alexa integration, was a problem. I’d try the sleep function.

        Reply
  22. Bill

    It’s been quite awhile since I did any programming with the esp8266, but now that the wemo spoofing is available I’m trying to set up an esp8266-01 to control a 2 channel relay. I did a basic web server that works ok, by pulling pin 0 and 2 low to activate the relays. I can’t figure the correct syntax to do the same in your sketch so I can call to Alexa. I can get Alexa to discover the fauxmo.addDevice(“light one”);
    //fauxmo.addDevice(“light two”); but don’t know how to assign, say, “light one” to pin 0, and “light two” to pin 2. Any help would be appreciated.
    Thanks,
    Bill

    Reply
    1. Xose Pérez Post author

      You have to define a callback method that will be called when any of the virtual devices you define is triggered along with the new status. Like this:

      fauxmo.addDevice(“light one”);
      fauxmo.addDevice(“light two”);
      fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
         if (device_id == 0) digitalWrite(0, state);
         if (device_id == 1) digitalWrite(2, state);
      });
      

      So, whenever you say “Alexa, turn light one on” you will receive a message for client_id 0 (first device defined), with status set to true and it will turn on relay in GPIO0 And remember to call fauxmo.handle() in you loop.

      Reply
        1. Xose Pérez Post author

          Is it a problem with the device name (sometimes Alexa is a little picky), with the library or with your code?

          Reply
      1. Mike Wils

        I’m still doing my triggering from device_name, not from device_id, and it still works. Is there an advantage to using the device_id?

        Reply
          1. Mike Wils

            Got it. With multiple devices defined, you can use “switch case(device_id) instead of having to use a series of “if strcmp==”. I like it.

  23. Elliott

    Hi Xose

    I’m quite a fan of tzapu’s WiFiManager library for creating a captive portal to configure wifi on the ESP. It even has the ability to add additional fields to configure the device name, for example.

    I noticed that your fauxmo library requires the ESPAsyncWebServer whereas the WiFiManager uses the ESP8266WebServer. Without knowing too much about how these work I can already tell that they’re incompatible and it’s impossible to have both.

    Is there any way I could still use the captive portal system to configure my switches the first time they are powered on? Or is there another method you would recommend? I’d like to be able to change the wifi network they connect to and the device name without cracking my switches open to reprogram them every time.

    Thank you in advance!

    Reply
    1. Mike Wils

      I have them both working together. The only problem I found, which I thought had something to do with version 2, is that the ip of the 8266 needs to be static and set below .100. This isn’t really a problem to me since I prefer to keep devices like this below the DHCP pool.

      Reply
  24. Mike Wils

    I read that there is going to be a WEMO dimmer coming out soon. Hopefully its protocol will fit into an ESP8266.

    Reply
        1. Xose Pérez Post author

          Do you mean using Alexa to set different dimming levels? Not at the moment using the Wemo emulation. But Wemo has announced a new Alexa compatible dimming switch, maybe the have implemented it in the same fashion… In the meantime you can use Alexa Skill Kit to achieve that.

          Reply
          1. Mike Wils

            I’m anxiously awaiting the dimmer version of the Wemo. Until then, what I’ve done is create a high, medium, and low pwm setting which I step through each time the “on” command comes in. I also created a separate device call “night light” that sets two separately defined devices to a very low brightness setting. It’s all about making do with what you have to work with.

  25. ja4test

    Hi,

    I’m having problems with my echo dot discovering my esp8266.
    When I connect both on my home router (arris) Alexa won’t discover my esp8266.
    When connected both to my mobile hotspot of my phone (android) it will discover my devises.

    What can i do to make my Alexa discover the esp8266 on my home wifi network?

    Thank you in advanced.

    Reply
    1. Xose Pérez Post author

      Could it be that your router is isolating the devices in different networks? Discovery will only work if the devices are in the same network segment.

      Reply
      1. Ryan Sieber

        Still no luck. I’m on the same network segment and UPnP is enabled on my router. This is the first home automation device I’ve tried to add to the Echo. I may have to dig out an old router to see if my ISP supplied cable modem/router is the culprit. Are there any other setting other than UPnP which needs to be enabled for discovery to work? I can ping the IP of my FauxMo just fine.

        Can’t wait to get into ESPurna!

        Reply
        1. Xose Pérez Post author

          Do you have Alexa support enabled in ESPurna? It’s in the “General” menu. Double check it’s ON and save settings. You don’t need any special skill to use it from an Echo and I very much doubt the router is the culprit as long as you are in the same network.

          Reply
  26. Paul J Mcmaster

    Sorry I missed your comments. I wish there was comment reply notification (i’m so needy) or maybe there is and I don’t see it.

    Anyhow, here’s the code Xose helped me build. all credit to him really once you see it. It works super great with an optoisolator to push ANY buttons with on off commands. I wish I could use open/close or up/down etc. as well but for .71 cents each from aliexpress plus another .10 for the opto-iso you can’t beat it!

    #include 
    #include 
    #include 
    #include 
    #define SERIAL_BAUDRATE                 115200
    char state2Val;
    fauxmoESP fauxmo;
     
    void wifiSetup() {
     
        // Set WIFI module to STA mode
        WiFi.mode(WIFI_STA);
     
        // Connect
        Serial.printf("[WIFI] Connecting to %s ", "FiOS-SGVFL");
        WiFi.begin("XXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX");
        // Wait
        while (WiFi.status() != WL_CONNECTED) {
            Serial.print(".");
            delay(100);
        }
        Serial.println();
     
        // Connected!
        Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
     
    }
    void setup() {
      // put your setup code here, to run once:
        pinMode(2, OUTPUT);
        digitalWrite(2, HIGH);
        // Init serial port and clean garbage
        Serial.begin(SERIAL_BAUDRATE);
        Serial.println();
        Serial.println();
     
        // Wifi
        wifiSetup();
     
        // Fauxmo v2.0 vent "open" command
        fauxmo.addDevice("vent");
        fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
            Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
          String state2Val = String(state ? "ON" : "OFF");
          digitalWrite(2, !state);
          delay(3000);                  // waits for 3 second
          digitalWrite(2, state);
        });
             
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
        fauxmo.handle();
    }
    
    Reply
    1. Xose Pérez Post author

      Hi

      That 3 seconds delay in the callback is not a good idea. You better set a volatile flag to true there and handle it in the loop with a non-blocking delay.

      Reply
        1. Bill Gillies

          This is my actual working code for individual pin control.

              // Fauxmo
              fauxmo.addDevice("motor one");
              fauxmo.addDevice("motor two");
              fauxmo.addDevice("motor three");
              
              fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
                  Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
            if (device_id == 0) digitalWrite(0, LOW), digitalWrite(2, HIGH);
            if (device_id == 0) defer.once_ms(10000, turnOFF, 0);
            if (device_id == 1) digitalWrite(2, LOW), digitalWrite(0, HIGH);
            if (device_id == 1) defer.once_ms(10000, turnOFF, 2);
            if (device_id == 2) digitalWrite(0, HIGH), digitalWrite(2, HIGH);
          
          
              });
          
          Reply
          1. Bill

            “motor one” turns one way and makes sure the other pin is off.
            “motor two” turns the other way and makes sure the other pin is off.
            “motor three” stops the motor.
            the timing is set for ten seconds, needs to be custom set for individual purposes.

  27. Bill

    I’m stumped, anyone have an idea how to turn pins on and off in sequence? Example
    digitalWrite(led1, LOW), digitalWrite(led2, HIGH);
    defer.once_ms(10000, turnOFF, led1);
    if (led1= HIGH), digitalWriter(led2, LOW)

    Thanks,
    Bill

    Reply
    1. Xose Pérez Post author

      The code has some typos and it’s hard to know what you are trying to do. What does “in sequence” mean?

      Reply
      1. Bill

        thanks for the response Xose, What I’m trying to do is get led1 to turns on for a time, then turn off, when it turns off led 2 turns on for a time then turns off.

        Reply
        1. Xose Pérez Post author

          This should do: every 10 seconds the relays will flip.


          #include
          Ticker flipper;
          bool state = false;
          void flip() {
          state = !state;
          digitalWrite(LED1, state);
          digitalWrite(LED2, !state);
          }
          void setup() {
          pinMode(LED1, OUTPUT);
          pinMode(LED2, OUTPUT);
          flip();
          flipper.attach(10, flip);
          }
          void loop() {
          }

          Reply
          1. Bill

            got code working in the fauxmo code, now to start with both pins off, and end with both pins off. I use filpper.once(10, flip); which works for turning on two devices in sequence but program starts with one pin on (LOW in my case) and ends with other pin on. the “state=false;” doesn’t compile, I get ‘state’ does not name a type error. Any advice would be appreciated.

          2. Xose Pérez Post author

            Obviously there was a “bool” missing before that sentence. I have updated my previous comment.

          3. Bill

            thanks Xose, I did that but it doesn’t seem to change the way the program acts. What I’m trying to do is start the program with both pins off, then with the call to “turn on test one”, led 4 turns on for 10 sec. then off, then led 5 turns on for 10 sec. then off. then stop untill my next call to “turn on test one” Here’s my code
            for now.

            #include
            
            fauxmoESP fauxmo;
            bool state = false;
            
             
            void flip()
            
            {
             
            state = !state;
            digitalWrite(4, state);
            digitalWrite(5, !state);
            
            }
            
            // -----------------------------------------------------------------------------
            // Wifi
            // -----------------------------------------------------------------------------
            
            void wifiSetup() {
            
                // Set WIFI module to STA mode
                WiFi.mode(WIFI_STA);
            
                // Connect
                Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
                WiFi.begin(WIFI_SSID, WIFI_PASS);
            
                // Wait
                while (WiFi.status() != WL_CONNECTED) {
                    Serial.print(".");
                    delay(100);
                }
                Serial.println();
            
                // Connected!
                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();
                Serial.println();
                
                
            
                // Wifi
                wifiSetup();
            
                //LED
                pinMode(4, OUTPUT);
                digitalWrite(4, HIGH);
            
                pinMode(5, OUTPUT);
                digitalWrite(5, HIGH);
            
               
            flip();
            
            
            
            
                // Fauxmo
                fauxmo.addDevice("test one");
                fauxmo.addDevice("test two");
            
                
                fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
                    Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
              
              if (device_id == 0) flipper.once(10, flip);
              
               
                });
            
            }
            
            void loop() {
            
             
                fauxmo.handle();
            
            }
            
          4. Xose Pérez Post author

            Mmmm… first set the pin4 in the fauxmo callback and defer action:

            if (device == 0) {
              digitalWrite(4, HIGH);
              flipper.once(10, flip);
            }
            

            The in the “flip” method check your state and do accordingly:

            function flip() {
              if (digitalRead(4) == HIGH) {
                digitalWrite(4, LOW);
                digitalWrite(5, HIGH);
                flipper.once(10, flip);
              } else {
                digitalWrite(5, LOW);
              }
            }
            
  28. Mike Wils

    I tried these 3.3 volt switching regulators I found on Ebay. http://www.ebay.com/itm/281678046714. They work perfectly with the 8266. The only additional capacitor I used was a .1uF across the VCC and GND pins of the 8266.
    I paid for the expedited shipping and got them in 2 weeks to New York. It came to less than $10 for 4 of them. Definitely a good deal.
    For anyone making LED dimmers I found the Fairchild FDP6030BL mosfet works with 3.3 volts directly without a need for a level shifting interface.

    Reply
  29. Steve C

    Hi everyone,

    Is anyone else experiencing problems with the FauxMo ESP8266 library right now? I’ve had code running since December last year perfectly, yet over the last 5 days or so it’s suddenly stopped functioning.

    I think fauxmo.addDevice(…); call is failing since my echo is showing all devices as offline and won’t rediscover them. I can say from the debug messages that the ESP is definitely connecting to my LAN okay.

    Has anyone else experience problems of late, alternatively does anyone have this working as of today (20th March)? For what it’s worth my echo is on version 5264 and I have a genuine WeMo device on my network that is still operating just fine.

    Reply
    1. Xose Pérez Post author

      I have several devices using ESPurna with the fauxmoESP library and they are working just fine…
      You could try compiling your code with the “`-DDEBUG_FAXUMO=Serial“` flag set to monitor debug messages from the library in the debug console.

      Reply
  30. Markus

    Is there a way to control the ESP8266 also via the Belkin WeMo app or via GET-parameters?

    Cheers!

    Reply
    1. Xose Pérez Post author

      I’m not aware of a wemo emulation that works with the wemo app. But you can flash my ESPurna firmware that integrates the fauxmoESP library and supports REST-like requests too.

      Reply
  31. Brian T Mickley

    I am using the fauxmoESP library to control two relays via the Amazon Echo Dot. This is working fine. Thanks!

    Relays are named ‘Lamp 1’ and ‘Lamp 2’. The ‘Alexa, turn ON/OFF Lamp 1/2’ command works fine.

    Next I added a third ‘device’ which I called ‘ALERT DAD’ (for my handicapped and bedridden mother to beckon my father when she is in distress). The ‘ALERT DAD’ device is recognized by the Echo Dot and controls the proper GPIO pin successfully.

    PROBLEM HERE: I’m having trouble creating a loop when the ‘ALERT DAD’ name is recognized by Echo Alexa. I would like the code to rapidly cycle the GPIO from HIGH to LOW with a delay between cycles. Basically, the BLINK routine.

    I have tried every way I know to get a successful blinking of one relay – without any luck.

    Can you advise? Many thanks.

    Reply
    1. Xose Pérez Post author

      It would be good to see the code but if you are trying to add the loop inside the fauxmo callback you’d probably run into problems like the one you describe.
      Maybe you can try using the Ticker library to set a recurring timer to a toggle function, something like this (caution: untested):

      #include <Ticker.h>
      
      #define LED_PIN 4
      #define NOTIFICATION_DELAY 500
      #define NOTIFICATION_TIMES 10
      
      Ticker blinkTicker;
      
      function blinkMethod() {
         static unsigned char count = 0;
         digitalWrite(LED_PIN, !digitalRead(LED_PIN));
         count = (count + 1) % NOTIFICATION_TIMES;
         if (count == 0) blinkTicker.detach();
      }
      
      function setup() {
         ...
         fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
            ...
            if (device_id == 2) blinkTicker.attach_ms(NOTIFICATION_DELAY, blinkMethod);
            ...
         });
         ...
      }
      
      Reply
  32. Kenny

    Hi Xose, I’ve flashed a sonoff with your code, and alexa can turn it off and on, which is great. The button on the sonoff no longer turns the device on or off. I have tried to reflash with another firmware, but since installing this one, I get errors when attempting an upload. Do you know how I can solve this? Many thanks.The error is espcom_open failed.

    Reply
    1. Xose Pérez Post author

      That’s weird. The button in the Sonoff (assuming it’s a Sonoff basic or TH, not a TH10 or TH16) is tied to GPIO0. Most sonoffs use GPIO0 for the button since it doubles as flash mode setter. You just have to power the board while pressing the button. The error you are getting is probably due to the board not being in flash mode but you said you already flashed it once so I guess you know how to do it…

      Reply
    1. Xose Pérez Post author

      You would have to write a custom skill for that. The Wemo devices only accept ON/OFF at the moment.

      Reply
  33. Tom K.

    Hi Xose,

    Great that you have this page up for us all. I have my Alexa controlling 3 lights, a ceiling fan, and my garage door thanks to you.

    The ESP’s that control the lights are installed inside the switch box where the light switch would normally go and I set up a button to control the light as well.

    I do have one problem I’ve run in to so far. If my wireless router is not active when I power up the ESP they never connect up unless I power cycle them. This is a real pain because my router is on the same breaker as the devices that are in the wall now, not easy to get to to power cycle them.

    I’m not sure if the wireless connect is not looping correctly to keep trying to find the wireless router or if the code is locking up. To get around it for now I just powered my router from a different plug using an extension cord.

    Reply
    1. Xose Pérez Post author

      Thanks! The device should try (re-)connecting to a defined SSID after 2 minutes (that’s the WIFI_RECONNECT_INTERVAL in the general.h file). Those two minutes should give room enough to connect to the AP point it creats when it fails to connect to the router and change the WIFI configuration (this is important if you have mistyped the password or SSID in the WIFI configuration page, for instance).

      Reply
  34. Bill Webb

    ESP worked flawlessly form the first installation for me, until last night. Suddenly my commands were ignored. I rebooted the ESP many times and finally reloaded the program. Still no luck. It connects to my network just fine. I did a forget and now the ESP devices will not discover. Free heap remains a 40368. Could something have changed on the “Wemo” end to lock out these DYI devices? I really have no ideas on how to troubleshoot this further.

    Reply
    1. Xose Pérez Post author

      I have no evidence they had changed anything. Have you tried running a discovery process again? Is Alexa support enabled in the device?

      Reply
  35. Bill Webb

    Yes, I tried all of the above without success. Eventually by mindless fiddling, I discovered that the problem was my Cable Modem / Router had suddenly decided to block UPnP. It is under the control of my ISP so there is a limit to what settings I can change even as administrator. I discoved that UPnP was not blocked for the first few minutes after a reboot of the router. Doing discovery in that small window works.

    I have no idea why Time Warner / Spectrum suddenly started blocking UPnP and there is little chance of finding out.

    Reply
    1. Xose Pérez Post author

      Yeah, that’s why I like to flash custom firmware on my devices… to really own them.

      Reply
  36. Jake K

    I’m absolutely loving this project! Already rigged it up to a few light sources around the house, but I’ve got a project for someone else, where I need it to just output a short trigger over a transistor (350 ms or so), otherwise it’ll just hold it to go and stop over the course of the time of the “light” being on.

    Is there any way to put this in a sequence for the ESP to handle after just sending an “Alexa, turn X on”?
    I’ve already tried several scenarios, and I only seem to make it output a toggle instead of a trigger.

    Reply
    1. Xose Pérez Post author

      You can use the ticker library to defer an action for 350ms. You can then switch it on, set the defer (once_ms method) and switch it off in the callback.

      Reply
  37. Ryan Sieber

    Very cool and useful project. I’m having trouble discovering my FauxMo device though. I’ve tried both the Alexa app and http://alexa.amazon.com/spa/index.html#appliances

    I assume I must enable the Alexa WeMo Smart Home Skill before the FauxMo will be discoverable?

    “Enable smart home skills to control devices with Alexa. If you’ve already enabled your skill, discover devices below.”

    I’ve tried enabling the WeMo skill but Amazon tries to link accounts with WeMo and requests my SSID and the MAC address of the WeMo before continuing.

    Has the loophole been closed or is there a workaround?

    Reply
    1. Jonathan Littlejohn

      I’m having the same issue as Ryan.

      The libraries look fantastic, but I can’t discover my device…

      Code compiles fine, all the libraries are the latest, I can see my device on the network and ping it, but I can’t discover it? Serial port debug show the device connected to the network and the “free heap” ticker is ticking away..

      I really love this project and concept. Anyone have a work around for this yet?

      Reply
      1. Xose Pérez Post author

        Are you using any of the sample code or your own? You have to call “handle” method in you loop for it to work.

        If you still don’t know what’s going on or you think it’s a bug please file an issue on bitbucket. Thank you.

        Reply
        1. Jonathan Littlejohn

          Thanks for your reply, and thanks again for this project. Fantastic.

          I am just using the sample code. I didn’t modify anything.

          I don’t believe this is a bug with your code, I believe this is an issue with the amazon app, possibly due to an update? Not sure.

          I haven’t had a ton of time for this, but after a bit of fiddling, I’ve tried/discovered the following:

          I bought an legitimate WeMo device (light switch) so that I could install the skill. This may or may not have been necessary, but I needed one anyway. After installing the skill I still couldn’t see the devices when scanning through the Alexa app.

          it turns out that the Alexa device CAN see the units, I just can’t see them in the Alexa App on my phone. I.e. if I ask “Alexa, scan for automation devices”, it correctly identifies the unit and I can control then through voice. However I still can’t see them on my app and the app still can’t see them when it scans.

          After discovering them through using an Alexa voice command I can see them through https://alexa.amazon.com/spa/index.html#appliances, just not through the app.

          Anyway, good enough for me. I can control them, just not through the app (oh well). Not sure why.

          Automated sprinklers here I come…

          Thanks again. This project rocks!

          Jon

          Reply
  38. daniel gaitan

    First of all, thank you Xosue for sharing your work, vizca catalunia !!
    this is my code for a esp8266-01, with one button for toggle, and also a webpage with websockets, so you can control the led state (or relay, etc etc) in three way: Alexa, toogle button and webpage
    sorry for any mistakes I made, I an a newbie in the micro controllers world .
    it is a collection of pieces of code from different coders over the internet, and it is now well documented or commented by my, but i hope it will help to somebody

    https://gist.github.com/anonymous/ac1e8194b292b1f5048e1e9a2ece9707

    Reply

Leave a Reply (all comments are moderated, be patient)