-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightControl.py
More file actions
167 lines (141 loc) · 5.27 KB
/
LightControl.py
File metadata and controls
167 lines (141 loc) · 5.27 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#Running on Rpi
import time
from time import sleep
import threading
import RPi.GPIO as GPIO
import requests
import json
from TemperatureMonitoring import FanControl
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
TRIG = 16
ECHO = 20
LED = 18
controlBTN = 22
GPIO.setup(LED,GPIO.OUT)
#GPIO.setup(TRIG,GPIO.OUT)
#GPIO.setup(ECHO,GPIO.IN)
#GPIO.setup(autoBTN,GPIO.IN,pull_up_down=GPIO.PUD_UP)
#GPIO.setup(controlBTN,GPIO.IN)#,pull_up_down=GPIO.PUD_UP)
GPIO.output(LED,False)
ulr = ''
deviceId = "deviceId" # use the device ID and device Key given by Mediatek Cloud Sandbox
deviceKey = "deviceKey" # For privacy and security, the real device ID and Key are removed from here
def post_to_mcs(payload): # upload data points to mcs
host = "http://api.mediatek.com"
endpoint = "/mcs/v2/devices/" + deviceId + "/datapoints"
# url = ''.join([host,endpoint])
url = host + endpoint
headers = {"Content-type": "application/json", "deviceKey": deviceKey}
r = requests.post(url,headers=headers,data=json.dumps(payload))
def get_from_mcs(id): # get data points from mcs
host = "http://api.mediatek.com"
endpoint = "/mcs/v2/devices/" + deviceId + "/datachannels/" + id + "/datapoints"
# url = ''.join([host,endpoint])
url = host + endpoint
headers = {"Content-type": "application/json", "deviceKey": deviceKey}
r = requests.get(url,headers=headers)
value = (r.json()["dataChannels"][0]["dataPoints"][0]["values"]["value"])
return value
def ComputeDistance(): # compute the distance between any object facing the sensor
global TRIG # and the sensor
global ECHO
GPIO.setmode(GPIO.BCM)
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, False)
sleep(0.002)
GPIO.output(TRIG, True)
sleep(0.001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==False:
a=0
time1= time.time()
while GPIO.input(ECHO)==True:
a=1
time2= time.time()
during=time2-time1
return during*340/2 *100
def set_led(control): # depending on the control value, sets the LED on or off and
global LED # updates the LED's state on the cloud
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
if control==1:
GPIO.output(LED,True)
payload={"datapoints":[{"dataChnId":"1","values":{"value":1}}]}
else:
GPIO.output(LED,False)
payload={"datapoints":[{"dataChnId":"1","values":{"value":0}}]}
post_to_mcs(payload) # update the LED's state
def toggleLED(control): # when the button is pressed, change the control value on the cloud
if control==1: # consequently toggling the LED
payload={"datapoints":[{"dataChnId":"2","values":{"value":0}}]}
else:
payload={"datapoints":[{"dataChnId":"2","values":{"value":1}}]}
post_to_mcs(payload)
def turn_led_on(delay): # turns the LED on for a certain delay specified in the cloud
global LED # then turns it off while updating its state in the cloud
GPIO.output(LED,True)
payload={"datapoints":[{"dataChnId":"1","values":{"value":1}}]}
post_to_mcs(payload)
sleep(delay*60)
GPIO.output(LED,False)
payload={"datapoints":[{"dataChnId":"1","values":{"value":0}}]}
post_to_mcs(payload)
def display_feedback(mode,control,delay):
print("LED on mode",mode,end="\t")
print("Control set to",control,end="\t")
print("Delay set to",delay)
def check_controlBTN(): # check if the button has been pressed
print("Thread has started")
global controlBTN
GPIO.setmode(GPIO.BCM)
GPIO.setup(controlBTN,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while True:
try:
control=int(get_from_mcs("2"))
if GPIO.input(controlBTN)==False:
toggleLED(control)
sleep(0.2)
sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
exit()
def main():
########## few initializations before the loop begins ############################
LEDonDelay=1
distanceThreshold=20
post_to_mcs({"datapoints":[{"dataChnId":"4","values":{"value":LEDonDelay}}]})
post_to_mcs({"datapoints":[{"dataChnId":"1","values":{"value":0}}]})
########## Start a new thread to check if the button has been pressed ############
t=threading.Thread(target=check_controlBTN)
t.start()
f=FanControl() # create a FanControl object
t2=threading.Thread(target=f.run_temperature_monitoring)
t2.start()
while True:
try:
mode=int(get_from_mcs('3'))
control=int(get_from_mcs('2'))
delay=int(get_from_mcs('4'))
display_feedback(mode,control,delay)
if mode==1:
dist=ComputeDistance()
if dist<=distanceThreshold:
try:
turn_led_on(delay)
except:
print("There's been an error.\nDelay is set to default")
turn_led_on(1)
sleep(0.2)
else:
GPIO.output(LED,False)
sleep(0.2)
else:
set_led(control)
sleep(5)
except KeyboardInterrupt:
GPIO.cleanup()
exit()
if __name__=="__main__":
main()