Impowered Electrical Outlets

171,788

1,434

58

Published

Introduction: Impowered Electrical Outlets

About: There's always time for one more project!

This instructable will show you how to hack an off-the-shelf power adapter to make an internet enabled power adapter using the Electric Imp. This lets you turn on or off any mains powered device plugged into the adapter over the internet using a smartphone or web browser.

My garage "as-constructed" lighting is poor. You can use the available light for finding large objects like your car, but forget about that screw you just dropped. Never gonna happen. To fix the lighting problem I bought two 4 tube fluorescent shop lights to hang above my work area. Since the plug-point is in the ceiling roughly 12 feet high, I needed a way to remotely switch the lights on and off. I found a Stanley remote controlled power outlet at a local store and I was set, or so I thought. The first Stanley remote failed on day 1. The replacement unit failed after a couple of days. Third replacement unit failed a couple of months later. I never got around to taking it back to the store - I installed pull strings for the shop lights as an old-school temporary semi-remote solution.
 
What I needed was a WeMo or similar device. A perfect solution for my needs if maybe a little overkill. But, then it hit me. Most likely the failure point with the Stanley device is the RF circuit that controls the switching of the AC. If I replaced that circuit with an Electric Imp, I wouldn't need to mess with the AC circuit design which is already UL approved for safety, and would have a nicely made enclosure to boot.

I grabbed a Philips screwdriver and opened the Stanley unit up. The insides appeared to be PERFECT for an Electric Imp (http://www.electricimp.com) retrofit with minimal effort... or so I thought. My shop lights were headed into the internet cloud and accessible from anywhere with my Smartphone or web browser. Unfortunately the plan to just replace the RF circuit with an Electric Imp hit a major roadbump because the the DC power supply on the Stanley power strip cannot deliver enough current to power the Electric Imp. The Imp requires about 400mA on WiFi transmit, so the hack turned out to be more invasive than I had intended, involving an Imp, cellphone charger and relay board all shoehorned into the Stanley enclosure.

The final solution is perfect for those times when I forget to turn off the garage lights and have to walk back downstairs to check on them. No more - just check the status on the phone and turn them off if needed. Life just got easier!

Since the Holiday season is right around the corner,  this will allow you to control your light display from wherever you and your Smartphone happen to be, even if it is just from the couch to avoid the nightly trip into your snow filled yard.... just keep waterproofing in mind if you do this so that you don't unintentionally create an electrical hazard.

The Stanley unit I used is still available from various internet sites so if you want a basic WeMo'ish like device, with complete access to the source code and electronics that you can hack to perfection yourself, this Instructable will get you there. Think of this as Open WeMo'ish!


Step 1: How It Works

The Electric Imp is the core of the system operation. When the button on the Web App is activated, an AJAX HTTP Request is made to a URL specific to your Imp. This request is sent to the Imp Agent in the Electric Imp cloud that is specifically associated with your Electric Imp.  The Agent code is a mini Web Server that parses the request and if valid, passes it on to your Imp firmware via the cloud. This behind the scenes communication between the server based Agent and the hardware based firmware was developed by the talented people over at Electric Imp.    

The Imp firmware receives the message from the cloud based Agent and sets the relay output accordingly. This allows you to turn the Stanley outlets off or on by pressing the button on your SmartPhone or web browser with minimal software and firmware development effort. Nice!



Step 2: The Stuff You'll Need

There are many variations of remote controlled AC power switches. These are very popular during the December holiday season in the USA. Most likely, any of them can be hacked in a similar fashion. This is the parts and tool list for the Stanley unit I used.

Parts
  • Stanley Remote Powered Outlet. The unit I used is still available for about $16 from 1000Bulbs.com   I like the convenience of having 3 outlets
  • Electric Imp https://www.sparkfun.com/products/11395
  • Electric Imp carrier board https://www.sparkfun.com/products/11400
  • SainSmart 2-Channel Relay Board. Has opto-isolator inputs. I bought this board for another project. At that time (August 2013) it was under $5 delivered with Amazon Prime. The current price is $9 http://amzn.com/B0057OC6D8
  • 1 x 4 Pin Molex connector. I bought mine from Jameco.com
  • 1 x 3 Pin Molex Connector. Jameco.com
  • 5 x Connector contacts for Molex KK Series connectors. Jameco.com
  • Thermistor - 10K https://www.sparkfun.com/products/250 (Optional - I still need to write the code for this)
  • iPhone USB Charger for powering the imp - Amazon has the best prices
  • USB Cable that will be mutilated to supply power to the Imp. If you get one with a small plug end (like the one that comes with the iPhone, you will be able to avoid the connector mutilation step!)
Tools and supplies

  • Philips screw driver for disassembling the Stanley unit. Needless to say your warranty is void as soon as you do this
  • Soldering Iron and solder- Radioshack has a variety
  • 24 or 26 or 28 Gauge hookup wire for connecting the electric imp I/O to the relay. Radioshack has different colors of 24 gauge available http://www.radioshack.com/product/index.jsp?productId=21982396http://www.radioshack.com/product/index.jsp?productId=21982416&locale=en_US
  • Sharp utility knife
  • Small electrical tools (wire cutter, wire stripper etc)
  • Small drill bits (1/16") and drill for making the Imp Slot in the side of the Stanley unit. Alternately a Dremel tool with appropriate cutting bit.
  • Hot Glue Gun and Glue Sticks for mounting the Electric Imp and the Relay within the housing. I used the high-strength type..

Remember, as soon as you open the Stanley Remote AC Outlet switch, your warranty is void. Also, make sure you work with appropriate safety procedures for main powered devices. Unplug the Stanley unit before working on it. While it is unplugged, you can work on it safely without fear of accidentally shocking yourself.  If you attempt to work on the unit while it is plugged in, the live voltages (115VAC) inside the Stanley unit are enough to kill you. Never work on the internals of the Stanley unit when the cover is removed and the unit is plugged in. Be safe.

For tools use standard DIY safety procedures and always wear safety glasses.





Step 3: Disassembly and Modification

The Stanley unit is opened by removing the 5 screws on the back side using a small Philips screwdriver. One of the screws may be hidden underneath a sticker. Once all 5 screws are out, gently ease the two halves apart.

Remove the remote control PCB and dispose of it. We won't be using it again. This is where the Imp will be installed.

There are 2 large plastic bosses molded on the lower plastic housing that the original RF board was mounted to. These bosses can be cut off with a pair of side cutters and be smoothed down further with a dremel tool. The Imp and relay circuit boards will be glued to the base. Removing the bosses is needed so that the relay board will mount low enough to clear the top cover when the Stanley unit is reassembled.

Remove the main circuit board from the shell. Using a Dremel tool with a cut-off disk, carefully cut around the existing circuit so that you leave only the AC part of the circuit board as shown in the pictures. Cutting the PCB is necessary to provide clearance for the new relays that will switch the outlets on and off. You want to leave the main AC tracks that support the power outlets. 

To provide a slot for the electric imp card, a slot needs to be made in the side of the housing. Initially I had intended to use the Stanley unit built in regulator to power the imp. When this proved to be inadequate power, I had to relocate the slot to the opposite side of the lower housing. To make the slot, drill a number of 1/16" holes in roughly a straight line, and then use a small file to square up the slot.  Don't make your slot where these pictures show..... look at the final pictures in the last step to see where the slot should go!



Step 4: Electrical Detail and Assembly

The original relay in the Stanley unit was equipped with a 250VAC/10A relay that required a 12V coil voltage to switch. Difficult to actuate from a 5V circuit so it was replaced with a dual relay module.

Please make sure the Stanley unit is not plugged in before you start work on it. 

The relay board I used has two 10A relays that will switch with a 5V coil voltage. In order to ensure a 15A rating of the hacked unit, I used the relays in parallel to provide a load rating of 20A. This provides a decent safety factor for a power adapter connected to a standard 15A breaker protected home wiring circuit. If you use a higher current relay, you could switch the power outlets individually. If the power strip is rated at 15A, then any of the outlets must be safe to operate to 15A. By wiring the relays specified for this instructable in parallel, you can ensure that any single outlet or all 3 outlets together can draw 15A safely. More load than this and the circuit breaker in your home wiring panel will trip to protect the circuit.

The power for the electronics is supplied by a iPhone USB charger. I had a spare that I could use. They are inexpensive and available on Amazon and ebay. It is cheaper to buy the charger which comes with a USB cable than to buy the components to build your own power supply. The iPhone form factor works well in the limited interior space of the Stanley unit. Use spade terminals to connect directly to the AC terminals of the charger. Use heatshrink to cover the exposed parts of the AC terminals to prevent accidental contact. The USB cable I found in my project box had a bulky molded connector. Using a sharp knife, the plastic moldings were removed. The red and black wires of the USB cable where replaced with 26 gauge wire to provide more mechanical robustness and then the connector was further protected using heatshrink as seen in the pictures. 

Originally, I had intended to switch the relays independently so that I could control at least 2 outlets independently. That is how the circuit was originally wired up. After investigating the current carrying capacity of the relays, I chose to parallel the relay outputs. The connection between the Imp and the Relay board has two Imp pins controlling each relay. In reality, you can just connect one Imp pin to both relay inputs. The firmware switches both pins together so either way is fine.

The AC Neutral and the AC Ground is already connected to the electrical outlet sockets. Leave everything connected the way it comes from the factory.  The only wire that needs to be modified is the AC Live wire (Black Wire). This wire needs to be t'd or branched so that you have a live wire entry to each relay as shown on the schematic. Protect the join with heatshrink. I used a small piece of 16AWG wire for this branching. 16AWG multistrand wire can typically carry 22Amps when used for short haul chassis wiring.

The relay outputs are soldered to the underside of the AC PCB as shown in the picture. When all the connections are made, squeeze everything into the housing as shown and hot-glue the relay and imp boards to the housing base.







Step 5: Imp Firmware,Agent Code and Blink-up

In order to get your imp to work, it needs to connect to your wireless network. Electric Imp provides a tool that does the configuration optically. The process is called BlinkUp and is detailed here https://electricimp.com/docs/gettingstarted/1-blinkup/

Once the BlinkUp is complete, your imp will appear on your IDE under New Devices. If you have not used an Electric Imp before, I suggest you first try the Hello World example on the Electric Imp site to get the hang of the Imp. There is also a great guide to the imp on instructables written by @beardedinventor: https://www.instructables.com/id/Getting-Started-with-Electric-Imp/

The firmware for the imp is attached to this step as ImpoweredImp.nut
The code for the agent is attached as ImpoweredAgent.nut

Firmware and Agent code interaction
Your Agent has a URL specific to it. When an HTTP message is sent to this URL, your Agent code running in the Imp Cloud checks the validity of the request by comparing the incoming API Key (any key you make up - more bits make it harder to guess) with the API Key stored in memory of the Agent (API Key idea from http://forums.electricimp.com/discussion/comment/8281#Comment_8281). If the Key's match, the message is checked to determine if a buttonPress event or Status request is being passed in the HTTP Request packet.

If the HTTP Request is for status, the Agent code returns the On/Off status of the AC outlets. The status is asynchronously updated by the Imp firmware when the output pins change state.

If a buttonPress event is passed in the HTTP Request, this event is passed to the Imp firmware over the electric imp secure communication channel between the Imp and the server based Agent. The Imp "receives" this event through the agent.on("buttonPressed", function( value ) function. Every time a button press event is received by the Imp, the output state of Pin1 and 7 is toggled which turns the relays On or Off depending on the previous state.  The status variable is sent back to the agent via the Agent-Imp communication channel and is received by the Agent in the device.on function.



Step 6: IPhone HTML/Javascript Code

The client app is a bare bones HTML/Javascript app. A simple image is used as a button. When the image is pressed, the button animates to show WiFi packets emanating from the device.

Communicating with the Electric Imp is done by sending a message to the URL of the Agent which is specific to your particular Imp. In the HTML App, we create an XMLHttpRequest object and then post the button press event in a formatted JSON packet to the agent URL. 

xmlhttp.open("POST", "https://agent.electricimp.com/YourURL?timestamp=" + new Date().getTime(), true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("x-apikey", "Your API Key");
var impRequest = { "request": "buttonPressed", "button" : button.id  };
xmlhttp.send(JSON.stringify(impRequest));

To get status of the power outlets, the Agent is queried

xmlhttp.open("POST", "https://agent.electricimp.com/YourURL?timestamp=" + new Date().getTime(), true);
xmlhttp.setRequestHeader("x-apikey", "Your API Key");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
               
var impRequest = { "request": "GetStatus" };
xmlhttp.send( JSON.stringify( impRequest ));


To make the HTML page appear as a native App on your iPhone, you need to load it from a web site, and then add it to your home screen. The process is described in my previous instructable here: https://www.instructables.com/id/Electric-Imp-Garage-Door-Opener/step10/Configuring-IIS-Express-And-Loading-The-App-onto-y/

The iPhone files are attached to this step

Step 7: It Works!

After all the building, configuring and maybe even a little bit of cursing, you should have a WeMo'ish device that you can control from your iPhone/Smartphone/Web Browser from anywhere you happen to be. There are many uses for an internet enabled outlet that makes your home that much smarter. 
  • Remote control of your in-house devices like lamps, hi-fi, TV etc. You can enhance the software and build multiple devices to allow you to control the lighting of your home to make people think that the house is occupied while you are away. You can turn your TV on and off to add to the illusion. You could enhance the firmware and smartphone software to program each device to run on a different on/off schedule to simulate the random nature of real people within your home to complete the illusion for security purposes when you are away.
  • You can add remote control for difficult to access power points.... my garage lights for example!
  • You can see the status of your outlets to answer the questions that always seem to come up after you are already many miles into a trip away from home like "honey did you turn the iron, hair hot stick, fish tank filter, pool filter, light timers" on / off depending on the nature of each device.
  • Add motion sensor to automatically turn things on/off when motion is detected.
  • Add a Thermistor or other temperature sensing device to monitor the room temperature. 
  • Figure out how to connect to IFTTT.com so that you can, for example, email, FB Message, Tweet your outlet to turn on /off
  • Please comment below and I'll add your ideas to this list!

If you build one, please add to the comments and let me know of any additional features you added or alternative power outlet devices you hacked to achieve the same end. Also, if you figure out how to IFTTT this thing, I like to know how too!

Cheers!




Hardware Hacking

Second Prize in the
Hardware Hacking

Microcontroller Contest

Second Prize in the
Microcontroller Contest

Share

    Recommendations

    • Oil Contest

      Oil Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Clocks Contest

      Clocks Contest

    58 Discussions

    Wrong label after relay (AC Live).

    It should be cheaper use spark core instead electric imp.

    It should be cheaper use spark core instead electric imp.

    I see good opportunity connect this project to smartthing.

    I bought several of the wireless powered outlets a while back, deciding they weren't right for the project I had in mind... but you've given me many ideas with this Instructable. Thanks much!!

    1 reply

    Whith my first imp project i need a little help.

    Can you tell me why i get the message: ERROR: the index 'x-apikey' does not exist

    below my logging

    2014-10-25 09:52:35 UTC+2[Device]imp.configure command is deprecated2014-10-25 09:54:54 UTC+2[Agent]url and api: https://agent.electricimp.com/uct-1Our2-6q2014-10-25 09:54:54 UTC+2[Status]Device Booting; 3.09% program storage used2014-10-25 09:54:54 UTC+2[Device]Impowered2014-10-25 09:54:54 UTC+2[Device]imp.configure command is deprecated2014-10-25 09:56:44 UTC+2[Agent]HTTP Request Headers: (table : 0x7fc0ccc58880)2014-10-25 09:56:44 UTC+2[Agent]HTTP Request Body: 2014-10-25 09:56:44 UTC+2[Agent]HTTP Request Method:GET2014-10-25 09:56:44 UTC+2[Agent]ERROR: the index 'x-apikey' does not exist2014-10-25 09:56:44 UTC+2[Agent]ERROR: at webServer:26
    2 replies

    Hi, sorry I missed this message. Send me your email address via a PM and I will help you find the problem. All I can think of from the description is that you did not copy all the code properly into the agent.

    After putting in the APIKEY and the IMP id in the right places, it all works fine.

    Thanks for your help.

    I am working on this project and have a question.

    I have finished the hardware section, successfully blinked the imp to my home wifi, inserted the code into the Agent and Device sections of Electric Imp's IDE page (and sucsessfully built and ran the software).

    I am having problems getting the cellphone code/app to work. Is there any section in any of the code that I need to insert the url for my imp? Do I need any web hosting services or to publish the html code to get it to run on my iPhone? I have very limited code writing experience (only MatLAB in college) and am stuck. Thanks for any help!

    4 replies

    Jeremy, you do need to modify the HTML code. Search for a line:

    https://agent.electricimp.com/YourURL

    and then replace the YourURL text with the actual URL for your imp. This is typically a crytpic set of characters like c8fheu439

    You find this in the Imp IDE near the top of the screen. There's a link called "Agent link" CLick ti to expose your unique agent URL.

    The other change you need to make is to the API Key, This can be any text you want as long as the text in the Agent matches the text in the HTML file.

    To get the code loaded on your iphone, you can load the HTML and manifest file from any web server. Once cached to the phone, you no longer need the external web server.

    Hope that helps you move a step closer to completion!

    Thanks for the help! When I make those changes to the coding and save the HTML file it appears to work correctly when I launch it on my desktop computer. However, it is not supplying power to the device I have plugged in.

    I was looking over my hardware and the only thing I can think of is the single wire connection from the JD-VCC pin on the relay board to the Electric Imp breakout board. Where exactly did you make the connection on the Imp board? The circuit diagram at the top of page 9 looks like it is tied into the +5 Volts DC line from the USB charger but the picture in the middle of page 10 looks like it is connected to the battery pin of the board.

    I followed the picture on page 10 and have the wire connected to the battery pin (I still have the bridge attached to the other 2 pins right next to it). Should I change this and solder the wire to the +5V DC wire going into the Vin connection on the Imp breakout board?

    Once again thanks for your time!

    Jeremy, look at the schematic at the start of step 4 and just follow that. DC+5V goes to VIN on the Imp which goes to JD-VCC. It is the common 5V line.

    Hey,

    Just curious, is it possible to control multiple imps from the same web address/page? I am trying to build something to control multiple devices nearby. This tutorial is one of the more simple ways I thought about going about this. Any input would be greatly appreciated! :)

    1 reply

    Yes it is. Each imp has a unique Agent URL by design. To address an imp you send a message to that unique URL. Your client side HTML page can incorporate a separate button for each imp for example. So you would load a single html page which addresses multiple imps. If you had 5 imps controlling 5 different outlets for example, you would have 5 on off buttons on your html page and each button would be associated with a specific imp URL.

    @midnight thanks mate, I looked at for like two hours trying to figure it out from the comments and photos..lol. Great projects, I am hoping I have the skills to pull it off, if I should have any questions is it ok to ask you sir?