Using The Python MQTT-SN Client

  • The MQTT-SN client used here is the client from the RSMB source files but upgraded to python 3 and modified to work in a similar fashion to the Paho MQTT client.

    You should note it is still a work in progress so I would be grateful if you would let me know of any problems with it.

    Downloads are at the end.

    The client consists of 4 files.

    MQTT-SN-python-client-files

    You can copy these source files into the site packages folder under a suitable folder (e.g mqtt-sn) or alternatively set the path in your scripts for access the client.

    Example:

    sys.path.append('c:/python34/steve/mqttsclient/')

    Client Files Overview

    The MQTTSN.py handles the packet packing and unpacking, and you don’t need to edit this.



    It has a very important function right at the end called unpackPacket() shown below:

    def unpackPacket((buffer, address)):
      if MessageType(buffer) != None:
        packet = objects[MessageType(buffer)]()
        packet.unpack(buffer)
      else:
        packet = None
      return packet, address
    

    This function extracts the message Type from the received packet and calls the appropriate function to decode the packet.

    Note: objects is a list of message type names and is just above the function.

    objects

    The function is called from the MSQTTInternal.py script.

    The MSQTTInternal.py script handles the incoming packet in the receive function.

    receive-function

    The receive function is called as part of a loop or can be called manually.

    Normally it is started from the call function (at bottom of script ) which itself is started from the MQTTClient script when you do a client.start() or client.loop_start()

    mqtt-sn-call-function

    This is the loop() as per the MQTT client

    This file also has two important functions at the top. They are lookfor() and waitfor.

    These functions basically look for a message type and wait for a message type they are generally used as follows:

    look for CONNACK
    wait for CONNACK

    Note: You need to do a lookfor before a waitfor or the waitfor will always fail.

    The observe if statement which is part of the receive decides if the message type just received matches what we are looking for.

    observe-function

    Underneath you will see a collection of if and if else statements which look for packet type and then call the appropriate callback function if available

    mqtt-sn-test-packet-trigger-callback

     MQTTSNclient.py File

    The main file is the MQTTSNclient.py file.

    The file has two classes the callback and the Client classes.

    You will need to include it at the top of you scripts using:

    import MQTTSNclient.callback as callback
    import MQTTSNclient.Client as Client
    

    Note : MQTTSNclient is the folder that contains the scripts.

    Example Usage Publish and Subscribe Script

    The following script subscribes to a topic and publishes messages on the same topic and displays the published and received messages.

    We start with the imports.

    mqtt-sn-script-0

    Notice the sys.path call which you will need to set depending on where you place the MQTT-SN client files.

    Now we create the connection:mqtt-sn-script-1

    Notice the use of lookfor() followed by waitfor(). You should also notice flags like client.connected_flag=False.

    This follows the same style as used in the MQTT client and so you could use the following to replace the lookfor and waitfor sections.

    mqtt-sn-script-2

    I’ve shown the alternative code using the flags that are set in the callback functions as per the mqtt client.

    The callback functions are in the MQTTSNclient script but can be overridden in the script as I’ve done with the messgeArrived callback which we see later.

    Now we subscribe and get the topic_id which we use when we publish.

    But first we have to start a loop to process the callbacks.

    mqtt-sn-script-3

    The messgeArrived callback is overridden in the script as shown below

    mqtt-sn-script-5

    The script output is shown below:

    mqtt-sn-script-4

    Client Download

    Examples Download

    The following example scripts are included:

    1. The pub_sub-sn.py script from above.
    2. A standalone publish script publish-sn.py that uses the topic registration to publish.
    3. A publish script that publishes without a connection. (quick_publish_sn.py)
    4. A stanalone subscribe script that subsribes to a topic and displays received messages in a continuous loop
    5. A Gateway discovery script which finds a gateway and published to it (gateway.py)
    6. A multicast scan test script. Change the port ant multicast group to see messages on that group/port (multicast-scan.py)

    Related Tutorials

Please rate? And use Comments to let me know more
[Total: 1    Average: 3/5]

Leave a Reply

Your email address will not be published. Required fields are marked *