DroneScout Receiver Manual - version 1.0 May 2022 - © BlueMark Innovations BV 2022
23/28
client.on_connect = on_connect
client.connect(broker, port)
subscribe(client)
return client
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
print(f"Received message from topic `{msg.topic}`")
try:
#not compressed
payload = msg.payload.decode()
# remove newline char as it will prevent decoding of json
if ord(payload[-1:]) == 10:
payload = payload[:-1]
except (UnicodeDecodeError, AttributeError):
#lzma compressed
payload = lzma.decompress(msg.payload)
# remove \0 char as it will prevent decoding of json
if ord(payload[-1:]) == 0:
payload = payload[:-1]
#print(payload) #uncomment tp print raw payload
json_obj = json.loads(payload)
try:
if json_obj.get('protocol') == 1.0:
try:
data_json = json_obj.get('data')
UASdata = base64.b64decode(data_json.get('UASdata'))
print("data message")
print("sensor ID......", data_json.get('sensor ID'))
print("RSSI......", data_json.get('RSSI'))
print("channel......", data_json.get('channel'))
print("timestamp......", data_json.get('timestamp'))
epoch_timestamp =
datetime.datetime.fromtimestamp(data_json.get('timestamp')/1000)
print("time (UTC)......",
epoch_timestamp.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
print("MAC address......", data_json.get('MAC address'))
print("type......", data_json.get('type'))
BasicID0_valid = UASdata[892]
BasicID1_valid = UASdata[893]
LocationValid = UASdata[894]
SelfIDValid = UASdata[911]
SystemValid = UASdata[912]
OperatorIDValid = UASdata[913]
#BasicID message 0
if BasicID0_valid == 1:
BasicID0_start_byte = 0
[UAType] = struct.unpack('i',
UASdata[BasicID0_start_byte:BasicID0_star 4])
[IDType] = struct.unpack('i', UASdata[BasicID0_star
4:BasicID0_star 4 + 4])
print("Basic ID 0 data")
print("UAType......", UAType)
print("IDType......", IDType)
print("Basic ID......", UASdata[BasicID0_star
8:BasicID0_sta 8 + 21].decode('ascii'))
print("")