-
Notifications
You must be signed in to change notification settings - Fork 1
/
receiver_publish_mqtt.py
69 lines (55 loc) · 1.93 KB
/
receiver_publish_mqtt.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
# Uses pi_switch from https://github.com/lexruee/pi-switch-python
# See pi_switch readme for details on setup
from pi_switch import RCSwitchReceiver
import time
import paho.mqtt.client as mqtt
receiver = RCSwitchReceiver()
receiver.enableReceive(2)
def publish_message():
# Initialize the client that should connect to the Mosquitto broker
client = mqtt.Client()
connOK=False
while(connOK == False):
try:
print("try connect")
client.connect("192.168.1.16", 1883, 60)
connOK = True
except:
connOK = False
time.sleep(2)
client.publish("Home/Frontdoor/Opened","1.0")
time.sleep(5)
client.disconnect()
acceptedTypes = { 1 : "Light", 2 : "Temp [C]", 3: "Humidity [%]", 4: "Door open" }
prev_value = 0L
while True:
if receiver.available():
value = receiver.getReceivedValue()
if value == prev_value:
# we have already seen this measurement, so ignore it
continue
# decode byte3
byte3 = (0xFF000000 & value) >> 24
typeID = int((0xF0 & byte3) >> 4)
seqNum = int((0x0F & byte3))
# decode byte2 and byte1
data = int((0x00FFFF00 & value) >> 8)
# decode byte0
checkSum = int((0x000000FF & value))
calculatedCheckSum = 0xFF & (typeID + seqNum + data)
# Sanity checks on received data
correctData = True
if calculatedCheckSum != checkSum:
correctData = False
elif data > 1023:
correctData = False
elif typeID not in acceptedTypes:
correctData = False
elif seqNum > 15:
correctData = False
if correctData:
print(str.format("{0}: {1}={2} SeqNum={3}",time.ctime(),acceptedTypes[typeID],data,seqNum))
prev_value = value
if (typeID == 0x04):
publish_message()
receiver.resetAvailable()