-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdr-mosq-v3.py
167 lines (139 loc) · 5.06 KB
/
sdr-mosq-v3.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
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
#!/usr/bin/python3
#********************************************************************
# sdr-mosq.py
#
# 7 July 2018
# written by Gerald Swann
#
# This program reads from an SDR dongle using the excellent rtl_433
# program and publishes the data using mosquitto_pub
#
# Unlike the earlier bash script I used, this does not require jq to
# be installed on the system. Uses instead the python JSON decoder
#
# The sensors often transmit their data 2 or 3 times. Using Runme.sh,
# you would see all the lines in your MQTT data. This program removes
# the duplicate lines
#
# Copy this python script to your rtl_433 folder
#
# To see only the MQTT data from this program on your
# Home Assistant system, try this command:
#
# mosquitto_sub -v -t 'rtl_433/#' -u hass -P hass
#
# Updated 15 july 2018
# Now, don't publish every code that comes across. At every 30 seconds,
# they are too frequent.
# The program variable "between_time" sets minimum time between packets
#********************************************************************
import time
import os
import subprocess
import json
import pdb
import socket
#pdb.set_trace()
debug = 0
def checkin():
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'amd1'
port = 3004
try:
s.connect((host, port))
# Receive no more than 1024 bytes
s.sendall(b'mysend')
msg = s.recv(1024)
s.close()
if debug == 1:
print (msg.decode('ascii'))
except:
if debug == 1:
print('couldnt connect')
checkin()
#between_time = 65
between_time = 25
# starting clock time of this program
# it's carried in the 'a' array when value
# is published
start = time.time()
#print (time.time())
# list of ID codes, publish topic, and last published time
from sdrlist import a
print (time.time() - start)
print (round(time.time() - start,1))
oldLine = "xxx"
with os.popen('../rtl_433/build/src/rtl_433 -F json -f 433920000 -G -M level') as sdr:
for myLine in sdr:
if myLine != oldLine:
oldLine = myLine
# don't print the new line at the end
# print(myLine[0:-1])
decoded = json.loads(str(myLine))
try:
print (decoded['sid'],end = " ")
myID = decoded['sid']
except:
myID = 999
myTopic = "x"
if (myID == 0):
myTopic = "rtl_433/acurite/osv1"
mySeconds = round(time.time() - start,1)
print (round(mySeconds - a[myID][1],1), end = " ")
if (mySeconds - a[myID][1] > between_time):
print ('\033[33;40;1m',end='')
print("publishing myID " + str(myID) + " " + myTopic )
print('\033[0m',end='')
print(myLine[0:-1])
print("")
else:
print('\033[31m',end='')
print("dropped myID " + str(myID) + " " + myTopic )
print('\033[0m',end='')
print(myLine[0:-1])
print("")
checkin()
try:
if (myTopic == "x"):
try:
print (decoded['id'],end =" ")
myID = decoded['id']
except:
print(myLine[0:-1])
raise
print (decoded['sid'],end =" ")
myID = decoded['sid']
# pdb.set_trace()
if (myID in a.keys()):
myTopic = a[myID][0]
mySeconds = round(time.time() - start,1)
print (round(mySeconds - a[myID][1],1), end = " ")
if (mySeconds - a[myID][1] > between_time):
print ('\033[33;40;1m',end='')
print("publishing myID " + str(myID) + " " + myTopic )
print('\033[0m',end='')
print(myLine[0:-1])
print("")
a[myID][1] = mySeconds
p = subprocess.Popen(["mosquitto_pub","-l","-h","192.168.1.40","-u","hass","-P","hass","-t", myTopic ],stdin=subprocess.PIPE)
p.communicate(bytes(myLine,'UTF-8'))
# checkin()
else:
print('\033[31m',end='')
print("dropped myID " + str(myID) + " " + myTopic )
print('\033[0m',end='')
print(myLine[0:-1])
print("")
checkin()
myID = "x"
else:
myTopic = "rtl_433/acurite/unk2"
except:
raise
myTopic = "rtl_433/acurite/noid"
if (myID != "x"):
#print("what? " + myTopic)
# here publish osv1
p = subprocess.Popen(["mosquitto_pub","-l","-h","192.168.1.40","-u","hass","-P","hass","-t", myTopic ],stdin=subprocess.PIPE)
p.communicate(bytes(myLine,'UTF-8'))