# Easy Alexa (Echo) Control of your ESP8266 Huzzah

## Overview

https://www.youtube.com/watch?v=NmkEG5FmqVc

If you've already got an Amazon Echo&nbsp;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/](http://tinkerman.cat/emulate-wemo-device-esp8266/) which is based off of the Python example code by [https://github.com/makermusings/fauxmo](https://github.com/makermusings/fauxmo)

thanks!

# Required Parts!

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

### Adafruit Feather HUZZAH with ESP8266 - Loose Headers

[Adafruit Feather HUZZAH with ESP8266 - Loose Headers](https://www.adafruit.com/product/2821)
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 cores.

This is the&nbsp; **Adafruit Feather HUZZAH ESP8266** &nbsp;- our take on an...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2821)
[Related Guides to the Product](https://learn.adafruit.com/products/2821/guides)
![Angled shot of black, rectangular WiFi development board.](https://cdn-shop.adafruit.com/640x480/2821-07.jpg)

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

### Adafruit HUZZAH ESP8266 Breakout

[Adafruit HUZZAH ESP8266 Breakout](https://www.adafruit.com/product/2471)
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 WiFi front-end (both as client and access point) and TCP/IP stack with DNS support as well. While this chip...

In Stock
[Buy Now](https://www.adafruit.com/product/2471)
[Related Guides to the Product](https://learn.adafruit.com/products/2471/guides)
![Angled shot of a Adafruit HUZZAH ESP8266 Breakout. ](https://cdn-shop.adafruit.com/640x480/2471-06.jpg)

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

### NeoPixel FeatherWing - 4x8 RGB LED Add-on For All Feather Boards

[NeoPixel FeatherWing - 4x8 RGB LED Add-on For All Feather Boards](https://www.adafruit.com/product/2945)
A Feather board without ambition is a Feather board without FeatherWings! This is the&nbsp; **NeoPixel FeatherWing, a&nbsp;4x8 RGB LED Add-on For All Feather Boards**!&nbsp;Using our&nbsp;[Feather Stacking Headers](https://www.adafruit.com/products/2830)&nbsp;or&nbsp;<a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/2945)
[Related Guides to the Product](https://learn.adafruit.com/products/2945/guides)
![Video of a NeoPixel FeatherWing lighting various colors. ](https://cdn-shop.adafruit.com/product-videos/640x480/2945-05.jpg)

### Adafruit Power Relay FeatherWing

[Adafruit Power Relay FeatherWing](https://www.adafruit.com/product/3191)
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. Put simply, you can now turn on and off lamps, fans, solenoids, and other small appliances that run on up to...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3191)
[Related Guides to the Product](https://learn.adafruit.com/products/3191/guides)
![Angled shot of a Adafruit Power Relay FeatherWing. ](https://cdn-shop.adafruit.com/640x480/3191-04.jpg)

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

# Easy Alexa (Echo) Control of your ESP8266 Huzzah

## Software Setup

# 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!](../../../../adafruit-feather-huzzah-esp8266/)

# 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](../../../../adafruit-all-about-arduino-libraries-install-use/common-library-problems?view=all)

Open up the Arduino library manager:

1. [ESPAsyncTCP library](https://github.com/me-no-dev/ESPAsyncTCP)(to [download zip click here](https://github.com/me-no-dev/ESPAsyncTCP/archive/master.zip))
2. [ESPAsyncUDP library](https://github.com/me-no-dev/ESPAsyncUDP) (to [download zip click here](https://github.com/me-no-dev/ESPAsyncUDP/archive/master.zip))
3. [FauxMoESP library](https://bitbucket.org/xoseperez/fauxmoesp) (to [download zip click here](https://bitbucket.org/xoseperez/fauxmoesp/downloads) and then select _download repository_)

![](https://cdn-learn.adafruit.com/assets/assets/000/084/027/medium800/wireless_library_manager_menu.png?1573521136)

Search for the **FauxMoExp** &nbsp;library and install it

![](https://cdn-learn.adafruit.com/assets/assets/000/084/028/medium800/wireless_fauxmoesp.png?1573522292)

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](../../../../adafruit-all-about-arduino-libraries-install-use/common-library-problems?view=all)

Install the [ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP)&nbsp;library from the link below

[ESPAsyncTCP Library](https://github.com/me-no-dev/ESPAsyncTCP/archive/master.zip)
Install the [ESPAsyncUDP](https://github.com/me-no-dev/ESPAsyncUDP) library from the link below

[ESPAsyncUDP Library](https://github.com/me-no-dev/ESPAsyncUDP/archive/master.zip)
# Compile an Example

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

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/fauxmo_guide/fauxmo-basic/fauxmo-basic.ino

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](https://github.com/esp8266/arduino) by following the instructions at [https://github.com/esp8266/arduino#using-git-version](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&nbsp; **Devices**.&nbsp;

Tap the PLUS icon, then tap&nbsp; **Add Device**.

![wireless_Add_Device.png](https://cdn-learn.adafruit.com/assets/assets/000/070/573/medium640/wireless_Add_Device.png?1549308177)

![wireless_add_device_2.png](https://cdn-learn.adafruit.com/assets/assets/000/070/574/medium640/wireless_add_device_2.png?1549308188)

For the Type of Device, select&nbsp; **Other.**

The Echo device will attempt to discover new devices.

![wireless_add_device__.png](https://cdn-learn.adafruit.com/assets/assets/000/070/575/medium640/wireless_add_device__.png?1549308272)

![wireless_IMG_2049.png](https://cdn-learn.adafruit.com/assets/assets/000/070/576/medium640/wireless_IMG_2049.png?1549308310)

Warning: 

The Alexa app should recognize the new **pixels** & **relay** devices as seen&nbsp;above. &nbsp;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!

![](https://cdn-learn.adafruit.com/assets/assets/000/037/588/medium800/feather_faumo.png?1480306331)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/037/589/medium800/feather_faumo.png?1480306390)

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! :)

# Easy Alexa (Echo) Control of your ESP8266 Huzzah

## NeoPixel + Relay Demo

Thanks to the easy-to-use [Feather system](https://learn.adafruit.com/adafruit-feather), 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...

![](https://cdn-learn.adafruit.com/assets/assets/000/037/590/medium800/feather_fauxmowired.png?1480306906)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/037/591/medium800/feather_fauxmo_doubler_bb.png?1480307033)

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:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/fauxmo_guide/fauxmo-relay-neopixel/fauxmo-relay-neopixel.ino

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 **str** ing **c** o **m**** p**arison 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&lt; strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) &amp; 255));
    }
    strip.show();
    j++;
    delay(20);
  }
```

If it's **false** , turn off all the pixels

```
else {
  for(int16_t i=0; i&lt; 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"?


## Featured Products

### Adafruit Feather HUZZAH with ESP8266 - Loose Headers

[Adafruit Feather HUZZAH with ESP8266 - Loose Headers](https://www.adafruit.com/product/2821)
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 cores.

This is the&nbsp; **Adafruit Feather HUZZAH ESP8266** &nbsp;- our take on an...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2821)
[Related Guides to the Product](https://learn.adafruit.com/products/2821/guides)
### ESP8266 SMT Module

[ESP8266 SMT Module](https://www.adafruit.com/product/2491)
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 WiFi front-end (both as client and access point) and TCP/IP stack with DNS support as well. **&nbsp;...**

In Stock
[Buy Now](https://www.adafruit.com/product/2491)
[Related Guides to the Product](https://learn.adafruit.com/products/2491/guides)
### Huzzah! Adafruit.io Internet of Things Feather ESP8266

[Huzzah! Adafruit.io Internet of Things Feather ESP8266](https://www.adafruit.com/product/2680)
OK you've signed up for [Adafruit.io](http://adafruit.io/) and you're ready to build something cool and Internet-connected. All you need is this starter kit which will get you going with lots of parts that go very well with our service and are the most common sensors and...

In Stock
[Buy Now](https://www.adafruit.com/product/2680)
[Related Guides to the Product](https://learn.adafruit.com/products/2680/guides)
### Azure ESP8266 Kit for Microsoft Workshop

[Azure ESP8266 Kit for Microsoft Workshop](https://www.adafruit.com/product/3035)
No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3035)
[Related Guides to the Product](https://learn.adafruit.com/products/3035/guides)
### FeatherWing Doubler - Prototyping Add-on For All Feather Boards

[FeatherWing Doubler - Prototyping Add-on For All Feather Boards](https://www.adafruit.com/product/2890)
This is the **FeatherWing Doubler** - a prototyping add-on and more for all Feather boards. This is similar to our [FeatherWing Proto](https://www.adafruit.com/products/2884) except there are two! The magic of the Doubler comes when stacking a Feather and another...

In Stock
[Buy Now](https://www.adafruit.com/product/2890)
[Related Guides to the Product](https://learn.adafruit.com/products/2890/guides)
### NeoPixel FeatherWing - 4x8 RGB LED Add-on For All Feather Boards

[NeoPixel FeatherWing - 4x8 RGB LED Add-on For All Feather Boards](https://www.adafruit.com/product/2945)
A Feather board without ambition is a Feather board without FeatherWings! This is the&nbsp; **NeoPixel FeatherWing, a&nbsp;4x8 RGB LED Add-on For All Feather Boards**!&nbsp;Using our&nbsp;[Feather Stacking Headers](https://www.adafruit.com/products/2830)&nbsp;or&nbsp;<a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/2945)
[Related Guides to the Product](https://learn.adafruit.com/products/2945/guides)
### Adafruit Non-Latching Mini Relay FeatherWing

[Adafruit Non-Latching Mini Relay FeatherWing](https://www.adafruit.com/product/2895)
A Feather board without ambition is a Feather board without FeatherWings! This is&nbsp;the **Non-Latching Mini Relay FeatherWing**.&nbsp;It gives you power to control, and control over power. Put simply, you can now turn on and off lamps, fans, solenoids, and other small...

In Stock
[Buy Now](https://www.adafruit.com/product/2895)
[Related Guides to the Product](https://learn.adafruit.com/products/2895/guides)
### Adafruit Latching Mini Relay FeatherWing

[Adafruit Latching Mini Relay FeatherWing](https://www.adafruit.com/product/2923)
A Feather board without ambition is a Feather board without FeatherWings! This is the **Latching&nbsp;Mini Relay FeatherWing**.&nbsp;It gives you power to control, and control over power. Put simply, you can now turn on and off lamps, fans, solenoids, and other small appliances...

In Stock
[Buy Now](https://www.adafruit.com/product/2923)
[Related Guides to the Product](https://learn.adafruit.com/products/2923/guides)

## Related Guides

- [Adafruit Feather HUZZAH ESP8266](https://learn.adafruit.com/adafruit-feather-huzzah-esp8266.md)
- [Adafruit NeoPixel FeatherWing](https://learn.adafruit.com/adafruit-neopixel-featherwing.md)
- [Mystery Box: Haunted Radio](https://learn.adafruit.com/mystery-box-haunted-radio.md)
- [CircuitPython 2FA TOTP Authentication Friend](https://learn.adafruit.com/circuitpython-totp-otp-2fa-authy-authenticator-friend.md)
- [CircuitPython LED Animations](https://learn.adafruit.com/circuitpython-led-animations.md)
- [Haunted Air Blaster](https://learn.adafruit.com/automated-air-blaster.md)
- [MicroPython Smart Holiday Lights](https://learn.adafruit.com/micropython-smart-holiday-lights.md)
- [Automatic Twitch On-Air Sign](https://learn.adafruit.com/automatic-twitch-on-air-sign.md)
- [FeatherWing Proto, Doubler  and Tripler](https://learn.adafruit.com/featherwing-proto-and-doubler.md)
- [MicroPython Basics: What is MicroPython?](https://learn.adafruit.com/micropython-basics-what-is-micropython.md)
- [3D Printed IoT On Air Sign for Twitch](https://learn.adafruit.com/3d-printed-iot-on-air-sign-for-twitch.md)
- [32x32 Square Pixel Art Animation Display](https://learn.adafruit.com/32x32-square-pixel-display.md)
- [MIDI Melody Maker](https://learn.adafruit.com/midi-melody-maker.md)
- [Adafruit IO Basics: Servo](https://learn.adafruit.com/adafruit-io-basics-servo.md)
- [Build a Cloud-Connected ESP8266 Power Meter](https://learn.adafruit.com/build-a-cloud-connected-esp8266-power-meter.md)
- [Adafruit ESP32-S2 TFT Feather](https://learn.adafruit.com/adafruit-esp32-s2-tft-feather.md)
