If you've already got an Amazon Echo or Echo Dot in your home or office, you can easily add your very own devices. In this quick project we'll show how to use an Adafruit ESP8266 Feather HUZZAH to control NeoPixels or a relay.

You can easily adapt the code to add any number of devices to a single Feather for all sorts of interactive home automation projects with ease!

No external server, gateway, service, computer, Raspberry Pi, etc. required!

Credits!

Code and tutorial based on the great open source lib & example code at http://tinkerman.cat/emulate-wemo-device-esp8266/ which is based off of the Python example code by https://github.com/makermusings/fauxmo

thanks!

Required Parts!

You'll need an ESP8266 of some sort. We suggest our Feather (all-in one!)

Angled shot of rectangular microcontroller.
Feather is the new development board from Adafruit, and like its namesake, it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller...
$14.95
In Stock

Or HUZZAH breakout (harder to use, requires an FTDI cable or USB-Serial adapter)

Angled shot of a Adafruit HUZZAH ESP8266 Breakout.
Add Internet to your next project with an adorable, bite-sized WiFi microcontroller, at a price you like! The ESP8266 processor from Espressif is an 80 MHz microcontroller with a full...
$9.95
In Stock

You'll also need a USB cable for the above and/or a battery for portable use.

Our demo code also uses a Relay FeatherWing and a NeoPixel Featherwing

Video of a NeoPixel FeatherWing lighting various colors.
A Feather board without ambition is a Feather board without FeatherWings! This is the NeoPixel FeatherWing, a 4x8 RGB LED Add-on For All Feather...
$14.95
In Stock
Angled shot of a Adafruit Power Relay FeatherWing.
A Feather board without ambition is a Feather board without FeatherWings! This is the Power Relay FeatherWing. It gives you power to control, and control over power....
$9.95
In Stock

You can also use a Powerswitch tail if you want to control an outlet without splicing any cables

Pre-Requisite Setup

Before you begin you'll want to make sure your Feather ESP8266 is running properly, you have drivers installed, Arduino IDE etc.

Visit the product tutorial for the Feather ESP8266 to get setup. Once you've tested it out and got it all working, come back!

Required Libraries

You'll need a few libraries installed in Arduino to continue. Here's a list of them, install by downloading the linked Zip file, uncompressing and installing in your sketchbook's libraries folder. See our Arduino Libraries Guide for details if you've never installed libraries

Open up the Arduino library manager:

  1. ESPAsyncTCP library (to download zip click here)
  2. ESPAsyncUDP library (to download zip click here)
  3. FauxMoESP library (to download zip click here and then select download repository)

Search for the FauxMoExp library and install it

The rest of the required libraries aren't available on the Arduino library manager. Install these by downloading the linked Zip files, uncompressing them and installing them in your sketchbook's libraries folder. See our Arduino Libraries Guide for details if you've never installed libraries

Install the ESPAsyncTCP library from the link below

Install the ESPAsyncUDP library from the link below

Compile an Example

Instead of the example that comes with FauxMoESP, try this all-in-one sketch.

// SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
//
// SPDX-License-Identifier: MIT

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

#define WIFI_SSID "YOUR_SSID"
#define WIFI_PASS "YOUR_WIFI_PASSWORD"
#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("FauxMo demo sketch");
  Serial.println("After connection, ask Alexa/Echo to 'turn <devicename> on' or 'off'");

  // Wifi
  wifiSetup();

  // Fauxmo
  fauxmo.addDevice("relay");
  fauxmo.addDevice("pixels");
  // Gen3 Devices or above
  fauxmo.setPort(80);

  // Allow the FauxMo to be discovered
  fauxmo.enable(true);
  
  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
    Serial.print("Device: ");Serial.print(device_name);
    Serial.print(" state");
    if(state) {
      Serial.println("ON!");
    }
    else {
      Serial.println("off");
    }

  });

}

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

Just change

#define WIFI_SSID "..."
#define WIFI_PASS "..."

To your SSID & Password for your WiFi network!

Try to compile it (no need to upload yet!)

Compilation Problems?

Please note: as of November 27, 2016 the ESP8266 release core v2.3.0 did not have the right lwip code so there were some compilation issues. If you get a complaint about udp_set_multicast_ttl not being defined, you'll need to uninstall the ESP8266 board support, then manually install the most recent core from https://github.com/esp8266/arduino by following the instructions at https://github.com/esp8266/arduino#using-git-version

Essentially, you'll need to git clone https://github.com/esp8266/Arduino.git into your Arduino sketchbook folder under hardware/esp8266com/esp8266 and then in a terminal shell in hardware/esp8266com/esp8266/tools run python get.py

Once that's done, go back and test out your ESP8266 to make sure you can compile/upload code to it.

Upload & Test!

Once you've compiled and uploaded the code, open up the Alexa app on your phone.

From the Alexa App, navigate to Devices

Tap the PLUS icon, then tap Add Device.

For the Type of Device, select Other.

The Echo device will attempt to discover new devices.

Not detecting your device? There's an ongoing issue with the newest version of this library not detecting devices on the fauxmoESP repository: https://bitbucket.org/xoseperez/fauxmoesp/issues/58/v30-not-discovered-by-alexa

The Alexa app should recognize the new pixels & relay devices as seen above.  If it doesn't, try resetting the Feather and running the discovery process again.

Now open up the Arduino IDE serial console at 115200 baud.

Ask your Alexa (or Echo):

"Alexa, turn pixels on"

and

"Alexa, turn pixels off"

You'll see the ESP8266 print out the commands it received!

You can also try it with relay, the second device it is emulating

Now that you have it working you can change the names of the devices. Try to make them things that are easy to pronounce so the Alexa/Echo can understand them when yelled! :)

Thanks to the easy-to-use Feather system, we can quickly craft a working demo with lights and a relay. Grab a NeoPixel FeatherWing and a Relay FeatherWing (there's three types of Relay 'wings, use any which one you like).

We'll connect the NeoPixel data line to ESP pin #2 and the relay signal to #13 (you can change these pins around, except NeoPixel can't be on pin #16). You can either 'freewire' these boards together and just make sure you get the power and ground pins...

Or use a Featherwing Doubler + stacking headers on the ESP8266, and then stack the NeoPixel wing on top of it. Then put the relay board on the side

Then just make sure you cut/solder the trace on the bottom of each wing so that it is connected to the digital control pin specified above

Test Wings

You may want to go through the tutorials for both relay and NeoPixel wing to make sure they're working on the pins you want! That way you wont have to debug both the Alexa/WeMo code as well as your hardware

Full Demo Code

Now upload this sketch:

// SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
//
// SPDX-License-Identifier: MIT

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

#define WIFI_SSID "wifi_ssid"
#define WIFI_PASS "wifi_pass"

#define SERIAL_BAUDRATE 115200

fauxmoESP fauxmo;

#define RELAY_PIN 13
#define NEOPIX_PIN 2
Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, NEOPIX_PIN, NEO_GRB + NEO_KHZ800);
volatile boolean neopixel_state = false; // off by default!

uint32_t Wheel(byte WheelPos); // function prototype

// -----------------------------------------------------------------------------
// 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()
{
  strip.begin();
  strip.setBrightness(20);
  strip.show(); // Initialize all pixels to 'off'
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);

  // Init serial port and clean garbage
  Serial.begin(SERIAL_BAUDRATE);
  Serial.println();
  Serial.println();
  Serial.println("FauxMo demo sketch");
  Serial.println("After connection, ask Alexa/Echo to 'turn pixels on' or 'off' or 'turn relay on' or 'off'");

  // Wifi
  wifiSetup();
  // for gen3 devices or above
  fauxmo.setPort(80);
  fauxmo.enable(true);

  // Fauxmo
  fauxmo.addDevice("relay");
  fauxmo.addDevice("pixels");

  fauxmo.onSetState([](unsigned char device_id, const char *device_name, bool state, unsigned char value) {
    Serial.printf("[MAIN] %s state: %s\n", device_name, state ? "ON" : "OFF");

    if ((strcmp(device_name, "pixels") == 0))
    {
      // this just sets a variable that the main loop() does something about
      if (state)
      {
        neopixel_state = true;
      }
      else
      {
        neopixel_state = false;
      }
    }

    if ((strcmp(device_name, "relay") == 0))
    {
      // adjust the relay immediately!
      if (state)
      {
        digitalWrite(RELAY_PIN, HIGH);
      }
      else
      {
        digitalWrite(RELAY_PIN, LOW);
      }
    }
  });
}
uint8_t j = 0; // color swirl incrementer
void loop()
{
  fauxmo.handle();
  if (neopixel_state)
  {
    for (int16_t i = 0; i < strip.numPixels(); i++)
    {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    j++;
    delay(20);
  }
  else
  {
    for (int16_t i = 0; i < strip.numPixels(); i++)
    {
      strip.setPixelColor(i, 0);
    }
    strip.show();
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85)
  {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
  else if (WheelPos < 170)
  {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  else
  {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Since there's two devices we'll show two ways of using the call-back function to do what you want.

The callback

The way the code works is that when a new command is received, the fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) function is called with the name as a character array and then a boolean with true/false depending on whether the device should be on or off.

The state boolean is easy to deal with. For the character array, you'll want to use strcmp which will do a string comparison for you. If the strings are identical, it will return 0.

Thus to test if you got a relay command, the line if ( (strcmp(device_name, "relay") == 0) ) will test to see if that's the device requested!

In-line control (Relay)

Once you know what string is sent, the code can do something simple based on the state variable. For example, just turn a pin on or off!

  if ( (strcmp(device_name, "relay") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY_PIN, HIGH);
    } else {
      digitalWrite(RELAY_PIN, LOW);
    }
  }

External Flag control (NeoPixels)

For NeoPixels, we want to have the pixels do a rainbow swirl in loop so that we aren't in the callback for more than a millisecond. Rather than halt the callback to do pixel management, we set an external volatile variable. It's key that we indicate it is volatile because that tells the ESP8266 "hey, this variable will change in a callback or interrupt so don't assume it's the same value it was a second ago"

volatile boolean neopixel_state = false; // off by default!

...

  if ( (strcmp(device_name, "pixels") == 0) ) {
    // this just sets a variable that the main loop() does something about
    if (state) {
      neopixel_state = true;
    } else {
      neopixel_state = false;
    }
  }

Then in the main loop() code, we can test to see if neopixel_state is true or false. If it's true, it should continue with the color swirl output. The swirl code is from the rainbow() NeoPixel example, by the way, it just sets each pixel to an equidistant color on the color wheel.

  if (neopixel_state) {
    for(int16_t i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    j++;
    delay(20);
  }

If it's false, turn off all the pixels

else {
  for(int16_t i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
  }
  strip.show();
}

Now that you've got it working - try other devices like servos, motors, IR or RF remote controls, etc! Maybe call the device "party" and then you can tell Alexa to "turn the party on"?

This guide was first published on Nov 27, 2016. It was last updated on Nov 27, 2016.