From 7b50b743dbe8dc5f75bde5f38ff129d2568effde Mon Sep 17 00:00:00 2001 From: Andrei Svirida Date: Thu, 7 Jan 2021 08:51:19 +0100 Subject: [PATCH] Added Dragino LHT65 decoder --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 4 +- README.md | 2 + soilmoisture_alarming/images/.DS_Store | Bin 6148 -> 0 bytes timestream/images/.DS_Store | Bin 6148 -> 0 bytes .../python/dragino_lht65.py | 105 ++++++++++++++++++ 6 files changed, 109 insertions(+), 2 deletions(-) delete mode 100644 .DS_Store delete mode 100644 soilmoisture_alarming/images/.DS_Store delete mode 100644 timestream/images/.DS_Store create mode 100644 transform_binary_payload/src-payload-decoders/python/dragino_lht65.py diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 59a3d7ddda0f48259bcaa3dd3debbc065609a57c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKJ8r`;3?*9+Mzdt>s4L_KLXe&y7wE?>P#{HsB;7sbTsc}FKSKj2o4Xi~0QDq_ zPlDcHnj)g>pKc|x6pW`Hl$JMa<7mZEJ8U!E?^R+FbE7bX;P`mxqP&|`4kq4JEe$u1G8^; zcHS=g3XMiYwEG;MMOqQr!VTryhNaoQ`NAGDqChy#_>vD9hV%RQc3fot9x(1kPT-y8 zaLqrCwnd`?RDcRl0V+TRR#qSj?DlEpb9o#UpaTD20sB4_xM5A40{zp0!CL@egRmRs z-b(a zV=P;%|1amDrVEV= 2.65v) + battery_status_flag = (decoded[0] & 0b11000000) >> 6 + battery_status = "unknown" + if battery_status_flag == 0b00: + battery_status = "very low" + elif battery_status_flag == 0b01: + battery_status = "low" + elif battery_status_flag == 0b10: + battery_status = "OK" + elif battery_status_flag == 0b11: + battery_status = "Good" + + # Battery voltage + battery_value = ((decoded[0] << 8 | decoded[1]) & 0x3FFF) / 1000 + + # Internal sensor temperature + if (decoded[2] & 0b1000000): + internal_temperature = ((decoded[2] << 8 | decoded[3]) - 0xFFFF)/100 + else: + internal_temperature = (decoded[2] << 8 | decoded[3])/100 + + # Humidity + humidity = ((decoded[4] << 8 | decoded[5])/10) + + # External sensor temperature + if (decoded[7] & 0b1000000): + external_temperature = ( + ((decoded[7] << 8 | decoded[8]) - 0xFFFF) / 100) + else: + external_temperature = ((decoded[7] << 8 | decoded[8]) / 100) + + result = { + "battery_status": battery_status, + "battery_value": battery_value, + "temperature_internal": internal_temperature, + "humidity": humidity, + "temperature_external": external_temperature + } + + return result + + +# Tests +if __name__ == "__main__": + test_definition = [ + { + "input": "CBF60B0D0376010ADD7FFF", + "output": { + "battery_status": "Good", + "battery_value": 3.062, + "temperature_internal": 28.29, + "humidity": 88.6, + "temperature_external": 27.81 + + } + }, + { + "input": "CBBDF5C6022E01F54F7FFF", + "output": { + "battery_status": "Good", + "battery_value": 3.005, + "temperature_internal": -26.17, + "humidity": 55.8, + "temperature_external": -27.36 + + } + } + ] + + for test in test_definition: + base64_input = base64.b64encode( + bytearray.fromhex(test.get("input"))).decode("utf-8") + output = dict_from_payload(base64_input) + for key in test.get("output"): + if(test.get("output").get(key) != output.get(key)): + raise Exception( + f'Assertion failed for input {test.get("input")}, key {key}, expected {test.get("output").get(key)}, got {output.get(key)} ')