How can I send Downlink to end device trough MQTT - USING PYTHON

Issue: send Downlink to end device trough MQTT using python

Setup:MQTT

LoRa® Server:RAK7258

Details:
I would like to automatically send a message to a node through my RAK7258 using a python program.
I know how to do it manually thanks to the Topic “How can I send Downlink to end device through MQTT”, but I need to know if it is possible to make a python program that calls my RAK7258 and sends a message to the node. Any suggestions?
Thanks

The topic with that name doesn’t show how to do it with MQTT - it ends with sending it via the console.

Have you got an MQTT client working and sending downlinks?

There are many Python libraries for using MQTT - how are you on coding in Python?

Hello
Yes, I know that the topic ends in how to send the message from the console. I have a broker that can send the downstream message but I don’t know how to send it. The Downlink Topic
It is {{application_name}} / {{device_name}} / down but I need an example of how to send it so that the RAK will forward it to the node

So the answer is No?

Have you got uplinks appearing in your MQTT client (a broker is the server)

And the answer to this is?

I’m not coding it yet, I don’t know where to start … that’s the point

What I mean is how good are you at Python?

And have you got uplinks appearing in your MQTT client?

I am an advanced python programmer
Yes, uplinks like this appear {“applicationID”:“5”,“applicationName”:“Data_Pig”,“devEUI”:“a84041af31828e9b”,“deviceName”:“2/DRAGINO/ES/0”,“timestamp”:1618438941,“fCnt”:411,“fPort”:2,“data”:“00000000000000003CFF41”,“data_encode”:“hexstring”,“adr”:true,“rxInfo”:[{“gatewayID”:“60c5a8fffe783da9”,“loRaSNR”:10.8,“rssi”:-50,“location”:{“latitude”:0.000000,“longitude”:0.000000,“altitude”:0}}],“txInfo”:{“frequency”:867700000,“dr”:0}}

I am trying to post this message to simiar a mqtt download message
mosquitto_pub -d -t ‘Data_Pig / 2 / DRAGINO / ES / 0 / down’ -m ‘{“port”: 1, “data”: “030100”}’

Without setting up a test, I think the use of / in the device name is probably confusing the whole setup given that the / is used as a path separator - how can the software tell which part of the path is which?

Can you setup a test device with a plain name without / in it even if it doesn’t really exist, so you can try with that?

Ok. I renamed the device like DRAGINO_IO The message now is Wed Apr 14 22:42:51 2021 daemon.info appSrv[23626]: [Uplink] - {“applicationID”:“5”,“applicationName”:“Data_Pig”,“devEUI”:“a84041af31828e9b”,“deviceName”:“DRAGINO_IO”,“timestamp”:1618440171,“fCnt”:452,“fPort”:2,“data”:“00000000000000003CFF41”,“data_encode”:“hexstring”,“adr”:true,“rxInfo”:[{“gatewayID”:“60c5a8fffe783da9”,“loRaSNR”:10.0,“rssi”:-45,“location”:{“latitude”:0.000000,“longitude”:0.000000,“altitude”:0}}],“txInfo”:{“frequency”:867300000,“dr”:0}}

And what happens when you try the downlink?

I try this message
mosquitto_pub -d -t ‘Data_Pig/DRAGINO_IO/down’ -m '{“port”:1,“data”:“030100”}
to simulate this

but nothing happen

Nothing from the command prompt? Nothing on the gateway MQTT log?

Sorry for the length of the message

We already have 3 RAK7258 and we are very happy with the product. All the systems are equal

  1. RAK7258 as Lora Gateway
  2. An MQTT broker installed on a raspy
  3. One DRAGINO MULTIPLE I / O CONTROLLER LT22222-L network node
    Gateway configuration is
    image
    image
    image
    image
    image
    image
    image
    image
    image
    image
    image

This is the message that the LT22222-L sends every so often

Sat Apr 24 12:43:37 2021 daemon.info appSrv[31418]: [Uplink] - {“applicationID”:“1”,“applicationName”:“Data_Pig”,“devEUI”:“a84041af31828e9b”,“deviceName”:“DRAGINO_IO”,“timestamp”:1619261017,“fCnt”:48,“fPort”:2,“data”:“00000000000000003CFF41”,“data_encode”:“hexstring”}

Now a new goal is to send a downlink message to DRAGINO MULTIPLE I / O CONTROLLER LT22222-L to open or close a intarnal relay

If I do it from here it works
image

But I need send a automatic message to the node, but if I run the following program into the raspy to post a message to the node it doesn’t work (I try 3 different formats)

import paho.mqtt.client as paho
broker=“localhost”
port=1883
def on_publish(client,userdata,result): #create function for callback
print(“data published \n”)
pass
client1= paho.Client(“control1”) #create client object
client1.on_publish = on_publish #assign function to callback
client1.connect(broker,port) #establish connection
data=’{“confirmed”: “true”, “fPort”: 1, “data”: “030100”}’
ret= client1.publish(‘Data_Pig/DRAGINO_IO/down’,data) # publish option 1
ret= client1.publish(‘Data_Pig/1/DRAGINO_IO/a84041af31828e9b/tx’,data) # publish option 2
ret= client1.publish(‘gateway/a84041af31828e9b/tx’,data) # publish option 3

I can’t see anything

It seems that the message does not go from the Gateway to the Node
Could it be some incorrect configuration of the gateway?

IIRC downlink topics match uplink ones, they use raw ID’s not custom names.

You’re mixing up fields you see in the mqtt message payload with the topic pieces.

Subscribe to the uplink with the -v flag to mosquitto_sub so you see the actual topic, then use its complement for the downlink.

Once it works at the command line implement in Python.

Hi.
Sorry I don`t understand

I use
mosquitto_sub -v -d -t Data_Pig/#

The message was
Client mosqsub|26582-monegros sending PINGREQ
Client mosqsub|26582-monegros received PINGRESP
Client mosqsub|26582-monegros received PUBLISH (d0, q0, r0, m0, ‘Data_Pig/1/DRAGINO/TH/3/up’, … (398 bytes))
Data_Pig/1/DRAGINO/TH/3/up {“applicationID”:“1”,“applicationName”:“Data_Pig”,“devEUI”:“a840416151827712”,“deviceName”:“1/DRAGINO/TH/3”,“timestamp”:1620246251,“fCnt”:3824,“fPort”:2,“data”:“CBE10A3B0205017FFF7FFF”,“data_encode”:“hexstring”,“adr”:true,“rxInfo”:[{“gatewayID”:“60c5a8fffe783da9”,“loRaSNR”:8.5,“rssi”:-84,“location”:{“latitude”:0.000000,“longitude”:0.000000,“altitude”:0}}],“txInfo”:{“frequency”:867300000,“dr”:0}}

How does the uplink message have to be?

A new software using ID , but nothing happen

import paho.mqtt.client as paho
broker=“localhost”
port=1883
def on_publish(client,userdata,result): #create function for callback
print(“data published \n”)
pass
client1= paho.Client(“control1”) #create client object
client1.on_publish = on_publish #assign function to callback
client1.connect(broker,port) #establish connection
data=’{“confirmed”: “true”, “fPort”: 1, “data”: “030100”}’
ret= client1.publish(‘1/a84041363182277d/down’,data) # publish option 1
ret= client1.publish(‘a84041363182277d/down’,data) # publish option 2
ret= client1.publish(‘1/a84041363182277d/tx’,data) # publish option 3
ret= client1.publish(‘a84041363182277d/tx’,data) # publish option 4