(Note: these posts are migrated from my previous medium.com blog)
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:
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.
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 :(
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!
Solid State Relay (no clicking sounds, yay!)
Wires (for low voltage DC connections)
Electrical Box (for all the components)
Duplex Outlet (socket)
Wall Plate (to hide everything in the box)
Extension Cord (for the socket plug and mains wires)
Glue gun (for insulating any mains connections, be careful!)
USB-Serial adapter (for programming the module)
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.
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.
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)
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!
**Update** the author has an updated the repo to support multiple switches!
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:
Did it work? Let’s check the app!
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!”
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:
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:
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!