Building a WiFi Outlet
Using a Spark Core and a relay to build a WiFi controlled outlet.
May 19, 2014

After months of waiting, my Spark Core finally arrived. It’s basically an Arduino board with on-chip wifi, and an HTTP endpoint. I no longer need to [hack my own web API]](/posts/hal) to an Arduino using a Raspberry Pi, a serial connection, and a Flask web server - now I could build a self-contained project using a single board at the very affordable cost of US$39.

With summer around the corner and my recently-ordered air conditioner on a UPS truck, I decided to build a wifi-enabled outlet with my Spark Core. This way I’d be able to power on the AC on while walking home on a hot day, or turn on a reading light without sitting up. (sometimes I am that lazy)

photo of outlet complete
The Hardware

I realize that there are premade solutions that do exactly what I’ve built, but what’s the fun in buying something that you could build better yourself? I tried out one of Belkin’s “WeMo” wifi-enabled light switch and let’s just say - never again. With minimal parts and a little bit of solder, I went about building my own solution. The parts needed ended up costing about $55 bucks total (give or take the supply and demand of electrical stuff on Amazon) - really not too much more than a premade solution and with a lot more freedom.

The Spark Core acts as the controller for the project and powers devices on an off via the relay. The relay’s purpose is to give us the ability to control a high-voltage AC (alternating current) like what comes out of a wall via a low voltage DC (direct current) like what we get from an Arduino or embedded system.

Originally I thought I would be able to drive the relay with the 3.3V Spark Core, but as this particular relay requires 5V to operate, I had to use a little transistor magic to make things work. A transistor is similar to a relay in that it allows one voltage to manipulate another however it differs in that it is DC to DC whereas a relay is DC to AC; in my case, I used the Spark’s 3.3V output to control the flow a of 5V current to the relay’s input.

photo of outlet circuit

In addition to the two outlets, I’d also need to provide USB power to the Spark Core, and drive the voltage needed to control the relay. At first I had two USB cables coming out of the enclosure - one to power the Core and one to provide the 5V needed for the relay; it worked fine, but it looked bad and it really comprimised the self-contained aspect of the system. I ended up internalizing the 5V power by stripping apart an old USB power adapter, and soldering up AC wire from the power cable as well as an additional two wires to the back of where the USB port was soldered to the board on the +/- power pins.

photo of outlet disassembled
photo of sparkcore

Fitting everything in the enclosure was a little bit tricky, but definitely doable. I left my Core in a mini breadboard as opposed to soldered directly to other the other components as I may add new components to the system (such as buttons to manually trigger actions).

photo of outlet front open
photo of outlet back open
The Software

The software end of this project is really quite trivial. The Core comes with a preinstalled software called Tinker that allows users to make simple POST requests to the Core to set pins HIGH or LOW.

curl -d access_token=ACCESS_TOKEN -d params=D0,HIGH

I added some code to my existing home-automation webapp and now with the press of a button I can power on my AC or whatever else I’ve got plugged into the outlet.

Future Improvement

There are a few things I’d like to do to improve the system. From a software perspective, it’d be nice to be able to be able to do a few more things with it. For one I want to be able to get the status of a pin after I set it, but for now, the Spark Core API doesn’t allow you to read a pin while applying a voltage to it, so for now my only option is to keep track of the status in the Flask app itself.

It’d also be nice to be able to schedule tasks or trigger them asynchronously. I’ve also been thinking about adding a temperature and humidity sensor and controlling the AC based on the current room conditions à la Nest. Perhaps a job for a long weekend.

photo of outlet angle