Introduction: Impower - the (Electric Imp Powered) Internet Connected AC Outlet
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
You can grab all of the required components from this handy SparkFun Wish List:
- 1x Electric Imp Card
- 1x Electric Imp Breakout Board
- 1x PowerSwitch Tail II
- 1x USB Wall Charger & USB Mini Cable*
*Unfortunately we can't power the imp from the PowerSwitch itself, so we need to plug it in externally.
- Wire Cutters/Strippers
- Soldering Iron + Solder
- Small Flat Head Screwdriver
- Double Sided Foam Tape
Step 2: Building the Hardware
This is an incredibly easy hardware build..
- Cut two lengths of wire that are each about 4" long and strip about 1/8" off each end*
- Solder one of the wires to the April board's Pin9 pin.
- Solder the other wire to the April board's GND.
- Screw the other end of the Pin9 wire into terminal 1 of the PowerSwitch.
- Screw the other end of the GND wire into terminal 2 of the PowerSwitch.
- 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:
- Head over to ide.electricimp.com and create an account (if you don't already have one)
- Download the Electric Imp developer app on your iOS or Android device (App Store | Google Play)
- Sign into the Electric Imp app with the developer account you created in (1)
- If you are using an Android device, click "Configure an imp" (if you're on an iOS device, skip this step)
- If you see the SSID of the network you want to connect to, click it
- otherwise click "Other Network.." (iOS) or "Change Network" (Android)
- Enter the network's password
- You're just about ready for BlinkUp - but don't click "BlinkUp" quite yet..
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.
- Plug your imp into the SD socket. It should click in place.
- 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.
- Your imp should now be flashing orange to indicate it is not yet configured.
- 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).
- Hold the screen of your phone as close as possible to the imp until BlinkUp is complete.
- 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."
- Click the Gear Icon beside the pre-assigned device name
- 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."
- Hit Enter, then click "Save Changes
- PowerSwitch should show up in the left hand nav bar - click it to select the model we're working on.
- 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:
- Agent - the device's server code
- Device - the device's hardware code
- 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.
- Copy the code from powerswitch.device.nut to the device window
- Copy the code from powerswitch.agent.nut to the agent window
- Make sure you change the password on line 49 to your desired password
- 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).