From 5848252bff1dfc7c01a410163a2ae012d3905a23 Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Sat, 8 Feb 2020 16:27:46 +0100 Subject: [PATCH] Added Decoder for New Binary Payload Format --- sensord/RFM69.py | 2 ++ sensord/RFM69receiver.py | 69 +++++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/sensord/RFM69.py b/sensord/RFM69.py index 418fa7f..4ad076d 100644 --- a/sensord/RFM69.py +++ b/sensord/RFM69.py @@ -5,6 +5,8 @@ import spidev import RPi.GPIO as GPIO import time +GPIO.setwarnings(False) + class RFM69(object): def __init__(self, freqBand, nodeID, networkID, isRFM69HW = False, intPin = 18, rstPin = 28, spiBus = 0, spiDevice = 0): diff --git a/sensord/RFM69receiver.py b/sensord/RFM69receiver.py index 0fd29db..1b2863d 100644 --- a/sensord/RFM69receiver.py +++ b/sensord/RFM69receiver.py @@ -4,7 +4,7 @@ rfm69receiver - A RFM69 433MHz Receiver Using the Observer Pattern import time import struct -from pprint import pprint +from pprint import pprint, pformat import RFM69 from RFM69registers import RF69_433MHZ @@ -13,7 +13,7 @@ class RFM69Receiver(object): Multiple Observers to Process the Messages """ - def __init__(self, nodeID, netID, encKey=None): + def __init__(self, nodeID, netID, encKey=None, debug=False): """ Initialize the Transceiver Module Arguments: @@ -26,6 +26,7 @@ class RFM69Receiver(object): self.__running = False self.__lastsid = None self.__lastts = 0 + self.__debug = debug try: print("Initializing RFM69 Module as Node " + str(nodeID) + " on Network " + str(netID)) @@ -42,23 +43,14 @@ class RFM69Receiver(object): print("Shutting Down RFM69 Module") self.__rfm.shutdown() - def _rawbytes(self, s): - """Convert a string to raw bytes without encoding""" - outlist = [] - for cp in s: - num = ord(cp) - if num < 255: - outlist.append(struct.pack('B', num)) - elif num < 65535: - outlist.append(struct.pack('>H', num)) - else: - b = (num & 0xFF0000) >> 16 - H = num & 0xFFFF - outlist.append(struct.pack('>bH', b, H)) - return b''.join(outlist) + def _debug(self, message): + """ Print Debug Output """ + if self.__debug: + print('[' + time.strftime('%H:%M:%S %d %b %Y') + '][DBG] ', end='') + print(message) - def _process(self, sid, data, rssi): + def _process(self, sid, rdata, rssi): """ Process received Data and Notify Registered Observers Arguments: @@ -77,6 +69,12 @@ class RFM69Receiver(object): msg = {} msg['sid'] = sid msg['rssi'] = rssi + + try: + data = rdata.decode("ascii") + except UnicodeDecodeError: + data = "" + if (data.find(';') != -1 and data.find('=') != -1): values = data.split(";") for value in values: @@ -88,13 +86,29 @@ class RFM69Receiver(object): else: msg[vsplit[0]] = int(vsplit[1].rstrip()) else: - bdata = self._rawbytes(data) - msg['t'] = int.from_bytes(bdata[0:3], byteorder='little', signed=True)/100 - msg['p'] = int.from_bytes(bdata[4:7], byteorder='little', signed=True)/100 - msg['h'] = int.from_bytes(bdata[8:11], byteorder='little', signed=True)/100 - msg['v'] = int.from_bytes(bdata[12:15], byteorder='little', signed=True)/1000 + self._debug("Interpreting " + str(len(rdata)) + " Bytes of Binary Data from " + str(sid) + ": " + pformat(rdata)) + if len(rdata) == 9: + voltage, temp, humidity = struct.unpack('