MQTT¶
Overview¶
The MQTT sample demonstrates how to communicate with an MQTT broker.
The sample connects to an LTE network using the Connection Manager. Then it connects to a configured MQTT server (default is mqtt.nordicsemi.academy), where it publishes messages to the topic <clientID>/my/publish/topic. You can also trigger message publication by pressing the USR/P25 button on the board.
The sample also subscribes to the topic <clientID>/my/subscribe/topic, and receives any message published to that topic.
The sample supports Transport Layer Security (TLS) and it can be enabled through overlay configuration files included in the sample.
Requirements¶
Before you start, check that you have the required hardware and software:
- 1x nRF9151 Connect Kit
- 1x nano-SIM card with LTE-M or NB-IoT support
- 1x U.FL cabled LTE-M/NB-IoT/NR+ Flexible Antenna (included in the box)
- 1x USB-C Cable
- An MQTT Client such as MQTTX for testing
- A computer running macOS, Ubuntu, or Windows 10 or newer
Set up your board¶
- Insert the nano-SIM card into the nano-SIM card slot.
- Attach the U.FL cabled LTE-M/NB-IoT/NR+ Flexible Antenna.
- Connect the nRF9151 Connect Kit to the computer with a USB-C cable.

Building the sample¶
To build the sample, follow the instructions in Getting Started Guide to set up your preferred building environment.
Use the following steps to build the MQTT sample on the command line.
-
Open a terminal window.
-
Go to
NCS-Project/nrf9151-connectkitrepository cloned in the Getting Started Guide. -
Build the sample using the
west buildcommand, specifying the board (following the-boption) asnrf9151_connectkit/nrf9151/ns.The
-palways option forces a pristine build, and is recommended for new users. Users may also use the-p autooption, which will use heuristics to determine if a pristine build is required, such as when building another sample.Note
This sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M).
-
After building the sample successfully, the firmware with the name
tfm_merged.hexcan be found in thebuild/mqtt/zephyrdirectory.
Flashing the firmware¶
Set up your board before flashing the firmware. You can flash the sample using west flash:
Tip
In case you wonder, the west flash will execute the following command:
Testing¶
After programming the sample, test it by performing the following steps:
-
Open up a serial terminal, specifying the correct serial port that your computer uses to communicate with the nRF9151 SiP:
- Start PuTTY.
-
Configure the correct serial port and click Open:

-
Press the DFU/RST button to reset the nRF9151 SiP.
-
Start MQTTX, set up a New Connection and then Connect:
- Host:
mqtt.nordicsemi.academy - Port:
8883 - SSL/TLS: Yes
- CA File: mqtt.nordicsemi.academy.pem
- MQTT Version:
3.1.1
- Host:
-
Click New Subscription, subscribe the topic
<clientID>/my/publish/topic, and Confirm: -
Wait for the nRF9151 Connect Kit connecting to the newtwork. MQTTX will display the messages that the board published to
<clientID>/my/publish/topic. You can also trigger message publication by pressing the USR/P25 button on the board. -
In the MQTTX client, you can pushlish messages to the topic
<clientID>/my/subscribe/topic. The nRF9151 Connect Kit will display the messages published to that topic. -
Observe the output of the terminal. You should see the output, similar to what is shown in the following:




