Tuesday, December 10, 2013

electric imp-Controlled 120VAC Relay Switcher

     I bought an electric imp about a year and a half ago when they were released. I loved it's simplicity and made a few temporary projects involving it but wanted to keep it for something more advanced. After all this time, I decided it's better just to use it for anything rather than leave it just sitting in a box in my room, so I've made an internet-controlled 120VAC relay switcher for it and an "app" for my iPhone to switch it remotely. I'm sure there are other ways of accomplishing this project, but I was able to build the whole thing using parts I already had on hand. This blog post will be a high-level overview of what I did to make it.

Update: I've updated the software as shown in this blog post.


     The electric imp code is heavily based on this example from the Getting Started pages of the documentation. I used an electric imp and the electric imp breakout for the brains of the operation. Since the electric imp is a 3.3V device and the only relay I had that could switch 120VAC was 5V, I used an N-channel MOSFET for the voltage converter. The MOSFET was a FQP30N06L and I used the electric imp's 3.3V pin9 at the gate to switch it. Since I am switching an inductive load, I also put a diode in parallel with the relay coil to prevent the voltage spike from damaging the other circuitry. I have a momentary push-button switch attached to pin1 that allows me to switch the circuit on and off from the device, instead of having to get my phone out. This circuit is powered from a 5V AC-DC adapter that was removed from it's outer casing.

Edit: As Perry mentioned in the comments, switch the black hot line, not the white neutral line. I've updated the diagram below to show this, but the photographs still show me switching the white line.

AC circuitry

DC circuitry

AC lines in and out, power supply, relay and electric imp

MOSFET and large resistor to pull gate to ground

     The electric imp, relay, and power supply are all mounted in a 3D-printed case that allows the electric imp's LED and photodiode to be accessible from the side (to see status from the LED and to configure the Wi-Fi network via BlinkUp) and the push-button to be accessible from the top. The lid for this box is friction-fit and actually makes up the bottom of the box when it is assembled.

Case with slot for electric imp and divots for AC lines

Button hot-glued in place

electric imp in place

All circuitry jammed in with the lid beside

electric imp sticking out the front and button on the top

     I don't have an Apple computer so I couldn't make a native app for the switching, but I used the amazing iWebKit to make a webapp. There is an excellent tutorial included in the download that got the app up and running very quickly. There are 3 pages to this webapp. The first one is a main page that gives the option to either turn on or turn off the relay. Each of those pages does the corresponding action via an httpGet() to the AgentURL with a parameter that tells the imp to switch it on or off (On a side note, I tried to modify the app so that it checks the response of this httpGet() and displays whether or not there was an error. After a few hours of research, I found the only way to do this would be to send a response from the electric imp to a server and then read the status from the server. This was more complicated than I wanted to deal with, so I'll just assume it always works. If there's a problem, it's not like this is an essential piece of equipment anyways.) Each of those web pages also has a link to the opposite action, so if you are on the "turn on" page, there will be a link to turn it off. I also created a simple icon for this app by searching for a icon-sized royalty-free image on Google.

"Relay Swtr" app, index.html, on.html, off.html

The source code for the electric imp and iPhone webapp is located on my Github here. The files for the 3D-printed case are located on my Thingiverse here. Additional photos can be found in my Google+ album here.