Scheduling a downlink with MQTT

I have a RAK2287 gateway/application server/network server. I have successfully written a python script to access the uplink messages from my node, using paho-mqtt.

I would now like to schedule downlinks using MQTT. I have found this page (https://www.chirpstack.io/application-server/integrations/mqtt/) and I’ve attempted to follow it, using paho-mqtt’s connect(), subscribe() and publish() commands. Unfortunately, despite my best efforts, I can’t schedule a downlink.

I don’t know what’s wrong because I don’t know of anywhere in chirpstack I can go to see why my MQTT messages are being rejected. Can anyone help with debugging?

This is the basics of my code:

import paho.mqtt.client as mqtt
import json
import base64

def on_connect(client,userdata,flags,rc):
    print("Connected with result code:"+str(rc))
    client.subscribe("application/4/device/MYEUI/command/down")

mqttc= mqtt.Client()
mqttc.on_connect=on_connect

mqttc.connect("192.168.xxx.xxx",1883,10)

datatosend = b'\x01\x02\x04\x00\x00\x00\x00'
datatosendb64 = base64.b64encode(datatosend)

packettosend = {
    "confirmed": 'true',
    "fPort": 3,
    "data": str(datatosendb64)
}

json_packettosend = json.dumps(packettosend)

mqttc.publish("application/4/device/MYEUI/command/down", json_packettosend, 0, False)

Many thanks for any help you can offer.

Might sound stupid, but have you tried quoting your base64 string?
I’d look at the application-server logs for errors

Yes, I’ve printed the entire json structure. It looks like this:
{"confirmed": "true", "fPort": 3, "data": "AQIEAAAAAA=="}

Can you advise on how I can access the application server logs?

This is coming too late, but in my case it was due to boolean uppercase True instead of true

Correct one

Log from chirpstack (v4) log

2023-05-16T13:23:37.412492Z ERROR chirpstack::integration::mqtt: Processing command error: invalid type: string “true”, expected a boolean at line 1 column 51 topic=“application/bd6d3d7f-c699-4ef9-8603-481aa9412675/device/ac1f09fffe08e82c/command/down” qos=0

Hope that helps someone

@sergi_jini

Try Boolean without single quote

packettosend = {
    "confirmed": true,
    "fPort": 3,
    "data": str(datatosendb64)
}

Hello @sagarpatel
I mean this one actually worked for me. It should be ‘True’ instead of ‘true’ or true

packettosend = {
    "confirmed": 'True',
    "fPort": 3,
    "data": str(datatosendb64)
}

It’s probably working for reasons you aren’t expecting. You’re setting confirmed to a string, which may be interpreted as not-false. I wouldn’t rely on this behavior, though.

@bconway actually you are correct, I posted the code before I corrected it (was too happy it worked eventually :slight_smile: ). It should be True, capital T, without quotes, to avoid passing it as a text.

image

Correct format:
image

Indeed, passing boolean in quotes will not work

Thanks for noticing.
@sagarpatel I guess your initial remark was also correct about quotes.

1 Like