nRF5 SDK v16.0.0
MQTT Client - Subscriber
This information applies to the following SoftDevices: S132, S140

The MQTT subscriber example is an MQTT client that connects to the broker identified by the broker address configured in the example at compile time. If the connection succeeds, it is ready to subscribe to the LED state information under the topic "led/state".

The example allows the user to unsubscribe from the topic, disconnect the MQTT client from the broker, and then reconnect.

An overview of how the examples could be used is shown in the scenarios below. Scenario 1 is a complex, but possibly a real-time scenario where there are one or more publishers and subscribers. In this scenario, not all MQTT clients (publishers/subscribers) have to be BLE-enabled IPv6 devices. They could as well be computer applications and/or embedded devices, wired or wireless, that use MQTT as application protocol over the IP stack. This scenario is seen as a superset of possible scenarios.


MQTT_Overall.svg
Scenario 1: Setup of the lwIP-based MQTT application on the nRF5 SoC


Scenario 2 shows a possible use case where the nRF5 SoC MQTT subscriber is used to receive messages that come from not BLE MQTT clients, such as computer applications. This scenario is realized when the Mosquitto publisher application is used to test the subscriber application on the nRF5 SoC.


MQTT_Server.svg
Scenario 2: Setup of the lwIP-based MQTT client publisher application on the nRF5 SoC


Scenario 3 shows a possible use case where all the MQTT clients are nRF5 devices running MQTT clients, either publisher or subscriber. This scenario is realized when the subscriber and publisher applications included in this SDK are used to connect to the MQTT broker.


MQTT_Client_Server.svg
Scenario 3: Setup of the lwIP-based MQTT client Subscriber and Publisher on the nRF5 SoC


Common Modules Dependency and Usage

This section summarizes the usage of the nRF5 SoC resources and common modules in the examples, apart from the IoT 6LoWPAN and lwIP stack library.

Module Inclusion/Usage Description
Timer 3 Timer for lwIP, LEDs, and the button module.
Button 3 Buttons are used to control the application. See Button assignments.
LEDs 4 LEDs are used to indicate the application states. See LED assignments.
Adv Data Encoder Yes The device name used is 'MQTTPublisher'. The IPSP Service UUID is included in the UUID list.
Scheduler Yes Scheduler is used for processing stack events.
Note
The lwIP library used for this example is under BSD-style license, which is different from the Nordic SDK license. The license text can be found at <InstallFolder>external/lwip/license.txt

Setup

You can find the source code and the project file of the example in the following folder: <InstallFolder>\examples\iot\mqtt\lwip\subscriber

LED assignments

Button assignments

Button Mapped Action
1 MQTT Connection Request
2 Subscribe/Unsubscribe from a Topic
3 MQTT Disconnection
Note
If commissioning is enabled, additional LED and Button assignments are made.

The example by default requests a secure connection on MQTT Secure port 8883. In order to disable security for MQTT clients, follow these steps.

  1. Change the MQTT broker port from 8883 to 1883 (or the non-secure port that MQTT broker is configured to listen on).
  2. Edit the connection parameters for the mqtt_connect request as below. Change from:
    m_app_mqtt_id.transport_type = MQTT_TRANSPORT_SECURE;
    m_app_mqtt_id.p_security_settings = &m_tls_keys;
    uint32_t err_code = mqtt_connect(&m_app_mqtt_id);
    APP_ERROR_CHECK(err_code);
    Change to:
    m_app_mqtt_id.transport_type = MQTT_TRANSPORT_NON_SECURE;
    m_app_mqtt_id.p_security_settings = NULL;
    uint32_t err_code = mqtt_connect(&m_app_mqtt_id);
    APP_ERROR_CHECK(err_code);

MQTT Broker Setup

Refer to Setting up the Mosquitto MQTT broker for a detailed description of how to set up Mosquitto in various configurations.

Since the example uses security by default, the broker must be set up to use TLS.

MQTT Publisher setup

This section describes how Mosquitto can be used as a publisher application to test this example.

# Installation of Mosquitto.
sudo apt-get install mosquitto-clients
# Publish 1 in verbosity mode on default port 1883.
mosquitto_pub -t "led/state" -m 1
# Publish 0 in verbosity mode on default port 1883.
mosquitto_pub -t "led/state" -m 0

Testing

See Connecting devices to the router for a list of relevant Linux commands.

  1. Ensure that the MQTT broker address is set up correctly in the application.
  2. Compile and program the application. Observe that the device is advertising.
  3. Open a terminal program (for example PuTTY) to monitor the messages from the kit on the COM port.
  4. Prepare the Linux router device by initializing the 6LoWPAN module.
  5. Discover the advertising device by using the hcitool lescan command.
  6. Connect to the discovered device from the Linux console by using the Bluetooth 6LoWPAN connect command.
  7. Check if the connected state is reflected by the LEDs.
  8. Prepare the IPv6 global prefix for the btX interface.
  9. Press Button 1. Observe that the Mosquitto broker reports a new connection from the client "nrfSubscriber".
  10. Observe that LED 2 is lit, which means that the connection is established. In case LED 2 is not turned on within 30 seconds, the procedure might have failed. This can be verified by observing a notification of MQTT_EVT_CONNACK with a failure (non-zero result code). Retry MQTT connection by pressing Button 1. Pressing the button before the procedure is complete, either with success or failure, will result in application assertion.
  11. Press Button 2.
  12. Observe that the MQTT subscribe message from the client is reported by the broker. An acknowledgement is sent back by the broker.
  13. Observe that LED 3 turns on once when the subscription acknowledgement is received.
  14. Not much happens after this stage, unless the publisher publishes on the topic "led/state".
  15. Observe that when a publisher publishes "led/state", LED 4 changes its state to on or off, based on the message published for the topic being 1 or 0.
    Note
    If the nRF5 Development Kit running the MQTT publisher is also connected to the broker, observe that LED 4 of the subscriber synchronises with LED 4 of the publisher each time the topic is published.
  16. Disconnect the MQTT connection with the broker by pressing Button 3. LED 3 is turned off on disconnection.
  17. Disconnect from the device by using the Bluetooth 6LoWPAN disconnect command.
  18. Observe that only the advertising LED is lit.

Documentation feedback | Developer Zone | Subscribe | Updated