# Has anyone used mqtt paho python library to send downlink message to the end device?

**URL:** <https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457>\
**Category:** Application Development\
**Created:** [January 17, 2020, 4:16pm UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457 "2020-01-17T16:16:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Njabulo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/njabulo/32/36273_2.png) [@Njabulo](https://www.thethingsnetwork.org/forum/u/Njabulo)\
**Post date:** [January 17, 2020, 4:16pm UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/1 "2020-01-17T16:16:19Z")

</div>

Most of the sources use MQTT Paho python library for uplink messages. I want to know whether I can send the downlink messages using this library.

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [January 18, 2020, 2:48pm UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/2 "2020-01-18T14:48:10Z")

</div>

Yes. See the source code of the (deprecated) Python SDK; [https://github.com/TheThingsNetwork/python-app-sdk/blob/master/ttn/ttnmqtt.py](https://github.com/TheThingsNetwork/python-app-sdk/blob/master/ttn/ttnmqtt.py)

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [January 19, 2020, 12:02pm UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/3 "2020-01-19T12:02:45Z")

</div>

By the way: you might want to define the logging callback, as Paho tends to [swallow many exceptions in callbacks](https://github.com/eclipse/paho.mqtt.python/blob/v1.5.0/src/paho/mqtt/client.py#L3421-L3425).

When looking at [the (deprecated) TTN SDK](https://github.com/TheThingsNetwork/python-app-sdk/blob/master/ttn/ttnmqtt.py), add something like:

```python
import logging
import paho.mqtt.client as mqtt
...
class MQTTClient:
    ...
    def connect(self):
        self.__client.on_log = self._on_log
        ...

    def _on_log(self, client, userdata, level, buf):
        """
        Log all MQTT protocol events, including the exceptions
        in callbacks that are caught by Paho.
        """
        logging_level = mqtt.LOGGING_LEVEL[level]
        logging.log(logging_level, buf)

```

Also, you might want to use TLS (and port 8883 rather than the non-TLS 1883). Just adding the following at some point before invoking `self.__client.connect` will do:

```python
# By default, on Python 2.7.9+ or 3.4+, the default certification
# authority of the system is used.
self.__client.tls_set()

```

Alternatively, explicitly refer to the Certificate Authority chain as used by the Let’s Encrypt server certificate, which is available in [https://console.thethingsnetwork.org/mqtt-ca.pem](https://console.thethingsnetwork.org/mqtt-ca.pem):

```python
self.__client.tls_set(ca_certs="mqtt-ca.pem")

```

I’ve never used the TTN SDK, and maybe the full SDK somehow actually supports TLS already (like given its reference to `GRPC_SSL_CIPHER_SUITES ` in its MQTT code, which given the name `GRPC` might very well be the result of some copy-paste though). But when looking at the Paho documentation I doubt that, and for a plain Paho client you’ll need to enable it explicitly.

Finally: beware that you’re actually _scheduling_ downlinks, not sending them. Timing is critical, and a downlink scheduled as the result of an uplink might very well only be transmitted after receiving the next uplink. (The TTN community network only supports Class A devices, which only allow for downlinks as a response to an uplink.)

---

<div class="post-metadata">

**Author:** ![Njabulo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/njabulo/32/36273_2.png) [@Njabulo](https://www.thethingsnetwork.org/forum/u/Njabulo)\
**Post date:** [January 23, 2020, 11:40pm UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/5 "2020-01-23T23:40:49Z")

</div>

Thanks Arjan. Concerning the downlinks. In order for me to work with Class C devices that means I have to setup the private network since, it is not supported on public network? Would be the same library still be compatible?

---

<div class="post-metadata">

**Author:** ![Florianh1](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/florianh1/32/25844_2.png) [@Florianh1](https://www.thethingsnetwork.org/forum/u/Florianh1)\
**Post date:** [April 24, 2020, 8:53am UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/6 "2020-04-24T08:53:51Z")

</div>

Do you already have your downlinks working with MQTT? since the ttn sdk for python is discontinued if there is a good way to create downlinks with something like paho, receiving uplinks works fine, but I can’t seem to find a way to create downlinks.

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [April 24, 2020, 9:10am UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/7 "2020-04-24T09:10:42Z")

</div>

Welcome, but please don’t bump multiple old topics with the same question. I’ve removed your other post. Also, a link to a reference solution (in the code of the deprecated SDK) has been provided in this very topic.

So: what is not working for you?

See also a simpler (but quite specific) working example in [Time synchronisation of a Node](https://www.thethingsnetwork.org/forum/t/time-synchronisation-of-a-node/1641/35).

---

<div class="post-metadata">

**Author:** ![Florianh1](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/florianh1/32/25844_2.png) [@Florianh1](https://www.thethingsnetwork.org/forum/u/Florianh1)\
**Post date:** [April 24, 2020, 9:39am UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/8 "2020-04-24T09:39:04Z")

</div>

Hello Arjan, sorry for the multiple bumps but I found the other topic later and maybe it had more in common with my “issue”. It is fairly simple I guess but I can’t seem to get a downlink on my TTN application data, when I simulate an uplink I will get it in my python application using paho, but I can’t find an example for sending downlinks to the application. And since the ttn sdk was discontinued I thought paho should be the way for python applications nowadays.

Edit: I basically just want to publish a message.

---

<div class="post-metadata">

**Author:** ![Florianh1](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/florianh1/32/25844_2.png) [@Florianh1](https://www.thethingsnetwork.org/forum/u/Florianh1)\
**Post date:** [April 24, 2020, 11:30am UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/10 "2020-04-24T11:30:40Z")

</div>

Hello Arjan,

I think what i’m just wandering if it is possible and how is it possible to publish a message on my ttn application schedule ready for being downlinked.

I’m not very familiar with Python and even less so with TTN but what I want to do is a mosquitto like operation like:  
mqttc.publish(’((app-id))r/devices/((dev-id))/down’, payload=“AA”)

if that is in any way possible? I dont completely understand how to use the example you sent in the link with my basic knowledge of Python.

---

<div class="post-metadata">

**Author:** ![gnsbglora](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gnsbglora/32/35697_2.png) [@gnsbglora](https://www.thethingsnetwork.org/forum/u/gnsbglora)\
**Post date:** [February 23, 2021, 4:00pm UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/11 "2021-02-23T16:00:13Z")

</div>

I know this is an old thread but since it took me a while to figure it out - here my solution:

```
!/usr/bin/python3 -u                                                                                                                      

import paho.mqtt.client as mqtt
import json
import time
import datetime
import sys
import base64

broker = "us-west.thethings.network" 
port = 8883
APPID = "your appid" 
DEVID = "your dev id"
PSW = "your key"
topic = APPID + "/devices/" + DEVID + "/down"

try: 
     str(sys.argv[1])
except IndexError:
     print("empty input")
     exit(1)
     
command = str(sys.argv[1])
message = '{"port":1,"payload_raw":"' + base64.b64encode(command.encode("ascii")).decode("ascii") + '"}' 

print('starting up ...')
def on_connect(client, userdata, flags, rc): 
     if rc==0:
         client.connected_flag=True #set flag
         print("connected OK")
         client.subscribe('+/devices/+/up')
     else:
         print("Bad connection Returned code=",rc)
         exit(1)

def on_publish(client,userdata,result):
     print("command:",command," userdata:",userdata," result:",result)
     print("data published \n")
     pass

client = mqtt.Client()
client.on_connect = on_connect 
client.on_publish = on_publish
#client.on_message = on_message
client.tls_set()
client.username_pw_set(APPID, PSW)
 
client.connect(broker, port, 60) 
client.publish(topic,message)
 
client.loop_start()
client.loop_stop()
client.disconnect()
```

---

<div class="post-metadata">

**Author:** ![kersing](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kersing/32/44777_2.png) [@kersing](https://www.thethingsnetwork.org/forum/u/kersing)\
**Post date:** [February 23, 2021, 4:30pm UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/12 "2021-02-23T16:30:33Z")

</div>

> [@gnsbglora](#):
>
> I know this is an old thread but since it took me a while to figure it out - here my solution:

Please take a moment to properly [format](https://www.thethingsnetwork.org/forum/t/how-do-i-format-my-forum-post-howto/8735) your post!

---

<div class="post-metadata">

**Author:** ![gnsbglora](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gnsbglora/32/35697_2.png) [@gnsbglora](https://www.thethingsnetwork.org/forum/u/gnsbglora)\
**Post date:** [February 23, 2021, 4:59pm UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/13 "2021-02-23T16:59:30Z")

</div>

better?

---

<div class="post-metadata">

**Author:** ![kersing](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kersing/32/44777_2.png) [@kersing](https://www.thethingsnetwork.org/forum/u/kersing)\
**Post date:** [February 23, 2021, 5:08pm UTC](https://www.thethingsnetwork.org/forum/t/has-anyone-used-mqtt-paho-python-library-to-send-downlink-message-to-the-end-device/33457/14 "2021-02-23T17:08:47Z")

</div>

Yep, for a programming language that depends on indentation the code makes a lot more sense now 😆
