forked from nstrain/cs326final
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraspberryPi.py
133 lines (117 loc) · 3.55 KB
/
raspberryPi.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# project for cs 326
# written by Nathan Strain and Sam Mayfield
import Adafruit_MCP3008
import signal
from datetime import datetime
import RPi.GPIO as GPIO
import subprocess
import requests
# Constants
SAMPLE_TIME = 5
A2D_CHANNEL = 0
LED = 16
IFTTT_LINK = #LEFT OUT
IFTTT_KEY = #LEFT OUT
# SPI pin assignments
CLK = 25
MISO = 24
MOSI = 23
CS = 18
LIGHT = 75
MacNames = #LEFT OUT
place = 0
#2 mins
LIGHT_MAX = (5)*(60/SAMPLE_TIME)
WEB_LINK = #LEFTOUT
light_val = [False, 0, LIGHT_MAX]
# Instantiate a new A/D object
a2d = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)
# timer signal callback
def handler(signum, frame):
try:
global place
lostFound = [[],[]]
print("evaluating")
current_light = checkLight()
place = (place + 1) % 12
if(place%3 == 2):
lostFound = btCheck()
uploadStatus(lostFound)
elif(place == 0):
syncSQL()
if(current_light) < LIGHT:
if(light_val[2] < LIGHT_MAX):
light_val[2] -= 1
if light_val[2] == 0:
light_val[2] = LIGHT_MAX
GPIO.output(LED, False)
requests.get(IFTTT_LINK + "pi_off" + IFTTT_KEY)
if light_val[1] > LIGHT and current_light < LIGHT:
GPIO.output(LED, True)
requests.get(IFTTT_LINK + "pi_on" + IFTTT_KEY)
light_val[2] -= 1
if (len(lostFound[1]) > 0):
GPIO.output(LED, True)
requests.get(IFTTT_LINK + "pi_on" + IFTTT_KEY)
light_val[2] -= 1
else:
GPIO.output(LED, False)
requests.get(IFTTT_LINK + "pi_off" + IFTTT_KEY)
light_val[2] = LIGHT_MAX
light_val[1] = current_light
print("\tdone")
except Exception as e:
print(e)
def checkLight():
''' Timer signal handler
'''
value = a2d.read_adc(A2D_CHANNEL)
time = datetime.now().time()
return value
def btCheck():
returnList = [[],[]]
for key in MacNames.keys():
btName = ""
btName = str(subprocess.Popen("hcitool name " + str(key),shell=True,stdout=subprocess.PIPE).stdout.read())
#Not found
if(not len(btName) > len("b''")):
#recently lost
if(MacNames[key][1] > 0):
returnList[0].append(MacNames[key][0])
MacNames[key][1] = 0
continue
#recently found
elif(MacNames[key][1] == 0):
returnList[1].append(MacNames[key][0])
MacNames[key][1] = 1
return returnList
def uploadStatus(lostFound):
print("\t"+str(lostFound))
for i in lostFound[0]:
print("\t\t"+i)
requests.put(WEB_LINK + i + "/0")
print("\tFound")
for i in lostFound[1]:
print("\t\t"+i)
requests.put(WEB_LINK + i + "/1")
def syncSQL():
data = requests.get(WEB_LINK).json()
for key in MacNames.keys():
for i in data:
if(MacNames[key][0] == i['name']):
if(MacNames[key][1] != i['status']):
requests.put(WEB_LINK + i + "/" + str(MackNames[key][1]))
continue
# Setup interval timer signal every sample time
signal.signal(signal.SIGALRM, handler)
signal.setitimer(signal.ITIMER_REAL, 1, SAMPLE_TIME)
print('Press Ctrl-C to quit...')
try:
while True:
signal.pause()
except KeyboardInterrupt:
signal.setitimer(signal.ITIMER_REAL, 0, 0) # Cancel inteval timer
GPIO.cleanup()
print('Done')