-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart-lamp.py
53 lines (44 loc) · 1.33 KB
/
smart-lamp.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
#!/usr/bin/env python
############################################
############################################
import RPi.GPIO as GPIO
import urllib2
import cPickle
import datetime
import os, time
from sklearn.svm import SVC
# setup
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
lamp = 21
GPIO.setup(lamp, GPIO.OUT)
pretrained_model_path = "pretrained-smart-lamp.model"
print "loading pretrained model..."
if os.path.isfile(pretrained_model_path):
with open(pretrained_model_path, 'rb') as f:
model = cPickle.load(f)
else:
print "Error: file '{}' not found".format(pretrained_model_path)
exit()
print("Starting...")
GPIO.output(lamp, 1)
time.sleep(1.5)
GPIO.output(lamp, 0)
time.sleep(0.2)
print("Listening...")
t = time.time()
while True:
# get the lamp status from the server
status = urllib2.urlopen("http://udevcommunity.org/uconf2-iot-test/get_lamp_status.php").read()
if (status == '1'):
GPIO.output(lamp, 1)
now = datetime.datetime.now()
predicted_delay = model.predict([now.hour])
print(predicted_delay)
time.sleep(predicted_delay)
GPIO.output(lamp, 0)
status = urllib2.urlopen("http://udevcommunity.org/uconf2-iot-test/change_lamp_status.php")
elif (status == '0'):
GPIO.output(lamp, 0)
#GPIO.cleanup()