From a3a7e60403210bd15bd564ca8c1b3f98939ead0c Mon Sep 17 00:00:00 2001 From: "S. Seegel" Date: Sat, 18 Jan 2020 19:09:51 +0000 Subject: [PATCH] clean up & decoding --- maxrx.py | 144 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 98 insertions(+), 46 deletions(-) diff --git a/maxrx.py b/maxrx.py index 2787bb3..507f9a3 100755 --- a/maxrx.py +++ b/maxrx.py @@ -7,11 +7,13 @@ import time import threading devices = { - '0x19cf40': 'SchlafWall', - '0x1a2a84': 'SchlafThermos', - '0x12c400': 'SchlafThermos2', - '0x192b8d': 'Wall2', - '0x192765': 'Wall3', + '0x19cf40': 'WallSchlaf', + '0x1a2a84': 'HeatSchlaf1', + '0x12c400': 'HeatSchlaf2', + '0x192b8d': 'WallWohn', + '0x192765': 'WallMusik', + '0x1ac690': 'HeatWohnR', + '0x1ac36b': 'HeatWohnL', } if Rfm69.Test(1): @@ -24,52 +26,102 @@ else: print("No RFM69 module found!") exit() -def CallbackSync(): - lfsr = 0x1ff - frame = [] - frame.append(rfm.ReadFifoWait()) - len = frame[0] ^ 0xFF #invert due to whitening - - for i in range(len): - frame.append(rfm.ReadFifoWait()) - - rfm.WhitenTI(frame) - - x = {} - x['cnt'] = frame[1] - x['flag'] = hex(frame[2]) - x['type'] = hex(frame[3]) - x['src'] = hex(frame[4] << 16 | frame[5] << 8 | frame[6]) - x['dst'] = hex(frame[7] << 16 | frame[8] << 8 | frame[9]) - x['grp'] = frame[10] - x['pay'] = [] - for i in range(len - 10): - x['pay'].append(frame[i + 11]) - - if x['src'] in devices: - x['src'] = devices[x['src']] - - if x['dst'] in devices: - x['dst'] = devices[x['dst']] - - info = "" - if x['type'] == '0x42': - info = str(x['pay'][0] / 2.0) + ' / ' + str(x['pay'][1] / 10.0) - - print(x['src'] + "->" + x['dst'] + " type " + x['type'] + ':', x['pay'], info) - rfm.SetParams( Freq = 868.300, #MHz center frequency ModulationType = rfm69.FSK, #modulation Datarate = 9.992, #kbit/s baudrate - Deviation = 19.042, #kHz frequency deviation -> OBW = 48 kHz, h = 3.81 + Deviation = 19.042, #kHz frequency deviation SyncPattern = [0xc6, 0x26, 0xc6, 0x26], #syncword - Bandwidth = 100, #kHz bandwidth (101.5) - RssiThresh = -100, #-100 dB RSSI threshold - CallbackSync = CallbackSync + Bandwidth = 100, #kHz bandwidth + RssiThresh = -105, #dBm RSSI threshold ) -print "Waiting for sensors..." +rxFifo = [] +rxEvent = threading.Event() +rxMutex = threading.Lock() -while 1: - data = rfm.ReceivePacket(64) \ No newline at end of file +class RxThread(threading.Thread): + def __init__(self, rfm): + self.__rfm = rfm + threading.Thread.__init__(self) + + def __callback(self): + frame = rfm.ReadFifoWait(1) + len = frame[0] ^ 0xFF #invert due to whitening + frame += rfm.ReadFifoWait(len) + + rxMutex.acquire() + rxFifo.append(frame) + rxMutex.release() + rxEvent.set() + + def run(self): + while True: + self.__rfm.StartRx(self.__callback) + + +rxThread = RxThread(rfm) +rxThread.daemon = True +rxThread.start() + +def Decode(frame): + #decode MAX! frame + cnt = frame[1] + flag = hex(frame[2]) + type = frame[3] + srcadr = hex(frame[4] << 16 | frame[5] << 8 | frame[6]) + dstadr = hex(frame[7] << 16 | frame[8] << 8 | frame[9]) + grp = frame[10] + payload = frame[11:] + + if srcadr in devices: + srcadr = devices[srcadr] + + if dstadr in devices: + dstadr = devices[dstadr] + + info = "" + if type == 0x42: + info = "set T: " + str(payload[0] / 2.0) + ' / act T:' + str(payload[1] / 10.0) + + if type == 0x40: + info = "set T: " + str(payload[0] / 2.0) + + elif type == 0x60: + devflags = payload[0] + valve = payload[1] + dst = payload[2] / 2.0 + info += ", mode " + str(devflags & 0x03) + if (devflags & (1<<7)) <> 0: + info += ", bat err" + if (devflags & (1<<6)) <> 0: + info += ", com err" + if (devflags & (1<<5)) <> 0: + info += ", locked" + info += ", valve " + str(valve) + "%" + info += ", set T " + str(dst) + + elif type == 0x00: + info = "FW " + str(payload[0] / 16) + "." + str(payload[0] % 16) + ", devtype " + str(payload[1]) + ", serial " + for c in payload[3:]: + info += chr(c) + + info += ", raw: " + str(payload) + print(srcadr + "->" + dstadr + " type " + hex(type) + ': ' + info) + +while True: + if rxEvent.wait(0.5) == False: + continue + + left = True + while left: + rxMutex.acquire() + rxEvent.clear() + frame = rxFifo.pop(0) + left = len(rxFifo) > 0 + rxMutex.release() + + rfm.WhitenTI(frame) + Decode(frame) + if not left: + break