I'm hoping that the software below is in the correct format. It is exactly how it is formatted on my screen. It works well.
The server timestamp is an integer number in milliseconds from Linux epoch, 1 Jan 1970.`
Here is my Python code (first python code I have ever written! The ** lines are the bits that handle the decoding of the timestamp.
# Python MQTT client for RPi
import base64
import random
import json
import paho.mqtt.client as mqtt
** from datetime import datetime
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("\nConnected to RAK7249 Server (Barn)")
else:
print("Failed to connect, return code %d\n", rc)
def on_subscribe(client, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_message(client, userdata, msg):
# if (not valid msg) return
print("\nMessage received: ")
print(" " + str(msg.payload.decode("utf-8")))
print(" Topic: ")
print(" " + msg.topic)
print(" Payload: ")
print(" " + str(msg.payload))
json_string = str(msg.payload)
print(" json_string:")
print(" " + json_string)
print(type(json_string))
print(json.loads(msg.payload))
parsed_json = json.loads(msg.payload)
print(parsed_json['data'])
print(base64.b64decode(parsed_json['data']).decode('utf-8'))
print(parsed_json['devEUI'])
** print(parsed_json['timestamp'])**
** timestampp = (parsed_json['timestamp'])**
** dt_object = datetime.fromtimestamp(timestampp)**
** print("dt_object =", dt_object)**
def on_log(client, obj, level, string):
print(string)
client = mqtt.Client(f'python-mqtt-{random.randint(0, 100)}')
client.on_message = on_message
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_log = on_log
client.connect("192.168.1.227", 1883, 60)
client.subscribe("application/+/device/+/rx")
client.loop_forever()
Here is the output from this sketch.
Message received:
{"applicationID":"4","applicationName":"RAK4061","devEUI":"007e47c0ac6183b2","deviceName":"RAK4601","timestamp":1646022265,"fCnt":24368,"fPort":1,"data":"SGVsbG8sIHdvcmxkIDE0MTM0","data_encode":"base64","adr":true,"rxInfo":[{"gatewayID":"ac1f09fffe004ecf","loRaSNR":5.5,"rssi":-104,"location":{"latitude":-44.076880,"longitude":170.781970,"altitude":478},"time":"2022-02-28T04:24:25.068840Z"}],"txInfo":{"frequency":917800000,"dr":5}}
Topic:
application/4/device/007e47c0ac6183b2/rx
Payload:
b'{"applicationID":"4","applicationName":"RAK4061","devEUI":"007e47c0ac6183b2","deviceName":"RAK4601","timestamp":1646022265,"fCnt":24368,"fPort":1,"data":"SGVsbG8sIHdvcmxkIDE0MTM0","data_encode":"base64","adr":true,"rxInfo":[{"gatewayID":"ac1f09fffe004ecf","loRaSNR":5.5,"rssi":-104,"location":{"latitude":-44.076880,"longitude":170.781970,"altitude":478},"time":"2022-02-28T04:24:25.068840Z"}],"txInfo":{"frequency":917800000,"dr":5}}'
json_string:
b'{"applicationID":"4","applicationName":"RAK4061","devEUI":"007e47c0ac6183b2","deviceName":"RAK4601","timestamp":1646022265,"fCnt":24368,"fPort":1,"data":"SGVsbG8sIHdvcmxkIDE0MTM0","data_encode":"base64","adr":true,"rxInfo":[{"gatewayID":"ac1f09fffe004ecf","loRaSNR":5.5,"rssi":-104,"location":{"latitude":-44.076880,"longitude":170.781970,"altitude":478},"time":"2022-02-28T04:24:25.068840Z"}],"txInfo":{"frequency":917800000,"dr":5}}'
<class 'str'>
{'applicationID': '4', 'applicationName': 'RAK4061', 'devEUI': '007e47c0ac6183b2', 'deviceName': 'RAK4601', 'timestamp': 1646022265, 'fCnt': 24368, 'fPort': 1, 'data': 'SGVsbG8sIHdvcmxkIDE0MTM0', 'data_encode': 'base64', 'adr': True, 'rxInfo': [{'gatewayID': 'ac1f09fffe004ecf', 'loRaSNR': 5.5, 'rssi': -104, 'location': {'latitude': -44.07688, 'longitude': 170.78197, 'altitude': 478}, 'time': '2022-02-28T04:24:25.068840Z'}], 'txInfo': {'frequency': 917800000, 'dr': 5}}
SGVsbG8sIHdvcmxkIDE0MTM0 (Note - this is the data, encoded Base64
Hello, world 14134 (this is the decoded message
007e47c0ac6183b2 (This is the devEUI
1646022265 (This is the timestamp Linux epoch)
dt_object = 2022-02-28 17:24:25 (This is the decoded timestamo into normal date format
What I want to do is have the last two lines, immediately above, created using C# on an Arduino Mega.
Regards