nRF5 SDK for Thread and Zigbee v4.1.0
Thread MQTT-SN Example

Table of Contents

This information applies to the following SoCs: nRF52833 and nRF52840.

The Thread MQTT-SN example demonstrates the interaction between nodes with the use of OpenThread and built-in MQTT-SN protocol.

The purpose of this example is to toggle the LED on the subscriber board by publishing LED_ON and LED_OFF commands from the publisher board. Since MQTT-SN network topology demands a gateway, which serves as a translator between MQTT-SN clients and an MQTT broker, a third-party MQTT-SN gateway has been used. It has been taken from Eclipse Paho project. For additional information on the gateway used, see Eclipse Paho MQTT-SN C/C++ client for Embedded platforms.

As the MQTT-SN network in this example runs on Thread, the MQTT-SN Gateway plays the role of a Thread Border Router, which means it is a gateway between the Internet and the Thread Network. It can be run on a Raspberry Pi with Raspbian-based firmware. To connect the MQTT-SN Gateway to the Thread network, you need Nordic's nRF52840 or nRF52833 Development Kit running the Network Co-Processor (NCP). For details on how to set up a Thread Border Router and run MQTT-SN Gateway, refer to the Thread Border Router documentation.

Both client nodes indicate their Thread network state on LED1. When the device is not connected to the network, the LED starts blinking. Once the device connects to a network, either as a Child or as a Router, the LED stops blinking and remains turned on. On startup, nodes automatically enter the Thread network with default parameters. The first device that is turned on becomes a Leader of the network, while the rest of the nodes become Children and Routers.


Setup

You can find the source code and the makefiles in the following folders:

Subscriber

The subscriber node is controlled by specific actions triggered by pressing the buttons. The effect of pressing the buttons is reflected on the LED state.

Subscriber button assignments

When a MQTT-SN client has subscribed to a topic, every message published by other nodes on that topic is sent to the subscribing client. Unsubscribing from a topic cancels the subscription, so that no messages on that topic are forwarded to the client.

Subscriber LED assignments

The Subscriber uses LED assignments as described in Thread BSP LED and button reference for the following LEDs:

The following LED reserved for user application purposes are assigned in this example on subscriber:

Publisher

The subscriber node is controlled by specific actions triggered by pressing the buttons. The effect of pressing the buttons is reflected on the LED state.

Publisher button assignments

The button assignments are identical to Subscriber button assignments, with the exception of the following button:

Note
The Publisher node sends LED_ON and LED_OFF commands alternately, regardless of the state of LED3 on the subscriber board. It might happen that the state of LED4 on the subscriber board and the published command are the same. In such a situation, pressing Button 4 on the publisher board will have no effect on the state of LED3 on the subscriber board, despite fully functional and working network. Pressing Button 4 on the publisher board again will change the command and the publisher will toggle the LED3 state.

Publisher LED assignments

The publisher uses LED assignments as described in Thread BSP LED and button reference for the following LEDs:

The following LED reserved for user application purposes are assigned in this example on publisher:


Testing

  1. Build the example according to the instructions in Building examples.
  2. Turn the MQTT-SN gateway on according to the instructions in Thread Border Router.
  3. Program a development board with the MQTT-SN Subscriber application.
  4. Program another development board with the MQTT-SN Publisher application.
  5. Let the clients find the MQTT-SN gateway by pressing Button 2 on both boards.
  6. Connect the clients to the gateway by pressing Button 3 on both boards.
  7. Subscribe by pressing Button 4 on the subscriber client board.
  8. Try toggling LED3 on the subscriber by pressing Button 4 on the publisher board.
  9. Unsubscribe by pressing Button 4 on the subscriber board again. As a result, pressing Button 4 on the publisher board will have no effect on the state of LED3 on the subscriber board.
Note
The majority of the messages is sent through a broker in the cloud. It might be necessary to wait a few seconds for all messages to be delivered.

Documentation feedback | Developer Zone | Subscribe | Updated