MQTT-SN Publish and Subscribe Test Tools

Anyone familiar with MQTT is familiar with the mosquitto_pub and mosquitto_sub test tools.

MQTT-SN also comes with publish and subscribe equivalents mqtt-sn-pub and mqtt-sn-sub.

The tools are available from this Github link

You will need to compile them to run them which is straightforward on LInux but I haven’t managed to do it on Windows.

There are actually three tools available they are mqtt-sn-pub,mqtt-sn-sub, and mqtt-sn-dump.

Using The Tools

The following is taken from the readme file that comes with the tools.

Publishing
———-

Usage: mqtt-sn-pub [opts] -t <topic> -m <message>

-d Increase debug level by one. -d can occur multiple times.
-f <file> A file to send as the message payload.
-h <host> MQTT-SN host to connect to. Defaults to ‘127.0.0.1’.
-i <clientid> ID to use for this client. Defaults to ‘mqtt-sn-tools-‘ with process id.
-k <keepalive> keep alive in seconds for this client. Defaults to 10.
-e <sleep> sleep duration in seconds when disconnecting. Defaults to 0.
-m <message> Message payload to send.
-l Read from STDIN, one message per line.
-n Send a null (zero length) message.
-p <port> Network port to connect to. Defaults to 1883.
-q <qos> Quality of Service value (0, 1 or -1). Defaults to 0.
-r Message should be retained.
-s Read one whole message from STDIN.
-t <topic> MQTT-SN topic name to publish to.
-T <topicid> Pre-defined MQTT-SN topic ID to publish to.
–fe Enables Forwarder Encapsulation. Mqtt-sn packets are encapsulated according to MQTT-SN Protocol Specification v1.2, chapter 5.5 Forwarder Encapsulation.
–wlnid If Forwarder Encapsulation is enabled, wireless node ID for this client. Defaults to process id.
–cport <port> Source port for outgoing packets. Uses port in ephemeral range if not specified or set to 0.

Subscribing
———–

Usage: mqtt-sn-sub [opts] -t <topic>

-1 exit after receiving a single message.
-c disable ‘clean session’ (store subscription and pending messages when client disconnects).
-d Increase debug level by one. -d can occur multiple times.
-h <host> MQTT-SN host to connect to. Defaults to ‘127.0.0.1’.
-i <clientid> ID to use for this client. Defaults to ‘mqtt-sn-tools-‘ with process id.
-k <keepalive> keep alive in seconds for this client. Defaults to 10.
-e <sleep> sleep duration in seconds when disconnecting. Defaults to 0.
-p <port> Network port to connect to. Defaults to 1883.
-q <qos> QoS level to subscribe with (0 or 1). Defaults to 0.
-t <topic> MQTT-SN topic name to subscribe to. It may repeat multiple times.
-T <topicid> Pre-defined MQTT-SN topic ID to subscribe to. It may repeat multiple times.
–fe Enables Forwarder Encapsulation. Mqtt-sn packets are encapsulated according to MQTT-SN Protocol Specification v1.2, chapter 5.5 Forwarder Encapsulation.
–wlnid If Forwarder Encapsulation is enabled, wireless node ID for this client. Defaults to process id.
-v Print messages verbosely, showing the topic name.
-V Print messages verbosely, showing current time and the topic name.
–cport <port> Source port for outgoing packets. Uses port in ephemeral range if not specified or set to 0.

Dumping
——-

Displays MQTT-SN packets sent to specified port.
Most useful for listening out for QoS -1 messages being published by a client.

Usage: mqtt-sn-dump [opts] -p <port>

-a Dump all packet types.
-d Increase debug level by one. -d can occur multiple times.
-p <port> Network port to listen on. Defaults to 1883.
-v Print messages verbosely, showing the topic name.

Publish Examples

The following screen shot shows :

  • a normal publish
  • Publish with retain flag
  • Puplish with retain flag and qos of 1

mqtt-sn-pub-examples

Subscribe Examples

The screen shot below shows how to subscribe with a QOS of 1 and use the debug flag.

You should notice the amount of information presented by the debug flag and also the fact that we receive the retained message from the previous publish

 

mqtt-sn-subscribe-example-1

To remove the retained message we send a null message with the retained flag set as shown below:

mqtt-sn-remove-retain

To subscribe or publish using a persistent connection you use the c flag.

mqtt-sn-clean

Related Tutorials and Resources

Please rate? And use Comments to let me know more

Leave a Reply

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