Eclipse Paho is an umbrella project on a mission to provide high quality implementations of tools and libraries for M2M communications.
It covers MQTT client implementations in several programming languages such as Java, Python, Go, etc.
Follow this guide to learn how to connect to The Things Stack MQTT Server, to receive and to send messages using the Eclipse Paho client.
This document contains instructions to use Eclipse Paho MQTT Python client library, which implements MQTT v3.1 and v3.1.1 protocol. To compare this library with other Paho project implementations visit Eclipse Paho Downloads page. To find more about the usage of the Python implementation, visit this page.
The examples in this guide are suitable for The Things Stack Open Source deployment. If you are using a different The Things Stack deployment, make sure your read a Note on Using the tenant ID.
Prerequisites
-
Python v2.7 or v3.x installed on your system.
-
Eclipse Paho MQTT Python client installed on your system.
Subscribing to Upstream Traffic
This section follows the example for subscribing to upstream traffic in MQTT Server guide.
To keep things simple, you can use the existing Python scripts from the examples folder contained in your installation folder and adjust them according to your setup.
Enter the examples folder and create a new file named subscribe.py
.
Open the file you created, paste the code below and modify it to match your setup:
import context
import paho.mqtt.subscribe as subscribe
m = subscribe.simple(topics=['#'], hostname="thethings.example.com", port=1883, auth={'username':"app1",'password':"NNSXS.VEEBURF3KR77ZR.."}, msg_count=2)
for a in m:
print(a.topic)
print(a.payload)
Save the file and run it with:
python subscribe.py
Running this script will show the most recent msg_count
messages published during the last 60 seconds.
To use TLS for security, change the port value to 8883
and pass the tls
argument to the simple
function according to its definition, where tls
has to at least contain the path to the CA certificate for your deployment.
Publishing Downlink Messages
This section follows the example for publishing downlink messages in MQTT Server guide. See Publishing Downlink Messages for a list of available topics.
Create a new file named publish.py
in the examples folder.
Open the file you created and paste the code below:
import context
import paho.mqtt.publish as publish
publish.single("v3/{application-id}/devices/{device-id}/down/push", '{"downlinks":[{"f_port": 15,"frm_payload":"vu8=","priority": "NORMAL"}]}', hostname="thethings.example.com", port=1883, auth={'username':"app1",'password':"NNSXS.VEEBURF3KR77ZR.."})
Note:
For scheduling downlink messages, thef_port
values from 1
to 233
are allowed.
Save the file and run it the terminal with:
python publish.py
You will see the scheduled message in the console under the Live data tab and your end device will receive the message after a short time.
In case of using TLS, adjust the port
value and pass the tls
argument to the simple
function as described in section above.