TL;DR – We built a Wifi-capable four-way mains socket using a NodeMCU ESP8266 development board, some relays, and the Arduino libraries (but not their hardware). We coded it in C++, which you’ll find on GitHub.
My son Jake and I have lately become interested in smart-home technology, probably because I’ve finally caved and welcomed Alexa (and likely the NSA) into my home. Being the kind of tech-obsessed people we are, we decided it might be fun to try and build our own smart device, and see if we could control it via the Echo.
After a bit of deliberation, we settled on the idea of building a Wifi-enabled smart socket, with the view that if we had such a thing, then all devices that connected to the mains would become smart devices! I already have Phillips Hue lighting hooked up to Alexa, and we thought it would be cool if she could also control standard mains devices like lamps. And sandwich toasters, of course.
After doing a bit of research, we settled on the parts we’d need for the build.
- A NodeMCU ESP8266 development board.
- Four Fotek SSR-40-DA relays for the switching.
- A 240VAC to 3.3VDC step-down converter.
We’d do the initial build using the development board as it includes USB to serial and other supporting stuff, so it’d make it easier to iterate our design when we got to the coding stage.
We decided we were going to put it together on a solderless breadboard to begin with, connecting everything with jumper wires as usual. I already had all that stuff lying around, and as a bonus I have a little step-down board that takes a 12V adapter and spits 5V and 3.3V onto the two power rails on the board, so as soon as the ESP and the relays arrived (from the UK) we could get started right away without waiting for the transformers (shipping from China).
Here’s the build in its first iteration, with just one relay connected:
Out of the box, the ESP8266 board supports LUA, but we decided to get rid of the standard firmware and replace it with Arduino-compatible code, meaning we could use our usual toolchain (including the excellent Sloeber eclipse IDE for Arduino) and work with libraries and languages we are already familiar with.
We found (thanks to the legwork already done in this awesome project) that the easiest way to get Alexa to talk to the device over WiFi was to emulate a Belkin smart switch, using their Wemo protocol. The design involves setting up the 8266 to run a web server that’s used to control the device, and a uPnP responder that makes it discoverable on the network.
I’m not going to go into too much detail about the code here, but if you’re interested you can find it all on GitHub. We found the ESP8266 is a joy to program, and is already well supported by the libraries (for obvious reasons, it’s a very popular board with builders like us). In the space of an afternoon we had it up and working!
Testing it, however, presented a new problem – it turns out I don’t actually own that many devices that just switch on at the mains! Everything is either set up to be switched on all the time, in standby mode (TV, PlayStation, etc), or is already smart-home enabled (All my lamps).
Fortunately, digging around in the cupboards I found this Salter sandwich toaster, which has a handy lamp indicating whether or not it’s powered. With our first test device secured, and keeping clear of all exposed 240VAC live components, we fired it up.
Opening the Alexa app on my phone and searching for new smart home devices worked perfectly. The serial logs from the board showed it was responding to a flurry of requests, and the app showed the new devices:
The test was a success (I’ll update the post soon with a video), the sandwich toaster turned on and off in response to Alexa commands (we renamed switch #1 to “Sandwich Toaster” for the test, so we could just say “Alexa! Turn on Sandwich Toaster!”).
We had a lot of fun with this project, and we’re not done yet. We’ve expanded it to fully support four switches, but it’s still not properly built. We’re on the lookout for a four-way extension with space for the electronics inside (probably with some smaller relays – 40A is overkill here anyway), and we’ve got ten more ESP8266s on the way (as mountable chips, not the dev boards). Once we’ve got all the pieces in place we’ll solder it up properly and consider it done.
Now we just need to decide what we’re going to do with the rest of those WiFi chips when they arrive. Any suggestions?