I’ve got it working, finally!
Noting the comment at https://www.chirpstack.io/application-server/integrations/mqtt/ that I might need to use the /tx
topic instead of the /command/down
topic, I tried /tx
.
This time, I finally saw messages relevant to the downlink in the application server log (accessed with journalctl -f -n 100 -u chirpstack-application-server
) as follows:
Oct 09 10:20:47 rak-gateway chirpstack-application-server[27928]: time="2020-10-09T10:20:47+01:00" level=info msg="integration/mqtt: data-down payload received" topic=application/3/device/MYEUI/tx
Oct 09 10:20:47 rak-gateway chirpstack-application-server[27928]: time="2020-10-09T10:20:47+01:00" level=error msg="integration/mqtt: tx payload unmarshal error: json: cannot unmarshal string into Go struct field DataDownPayload.confirmed of type bool" data_base64="eyJjb25maXJtZWQiOiAidHJ1ZSIsICJmUG9ydCI6IDAsICJkYXRhIjogImInTVRJME9BPT0nIn0="
The first message gave me encouragement that /tx
is the correct topic to use. The second one made me think the issue was the confirmed
field in my json. It was complaining that I had not supplied a bool.
The relevant line in my code was:
"confirmed": 'true'
Indeed, I hadn’t supplied a bool. The problem was that if I changed this code to
"confirmed": true
then I got this error message when I ran the code:
Traceback (most recent call last):
File “mqtt_chirpstack_pHat_send.py”, line 82, in
“confirmed”: true,
NameError: name ‘true’ is not defined
Turns out that assigning a variable to be true in python requires True
to have a capital T!
Final issue:
Oct 09 10:34:45 rak-gateway chirpstack-application-server[27928]: time="2020-10-09T10:34:45+01:00" level=error msg="integration/mqtt: tx payload unmarshal error: illegal base64 data at input byte 1" data_base64=eyJjb25maXJtZWQiOiB0cnVlLCAiZlBvcnQiOiAwLCAiZGF0YSI6ICJiJ01USTBPQT09JyJ9
Turns out I need to strip the b'
from the beginning and the '
from the end of my base64 data so:
"data": (str(datatosendb64))[2:len(str(datatosendb64))-1]
Hey presto it now works!
The scheduled message is shown in the Download queue in the chirpstack GUI.
The application server log shows:
Oct 09 10:45:48 rak-gateway chirpstack-application-server[27928]: time="2020-10-09T10:45:48+01:00" level=info msg="downlink device-queue item handled" confirmed=true dev_eui=MYEUI f_cnt=1
And my node reports and downloads the downlink when asked to.
So my code now looks like this:
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/tx")
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)[2:len(str(datatosendb64))-1]
}
json_packettosend = json.dumps(packettosend)
mqttc.publish("application/4/device/MYEUI/tx", json_packettosend, 0, False)
Please note that MYEUI
should be replace with your own DevEUI, 192.168.xxx.xxx
should be replaced with the IP address of your own Chirpstack Server and datatosend
should be replaced with the bytes you want to send.
Sorry about the long post but I hope it might help others who are looking to schedule a downlink in Chirpstack using MQTT.