forked from floyduww/MeaterBlockMQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meater_link.py
executable file
·276 lines (202 loc) · 8.28 KB
/
meater_link.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/python3
from socket import *
import select
import paho.mqtt.client as mqtt
import math
from string import Template
import time
import ast
from configparser import ConfigParser
UDP_IP = "255.255.255.255"
# This is build the device broadcast string. Needs to be customized with mac, dev type, sw version etc.
MAC_HEX = "bfbaa9282d75990f"
DEV_TYPE = "Py Meater V0.01" # (14 bytes available w/o doing calculations)
DEV_TYPE_HEX = "5079204d65617465722056302e3031"
SW_VERSION_HEX = "322e35"
MESSAGE = "0a1308caa801100c1801200129" + MAC_HEX + "12460a28d01734191dc7f8d26b55c48be005b30c199f436383e2b5331aa0f176215aa44200e31aaeb62525671002220f" + DEV_TYPE_HEX + "2a03" + SW_VERSION_HEX + "32023131"
msg_as_byte = bytearray.fromhex(MESSAGE)
config = ConfigParser()
config.read('config.ini')
MQTT_HOSTNAME = config.get('mqtt', 'MQTT_HOSTNAME')
MQTT_PORT = config.getint('mqtt', 'MQTT_PORT')
MQTT_TIMEOUT = config.getint('mqtt', 'MQTT_TIMEOUT')
BLOCK_TIMEOUT = config.getint('block', 'BLOCK_TIMEOUT')
BLOCK_UDP_PORT = config.getint('block', 'BLOCK_UDP_PORT')
def probe_data(offset, data):
probe = {}
big_sep = bytes.fromhex("30013a")
big_sep2 = bytes.fromhex("30003a") # occurs rarely and not sure why
bc = int.from_bytes(data[offset:offset+1], "little")
probe["offset"] = offset
probe["end"] = offset + bc + 1
probeData = data[offset + 1: probe["end"]]
probe["bc"] = bc
probe["data"] = probeData.hex()
probe["batt"] = int.from_bytes(probeData[21:22], "little")
first_data_chunk_end = probeData.find(big_sep)
if (first_data_chunk_end < 0):
first_data_chunk_end = probeData.find(big_sep2)
probe["sig"] = probeData[23:first_data_chunk_end].hex()
begin_cook_data = first_data_chunk_end + 3
cook_data_bc = int.from_bytes(probeData[begin_cook_data:begin_cook_data + 1], "little")
cook_data = probeData[begin_cook_data:begin_cook_data + cook_data_bc]
probe["cooking"] = cook_data[4:5].hex()
temp_data_start = begin_cook_data + cook_data_bc + 2
temp_data_bc = int.from_bytes(probeData[temp_data_start:temp_data_start + 1], "little")
temp_data = probeData[temp_data_start + 1:temp_data_start + 1 + temp_data_bc]
probe["m_temp"] = math.floor(toFahrenheit(convertBytes(temp_data[1:3])))
convertBytes(temp_data[1:3])
probe["a_temp"] = math.floor(toFahrenheit(convertBytes(temp_data[4:6])))
convertBytes(temp_data[4:6])
version_start = temp_data_start + temp_data_bc + 2
version_bc = int.from_bytes(probeData[version_start:version_start + 1], "little")
version = probeData[version_start + 1:version_start + 1 + version_bc]
probe["version"] = version.decode("UTF-8")
probe["targ_temp"] = 0
probe["cook_name"] = ""
probe["meat_type"] = ""
if(probe["cooking"] != "00"):
probe["targ_temp"] = math.floor(toFahrenheit(convertBytes2(cook_data[6:8])))
if(cook_data[10:11].hex() == "2a"):
probe["meat_type"] = cook_data[9:10].hex()
cook_name_pos = 11
else:
probe["meat_type"] = cook_data[9:11].hex()
cook_name_pos = 12
cook_name_bc = int.from_bytes(cook_data[cook_name_pos:cook_name_pos + 1], "little")
if cook_name_bc:
cook_name = cook_data[cook_name_pos + 1: cook_name_pos + 1 + cook_name_bc]
probe["cook_name"] = cook_name.decode("UTF-8")
else:
if probe["meat_type"] in dictionary.keys():
probe["cook_name"] = dictionary[probe["meat_type"]]
else:
probe["cook_name"] = probe["meat_type"]
return probe
# convert an excess 128 byte to int
def convertBytes(data):
incrementor = int.from_bytes(data[0:1], "little") - 128
count = int.from_bytes(data[1:2], "little")
return ((count*128) + incrementor)
def convertBytes2(data):
incrementor = int.from_bytes(data[0:1], "little") - 128
count = int.from_bytes(data[1:2], "little")
return (((count*128) + incrementor) * 2)
def toCelsius(value):
return (float(value))/32.0
def toFahrenheit(value):
return ((toCelsius(value)*9)/5)+32.0
def on_publish(client, userdata, mid):
return True
def on_disconnect(mqttc, userdata, rc):
if rc != 0:
print("Unexpected disconnection. Reconnecting...")
mqttc.reconnect()
else:
print("Disconnected successfully")
def sendBlockOn():
mqttc.publish(topicBlockStatus, "on", qos=0, retain=True)
return 1
def sendBlockOff(blockStatus):
if blockStatus:
mqttc.publish(topicBlockStatus, "off", qos=0, retain=True)
print("blockOff")
return 0
def processPacket(packet):
global blockStatus
global lastReceive
print("-----------------")
print('len(packet)='+str(len(packet)))
print('len(packet[0])='+str(len(packet[0])))
print('len(packet[1])='+str(len(packet[1])))
print(packet[1])
theData = packet[0]
print(theData)
hex_string = "".join("%02x " % b for b in packet[0])
print(hex_string)
# check byte 9. (01 for phone, 02 for block)
if (theData[9:10].hex() == "01"):
print("This is the phone app")
# check if byte 21 is '1a', I think this means 'I have data'
if (theData[21:22].hex() == "1a"):
print("I am Meater Link")
else:
print("Move along")
return False
else:
print("Use meater_reader_v2.py to read from the block")
return False
blockStatus = sendBlockOn()
lastReceive = time.time()
probes = {}
block_power = theData[42:43]
powerStatusInt = int.from_bytes(block_power, "little")
mqttc.publish(topicBlockPower, str(powerStatusInt), qos=0, retain=True)
if (theData[23:24].hex() == "08"):
probe_start = 27
else:
probe_start = 28
probe_num = 0
while(theData[probe_start:probe_start+1].hex() == '1a'):
probe_num = probe_num + 1
probes[probe_num] = probe_data(probe_start +3, theData)
probe_start = probes[probe_num]["end"]
for id in probes:
probe = probes[id]
print(probe)
mqttc.publish(topicCook.substitute(id=id), probe["cooking"], qos=0, retain=True)
mqttc.publish(topicBattery.substitute(id=id), probe["batt"], qos=0, retain=True)
mqttc.publish(topicCookName.substitute(id=id), probe["cook_name"], qos=0, retain=True)
mqttc.publish(topicMeatType.substitute(id=id), probe["meat_type"], qos=0, retain=True)
mqttc.publish(topicTargetTemp.substitute(id=id), probe["targ_temp"], qos=0, retain=True)
mqttc.publish(topicMeat.substitute(id=id), probe["m_temp"], qos=0, retain=True)
mqttc.publish(topicAmbient.substitute(id=id), probe["a_temp"], qos=0, retain=True)
# mqtt setup and connect
mqttc = mqtt.Client()
mqttc.on_publish = on_publish
mqttc.on_disconnect = on_disconnect
mqttc.connect(MQTT_HOSTNAME, MQTT_PORT, MQTT_TIMEOUT)
# udp socket to listen to
s_client = socket(AF_INET, SOCK_DGRAM)
s_client.setblocking(0)
s_client.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
s_client.bind(('', BLOCK_UDP_PORT))
# send initial broadcast
s_client.sendto(msg_as_byte, (UDP_IP,BLOCK_UDP_PORT))
# mqtt topics
topicCook = Template("meater/probe/$id/cook")
topicBattery = Template("meater/probe/$id/battery")
topicMeatType = Template("meater/probe/$id/meatType")
topicTargetTemp = Template("meater/probe/$id/targetTemp")
topicMeat = Template("meater/probe/$id/meat")
topicAmbient = Template("meater/probe/$id/ambient")
topicCookName = Template("meater/probe/$id/cookName")
topicBlockStatus = "meater/block/status"
topicBlockPower = "meater/block/power"
mqttc.loop_start()
inputs = [s_client]
outputs = []
socket_timeout = 1
lastReceive = time.time()
lastSend = time.time()
blockStatus = 1
file = open("meat_table.txt", "r")
contents = file.read()
dictionary = ast.literal_eval(contents)
file.close()
while(1):
readable, writable, exceptional = select.select(
inputs, outputs, inputs, socket_timeout)
for s in readable:
if s is s_client:
data = s.recvfrom(1024)
processPacket(data)
for s in writable:
print("write")
for s in exceptional:
print("exceptional")
if time.time() - lastSend > 15:
s_client.sendto(msg_as_byte, (UDP_IP,BLOCK_UDP_PORT))
lastSend = time.time()
if time.time() - lastReceive > BLOCK_TIMEOUT:
blockStatus = sendBlockOff(blockStatus)