The Thread MQTT-SN example demonstrates the interaction between nodes with the use of OpenThread and built-in MQTT-SN protocol.
- Thread MQTT-SN Subscriber subscribes to a topic corresponding to its BSP_LED_2 state.
- Thread MQTT-SN Publisher publishes data on that topic.
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.
Description
Both client nodes indicate their Thread network state on BSP_LED_0. When the device is not connected to the network, the LED blinks at a 100-ms period. 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.
The MQTT-SN network state of the clients is indicated on BSP_LED_3. When the device is not connected to the network, the LED is off. Once the device connects to a network, the LED is turned on.
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 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.
Subscriber
The subscriber node is controlled by specific actions triggered by pressing the buttons:
- Pressing BSP_BUTTON_1 results in:
- sending a multicast SEARCH GATEWAY message, which launches the gateway discovery procedure. It is necessary to connect to the gateway.
- Pressing BSP_BUTTON_2 results in:
- sending a unicast CONNECT message to the gateway, which connects the node to the MQTT-SN network if the node is not connected to the gateway. Once the connection is established, a topic is registered with a unicast REGISTER message.
- sending a unicast DISCONNECT message to the gateway, which disconnects the node from the MQTT-SN network if the node is connected to the MQTT-SN network.
- Pressing BSP_BUTTON_3 results in:
- sending a unicast SUBSCRIBE message to the gateway when the client is not subscribing. If the node is not connected to the network, pressing the button has no effect.
- sending a unicast UNSUBSCRIBE message to the gateway when the client is subscribing. If the node is not connected to the network, pressing the button has no effect.
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
- BSP_LED_0: Thread network state:
- Blinking – Disconnected.
- Solid - Connected.
- BSP_LED_2: Toggled by publisher.
- BSP_LED_3: MQTT-SN network state:
- Off - Disconnected.
- On - Connected.
Subscriber button assignments
- BSP_BUTTON_1: Search for a gateway..
- BSP_BUTTON_2: Connect or disconnect from the MQTT-SN network.
- BSP_BUTTON_3: Send a SUBSCRIBE/UNSUBSCRIBE message.
Publisher
The publisher node is controlled by specific actions triggered by pressing the buttons:
- Pressing BSP_BUTTON_1 results in:
- sending a multicast SEARCH GATEWAY message, which launches the gateway discovery procedure. It is necessary to connect to the gateway.
- Pressing BSP_BUTTON_2 results in:
- sending a unicast CONNECT message to the gateway, which connects the node to the MQTT-SN network if the node is not connected to the gateway. Once the connection is established, a topic is registered with a unicast REGISTER message.
- sending a unicast DISCONNECT message to the gateway, which disconnects the node from the MQTT-SN network if the node is connected to the MQTT-SN network.
- Pressing BSP_BUTTON_3 results in sending a unicast PUBLISH message to the gateway. If the node is not connected to the network, pressing the button has no effect.
- Note
- The Publisher node sends LED_ON and LED_OFF commands alternately, regardless of the state of BSP_LED_2 on the subscriber board. It might happen that the state of BSP_LED_3 on the subscriber board and the published command are the same. In such a situation, pressing BSP_BUTTON_3 on the publisher board will have no effect on the state of BSP_LED_2 on the subscriber board, despite fully functional and working network. Pressing BSP_BUTTON_3 on the publisher board again will change the command and the publisher will toggle the BSP_LED_2 state.
Publisher LED assignments
- BSP_LED_0: Thread network state:
- Blinking – Disconnected.
- Solid - Connected.
- BSP_LED_3: MQTT-SN network state:
- Off - Disconnected.
- On - Connected.
Publisher button assignments
- BSP_BUTTON_1: Search for a gateway.
- BSP_BUTTON_2: Connect or disconnect from the MQTT-SN network.
- BSP_BUTTON_3: Send a PUBLISH message.
Setup
You can find the source code and the makefiles in the following folders:
<InstallFolder>\examples\thread\mqttsn_client_publisher
<InstallFolder>\examples\thread\mqttsn_client_subscriber
Testing
- Build the example according to the instructions in Building examples.
- Turn the MQTT-SN gateway on according to the instructions in Thread Border Router.
- Program a development board with the MQTT-SN Subscriber application.
- Program another development board with the MQTT-SN Publisher application.
- Let the clients find the MQTT-SN gateway by pressing BSP_BUTTON_1 on both boards.
- Connect the clients to the gateway by pressing BSP_BUTTON_2 on both boards.
- Subscribe by pressing BSP_BUTTON_3 on the subscriber client board.
- Try toggling BSP_LED_2 on the subscriber by pressing BSP_BUTTON_3 on the publisher board.
- Unsubscribe by pressing BSP_BUTTON_3 on the subscriber board again. As a result, pressing BSP_BUTTON_3 on the publisher board will have no effect on the state of BSP_LED_2 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.