-
Notifications
You must be signed in to change notification settings - Fork 0
/
light controller.py
66 lines (51 loc) · 1.38 KB
/
light controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This code created to run the L298N H bridge
# Put jumper over enable pins (sideways pins)
# Else, use PWM on those pins
# IN1 - IN4 are set high to make motor x go direction y
# 12v is where power goes in. Remember common grounds
# Import required libraries
import sys
import time
import RPi.GPIO as GPIO
import urllib2
# Use BCM GPIO reference (physical pin #)
mode=GPIO.getmode()
# Stop showing errors
GPIO.setwarnings(False)
# Define motor pins
waterLight=38
dirtLight=40
# Set GPIO pins as out
GPIO.setmode(GPIO.BOARD)
GPIO.setup(waterLight, GPIO.OUT)
GPIO.setup(dirtLight, GPIO.OUT)
# Gets time from this website
def getTime():
pageURL = urllib2.urlopen("http://just-the-time.appspot.com")
timeDate = pageURL.read()
timeDate = timeDate.replace('-',' ')
timeDate = timeDate.replace(':',' ')
time = timeDate.split()
return time
# Turn lights on
def onLights():
GPIO.output(waterLight, GPIO.HIGH)
GPIO.output(dirtLight, GPIO.HIGH)
# Turn lights off
def offLights():
GPIO.output(waterLight, GPIO.LOW)
GPIO.output(dirtLight, GPIO.LOW)
# Iterates forever
while 1:
timeNow = getTime()
# Keeps lights on for 18 hrs per day
if int(timeNow[3]) < 6 :
offLights()
print "Lights off"
else:
onLights()
print "Lights on"
# Wait before checking time again
time.sleep(100)
print "Cleaning up"
GPIO.cleanup()