Introduction: Internet Enabled Power Outlet
PowerHub Webserver Project & Internet Enabled Outlet Setup Instructions
Central data persistence hub for Internet enabled light and outlets in your home!
Step 1: PowerHub Webserver Project & Internet Enabled Outlet Setup Instructions
Central data persistence hub for Internet enabled light and outlets in your home
Flashing RaspberriPi Hard Disk / Install Required Software (Using Ubuntu Linux) Download "RASPBIAN JESSIE LITE" https://www.raspberrypi.org/downloads/raspbian/
Create your new hard disk for DashboardPI
Insert the microSD to your computer via USB adapter and create the disk image using the dd command
Locate your inserted microSD card via the df -h command, unmount it and create the disk image with the disk copy dd command
$ df -h /dev/sdb1 7.4G 32K 7.4G 1% /media/XXX/1234-5678
$ umount /dev/sdb1
Caution: be sure the command is completely accurate, you can damage other disks with this command
if=location of RASPBIAN JESSIE LITE image file of=location of your microSD card
$ sudo dd bs=4M if=/path/to/raspbian-jessie-lite.img of=/dev/sdb (note: in this case, it's /dev/sdb, /dev/sdb1 was an existing factory partition on the microSD)
Setting up your RaspberriPi
Insert your new microSD card to the raspberrypi and power it on with a monitor connected to the HDMI port
Login
user: pi pass: raspberry
Change your account password for security
sudo passwd pi
Step 2: Enable RaspberriPi Advanced Options
sudo raspi-config
Choose: 1 Expand File System
9 Advanced Options
A2 Hostname change it to "WIFI-OUTLET"
A4 SSH Enable SSH Server
A7 I2C Enable i2c interface
Enable the English/US Keyboard
sudo nano /etc/default/keyboard
Change the following line: XKBLAYOUT="us"
Reboot PI for Keyboard layout changes / file system resizing to take effect
$ sudo shutdown -r now
Auto-Connect to your WiFi
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Add the following lines to have your raspberrypi automatically connect to your home WiFi (if your wireless network is named "linksys" for example, in the following example)
network={ ssid="linksys" psk="WIRELESS PASSWORD HERE" } Reboot PI to connect to WiFi network
$ sudo shutdown -r now
Now that your PI is finally on the local network, you can login remotely to it via SSH. But first you need to get the IP address it currently has.
$ ifconfig Look for "inet addr: 192.168.XXX.XXX" in the following command's output for your PI's IP Address
Go to another machine and login to your raspberrypi via ssh
$ ssh pi@192.168.XXX.XXX
Step 3: Start Installing Required Packages
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install vim git python-requests python-gpiozero python-smbus i2c-tools python-imaging python-smbus build-essential python-dev rpi.gpio python3 python3-pip libi2c-dev python3-spidev python-spidev
Update local timezone settings
$ sudo dpkg-reconfigure tzdata
select your timezone using the interface
Setup the simple directory l command [optional]
vi ~/.bashrc
add the following line:
alias l='ls -lh'
source ~/.bashrc
Fix VIM default syntax highlighting [optional]
sudo vi /etc/vim/vimrc
uncomment the following line:
syntax on
Install i2c Python Drivers
Step 4: BUILD THE WEB API
The following instructions will allow you to build the central hub that explains to the various wifi enabled outlets and wall switches you may build, which devices are switched on and off. This is a small PHP script to place on a webhost of your choosing. It has the following API to get and set information to it. Note: don't forget to include the .htaccess file for proper URL routing to take place.
Install the "index.php", "values/" folder and settings.php (configured to your own values) to a PHP enabled webserver of your choice. The secret key value in the "settings.php" file must match the secret key value you would set in the "settings.py" file. This will on a basic level prevent any other web traffic from setting / writing values, turning on your devices, you now need this hashed API key in the HTTP request header to do so.
The API supports the following features, for this project we'll be using only the "flag" values which are boolean values that tell the switch to turn on and off. Perhaps if you wanted to take advantage of the "reading" values you could have it so if a room temperature gets too hot by saving a temperature there, it could turn on a window fan, etc.
Get the Code
Clone the following project from GitHub to get the code to run the webserver and the outlet RaspberryPI.
https://github.com/khinds10/PowerHub
API Endpoints when running on webserver
http://myhost/message (get the current message set)
http://myhost/message/set (HTTP POST a raw string value to this URL to set a new message)
http://myhost/flag/{id} (get the current boolean status of the flag by integer: {id})
http://myhost/flag/all (get all the current boolean status of all the flags as an array)
http://myhost/flag/{id}/set (set the current boolean status to 'true' for the flag by integer: {id})
http://myhost/flag/{id}/unset (set the current boolean status to 'false' for the flag by integer: {id})
http://myhost/reading/{id} (get the current averaged value for the reading by integer: {id})
http://myhost/reading/all (get the current averaged value for all the readings as an array)
http://myhost/reading/{id}/set (HTTP POST a raw numeric value to this URL to add a new value to the current calculated average) -- see below for how many values in total are compiled to the average value Configuation The 'readings' values are calculated as averages of a certain number of recent persisted reading numeric values. Set the following constant to how many of the most recent readings should be included to produce the average.
$readingsAverageLimit = 5;
Datastore Server will persist values to simple files located by naming conventions below. Note: {id} will be replaced by the real integer presented by the incoming request's URL.
$valueFileFolder = 'values' (name of the folder to contain the measurement files) $messageFileName = 'message.msg' (name of the message text file) $readingsFilesNames = 'reading{id}.avg' (name of the CSV averaged readings file) $flagFilesNames = 'flag{id}.flg' (name of the boolean flag value flag file)
Make sure all the value files are writable by the system
chmod 777 values/*
Step 5: Supplies Needed
1 inch hole saw (for a drill)
5V 2 Channel Relay Module
Duplex Receptacle Wall Plate
Duplex Receptacle
Pi Zero W
Surface Mount Backbox Dual Gang
White Illuminated Push Buttons
Step 6: Building the Outlet
Break Outlet Tabs
Break the tab on the side of the outlet to have each socket turn on/off independently
Step 7: Install Buttons
On the Outlet faceplate using the 1" hole saw cut 2 holes for the buttons to be placed next to the outlets
Test Sizing of components
Make sure all the components will fit in the outlet surface mount box
Step 8: Wire Relay
Wire the Relay to the outlet, from the extension cord going to the wall, connect the ground (green) and neutral (white) directly to the outlet
For the hot (black) connect them to the relay then from the relay to each outlet hot connectors individually
Step 9: Additional Wiring and Assembly Images
Step 10: Wiring Diagram
Use the diagram to correctly wire the components to work with the RaspberryPI Python code.
Step 11: Test Dry Run
After final assembly before screwing the hole unit together, perform a dry run of the device to make sure there's no shorts, bad wiring
Step 12: Set the Startup Scripts
crontab -e
add the following lines
@reboot python /home/pi/PowerHub/device/outlet/buttons.py
@reboot python /home/pi/PowerHub/device/outlet/relay.py
Step 13: Finished!

Participated in the
Indoor Lighting Contest
6 Comments
3 years ago on Step 13
Unless I'm mistaken you have high voltage and low voltage in the same box, highly illegal set up, NEC specifies you cannot do that without a divider of some sort to divide high voltage and low voltage so they don't come in contact with each other or Arc. And It looks like you're having to use an outside source for your low voltage which is also ran through the same hole as your high power. Again and National Electric Code violation and fine not to mention can be deadly.
Reply 3 years ago
Wouldn't that only apply if you were a contractor or some sort of professional? I thought when it comes to home wiring projects and/or diy nec doesn't apply. Also, would a simple plastic divider satisfy the hi/low code requirements?
And it has been a while since I have worked as an electrician since what I do now pays more but do products like the following violate code?
https://www.homedepot.com/p/TruePower-15-Amp-AC-Wa...
Reply 3 years ago
If your house catches fire and they find wiring like this in it, insurance is unlikely to pay for the damages. That's why the codes exist, to prevent injury and loss of property. The plug you linked is probably UL listed which means it has been tested and met all the safety requirements. If a UL listed product fails and causes a total loss insurances normally have no problem paying the claims.
My late uncle was a State Farm agent until he retired and said home DIY projects (not just electrical, but unapproved construction work as well) is one of the biggest sticking points they run into when coverage is affected.
Reply 3 years ago
Yes a plastic divider would suffice keeping your low voltage and high voltage separate and running your low voltage through another hole into your outlet. And the plug that you sent me a link to I'm sure is separated by the case in the back. As well plus a shutter mechanism on outside that helps with low voltage and high voltage separation. Alot of the rules of the NEC have been put there because someone has died. "It's like changing a tire but not tightening the lug nuts something's going to happen" quote from my old boss.
3 years ago
A great addition would be rgb (or just white/red) variable lighting to visually indicate power draw. I would love to have these in every outlet in my home. I could see an Ethernet port at every (or most) power connections being useful as well. Just my two cents.
Reply 3 years ago
hey cool! I really wanted to use this to turn off air conditioners based on time and weather being home or not. However scared that's too much and would cause something to start smoking. Not sure if you're familiar but supposedly it's good up to 10amps. Have automatic window fans hooked up now ;)