Skip to content

Instantly share code, notes, and snippets.

@laundmo
Last active December 7, 2021 16:01
Show Gist options
  • Save laundmo/b807f7b804242f2407cceb08120924bb to your computer and use it in GitHub Desktop.
Save laundmo/b807f7b804242f2407cceb08120924bb to your computer and use it in GitHub Desktop.
Dragino NBSN95 decoder, all payloads.
"""
MIT License
Copyright (c) 2021 Laurin Schmidt laurinschmidt2001@gmail.com
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, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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.
"""
# Tested with python 3.9
from datetime import datetime
from typing import Dict, Union
from contextlib import suppress
DataTypes = Union[float, int, str, bool, datetime, None]
Data = Dict[str, DataTypes]
# Labels for which data apears in all frames
GLOBAL_LABELS = ["Device ID", "Ver", "BAT", "Signal Strength", "MOD"]
# Field sizes which appear in most frames (except CFGMOD=3)
FRAME_START = [6, 2, 2, 1, 1]
# Common field names
dstemp = "Temperature DS18B20"
shttemp = "Temperature SHT20/31"
shthum = "Humidity SHT20/31"
ds1temp = "Temperature1 DS18B20"
ds2temp = "Temperature2 DS18B20"
ds3temp = "Temperature3 DS18B20"
dgint = "Digital input and Interrupt"
ts = "Timestamp"
adc = "ADC"
FRAME_TYPES = {
1: (
FRAME_START + [2, 1, 2, 2, 2, 4],
GLOBAL_LABELS
+ [
dstemp,
dgint,
adc,
shttemp,
shthum,
ts,
],
),
2: (
FRAME_START + [2, 1, 2, 2, 4],
GLOBAL_LABELS
+ [
dstemp,
dgint,
adc,
"Distance",
ts,
],
),
3: (
[6, 1, 2, 1, 1] + [2, 1, 2, 2, 2, 2, 4],
GLOBAL_LABELS
+ [
"ADC1",
dgint,
"ADC2",
shttemp,
shthum,
"ADC3",
ts,
],
),
4: (
FRAME_START + [2, 2, 1, 2, 2, 4],
GLOBAL_LABELS
+ [
ds1temp,
adc,
dgint,
ds2temp,
ds3temp,
ts,
],
),
5: (
FRAME_START + [2, 2, 1, 2, 4],
GLOBAL_LABELS
+ [
dstemp,
adc,
dgint,
"Weight",
ts,
],
),
6: (FRAME_START + [4, 4], GLOBAL_LABELS + ["Pulse Count", ts]),
}
def decode_frame(byte_sizes: list, ba: bytearray):
res = []
prev = 0
for size in byte_sizes:
now = prev + size
block = ba[prev:now]
res.append(int.from_bytes(block, "big"))
prev = now
return res
def decode(raw_payload: bytes) -> Data:
ba = bytearray.fromhex(raw_payload.decode("utf-8"))
# determine the mode
if (
len(ba) == 26
): # CFGMOD=3 has 1 less byte for the version, it gets decoded seperately
mode = decode_frame([6, 1, 2, 1, 1], ba)[4]
else:
mode = decode_frame(FRAME_START, ba)[4]
# decode the frame with the correct type for the mode
frame_definition, labels = FRAME_TYPES[mode]
data = decode_frame(frame_definition, ba)
# Make it a dictionary
data = dict(zip(labels, data))
# timestamp as python datetime
data[ts] = datetime.fromtimestamp(data[ts])
# version as version string
data["Ver"] = ".".join(str(data["Ver"]))
# divide temp/hum values by 10
for key in [
dstemp,
shttemp,
shthum,
ds1temp,
ds2temp,
ds3temp,
]:
with suppress(KeyError):
data[key] = data[key] / 10
return data
if __name__ == "__main__":
print("CFGMOD=1", decode(b"72403155615900780c541901000000004200fc023260da7c4e"))
print("CFGMOD=2", decode(b"72403162907100780ca91102010b000ca8015860dacc87"))
print("CFGMOD=3", decode(b"724031629071780cf012030cbc000cef010a024b0cef60dbc494"))
print("CFGMOD=4", decode(b"72403162907100780cdf1504010a0cde0000fb010060dbcb3f"))
print("CFGMOD=5", decode(b"72403162907100780c94140501370c9300003a60dbe59e"))
print("CFGMOD=6", decode(b"72403162907100780cc714060000000260dc03e5"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment