This nRF UDP Client example shows the usage of Nordic's IPv6 Stack for sending and receiving UDP packets. The server is identified by its IPv6 address, which needs to be configured in
main.c
. The client application sends ICMPv6 echo requests to the server to determine whether it is reachable or not. Once an echo response is received, the application starts to send UDP packets to the peer. The port numbers for transmitting and receiving UDP packets are also configured in
main.c
. The payload of the UDP packets is 20 bytes in length and has the following format:
|
Packet sequence number
in uint32 format (4 bytes) |
16 bytes of random data |
Each transmitted packet is stored until it is received back or until the buffer is full. The buffer of transmitted packets is of PACKET_BUFFER_LEN in length. When it becomes full, the transmission of UDP packets stops and the buffer is emptied. The packet sequence number is set to zero. Echo requests are sent to the server to determine if it is reachable. When an echo response is received, the transmission of UDP packets starts again. The operation of the application is reflected by two LEDs on the board.
The UDP server could be a PC application communicating to the UDP client on the kit, as shown in Figure 1 below.
Or, the UDP server could be the example server application included in the SDK responding to requests from this example application in
Figure 2
.
Figure 3 shows the application state diagram.
Configuration parameters for all used modules are defined and described in the
sdk_config.h
file. This file is located in the
config
subfolder of the main application folder. For the server address and UDP port configuration, see
main.c
.
- Note
- This application is not power optimized!
- This application will start advertising again after disconnection.
Common module dependency and usage
This section summarizes the usage of nRF5x resources and common modules in the examples apart from the IoT 6lowpan and IPv6 stack library.
| Module | Inclusion/Usage | Description |
|---|---|---|
| Timer | 2 | Two timers are used - one timer for LED blinks and one for time-out on the echo response. |
| Buttons | 0 | No buttons are used in this example. |
| LEDs | 4 | LEDs are used to indicate the application states. See LED assignments . |
| Adv Data Encoder | Yes | The device name used is 'UDP6_Client_Node', IPSP Service UUID is included in the UUID list. |
| Scheduler | Yes | Scheduler is used for processing stack events. |
Setup
You can find the source code and the project file of the example in the following folder:
<InstallFolder>\examples\iot\udp\ipv6\client
LED assignments
| LED 1 | LED 2 | |
|---|---|---|
| Blinking | Off | Device advertising as BLE peripheral. |
| On | Off | BLE link established, IPv6 interface down. |
| On | Blinking | IPv6 interface up, echo requests are sent to the server. |
| Blinking | On | UDP packets are sent to the server. |
| On | On | Assertion failure in the application. |
- Note
- If commissioning is enabled, additional LED and Button assignments are made.
Testing
See Connecting devices to the router for a list of relevant Linux commands.
- Compile and program the application. Observe that the device is advertising.
- Prepare the Linux router device by initializing the 6LoWPAN module .
- Discover the advertising device by using the hcitool lescan command.
- Connect to the discovered device from the Linux console by using the Bluetooth 6LoWPAN connect command.
- Check if the connected state is reflected by the LEDs.
- Run the Wireshark or hcidump program to monitor the btX interface.
-
An ICMPv6 ping can be used on the link-local and on the global IPv6 address assigned to the device to check if the device is reachable.
- Note
- To find the global address, use the prefix assigned to the interface in Router Advertisement.
-
Use a UDP server to listen on UDP port number 9000 and observe that the packets from the client are received in the format as described above. When sending back a packet with the same payload, observe the LED light pattern as described in
Setup
.
- Note
- This example is designed to complement the Nordic UDP Server example, and they will work together provided that this application is modified with the servers address.
- Disconnect from the device using the Bluetooth 6LoWPAN disconnect command.
- Observe that the device is advertising.
Python Server Example
Use the code below to start a simple Python server that listens on port 9000, prints the sequence number from incoming packets and transmits them back to the sender.
Troubleshooting Guide
- It is possible that the global address is not immediately available on the connection as the Neighbor Discovery, Router Advertisement, and Duplicate Address Detection procedures take a few seconds to complete.
-
If you observe that the server responses are received at the btX interface but the client never receives them, it is possible that the forwarding between networks is not enabled. This can be done on Linux using the command
sysctl -w net.ipv6.conf.all.forwarding=1. - In case the client is reachable, but the echo requests from the client do not make it to the Server, it is possible that the application is not configured with the correct remote server address. Verify that the address SERVER_IPV6_ADDRESS in the client application matches the server address.