DEV Community

w4ilun
w4ilun

Posted on

Building an IoT power switch with the ESP8266 (and control it with your Amazon Echo!)

(Note: these posts are migrated from my previous medium.com blog)

*Cut to the chase, show me the build!

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

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

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:

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/)

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, Web Server, Neopixels, Firmata (hence, Johnny-Five!), 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

I didn’t buy any of these because I still had 10+ OG ESP8266s, but they look awesome!

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 smart bulbs stopped working :(

The smart home kit I bought. These bulbs use ZigBee with HA profile

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

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

Electronics

Hardware

Tools

  • Soldering Iron

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

  • USB-Serial adapter (for programming the module)

  • Jumper Cables/Breadboard

2. Programming your ESP8266

Reference: [http://benlo.com/esp8266/esp8266QuickStart.html](http://benlo.com/esp8266/esp8266QuickStart.html)

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

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

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/)

The response looks something like this:

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 logo kakopappa / arduino-esp8266-alexa-wemo-switch

Amazon Alexa + WeMos switch made with Arduino D1 Mini

**Update** the author has an updated the repo to support multiple switches!

GitHub logo kakopappa / arduino-esp8266-alexa-multiple-wemo-switch

multiple belkin wemos switch emulator using ESP8266

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

Did it work? Let’s check the app!

w00t! on first try! awesome work @kakopappa!

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

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

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)

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!

Top comments (0)