UDP

The UDP sample demonstrates how to perform sequential transmissions of UDP packets to a server using an IP-connected device. The sample connects to an LTE network using an nRF91 Series DK or a Thingy:91, or to Wi-Fi® using the nRF7002 DK.

Cellular connectivity is supported on the nRF91 Series SiPs, while Wi-Fi connectivity is supported on the nRF52 or nRF53 Series SoCs hosting the nRF70 Series Wi-Fi companion ICs.

The sample uses the connection manager that provides a common connectivity API for LTE and Wi-Fi stacks.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Build target

Thingy:91

PCA20035

thingy91_nrf9160

thingy91_nrf9160_ns

nRF9161 DK

PCA10153

nrf9161dk_nrf9161

nrf9161dk_nrf9161_ns

nRF9160 DK

PCA10090

nrf9160dk_nrf9160

nrf9160dk_nrf9160_ns

nRF9151 DK

PCA10171

nrf9151dk_nrf9151

nrf9151dk_nrf9151_ns

nRF7002 DK

PCA10143

nrf7002dk_nrf5340

nrf7002dk_nrf5340_cpuapp_ns

Additionally, the sample supports emulation using QEMU x86.

Overview

The sample connects through either an LTE network or Wi-Fi, depending on the selected target board. Once connected, it sets up a UDP socket and continuously transmits data over the socket to a configurable IP address and port number.

Configuration

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

Configuration options

Check and configure the following configuration options:

CONFIG_UDP_SAMPLE_DATA_UPLOAD_SIZE_BYTES - UDP data upload size

This configuration option sets the number of bytes that are transmitted to the server.

CONFIG_UDP_SAMPLE_DATA_UPLOAD_FREQUENCY_SECONDS - UDP data upload frequency

This configuration option sets how often the sample transmits data to the server.

CONFIG_UDP_SAMPLE_SERVER_ADDRESS_STATIC - UDP Server IP Address

This configuration option sets the static IP address of the server.

CONFIG_UDP_SAMPLE_SERVER_PORT - UDP server port configuration

This configuration option sets the server address port number.

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 NetworkSSID SecurityMode (OPEN, WPA2-PSK, WPA2-PSK-SHA256, WPA3-SAE) 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. 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 build 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 predefined configuration files for the following development kits:

  • prj.conf - General configuration file for all devices.

  • boards/nrf9151dk_nrf9151_ns.conf - Configuration file for the nRF9151 DK.

  • boards/nrf9161dk_nrf9161_ns.conf - Configuration file for the nRF9161 DK.

  • boards/nrf9160dk_nrf9160_ns.conf - Configuration file for the nRF9160 DK.

  • boards/thingy91_nrf9160_ns.conf - Configuration file for the Thingy:91.

  • boards/nrf7002dk_nrf5340_cpuapp.conf - Configuration file for the nRF7002 DK.

  • boards/qemu_x86.conf - Configuration file for QEMU x86 emulation.

Building and running

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

When built as firmware image for the _ns build target, the 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). To read more about CMSE, see Processing environments.

To build the sample with Visual Studio Code, follow the steps listed on the How to build an application page in the nRF Connect for VS Code extension documentation. See Configuring and building an application for other building scenarios, Programming an application for programming steps, and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Testing

After programming the sample to your device, test it by performing the following steps:

  1. Connect the kit to the computer using a USB cable. The kit is assigned a COM port (Windows) or ttyACM device (Linux), which is visible in the Device Manager.

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

  3. Observe that the sample shows output similar to the following in the terminal emulator:

    *** Booting nRF Connect SDK v2.4.99-dev2-114-g305275323644 ***
    [00:00:00.268,920] <inf> udp_sample: UDP sample has started
    [00:00:00.268,951] <inf> udp_sample: Bringing network interface up and connecting to the network
    [00:00:05.557,800] <inf> udp_sample: Network connectivity established
    [00:00:05.558,715] <inf> udp_sample: Transmitting UDP/IP payload of 38 bytes to the IP address 8.8.8.8, port number 2469
    

Troubleshooting

If you have issues with connectivity on nRF91 Series devices, see the Cellular Monitor documentation to learn how to capture modem traces in order to debug network traffic in Wireshark. This sample enables modem traces by default.

Dependencies

This sample uses the following nRF Connect SDK and Zephyr libraries: