Skip to content

abluethinginthecloud/nrf7002-mqtt-client-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nRF7002 MQTT Client Example

Wi-Fi MQTT client example for the Nordic Semiconductor nRF7002 DK using Zephyr and the nRF Connect SDK.

Connect the nRF7002 DK to a Wi-Fi network, establish an MQTT client session, subscribe to a topic and publish messages periodically from an embedded C application.


nRF7002 nRF7002 DK nRF5340 Zephyr MQTT Language License

Read the full technical article · Visit A Blue Thing In The Cloud · YouTube channel


Overview

This repository contains an embedded MQTT client example for the Nordic nRF7002 DK.

The application configures the board as a Wi-Fi station, connects to a Wi-Fi access point, establishes an MQTT session with a broker, subscribes to a configurable topic and periodically publishes a configurable message.

It is a practical starting point for IoT products that need Wi-Fi cloud connectivity, such as:

  • Wireless sensors
  • Smart home devices
  • Industrial monitoring nodes
  • MQTT telemetry devices
  • Wi-Fi connected prototypes
  • nRF7002 proof-of-concepts
  • Zephyr-based IoT applications

The project is written mainly in C and uses the Zephyr / nRF Connect SDK build system.


What this example does

The firmware performs the following sequence:

  1. Initializes the board and application modules.
  2. Configures the nRF7002 DK as a Wi-Fi station.
  3. Connects to the configured Wi-Fi network.
  4. Indicates Wi-Fi connection status using the board LED.
  5. Initializes the MQTT client.
  6. Connects to the configured MQTT broker.
  7. Subscribes to the configured MQTT topic.
  8. Publishes a configurable message periodically.
  9. Handles MQTT callbacks for connection, disconnection, incoming messages and published messages.
  10. Disconnects from MQTT if the Wi-Fi connection is lost.

Application flow

sequenceDiagram
    participant Board as nRF7002 DK
    participant WiFi as Wi-Fi Router
    participant Broker as MQTT Broker
    participant Tool as MQTT Explorer / MQTT Client

    Board->>WiFi: Connect as Wi-Fi station
    WiFi-->>Board: IP connectivity available
    Board->>Broker: Initialize MQTT client
    Board->>Broker: Connect
    Board->>Broker: Subscribe to topic
    Board->>Broker: Publish periodic message
    Tool->>Broker: Publish test message
    Broker-->>Board: Forward subscribed message
    Broker-->>Tool: Forward board message
Loading

Repository structure

nrf7002-mqtt-client-example/
├── boards/             # Board-specific configuration
├── src/
│   ├── common/         # Common application files
│   └── modules/        # Application modules
│       ├── error/      # Error handling module
│       ├── led/        # Optional LED module
│       ├── network/    # Wi-Fi / network handling
│       ├── sampler/    # Payload/sample generation
│       ├── transport/  # MQTT transport layer
│       └── trigger/    # Periodic trigger logic
├── CMakeLists.txt      # Zephyr build configuration
├── Kconfig             # Application Kconfig options
├── prj.conf            # Main application configuration
├── sample.yaml         # Zephyr/NCS sample metadata
├── LICENSE
└── README.md

Hardware requirements

Component Description
nRF7002 DK Nordic Semiconductor development kit with nRF7002 Wi-Fi companion IC and nRF5340 host processor
Wi-Fi access point Router or access point with Internet/network access
USB cable For programming, power and serial log output
Development PC Linux, macOS or Windows environment with nRF Connect SDK tools

The nRF7002 DK is designed for Wi-Fi 6 IoT development and combines the nRF7002 Wi-Fi companion IC with an nRF5340 host SoC.


Software requirements

You need a working Nordic / Zephyr development environment.

Recommended tools:

  • nRF Connect SDK
  • Zephyr west tool
  • nRF Command Line Tools
  • nRF Connect for Desktop
  • Visual Studio Code with nRF Connect extension, or another Zephyr-compatible workflow
  • MQTT Explorer or another MQTT client for testing

For the full setup process, see:

Getting started with nRF7002 DK


Configuration

Before building the firmware, edit:

prj.conf

The most important configuration options are grouped into two areas: Wi-Fi station configuration and MQTT client configuration.


Wi-Fi configuration

Set the Wi-Fi network credentials:

CONFIG_WIFI_CREDENTIALS_STATIC_SSID="your_wifi_ssid"
CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD="your_wifi_password"

Select the Wi-Fi security mode used by your router. For example, WPA2:

CONFIG_STA_KEY_MGMT_WPA2=y

Other security options may be available in the project configuration. Enable only the one that matches your Wi-Fi network.

Security note: avoid committing real Wi-Fi credentials to a public repository. Use local configuration files, overlays or ignored development-only files when adapting this example for your own projects.


MQTT configuration

Set your MQTT broker, topics, client ID and payload:

CONFIG_MQTT_SAMPLE_TRANSPORT_PUBLISH_TOPIC="publish/topic"
CONFIG_MQTT_SAMPLE_TRANSPORT_SUBSCRIBE_TOPIC="subscribe/topic"
CONFIG_MQTT_SAMPLE_TRANSPORT_BROKER_HOSTNAME="broker"
CONFIG_MQTT_SAMPLE_TRANSPORT_BROKER_USERNAME="noUser"
CONFIG_MQTT_SAMPLE_TRANSPORT_BROKER_PASSWORD="noPassword"
CONFIG_MQTT_SAMPLE_TRANSPORT_CLIENT_ID="clientID"
CONFIG_MQTT_SAMPLE_TRANSPORT_MESSAGE="message"
CONFIG_MQTT_SAMPLE_TRIGGER_TIMEOUT_SECONDS=15
Option Description
CONFIG_MQTT_SAMPLE_TRANSPORT_PUBLISH_TOPIC MQTT topic where the board publishes messages
CONFIG_MQTT_SAMPLE_TRANSPORT_SUBSCRIBE_TOPIC MQTT topic where the board listens for messages
CONFIG_MQTT_SAMPLE_TRANSPORT_BROKER_HOSTNAME MQTT broker hostname or IP address
CONFIG_MQTT_SAMPLE_TRANSPORT_BROKER_USERNAME MQTT broker username; use noUser if authentication is not required
CONFIG_MQTT_SAMPLE_TRANSPORT_BROKER_PASSWORD MQTT broker password; use noPassword if authentication is not required
CONFIG_MQTT_SAMPLE_TRANSPORT_CLIENT_ID MQTT client ID used by the nRF7002 DK
CONFIG_MQTT_SAMPLE_TRANSPORT_MESSAGE Message periodically published by the board
CONFIG_MQTT_SAMPLE_TRIGGER_TIMEOUT_SECONDS Publishing period in seconds

Getting started

1. Clone the repository

git clone https://github.com/abluethinginthecloud/nrf7002-mqtt-client-example.git
cd nrf7002-mqtt-client-example

2. Configure Wi-Fi and MQTT settings

Edit prj.conf and configure:

  • Wi-Fi SSID
  • Wi-Fi password
  • Wi-Fi security mode
  • MQTT broker hostname
  • MQTT publish topic
  • MQTT subscribe topic
  • MQTT client ID
  • MQTT payload
  • Publishing interval

3. Build the firmware

From a terminal with the nRF Connect SDK environment initialized:

west build -b nrf7002dk_nrf5340_cpuapp .

4. Flash the board

west flash

5. Open the serial console

Use your preferred serial terminal to monitor the board logs.

Typical options include:

  • nRF Terminal
  • PuTTY
  • Tera Term
  • minicom
  • screen

6. Test with MQTT Explorer

  1. Open MQTT Explorer.
  2. Connect MQTT Explorer to the same broker configured in prj.conf.
  3. Subscribe to the board publish topic.
  4. Publish a test message to the board subscribe topic.
  5. Verify that:
    • The board connects to Wi-Fi.
    • The board connects to the MQTT broker.
    • The board publishes periodic messages.
    • Incoming subscribed messages appear in the serial logs.
    • Published board messages appear in MQTT Explorer.

Example MQTT test scenario

A simple test setup is:

Device / Tool Action
nRF7002 DK Publishes periodically to publish/topic
nRF7002 DK Subscribes to subscribe/topic
MQTT Explorer Subscribes to publish/topic to receive board messages
MQTT Explorer Publishes to subscribe/topic to send test data to the board

This creates a simple two-way MQTT test loop between the development board and a desktop MQTT client.


Main files to study

File / folder Purpose
prj.conf Main Wi-Fi, MQTT, networking, ZBus and Zephyr configuration
Kconfig Application-level configuration menu and module Kconfig includes
CMakeLists.txt Adds the common code and application modules to the Zephyr build
src/modules/network/ Wi-Fi and network connection logic
src/modules/transport/ MQTT transport implementation
src/modules/sampler/ Message or payload generation logic
src/modules/trigger/ Periodic publishing trigger
src/modules/error/ Error handling
src/modules/led/ Optional LED status indication

Customizing the project

Change the publishing interval

Edit:

CONFIG_MQTT_SAMPLE_TRIGGER_TIMEOUT_SECONDS=15

Set the value to the desired publishing period in seconds.

Change the MQTT payload

Edit:

CONFIG_MQTT_SAMPLE_TRANSPORT_MESSAGE="message"

For a real product, replace this static payload with sensor data, device status, JSON telemetry or another application-specific format.

Change the topics

Edit:

CONFIG_MQTT_SAMPLE_TRANSPORT_PUBLISH_TOPIC="publish/topic"
CONFIG_MQTT_SAMPLE_TRANSPORT_SUBSCRIBE_TOPIC="subscribe/topic"

A common production-style convention is:

devices/<device-id>/telemetry
devices/<device-id>/commands
devices/<device-id>/status

Add sensor data

A typical next step is to connect the sampler module to real sensor readings.

Example payload ideas:

{
  "temperature_c": 24.7,
  "humidity_percent": 48.2,
  "battery_mv": 3720
}

Add production-grade behavior

For a production device, consider adding:

  • Secure credential provisioning
  • TLS configuration
  • Device identity management
  • Persistent configuration storage
  • Reconnection backoff
  • Last Will and Testament
  • Watchdog supervision
  • OTA firmware update support
  • Power profiling
  • Cloud-specific topic conventions

Troubleshooting

The board does not connect to Wi-Fi

Check that:

  • The SSID and password are correct.
  • The selected security mode matches your router.
  • The Wi-Fi network is available on a supported band.
  • The antenna and board are correctly connected and powered.
  • The serial log does not show credential or association errors.

The board connects to Wi-Fi but not to MQTT

Check that:

  • The broker hostname is reachable from the Wi-Fi network.
  • The broker port and authentication settings are correct.
  • The username and password match the broker configuration.
  • Your MQTT broker accepts the configured client ID.
  • Any firewall or router rules allow the connection.

Messages are not visible in MQTT Explorer

Check that:

  • MQTT Explorer is connected to the same broker.
  • The publish and subscribe topics match exactly.
  • Topic names are case-sensitive.
  • The board is still connected to the broker.
  • The publishing interval is not too long.

The build fails

Check that:

  • Your nRF Connect SDK environment is correctly installed.
  • You are building for nrf7002dk_nrf5340_cpuapp.
  • The project is located inside a valid Zephyr / west workspace or your environment variables are correctly configured.
  • Your SDK version supports the nRF7002 DK and required networking options.

Related resources


About A Blue Thing In The Cloud

A Blue Thing In The Cloud is an electronics engineering company focused on embedded firmware development, PCB design, wireless connectivity and IoT product development.

We help companies design and develop connected electronic products, from early prototypes to production-ready embedded systems.

If you are developing a Wi-Fi IoT device, an nRF7002 product, an MQTT-connected sensor, a BLE/Wi-Fi device or a custom embedded system, feel free to contact us:

Contact A Blue Thing In The Cloud


Keywords

nrf7002 · nrf7002-dk · nrf7002dk · nrf5340 · nrf-connect-sdk · zephyr · zephyr-rtos · mqtt · mqtt-client · wifi · wi-fi-6 · nordic-semiconductor · embedded-c · iot · wireless · wifi-station · mqtt-publish · mqtt-subscribe · embedded-firmware


License

This project is licensed under the GPL-3.0 License. See the LICENSE file for details.