Building an IoT power switch with the ESP8266 (and control it with your Amazon Echo!)
Tags: iot, alexa, esp8266, arduino
Published: 2020-08-22 19:58:09

***(Note: these posts are migrated from my previous [medium.com](https://medium.com/@monkeytypewritr) blog)***

[****Cut to the chase, show me the build!***](#building-an-iot-power-switch)

The ESP8266 is an amazing little module that packs WiFi connectivity and Arduino support for a little over $2 (less if you buy in bulk):

![](https://cdn-images-1.medium.com/max/2000/1*GIIEOGMe1KvdPoNKVFyHnA.png)

I first heard of this module about a year ago and was attracted by its price point and form factor. At the time, there weren’t a lot of system on a module (SOM) options for prototyping that included connectivity; often, you would have to add on an expensive and bulky WiFi/Bluetooth shield to your project.

The biggest appeal of this module to me however, was the support of [NodeMCU](http://nodemcu.com/index_en.html): a LUA runtime on the module, with networking and I/O APIs that makes it very easy to perform common tasks such as:

* WiFi Config (connecting, setting/saving the SSID, credentials)

* Performing HTTP requests, creating an HTTP server

* Use the hardware I/O (GPIO, I²C, PWM, etc.)

![](https://cdn-images-1.medium.com/max/2000/1*9p1tRo3-LTC9ULIhnhSmqw.png)

This was great! I immediately ordered a whole bunch of these (10+) for the many, many IoT projects I thought I would build. I started by building the IoT equivalent of Hello World: Blinking an LED from a browser:

 <iframe src="https://medium.com/media/a442f11660eb237d5ef23ebfd16c572c" frameborder=0></iframe>

A few weeks later, while casually browsing through my regular sites, I came across this piece of exciting news:

![[http://hackaday.com/2015/03/28/arduino-ide-support-for-the-esp8266/](http://hackaday.com/2015/03/28/arduino-ide-support-for-the-esp8266/)](https://cdn-images-1.medium.com/max/2000/1*CajpKEmpv1LxFalNwCxiTg.png)

w00t! Arduino support! This meant the module would have access to the vast number of libraries and extensions in the Arduino ecosystem. You could now easily use this module with [MQTT](https://github.com/knolleary/pubsubclient), [Web Server](https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer), [Neopixels](https://blog.adafruit.com/2015/06/30/updated-neopixel-support-for-esp8266-and-arduino-zero/), [Firmata](https://github.com/firmata/arduino/issues/257) (hence, [Johnny-Five](http://johnny-five.io/)!), and many, many more.

Arduino support helped this module gain popularity, and at the same time, the number of board/breakout offerings grew. Most of these boards aim to make development easier by including features like pin breakouts, voltage regulation, reset buttons, USB-Serial converters, etc.

![[https://www.adafruit.com/product/2471](https://www.adafruit.com/product/2471) — with the ESP 12](https://cdn-images-1.medium.com/max/2000/1*VHVmseLH1gyxbwggTpvv_A.jpeg)

![I didn’t buy any of these because I still had 10+ OG ESP8266s, but they look awesome!](https://cdn-images-1.medium.com/max/2048/1*tIsNAukjUdci5Ku1yB5sWg.jpeg)

This was all exciting news, but I honestly had no projects in mind. These awesome little modules were tucked away in my toolbox for the next little while…until my [GE Lighting](undefined) smart bulbs stopped working :(

 <iframe src="https://medium.com/media/835acedf34ec510a5ab7f10cc00fab63" frameborder=0></iframe>

![The smart home kit I bought. These bulbs use ZigBee with HA profile](https://cdn-images-1.medium.com/max/2000/1*sq9c2T5XAUWkgqpBBD4mWA.jpeg)

Well, what to do? I can’t go back to using a mechanical switch (*gasp)! I’ve grown accustomed to shouting at my lights to turn them on/off. Time to dust off those ESP8266's and make something!

## Building an IoT power switch

![](https://cdn-images-1.medium.com/max/7680/1*u1K7iBT1uCQSxue-elUHwQ.jpeg)

## 1. Bill of Materials (you’ll need these things)

**Electronics**

* [ESP8266 module](http://www.ebay.com/itm/For-Arduino-ESP8266-WIFI-Wireless-Transceiver-Module-Send-Receive-LWIP-AP-STA-BG-/121841140322)

* [Solid State Relay](http://www.ebay.com/itm/24V-380V-40A-250V-SSR-40-DA-Solid-State-Relay-Module-3-32V-DC-To-AC-/121915540741) (no clicking sounds, yay!)

* [AC-DC Power Supply 110/220V to 3.3V](http://www.ebay.com/itm/AC-DC-Power-Supply-Buck-Converter-Step-Down-Module-110V-220V-TO-3-3V-700mA-2-3W-/201620442875)

* Wires (for low voltage DC connections)

**Hardware**

* [Electrical Box](http://www.homedepot.com/p/1-Gang-12-cu-in-Electrical-Handy-Box-B112HBR/202077374) (for all the components)

* [Duplex Outlet](http://www.homedepot.com/p/Leviton-15-Amp-Duplex-Outlet-White-R52-05320-00W/202066670) (socket)

* [Wall Plate](http://www.homedepot.com/p/Leviton-1-Gang-Duplex-Outlet-Wall-Plate-White-R52-88003-00W/100025171) (to hide everything in the box)

* [Extension Cord](http://www.homedepot.com/p/Axis-6-ft-3-Outlet-Indoor-Extension-Cord-White-45502/206346669) (for the socket plug and mains wires)

**Tools**

* Soldering Iron

* Glue gun (for insulating any mains connections, be careful!)

* [USB-Serial adapter](http://www.ebay.com/itm/FTDI-FT232RL-USB-to-TTL-Serial-Converter-Adapter-Module-5V-3-3V-For-Arduino-SM-/152164631351) (for programming the module)

* Jumper Cables/Breadboard

## 2. Programming your ESP8266

![Reference: [http://benlo.com/esp8266/esp8266QuickStart.html](http://benlo.com/esp8266/esp8266QuickStart.html)](https://cdn-images-1.medium.com/max/2000/1*U_7dOEdX9Fa9WX81PNHlPA.jpeg)

You’ll want to start by programming your ESP8266 with a USB-Serial adapter. The other boards out there (such as the NodeMCU dev board) might have the adapter built in, for those you’ll just need to plug it into your USB port.

With our adapter, connect the pins according to the above diagram using your jumper wires/breadboard. Note the orange line in the above diagram: the pin GPIO0 needs to be pulled to GND during boot in order to flash it, make sure this is done before connecting it to your computer.

![messy wires](https://cdn-images-1.medium.com/max/7680/1*_gY8z9mcILprbXPlc4kiKg.jpeg)

You’ll also want to install the ESP8266 toolchain from the Arduino Boards Manager. Type in “esp8266” and install the latest version:

![](https://cdn-images-1.medium.com/max/2116/1*zlcpGiSqY-WkEjvMN8_JhA.png)

Once installed, you can browse through the bundled examples and play around with the API; you’ll find examples on HTTP servers, sending/receiving UDP packets, connecting to a network, etc. Our project is built on top of these APIs.

## 3. Amazon Echo, UDP/TCP, & WeMo Spoofing

The Amazon Echo is my smart home hub; all the “smart things” in my home can be controlled via voice with the Echo, thanks to the incredible job it has done to integrate with different smart devices on the market.

The simplest way we can get the Echo to discover and control our ESP8266, is by spoofing as a device supported by the integration, such as a Belkin WeMo switch!

Device discovery is achieved through UPnP broadcast over UDP, the ESP8266 simply needs to spoof the response to pretend to be a supported device (such as the WeMo)

![Reference: [http://hackaday.com/2015/07/16/how-to-make-amazon-echo-control-fake-wemo-devices/](http://hackaday.com/2015/07/16/how-to-make-amazon-echo-control-fake-wemo-devices/)](https://cdn-images-1.medium.com/max/2000/1*dQvNBsoJMswyOP4kMIKY_Q.png)

The response looks something like this:

{% gist https://gist.github.com/w4ilun/f8a52d802a705751df94bdf0c483a4c3 %}

While browsing through the different sites detailing the protocol and data exchange, I came across this incredible GitHub project that has everything I need: WeMo spoofing with the ESP8266!
{% github kakopappa/arduino-esp8266-alexa-wemo-switch no-readme %}

*****Update***** the author has an updated the repo to support multiple switches!
{% github kakopappa/arduino-esp8266-alexa-multiple-wemo-switch no-readme %}

Looks like my work is done!!! I edited the WiFi credentials to match my home network and gave the code a go. I told my Echo to discover devices and monitored the serial terminal:

![you could see a lot of UDP traffic going on](https://cdn-images-1.medium.com/max/2000/1*cHj2A_7sMtFBBbgPeWoA3g.png)

Did it work? Let’s check the app!

![w00t! on first try! awesome work @kakopappa!](https://cdn-images-1.medium.com/max/2000/1*K3-rNQQOHD2bHAJ4hYrVrQ.png)

So it found my ESP8266! The default name for the device is “box”, set by the String *device_name* in the code, but I assume it can be named anything (not sure what the rules are, probably best to stay away from symbols and numbers).

Now let’s try some commands! I hooked up a green LED to GPIO2 and yelled “Echo, box on!”

![it worksssssss](https://cdn-images-1.medium.com/max/7680/1*MCd2CU9Pwwbc-HtRJ5sd7g.jpeg)

## 4. Putting It Together

To create out IoT power switch, we just need to swap out the green LED for our solid state relay, and connect all the wires. Here’s the wiring diagram:

![Note: I couldn’t find my relay model in Fritzing, but the Sharp relay has the same connections](https://cdn-images-1.medium.com/max/2000/1*naLf9QOR8JYPTNeFEaywYg.png)

Be careful when working around mains AC! I used my glue gun to insulate all the mains connections (exposed PCB traces, wires, etc.).

Now stuff everything in your project box and make sure there are no shorts. Screw on the duplex outlet and the wall plate onto the box:

![Assembled! Looks like a plain ol’ socket (Note: the extension cord wires were not connected in this picture)](https://cdn-images-1.medium.com/max/7680/1*Sobr6ssONoL7z8lR5naZkw.jpeg)

## 5. All Done!

The solid state relay is rated for 40A so you could connect this IoT power switch to a lot of different appliances (e.g. a 12,000 BTU air conditioner draws about 10A). Mine’s connected to only 3 LED bulbs, which is well within the relay’s constraints.

The whole project cost me about $10 and 2 hours of time (thanks to kakopappa’s GitHub project!). Time to make use of my ESP8266 stash!