Introduction: Impower - the (Electric Imp Powered) Internet Connected AC Outlet

About: Lead Developer Advocate at Pebble (previously ran community at Electric Imp). I love coffee, robots, and building hardware that lives on the web!

This Instructable will show you how to hook a PowerSwitch Tail II up to an Electric Imp and build a simple webpage for controlling it!

The PowerSwitch allows you switch AC power with an opto-isolated control circuit. This means that the wires you plug into the PowerSwitch are not directly connected to the AC power. In other words, this is a very simple, and very safe way of controlling devices plugged into AC power.

This is a really quick and simple project; it should take you anywhere from 30 minutes to about 2 hours depending on your level of experience with soldering, electronics, and programming.

If you do not have access to a soldering iron, I encourage you to seek out a nearby hackerspace!

Step 1: Tools & Components

Components:

You can grab all of the required components from this handy SparkFun Wish List:

*Unfortunately we can't power the imp from the PowerSwitch itself, so we need to plug it in externally.


Tools:

Step 2: Building the Hardware

This is an incredibly easy hardware build..

Build it:

  1. Cut two lengths of wire that are each about 4" long and strip about 1/8" off each end*
  2. Solder one of the wires to the April board's Pin9 pin.
  3. Solder the other wire to the April board's GND.
  4. Screw the other end of the Pin9 wire into terminal 1 of the PowerSwitch.
  5. Screw the other end of the GND wire into terminal 2 of the PowerSwitch.
  6. Attach a piece of foam tape to the bottom of the April board, then stick it to the top of the PowerSwitch.

*I used a blue wire to data (Pin9), and a black wire for ground (GND), but you can use whatever colour wire you like!

That's it - you are now done the hardware part of this build! You may want to plug a lamp into the PowerSwitch Tail for testing purposes later on.

Step 3: Configure Your Imp

Now that we're done building the hardware, it's time to get connected! Getting connected with Electric Imp is a flash (pun intended):

Create account and get ready for BlinkUp:

  1. Head over to ide.electricimp.com and create an account (if you don't already have one)
  2. Download the Electric Imp developer app on your iOS or Android device (App Store | Google Play)
  3. Sign into the Electric Imp app with the developer account you created in (1)
  4. If you are using an Android device, click "Configure an imp" (if you're on an iOS device, skip this step)
  5. If you see the SSID of the network you want to connect to, click it
    • otherwise click "Other Network.." (iOS) or "Change Network" (Android)
  6. Enter the network's password
  7. You're just about ready for BlinkUp - but don't click "BlinkUp" quite yet..


BlinkUp:

Electric Imp uses a process called BlinkUp to configure imps. The BlinkUp process will flash your phone's screen at 60hz for about 10 to 20 seconds to optically transit the network's credentials. If you are sensitive to to bright, flashing light you should get someone to help you with the process. Additionally, if your phone's sound is turned on, chimes will play at the beginning and end of the process so you can look away during BlinkUp.

  1. Plug your imp into the SD socket. It should click in place.
  2. Power the April board by plugging the USB Mini cable into the imp and the wall socket, then plugging the wall socket into an AC power outlet.
  3. Your imp should now be flashing orange to indicate it is not yet configured.
  4. While the orange light is still flashing, click the BlinkUp button in the Electric Imp developer app.
    • The imp turns its status LED off after 1 minute. If the status LED is no longer blinking, you need to power cycle the imp before continuing (unplug the imp, wait a second, then plug it back in).
  5. Hold the screen of your phone as close as possible to the imp until BlinkUp is complete.
  6. The imp will go through a series of blink codes (a complete list can be found here), then blink green once it has connected.

The status light will turn off after 1 minute of being connected to conserve power, but don't worry, the imp is still powered on and connected.

Step 4: Test Code

Time to write some basic software, and make sure we hooked our hardware up correctly!


Create a New Model

The first thing we need to do is create a new model. A model is an Electric Imp project that consists of device code (code that runs on the hardware), and agent code (code that runs in Electric Imp's cloud).

If you successfully connected your device in the previous step, it should be listed under "Unassigned Devices."

  1. Click the Gear Icon beside the pre-assigned device name
  2. In the Device Settings dialog, click on the "Associated model" drop down, and start typing a name for your project. I'm going to call mine "PowerSwitch Tail II."
  3. Hit Enter, then click "Save Changes
  4. PowerSwitch should show up in the left hand nav bar - click it to select the model we're working on.
  5. Once you've selected the model, click on the blue "devices" dropdown below the model name, and select the device you want to work on (there should only be one).

Once you've selected your device, you should see 3 windows:

  1. Agent - the device's server code
  2. Device - the device's hardware code
  3. Logs - the device's log.


Write some Test Code

We're all setup and ready to write some code. The first thing we're going to do is configure Pin9 to be a digital output, then write it high. If your hardware is properly connected, this should turn the PowerSwitch Tail II on (you'll hear a satisfying click)!

Copy and paste the following code into the Device window, then click "Build and Run."

// device code
hardware.pin9.configure(DIGITAL_OUT);  //configure pin as digital output
hardware.pin9.write(1); // turn light on

To turn the device off, switch hardware.pin9.write(1) to hardware.pin9.write(0) and click "Build and Run" again.

Step 5: Application Code

Now that we know everything is working, it's time to grab the actual code - you can find it here.

  1. Copy the code from powerswitch.device.nut to the device window
  2. Copy the code from powerswitch.agent.nut to the agent window
  3. Make sure you change the password on line 49 to your desired password
  4. Click Build and Run - you should see a message in the Logs panel that says "Downloading new code."

Click on the Agent URL to load the agent webpage, and head on over to the next step to make sure things work!

Step 6: Testing Your Newly Connected Device

If everything is working, you should see the above webpage after clicking on the agent link. When you try to turn the switch on and off without the proper password, you should get an angry message. If the proper password was entered, you should be able to click On/Off and your device should respond.

Now that everything is working, you can put your newly connected power outlet anywhere you want (the imp will automatically re-connect to WiFi every time it boots up).

This project is designed to be fairly simple, and extendable. I encourage you to read more about Electric Imp, Twitter Bootstrap, and jQuery!