How to send a downlink to nodes via python and MQTT?


(Bismarck) #1

Hello,
I used example on TTN (https://www.thethingsnetwork.org/docs/applications/python/) to build a python SDK to send and receive message. I have successfully received uplink message and stored them. However, I had no idea about how to use codes in this website (https://www.thethingsnetwork.org/docs/applications/python/api-reference.html#send) especially the code following:

image

In addition, do I need this code to set? And how to use this?
image


(Juan Montoya) #2

Hi Bismarck,

a) I had the same problem. I just resolved by sending the message in base64 instead of hexadeximal. This means: 0 is “AA==” and 1 is “AQ==”. I think there is some bug in TTN because I couldn’t send it with hexadecimal.

Now, the following script sends a downlink with value 1:

import ttn
app_id = "app_id_XX"
access_key = "access_key_XXX"
handler = ttn.HandlerClient(app_id, access_key)
mqtt_client = handler.data()
mqtt_client.connect()
mqtt_client.send(dev_id="dev_id_xxx",  pay= "AQ==", port=1, conf=True, sched="replace")
mqtt_client.close()

b) You don’t need to use “set_downlink_callback”.

Best regards,

Juan


(Bismarck) #3

Thanks a lot! I have worked it out a few days ago. But your suggestion about not using “set_downlink_callback” is very useful.
By the way, have you figured out what is the function of “set_downlink_callback” ? Is it about ACK confirmed?