-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathrtl_433_custom.py
executable file
·79 lines (59 loc) · 2.24 KB
/
rtl_433_custom.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
#!/usr/bin/env python
"""Custom data handling example for rtl_433."""
# Start rtl_433 (rtl_433 -F syslog::1433), then this script
from __future__ import print_function
import socket
import json
# You can run rtl_433 and this script on different machines,
# start rtl_433 with `-F syslog:YOURTARGETIP:1433`, and change
# to `UDP_IP = "0.0.0.0"` (listen to the whole network) below.
UDP_IP = "127.0.0.1"
UDP_PORT = 1433
def parse_syslog(line):
"""Try to extract the payload from a syslog line."""
line = line.decode("ascii") # also UTF-8 if BOM
if line.startswith("<"):
# fields should be "<PRI>VER", timestamp, hostname, command, pid, mid, sdata, payload
fields = line.split(None, 7)
line = fields[-1]
return line
def rtl_433_listen():
"""Listen to all messages in a loop forever."""
# Open a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the UDP socket to a listening address
sock.bind((UDP_IP, UDP_PORT))
# Loop forever
while True:
# Receive a message
line, addr = sock.recvfrom(1024)
try:
# Parse the message format
line = parse_syslog(line)
# Decode the message as JSON
data = json.loads(line)
#
# Change for your custom handling below, this is a simple example
#
label = data["model"]
if "channel" in data:
label += ".CH" + str(data["channel"])
elif "id" in data:
label += ".ID" + str(data["id"])
# E.g. match `model` and `id` to a descriptive name.
if data["model"] == "LaCrosse-TX" and data["id"] == 123:
label = "Living Room"
if "battery_ok" in data:
if data["battery_ok"] == 0:
print(label + ' Battery empty!')
if "temperature_C" in data:
print(label + ' Temperature ', data["temperature_C"])
if "humidity" in data:
print(label + ' Humidity ', data["humidity"])
# Ignore unknown message data and continue
except KeyError:
pass
except ValueError:
pass
if __name__ == "__main__":
rtl_433_listen()