Skip to content

Commit

Permalink
Added Dragino LHT65 decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
asvand committed Jan 7, 2021
1 parent 9a54e4e commit 7b50b74
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
Binary file removed .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*/.DS_Store
.DS_Store
.DS_Store
tools/*
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# AWS IoT Core for LoRaWAN sample applications

AWS IoT Core for LoRaWAN enables you to set up a private LoRaWAN network by connecting your devices and gateways with no LoRaWAN Network Server setup required. This repository contains resources to quickly get started developing solutions using AWS IoT Core for LoRaWAN. It includes samples for typical design patterns ([binary decoder](transform_binary_payload), [downlink messaging](send_downlink_payload), [Thing shadow update](iotthingshadow)) and fully functional applications ([dashboards](timestream), [condition monitoring and alarming](soilmoisture_alarming)).

Please consider our [developer guide](https://docs.aws.amazon.com/iot/latest/developerguide/connect-iot-lorawan.html) to learn how to connect your wireless devices and gateways to AWS IoT Core for LoRaWAN.
Expand All @@ -24,6 +25,7 @@ Please consider our [developer guide](https://docs.aws.amazon.com/iot/latest/dev


## Getting help

- [API Reference](http://docs.aws.amazon.com/console/iot/wireless/intro/apiref)
- [Developer guide](http://docs.aws.amazon.com/console/iot/wireless/intro/devguide)
- [FAQs](https://aws.amazon.com/iot-core/faqs/#AWS_IoT_Core_for_LoRaWAN)
Expand Down
Binary file removed soilmoisture_alarming/images/.DS_Store
Binary file not shown.
Binary file removed timestream/images/.DS_Store
Binary file not shown.
105 changes: 105 additions & 0 deletions transform_binary_payload/src-payload-decoders/python/dragino_lht65.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


import base64


def dict_from_payload(base64_input: str):

decoded = base64.b64decode(base64_input)

# Batter status flag
# 00(b): Ultra Low ( BAT <= 2.50v)
# 01(b): Low (2.50v <=BAT <= 2.55v)
# 10(b): OK Good (2.55v <= BAT <=2.65v)
# 11(b): Good (BAT >= 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)} ')

0 comments on commit 7b50b74

Please sign in to comment.