MQTT-SN Publisher

The MQTT-SN Publisher sample demonstrates how to connect to an MQTT-SN gateway and publish and subscribe to topics, using Zephyr’s MQTT-SN client library. It runs on an nRF71 Series or nRF70 Series device connected over Wi-Fi®.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

nRF7120 DK

nrf7120dk

nrf7120dk/nrf7120/cpuapp

nRF7002 DK

PCA10143

nrf7002dk

nrf7002dk/nrf5340/cpuapp

Overview

This is Zephyr’s MQTT-SN publisher sample, built to run on the development kits listed in the Requirements section.

MQTT-SN is a lightweight publish or subscribe protocol derived from MQTT, adapted for constrained devices and non-TCP transports, such as UDP. The sample acts as an MQTT-SN v1.2 client. It connects to a gateway, which translates MQTT-SN messages to and from standard MQTT for a broker, publishes a periodic timestamp on the /uptime topic, and subscribes to the /number topic.

You can configure the gateway address statically, or discovered dynamically using the MQTT-SN Gateway Discovery procedure, selected with the CONFIG_NET_SAMPLE_MQTT_SN_STATIC_GATEWAY Kconfig option. If the connection to the gateway is later lost, the sample reconnects automatically with an exponential backoff, giving up after a configurable number of attempts (see Configuration options).

The sample itself has no output on success beyond the logging described in Sample output. Verify its functionality by observing the gateway and broker logs, or by subscribing to the sample’s topics from another MQTT client.

Configuration

See Configuring and building for information about how to permanently or temporarily change the configuration.

Configuration options

The following sample-specific Kconfig options are used in this sample (located in zephyr/samples/net/mqtt_sn_publisher/Kconfig):

  • CONFIG_NET_SAMPLE_MQTT_SN_STATIC_GATEWAY - Selects a statically configured gateway address instead of Gateway Discovery.

  • CONFIG_NET_SAMPLE_MQTT_SN_GATEWAY_ADDRESS - IP address and port of the MQTT-SN gateway, used when the gateway is statically configured.

  • CONFIG_NET_SAMPLE_MQTT_SN_BROADCAST_ADDRESS - IP address and port used to broadcast Gateway Discovery and topic registration messages.

  • CONFIG_NET_SAMPLE_MQTT_SN_BUFFER_SIZE - Size of the TX and RX buffers used by the MQTT-SN client.

  • CONFIG_NET_SAMPLE_MQTT_SN_RECONNECT_INITIAL_BACKOFF_MSEC - Delay before the first reconnect attempt, in milliseconds.

  • CONFIG_NET_SAMPLE_MQTT_SN_RECONNECT_MAX_BACKOFF_MSEC - Upper bound on the delay between reconnect attempts, in milliseconds.

  • CONFIG_NET_SAMPLE_MQTT_SN_RECONNECT_MAX_ATTEMPTS - Number of reconnect attempts before the sample gives up.

Configuring Wi-Fi access point credentials

This sample uses the Wi-Fi credentials library to manage Wi-Fi credentials. Before the sample can connect to a Wi-Fi network, you must configure at least one credential set.

Once you have flashed your device with this sample, connect to your device’s UART interface and add credentials using the following command:

wifi cred add -s NetworkSSID -k SecurityMode -p NetworkPassword

Where NetworkSSID is replaced with the SSID of the Wi-Fi access point you want your device to connect to, and NetworkPassword is its password. SecurityMode is replaced by the number as listed here:

  • 0: None

  • 1: WPA2-PSK

  • 2: WPA2-PSK-256

  • 3: SAE-HNP

  • 4: SAE-H2E

  • 5: SAE-AUTO

  • 6: WAPI (not supported on Nordic Semiconductor Wi-Fi devices)

  • 7: EAP-TLS

  • 8: WEP

  • 9: WPA-PSK

  • 10: WPA-Auto-Personal

  • 11: DPP (not supported on Nordic Semiconductor Wi-Fi devices)

If you are not sure which security mode to use, enable the CONFIG_NET_L2_WIFI_SHELL Kconfig option and use the wifi scan command to display a list of all accessible networks along with their corresponding security modes. Then either reboot the device or use the wifi cred auto_connect command to manually trigger a connection attempt.

From now on, these credentials will be automatically used when the configured network is reachable.

When building as firmware image for a non-secure board target, the Wi-Fi credentials backend will be set to PSA using TF-M.

See the Wi-Fi: Shell sample document for more details on the wifi cred command.

Wi-Fi static credential options

If you want to configure the credentials statically, set the CONFIG_WIFI_CREDENTIALS_STATIC Kconfig option to y.

Important

Do not use static credentials in production environments.

Other options for statically configuring your Wi-Fi credentials:

Configuration files

The sample provides a default configuration file, prj.conf, configured for a statically defined gateway, located in zephyr/samples/net/mqtt_sn_publisher.

To add a specific extra configuration file to the build, add the -DEXTRA_CONF_FILE=<extra_conf_file> flag to your west build command.

Wi-Fi

Use the Wi-Fi snippet to build and run the sample on the development kits that the sample supports.

Building and running

This sample can be found under samples/zephyr/net/mqtt_sn_publisher in the nRF Connect SDK folder structure.

To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Note

When building repository applications in the SDK repositories, building with sysbuild is enabled by default. If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.

Use the nrf7002dk/nrf5340/cpuapp or nrf7120dk/nrf7120/cpuapp board target together with the wifi-ipv4 snippet. For example:

# nRF7002 DK
west build -b nrf7002dk/nrf5340/cpuapp -S wifi-ipv4

# nRF7120 DK
west build -b nrf7120dk/nrf7120/cpuapp -S wifi-ipv4

Alternatively, build against the predefined test case in the sample.yaml file:

west build -b nrf7120dk/nrf7120/cpuapp -T nrf.extended.sample.net.mqtt_sn_publisher.wifi-ipv4

Before building, edit the prj.conf file to set the CONFIG_NET_SAMPLE_MQTT_SN_GATEWAY_ADDRESS and CONFIG_NET_SAMPLE_MQTT_SN_BROADCAST_ADDRESS Kconfig options to match your network, or set up an MQTT-SN gateway reachable at the addresses already configured there.

Testing

Testing this sample requires an MQTT-SN gateway and an MQTT broker reachable from the development kit’s Wi-Fi network. Follow Zephyr’s MQTT-SN publisher sample documentation to set up Mosquitto and the Eclipse Paho MQTT-SN Gateway . The same setup works unchanged, since the gateway and broker only need to be reachable over IP. Unlike the native_sim setup described there, set the CONFIG_NET_SAMPLE_MQTT_SN_GATEWAY_ADDRESS and CONFIG_NET_SAMPLE_MQTT_SN_BROADCAST_ADDRESS Kconfig options in the prj.conf file to addresses on your actual Wi-Fi network instead of the 192.0.2.x defaults, matching wherever you run the gateway.

Note

Mosquitto 2.x rejects anonymous clients by default once a listener is configured. Add allow_anonymous true to your mosquitto.conf file, or start it with mosquitto -v -p 1883 -c /dev/null to use the built-in defaults instead.

After programming the sample to your development kit, complete the following steps to test it:

  1. Connect the kit to the computer using a USB cable. The kit is assigned a serial port. Serial ports are referred to as COM ports on Windows, /dev/ttyACM devices on Linux, and /dev/tty devices on macOS. To list Nordic Semiconductor devices connected to your computer together with their serial ports, open a terminal and run the nrfutil device list command. Alternatively, check your operating system’s device manager or its equivalent.

  2. Connect to the kit with a terminal emulator (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.

  3. Wait for the sample to connect to the configured Wi-Fi network and to the MQTT-SN gateway.

  4. Observe the EVT_CONNECTED log line, followed by periodic Publishing timestamp lines.

  5. From a host on the same network, subscribe to the /uptime topic to see the published values:

    mosquitto_sub -h <broker_host_ip_address> -t /uptime
    

Sample output

This is a typical log output of the sample:

[00:00:26.508,590] <inf> mqtt_sn_publisher_sample: Connecting client
[00:00:27.008,705] <inf> net_mqtt_sn: MQTT_SN client connected
[00:00:27.008,708] <inf> mqtt_sn_publisher_sample: MQTT-SN event EVT_CONNECTED
[00:00:27.008,722] <inf> mqtt_sn_publisher_sample: Publishing timestamp
[00:00:27.008,862] <inf> net_mqtt_sn: Registering topic
                                      2f 75 70 74 69 6d 65                             |/uptime
[00:00:27.008,940] <inf> net_mqtt_sn: Can't publish; topic is not ready
[00:00:27.508,883] <inf> net_mqtt_sn: Publishing to topic ID 2
[00:00:37.010,461] <inf> mqtt_sn_publisher_sample: Publishing timestamp
[00:00:37.010,514] <inf> net_mqtt_sn: Publishing to topic ID 2
[...]
[00:01:26.519,023] <inf> mqtt_sn_publisher_sample: MQTT-SN event EVT_PINGRESP
[...]
[00:03:16.509,607] <wrn> net_mqtt_sn: Ping ran out of retries
[00:03:16.509,614] <inf> mqtt_sn_publisher_sample: MQTT-SN event EVT_DISCONNECTED
[00:03:17.509,700] <inf> mqtt_sn_publisher_sample: Reconnecting in 1000 ms (attempt 1/10)
[00:03:18.510,265] <inf> net_mqtt_sn: MQTT_SN client connected
[00:03:18.510,267] <inf> mqtt_sn_publisher_sample: MQTT-SN event EVT_CONNECTED
[...]
[00:05:27.060,501] <inf> mqtt_sn_publisher_sample: MQTT-SN event EVT_PUBLISH
[00:05:27.060,509] <inf> mqtt_sn_publisher_sample: Published data
                                                   34 32                                            |42

The gateway went silent (Ping ran out of retries), and the client reconnected on its own after the configured backoff. EVT_PUBLISH/Published data is a message arriving on the subscribed /number topic.

Troubleshooting

If you have issues with connectivity on nRF71 Series or nRF70 Series devices, see the Wi-Fi: Monitor sample documentation to learn how to capture and analyze Wi-Fi traffic in order to debug connectivity issues. Also, verify that the Wi-Fi credentials configured for your device match your access point, as described in the Configuration options section on this page.

References

Dependencies

This sample uses the following Zephyr libraries: