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
|
#!/usr/bin/env python3
from RFXtrx import PySerialTransport
import sys, datetime, os
#WIND ['04:00']
#RAIN ['eb:00', '7d:00', '02:00']
#TEMP ['10:02', 'ec:01', '25:01', '62:02', 'd9:00', '2a:0e', '60:01']
#KNOWN_DEVICES = ['']
if __name__ == "__main__":
transport = PySerialTransport('/dev/ttyUSB0')
transport.reset()
while True:
if os.path.isfile('/home/pi/weather/stop'):
break
recv = transport.receive_blocking()
if not recv:
continue
timestamp = datetime.datetime.timestamp(datetime.datetime.now())
sys.stderr.write("Raw: %s\n"%str(recv))
try:
device_id = recv.device.id_string
device_type = recv.device.type_string
except Exception as e:
sys.stderr.write("Cannot find device id, exception %s." % str(e))
continue
data = {}
for k,v in recv.values.items():
data[k.lower().replace(" ", "_")] = v
data_type = None
if "rain_rate" in data:
data_type = "rain"
elif "wind_gust" in data:
data_type = "wind"
elif "temperature" in data:
data_type = "temp"
else:
sys.stderr.write("Unknown device '%s' with id '%s', ignoring.\n"%(device_type, device_id))
continue
out = [ "type::%s" % data_type,
"timestamp::%f" % timestamp,
"device_type::%s" % device_type,
"device_id::%s" % device_id ]
for key in sorted(data.keys()):
out.append("%s::%s" % (key, data[key]))
print("\t".join(out))
sys.stderr.flush()
sys.stdout.flush()
sys.stderr.write("stop\n")
sys.stdout.flush()
|