Can anybody help to integrate chirptsack command with paho mqtt.
Thank you
from paho.mqtt import client as mqtt_client
import threading
import base64
import random
broker = None
port = 1883
client = None
topic_up = “application/1/device/+/event/up”
client_id = f’python-mqtt-{random.randint(0, 100)}’
def connect_mqtt() → mqtt_client:
“”“Connects MQTT client to broker”“”
def on_connect(client, userdata, flags, rc):
if rc == 0:
print(“Connected to MQTT Broker!”)
else:
print(“Failed to connect, return code %d”, rc)client = mqtt_client.Client(client_id) client.on_connect = on_connect client.connect(broker, port) return client
def subscribe(client: mqtt_client):
“”“MQTT subscribe function which the MQTT client listens on”“”
def on_message(client, userdata, msg):decoded_payload = str(msg.payload.decode("utf-8")) frame = json.loads(decoded_payload) deviceEUI = frame['devEUI'] frameCounter = frame['fCnt'] SNR = frame["rxInfo"][0]['loRaSNR'] RSSI = frame["rxInfo"][0]['rssi'] datarate = frame["txInfo"]['dr'] frequency = frame["txInfo"]['frequency'] / 1000000 payload = base64.b64decode(frame['data']).hex() client.subscribe(topic_up) client.on_message = on_message
def publish(client: mqtt_client, device_eui):
“”“Downlink new settings to sensor via ChirpStack”“”
payload = “AA BB FF”
topic_down = f"application/1/device/{device_eui}/command/down"frame = { "confirmed": True, "fPort": 2, "data": base64.b64encode(bytes.fromhex(payload)).decode() } data = json.dumps(frame) client.publish(topic_down, data)
def start_mqtt_client(hostname):
“”“Starts a MQTT client, which subscribes to a topic in a loop”“”
global client
global broker
broker = hostname
client = connect_mqtt()
time.sleep(0.1)
subscribe(client)
client.loop_start()
Thanks for the reply
Im little bit new can you please help to figure out what this code does.