-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_readings.py
executable file
·72 lines (46 loc) · 1.57 KB
/
process_readings.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
#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
import time
import json
import re
temp_sensors = ["28-000006dd5bbd", "28-000006ddbe63", "28-00000722cf9d", "28-000007486f53"]
PERIOD = 60
GPIO.setmode(GPIO.BCM)
last_sensor0 = 0
last_sensor1 = 0
sensor0 = 0
sensor1 = 0
def inc_sensor0(channel):
global sensor0
sensor0 = sensor0 + 1
def inc_sensor1(channel):
global sensor1
sensor1 = sensor1 + 1
GPIO.setup(17, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.add_event_detect(17, GPIO.FALLING, callback=inc_sensor0, bouncetime=50)
GPIO.add_event_detect(23, GPIO.FALLING, callback=inc_sensor1, bouncetime=50)
def wait_for_interval(period):
# Based on http://stackoverflow.com/a/19645797
current_time = time.time()
time_to_sleep = period - (current_time % period)
time.sleep(time_to_sleep)
try:
wait_for_interval(PERIOD)
last_sensor0 = sensor0
last_sensor1 = sensor1
while True:
wait_for_interval(PERIOD)
msg = { "sensor0": sensor0 - last_sensor0, "sensor1": sensor1 - last_sensor1 }
for sensor_id in temp_sensors:
with open("/sys/bus/w1/devices/" + sensor_id + "/w1_slave") as f:
content = f.readlines()
msg[sensor_id] = int(re.sub(r".*t=", "", content[1].strip()))
time.sleep(1)
msg["timestamp"] = int(time.time())
print json.dumps(msg, sort_keys=True)
last_sensor0 = sensor0
last_sensor1 = sensor1
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit