Package: ttnmqtt
The Things NetworkTable of ContentsDescriptionMQTTClientconnectdisconnectstartstartBackgroundstopBackgroundsetUplinkCallbackuplinkCallbacksetConnectBehaviorsetPublishCallbackpublishCallbackPublishLicenseDescriptionThis package provides you an easy way to connect to The Things Network
via MQTT. Take note that, you’ll first need to create an application
with a device to run the constructor of the MQTT client because you need
to provide, an applicationID and a deviceID. First include the package
in your file like this:fromttnmqttimportMQTTClientasmqttMQTTClientThe class constructor can be called following this scheme:mqtt(APPID,APPEUI,PSW)APPID: this the name you gave your application when you created
it.APPEUI: this the unique identifier of your application on the TTN
platform.PSW: it can be found at the bottom of your application page underACCESS KEYS. All the above informations can be found in your The
Things Network console. The constructor returns an MQTTClient object
set up with your application informations, ready for connection.connectConnects the previously created client to the The Things Network MQTT
broker by default.client.connect([address],[port])address: the address of the MQTT broker you wish to connect to.
Default toeu.thethings.networkport: the port on which you wish to connect. Default to1883disconnectDisconnects the MQTT client from which we call the method. Also able to
stop a forever loop in case the client was running on a loop launched by
thestart()method.client.disconnect()startStart a loop as the main loop of your process. You wont be able to run
anything else at the same time on this script.client.start()Take note that a loop need to be started in order to receive uplink
messages.startBackgroundStarts a loop for the client in the background so that it’s possible to
run another process (such as a web server) in the same script.client.startBackground()stopBackgroundStops a loop which was started with thestartBackground()method. It
also disconnect the client.client.stopBackground()setUplinkCallbackSet the callback function, to be called when an uplink message is
received.client.setUplinkCallback(uplinkCallback)uplinkCallbackThe callback function must be declared in your script following this
structure: *uplinkCallback(msg, client)*msg: the message
received by the client *client: the client from which the callback
is executed are callingOn each message reception, you should seeMESSAGE RECEIVEDin the
console, and the callback will be executed.setConnectBehaviorChange the connect callback function, following the paho-mqtt standart.client.setConnectBehavior(custom_function)custom_function(client, userdata, flags, rc): the function which
will be the new connection behavior for our MQTT client.client: the MQTT client from which we call the callback.userdata: the data of the user. Default to''flags: connection flagsrc: result from the connect method.0if the connection
succeeded.clickherefor more
information on the paho-mqtt package.setPublishCallbackSet the publish callback function, following the paho-mqtt standart.client.setPublishCallback(publishCallback)publishCallbackpublishCallback(mid, client): the function which will be the new
publish behavior for our MQTT client.mid: it matches the mid variable returned from the publish call
to allow sent messages to be tracked.client: the MQTT client from which we call the callback.publishPublishes a message to the MQTT broker.client.publish(deviceID,message)deviceID: the ID of the device you wish to send the message to.message: the message to be published to the broker. The message
that’s sent to the TTN broker needs to be a string and can follow
this example (it’s not mandatory but they are mostly build on this
format):json  {"port": 1, "confirmed": false, "payload_raw":"AA=="}This
message will send the payload 00 to your device.LicenseSource code for The Things Network is released under the MIT License,
which can be found in theLICENSEfile. A list of authors
can be found in theAUTHORSfile.