-
1Step 1
Gather all materials and make sure you have plenty of time, the soldering isn't too difficult but it is quite tedious. Working with the Raspberry Pi and troubleshooting everything will also take a bit of time if you're altering my code to fit your needs and getting everything to look and work the way that you want it to.
-
2Step 2
First we'll get started on setting up the RPi, it is assumed that it already has internet access and everything will be performed headless using SSH and your RPi has a fresh image
The basics, let's make sure everything is current
sudo apt-get updates -y sudo apt-get upgrade -y
Next the essentials for our web server, well be using apache and phpsudo apt-get install apache2 -y && sudo apt-get install php5 libapache2-mod-php5 -y
Now lets create our outlet script in python so we can test our hardware as soon as we put it togethercd /home/pi && nano outlet.py
This is the code I'm currently using, paste it into outlet.py then save and exit ( [ctrl] + [X], the [Y], and then [enter] ). The two on and off functions are not needed. The onOff function is the only one used, the delays are necessary but you may want to play around to find the fastest time that works reliably for switching outlets off and on, especially in rapid succession. My outlets are numbered 1-3, 0 represents the ALL button in the array. The update server function is so that my webserver knows how to position the switches when the webpage is loaded if the switches are turned off or on using this python script and a cron job.
#!/usr/bin/env python ##Syntax: "outlet.py [all,1,2,3] [on,off]" import RPi.GPIO as GPIO import time import sys import os ##format: [outlet number (0 = all),pin to turn ON, pin to turn OFF,output format to use] outlets=[[0,7,11,0],[1,18,22,1],[2,15,16,1],[3,12,13,1]] output=[["Turning "," outlets "],["Outlet "," is now "]] # turns pin on and then off for specified time def onOff(pin,on,off): GPIO.output(pin,GPIO.HIGH) time.sleep(on) GPIO.output(pin,GPIO.LOW) time.sleep(off) return # only turns pin on def on(pin): GPIO.output(pin,GPIO.HIGH) return # only turns pin off def off(pin): GPIO.output(pin,GPIO.LOW) return #updates webpage with latest state def update_server(outlt,state): if (state == "on"): state = 1 else: state = 0 if (outlt == "all"): for i in range(len(outlets)-1): command="echo "+str(state)+" > /var/www/html/perm/outlet_"+str(i+1)+".txt" os.system(command) else: command="echo "+str(state)+" > /var/www/html/perm/outlet_"+str(outlt)+".txt" os.system(command) return # to use Raspberry Pi board pin numbers GPIO.setmode(GPIO.BOARD) # initialize variables if (sys.argv[1] == "all"): outNum=int(0) if (sys.argv[2] == "on"): pin=int(outlets[outNum][1]) else: if (sys.argv[2] == "off"): pin=int(outlets[outNum][2]) else: outNum=int(sys.argv[1]) if (sys.argv[2] == "on"): pin=int(outlets[outNum][1]) else: if (sys.argv[2] == "off"): pin=int(outlets[outNum][2]) GPIO.setup(pin, GPIO.OUT) onOff(pin,.45,.05) update_server(sys.argv[1],sys.argv[2]) print output[(outlets[outNum][3])][0] + sys.argv[1] + output[(outlets[outNum][3])][1] + sys.argv[2] GPIO.cleanup()
-
3Step 3
- Let's schedule our lights to turn off and on automatically throughout the week with a cron job, before we schedule anything lets make sure our timezone is correctly set with
sudo raspi-config
The timezone settings are under internationalization options, now onto creating our cron jobs for schedulingsudo crontab -e
If it's your first time editing cron choose your editor, I prefer nano ( press [2] )Let's schedule our outlets to all turn off at midnight Sunday-Thursday and 2 AM on Friday and Saturday because we might be staying up later on the weekends
0 2 * * 5-6 python /home/pi/outlet.py all off 0 0 * * 0-4 python /home/pi/outlet.py all off
Now let's have them automatically turn on at the time we would like to get up on the weekdays, we'll assume 7 AM0 7 * * 1-5 python /home/pi/outlet.py all on
The python script I wrote allows us to control all of the outlets at once or individually, let's say one outlet controls a light near the front door and we would like it to be on when we get home from work so it won't be completely dark, we'll do 5:30 PM outlet #3 turns on only on week days again
30 17 * * 1-5 python /home/pi/outlet.py 3 on
-
4Step 4
<<<< Assembly Instructions of the breadboard circuit start here >>>>
-
5Step 5
<<<< Using outlet.py to turn outlets off and on individually or all at once >>>>
-
6Step 6
<<<< Setting up the webserver with scripts >>>>
-
7Step 7
<<<< Using the web interface on your local network >>>>
-
8Step 8
<<<< Connection methods to access it globally via the internet, vpn recommended >>>>
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.