-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsensor.py
82 lines (66 loc) · 2.01 KB
/
sensor.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
import RPi.GPIO as GPIO
import time
import json
import websocket
GPIO.setmode(GPIO.BOARD)
pinLDR = 7
pinLED = 11
GPIO.setup(pinLED, GPIO.OUT)
GPIO.output(pinLED, GPIO.LOW)
sleepTime = 0.1
def detectLight(pinLDR):
count = 0
GPIO.setup(pinLDR, GPIO.OUT)
GPIO.output(pinLDR, GPIO.LOW)
time.sleep(sleepTime)
GPIO.setup(pinLDR, GPIO.IN)
while (GPIO.input(pinLDR) == GPIO.LOW):
count += 1
return count
def sendData(sensorStatus = '', date = '', time = '', sensorValue = ''):
data = {
"type": "SENSOR",
"sensorData": {
"sensorType": "photoresistor",
"sensorStatus": sensorStatus,
"date": date,
"time": time,
"sensorValue": sensorValue,
},
}
return json.dumps(data)
def main(ws):
try:
while True:
ldrValue = detectLight(pinLDR)
# print(f"LDR Value: {ldrValue}")
if (ldrValue <= 300000):
ws.send(sendData("light", time.strftime("%d/%m/%Y", time.localtime()), time.strftime("%H:%M:%S", time.localtime()), ldrValue))
GPIO.output(pinLED, GPIO.HIGH)
else:
ws.send(sendData("dark", time.strftime("%d/%m/%Y", time.localtime()), time.strftime("%H:%M:%S", time.localtime()), ldrValue))
GPIO.output(pinLED, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("\nConnection Closed\n")
def on_open(ws):
main(ws)
if __name__ == "__main__":
HOST = "ws://192.168.29.239:8082"
print(f"Connecting to Server at {HOST}...")
ws = websocket.WebSocketApp(HOST,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
print("Connected!")
print("Sending Sensor Data...")
ws.run_forever()