Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Click or drag files to this area to upload. You can upload up to 2 files.

Exercise 2

v2.9.0 – v2.8.0

Measuring the throughput of a Wi-Fi connection

In this exercise, we will measure the throughput of the Wi-Fi connection between your device and an iPerf server that we will create in the exercise. .

The device will connect to the iPerf server running on a test PC, and will send and receive packets during the test duration. When the test is finished, the result of the throughput test will be printed to the terminal.

Exercise steps

In the GitHub repository for this course, go to the base code for this exercise, found in l3/l3_e2.

1. Set the necessary networking configurations

1.1 Enable zperf

Enable zperf shell utility through CONFIG_NET_ZPERF in the prj.conf file.

CONFIG_NET_ZPERF=y
Kconfig

1.2 Configure the relevant network configurations.

Enable network settings through CONFIG_NET_CONFIG_SETTINGS and set the networking timeout to 0 through CONFIG_NET_CONFIG_INIT_TIMEOUT.

Set the peer IPv4 address using CONFIG_NET_CONFIG_PEER_IPV4_ADDR. This is the IPv4 address of the computer you are running the zperf server on. You can find the IPv4 of your computer by typing “ipconfig” in your command prompt on Windows or “ifconfig” in your terminal on Linux and macOS.

CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_INIT_TIMEOUT=0
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="<IP address of PC running the iPerf server>"
Kconfig

1.3 Increase the heap memory pool size.

Throughput testing requires more memory than basic connectivity applications, so we must increase the heap memory pool.

Increase the heap memory pool size by adding the following Kconfig line in the prj.conf file

CONFIG_HEAP_MEM_POOL_SIZE=200000
Kconfig

1.4 Increase the network buffers.

In this exercise, we are testing the throughput so we need to increase the network buffers to allow the Wi-Fi stack to handle larger packets efficiently.

CONFIG_NET_BUF_FIXED_DATA_SIZE=y
CONFIG_NET_BUF_DATA_SIZE=1100
Kconfig

2. Include the header files for the zperf API and nrfx clock.

#include <zephyr/net/zperf.h>
#include <nrfx_clock.h>
C

3. Define the zperf server and test parameters

3.1 Define the port for the zperf server

#define PEER_PORT 5001
C

3.2 Define packet size, rate and test duration

#define WIFI_ZPERF_PKT_SIZE 1024
#define WIFI_ZPERF_RATE 10000
#define WIFI_TEST_DURATION 20000
C

4. Create a socket address struct for the server address

static struct sockaddr_in in4_addr_my = {
	.sin_family = AF_INET,
	.sin_port = htons(PEER_PORT),
};
C

5. Fill in the zperf upload parameters for the zperf client

5.1 Initialize a struct for storing the zperf upload parameters.

struct zperf_upload_params params;
C

5.2 Configure packet size, rate and duration from the defines created earlier.

params.packet_size = WIFI_ZPERF_PKT_SIZE;
params.rate_kbps = WIFI_ZPERF_RATE;
params.duration_ms = WIFI_TEST_DURATION;
C

5.3 Convert the server address from a string to IP address.

ret = net_addr_pton(AF_INET, CONFIG_NET_CONFIG_PEER_IPV4_ADDR, &in4_addr_my.sin_addr);
if (ret < 0) {
	LOG_ERR("Invalid IPv4 address %s\n", CONFIG_NET_CONFIG_PEER_IPV4_ADDR);
	return -EINVAL;
}
C

5.4 Add the zperf server address to the zperf_upload_params struct.

memcpy(&params.peer_addr, &in4_addr_my, sizeof(in4_addr_my));
C

6. Call zperf_udp_upload_async() to start the asynchronous UDP upload.

ret = zperf_udp_upload_async(&params, udp_upload_results_cb, NULL);
if (ret != 0) {
	LOG_ERR("Failed to start Wi-Fi benchmark: %d\n", ret);
	return ret;
}
C

7. Retrieve and print the results from the throughput test.

Handle the three zperf session statuses: started, finished, and error inside the zperf_status switch.

7.1 Inform the user that the UDP session has started

LOG_INF("New UDP session started");
C

7.2 If client_time_in_us is not zero, calculate the throughput rate in kilobit per second. Otherwise, set it to zero.

if (result->client_time_in_us != 0U) {
	client_rate_in_kbps = (uint32_t)
		(((uint64_t)result->nb_packets_sent *
		  (uint64_t)result->packet_size * (uint64_t)8 *
		  (uint64_t)USEC_PER_SEC) /
		 ((uint64_t)result->client_time_in_us * 1024U));
} else {
	client_rate_in_kbps = 0U;
}
C

7.3 Print the results of the throughput test

LOG_INF("Upload results:");
LOG_INF("%u bytes in %u ms",
		(result->nb_packets_sent * result->packet_size),
		(result->client_time_in_us / USEC_PER_MSEC));
LOG_INF("%u packets sent", result->nb_packets_sent);
LOG_INF("%u packets lost", result->nb_packets_lost);
LOG_INF("%u packets received", result->nb_packets_rcvd);
LOG_INF("%u kbps throughput",client_rate_in_kbps);
C

7.4 Inform the user that there is an error with the UDP session

LOG_ERR("UDP session error");
C

8. Install iPerf

Running this example requires the Wi-Fi iPerf application. Download and install iPerf by following the guide for your operating system.

8.1 Install chocolatey.

8.2 Add chocolatey to path.

8.3 Open a Windows Command Prompt window as Administrator. To do so, press the Windows key, type “cmd”, right-click the result, and choose Run as Administrator.

8.4 Use choco to install iPerf. Make sure to install version 2.0.8.

choco install iperf2 --version=2.0.8

8.1 Install iPerf using apt.

sudo apt install iperf

9. Start the iPerf server

Important

This exercise assumes a local connection. Therefore, you must ensure your Nordic device and the device you are running the iPerf server from are connected to the same network.

9.1 Run the following command in the command prompt to start the iPerf server in UDP mode.

iperf2 -s -i 1 -u

Allow iperf2 to communicate on public and private network if you get a pop up from Windows Defender Firewall.

9.1 Run the following command in a terminal to start the iPerf server in UDP mode.

iperf -s -i 1 -u

10. With the iPerf server running, build and flash the application to your board.

Due to the memory requirements of the exercise, you need to build the application without TF-M.

BoardBuild without TF-MExtra CMake arguments
nRF7002 DKnrf7002dk/nrf5340/cpuappN/A
nRF5340 DK + nRF7002 EKnrf5340dk/nrf5340/cpuapp-DSHIELD=nrf7002ek

If the application does not connect to the network, initiate a Wi-Fi connection by issuing the following commands

Connect to your Wi-Fi network by running the following commands in the terminal

wifi_cred add -s "<your_network_SSID>" -p "<your_network_password>" -k <key_mgmt>
wifi_cred auto_connect

Recall

You can run wifi scan to find the key management type of the network you want to connect to and wifi_cred add help for a list of the corresponding numbers.

Please note it can take a minute while messages are printed on the terminal.

*** Booting nRF Connect SDK 2.9.0-3758bcbfa5cd ***
[00:00:30.482,421] <inf> Lesson3_Exercise2: Starting nrf7002dk with CPU frequency: 128 MHz
[00:00:32.482,574] <inf> Lesson3_Exercise2: Waiting to connect to Wi-Fi
[00:00:37.485,260] <inf> Lesson3_Exercise2: Network connected
[00:00:37.498,229] <inf> Lesson3_Exercise2: IPv4 address 192.168.50.64
[00:00:37.498,229] <inf> Lesson3_Exercise2: Starting Wi-Fi throughput test: Zperf client
[00:00:37.498,504] <inf> Lesson3_Exercise2: New UDP session started
Terminal

11. The results of the throughput test will be printed after the test has finished

[00:00:28.882,141] <inf> Lesson3_Exercise2: Wi-Fi throughput test: Upload completed!
[00:00:28.882,171] <inf> Lesson3_Exercise2: Upload results:
[00:00:28.882,171] <inf> Lesson3_Exercise2: 20317184 bytes in 20001 ms
[00:00:28.882,171] <inf> Lesson3_Exercise2: 19841 packets sent
[00:00:28.882,171] <inf> Lesson3_Exercise2: 78 packets lost
[00:00:28.882,202] <inf> Lesson3_Exercise2: 19841 packets received
[00:00:28.882,202] <inf> Lesson3_Exercise2: 7935 kbps throughput
Terminal

You should see output similar to this from the iPerf server

------------------------------------------------------------
Server listening on UDP port 5001
Receiving 1470 byte datagrams
UDP buffer size:  208 KByte (default)
------------------------------------------------------------
[  3] local 192.168.10.102 port 5001 connected with 192.168.10.176 port 63316
[ ID] Interval       Transfer     Bandwidth        Jitter   Lost/Total Datagrams
[  3]  0.0- 1.0 sec   971 KBytes  7.95 Mbits/sec   0.656 ms    7/  978 (0.72%)
[  3]  1.0- 2.0 sec  1000 KBytes  8.19 Mbits/sec   0.718 ms    0/ 1000 (0%)
[  3]  2.0- 3.0 sec   995 KBytes  8.15 Mbits/sec   0.869 ms    0/  995 (0%)
[  3]  3.0- 4.0 sec   992 KBytes  8.13 Mbits/sec   0.948 ms    0/  992 (0%)
[  3]  4.0- 5.0 sec   990 KBytes  8.11 Mbits/sec   0.862 ms   71/ 1061 (6.7%)
[  3]  5.0- 6.0 sec   989 KBytes  8.10 Mbits/sec   0.720 ms    0/  989 (0%)
[  3]  6.0- 7.0 sec  1012 KBytes  8.29 Mbits/sec   0.991 ms    0/ 1012 (0%)
[  3]  7.0- 8.0 sec   978 KBytes  8.01 Mbits/sec   0.738 ms    0/  978 (0%)
[  3]  8.0- 9.0 sec   984 KBytes  8.06 Mbits/sec   0.673 ms    0/  984 (0%)
[  3]  9.0-10.0 sec   994 KBytes  8.14 Mbits/sec   0.676 ms    0/  994 (0%)
[  3] 10.0-11.0 sec   988 KBytes  8.09 Mbits/sec   0.653 ms    0/  988 (0%)
[  3] 11.0-12.0 sec  1012 KBytes  8.29 Mbits/sec   0.705 ms    0/ 1012 (0%)
[  3] 12.0-13.0 sec  1010 KBytes  8.27 Mbits/sec   0.698 ms    0/ 1010 (0%)
[  3] 13.0-14.0 sec  1012 KBytes  8.29 Mbits/sec   0.501 ms    0/ 1012 (0%)
[  3] 14.0-15.0 sec  1013 KBytes  8.30 Mbits/sec   0.641 ms    0/ 1013 (0%)
[  3] 15.0-16.0 sec   966 KBytes  7.91 Mbits/sec   0.948 ms    0/  966 (0%)
[  3] 16.0-17.0 sec   993 KBytes  8.13 Mbits/sec   0.689 ms    0/  993 (0%)
[  3] 17.0-18.0 sec   995 KBytes  8.15 Mbits/sec   0.645 ms    0/  995 (0%)
[  3] 18.0-19.0 sec   978 KBytes  8.01 Mbits/sec   0.668 ms    0/  978 (0%)
[  3]  0.0-19.9 sec  19.3 MBytes  8.13 Mbits/sec   0.848 ms   78/19841 (0.39%)
Terminal

Note

The Wi-Fi performance is affected by various factors, such as the distance to the access point and its placement, the Wi-Fi adapter on the PC running the iPerf server, frequency band, noise/interference, and more. Therefore, results will vary from one testing environment to the other.

v2.7.0 – v2.6.1

Measuring the throughput of a Wi-Fi connection

In this exercise, we will measure the throughput of the Wi-Fi connection between your device and an iPerf server that we will create in the exercise. .

The device will connect to the iPerf server running on a test PC, and will send and receive packets during the test duration. When the test is finished, the result of the throughput test will be printed to the terminal.

Exercise steps

In the GitHub repository for this course, go to the base code for this exercise, found in l3/l3_e2.

1. Set the necessary networking configurations

1.1 Enable zperf

Enable zperf shell utility through CONFIG_NET_ZPERF in the prj.conf file.

CONFIG_NET_ZPERF=y
Kconfig

1.2 Configure the relevant network configurations.

Enable network settings through CONFIG_NET_CONFIG_SETTINGS and set the networking timeout to 0 through CONFIG_NET_CONFIG_INIT_TIMEOUT.

Set the peer IPv4 address using CONFIG_NET_CONFIG_PEER_IPV4_ADDR. This is the IPv4 address of the computer you are running the zperf server on. You can find the IPv4 of your computer by typing “ipconfig” in your command prompt on Windows or “ifconfig” in your terminal on Linux and macOS.

CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_INIT_TIMEOUT=0
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="<IP address of PC running the iPerf server>"
Kconfig

1.3 Increase the heap memory pool size.

Depending on the board you are using, open either boards/nrf7002dk_nrf5340_cpuapp.conf or boards/nrf5340dk_nrf5340_cpuapp.conf, and increase the heap memory pool size by adding the following Kconfig line.

CONFIG_HEAP_MEM_POOL_SIZE=200000
Kconfig

2. Include the header files for the zperf API and nrfx clock.

#include <zephyr/net/zperf.h>
#include <nrfx_clock.h>
C

3. Define the zperf server and test parameters

3.1 Define the port for the zperf server

#define PEER_PORT 5001
C

3.2 Define packet size, rate and test duration

#define WIFI_ZPERF_PKT_SIZE 1024
#define WIFI_ZPERF_RATE 10000
#define WIFI_TEST_DURATION 20000
C

4. Create a socket address struct for the server address

static struct sockaddr_in in4_addr_my = {
	.sin_family = AF_INET,
	.sin_port = htons(PEER_PORT),
};
C

5. Fill in the zperf upload parameters for the zperf client

5.1 Initialize a struct for storing the zperf upload parameters.

struct zperf_upload_params params;
C

5.2 Configure packet size, rate and duration from the defines created earlier.

params.packet_size = WIFI_ZPERF_PKT_SIZE;
params.rate_kbps = WIFI_ZPERF_RATE;
params.duration_ms = WIFI_TEST_DURATION;
C

5.3 Convert the server address from a string to IP address.

ret = net_addr_pton(AF_INET, CONFIG_NET_CONFIG_PEER_IPV4_ADDR, &in4_addr_my.sin_addr);
if (ret < 0) {
	LOG_ERR("Invalid IPv4 address %s\n", CONFIG_NET_CONFIG_PEER_IPV4_ADDR);
	return -EINVAL;
}
C

5.4 Add the zperf server address to the zperf_upload_params struct.

memcpy(&params.peer_addr, &in4_addr_my, sizeof(in4_addr_my));
C

6. Call zperf_udp_upload_async() to start the asynchronous UDP upload.

ret = zperf_udp_upload_async(&params, udp_upload_results_cb, NULL);
if (ret != 0) {
	LOG_ERR("Failed to start Wi-Fi benchmark: %d\n", ret);
	return ret;
}
C

7. Retrieve and print the results from the throughput test.

Handle the three zperf session statuses: started, finished, and error inside the zperf_status switch.

7.1 Inform the user that the UDP session has started

LOG_INF("New UDP session started");
C

7.2 If client_time_in_us is not zero, calculate the throughput rate in kilobit per second. Otherwise, set it to zero.

if (result->client_time_in_us != 0U) {
	client_rate_in_kbps = (uint32_t)
		(((uint64_t)result->nb_packets_sent *
		  (uint64_t)result->packet_size * (uint64_t)8 *
		  (uint64_t)USEC_PER_SEC) /
		 ((uint64_t)result->client_time_in_us * 1024U));
} else {
	client_rate_in_kbps = 0U;
}
C

7.3 Print the results of the throughput test

LOG_INF("Upload results:");
LOG_INF("%u bytes in %u ms",
		(result->nb_packets_sent * result->packet_size),
		(result->client_time_in_us / USEC_PER_MSEC));
LOG_INF("%u packets sent", result->nb_packets_sent);
LOG_INF("%u packets lost", result->nb_packets_lost);
LOG_INF("%u packets received", result->nb_packets_rcvd);
LOG_INF("%u kbps throughput",client_rate_in_kbps);
C

7.4 Inform the user that there is an error with the UDP session

LOG_ERR("UDP session error");
C

8. Install iPerf

Running this example requires the Wi-Fi iPerf application. Download and install iPerf by following the guide for your operating system.

8.1 Install chocolatey.

8.2 Add chocolatey to path.

8.3 Open a Windows Command Prompt window as Administrator. To do so, press the Windows key, type “cmd”, right-click the result, and choose Run as Administrator.

8.4 Use choco to install iPerf. Make sure to install version 2.0.8.

choco install iperf2 --version=2.0.8

8.1 Install iPerf using apt.

sudo apt install iperf

9. Start the iPerf server

Important

This exercise assumes a local connection. Therefore, you must ensure your Nordic device and the device you are running the iPerf server from are connected to the same network.

9.1 Run the following command in the command prompt to start the iPerf server in UDP mode.

iperf2 -s -i 1 -u

Allow iperf2 to communicate on public and private network if you get a pop up from Windows Defender Firewall.

9.1 Run the following command in a terminal to start the iPerf server in UDP mode.

iperf -s -i 1 -u

10. With the iPerf server running, build and flash the application to your board.

Due to the memory requirements of the exercise, you need to build the application without TF-M.

BoardBuild without TF-MExtra CMake arguments
nRF7002 DKnrf7002dk_nrf5340_cpuappN/A
nRF5340 DK + nRF7002 EKnrf5340dk_nrf5340_cpuapp-DSHIELD=nrf7002ek

If the application does not connect to the network, initiate a Wi-Fi connection by issuing the following commands

wifi_cred add "<your_network_SSID>" WPA2-PSK "<your_network_password>"
wifi_cred auto_connect

Please note it can take a minute while messages are printed on the terminal.

*** Booting nRF Connect SDK 2.6.1-3758bcbfa5cd ***
[00:00:30.482,421] <inf> Lesson3_Exercise2: Starting nrf7002dk_nrf5340_cpuapp with CPU frequency: 128 MHz
[00:00:32.482,574] <inf> Lesson3_Exercise2: Waiting to connect to Wi-Fi
[00:00:37.485,260] <inf> Lesson3_Exercise2: Network connected
[00:00:37.498,229] <inf> Lesson3_Exercise2: IPv4 address 192.168.50.64
[00:00:37.498,229] <inf> Lesson3_Exercise2: Starting Wi-Fi throughput test: Zperf client
[00:00:37.498,504] <inf> Lesson3_Exercise2: New UDP session started
Terminal

11. The results of the throughput test will be printed after the test has finished

[00:00:28.882,141] <inf> Lesson3_Exercise2: Wi-Fi throughput test: Upload completed!
[00:00:28.882,171] <inf> Lesson3_Exercise2: Upload results:
[00:00:28.882,171] <inf> Lesson3_Exercise2: 20317184 bytes in 20001 ms
[00:00:28.882,171] <inf> Lesson3_Exercise2: 19841 packets sent
[00:00:28.882,171] <inf> Lesson3_Exercise2: 78 packets lost
[00:00:28.882,202] <inf> Lesson3_Exercise2: 19841 packets received
[00:00:28.882,202] <inf> Lesson3_Exercise2: 7935 kbps throughput
Terminal

You should see output similar to this from the iPerf server

------------------------------------------------------------
Server listening on UDP port 5001
Receiving 1470 byte datagrams
UDP buffer size:  208 KByte (default)
------------------------------------------------------------
[  3] local 192.168.10.102 port 5001 connected with 192.168.10.176 port 63316
[ ID] Interval       Transfer     Bandwidth        Jitter   Lost/Total Datagrams
[  3]  0.0- 1.0 sec   971 KBytes  7.95 Mbits/sec   0.656 ms    7/  978 (0.72%)
[  3]  1.0- 2.0 sec  1000 KBytes  8.19 Mbits/sec   0.718 ms    0/ 1000 (0%)
[  3]  2.0- 3.0 sec   995 KBytes  8.15 Mbits/sec   0.869 ms    0/  995 (0%)
[  3]  3.0- 4.0 sec   992 KBytes  8.13 Mbits/sec   0.948 ms    0/  992 (0%)
[  3]  4.0- 5.0 sec   990 KBytes  8.11 Mbits/sec   0.862 ms   71/ 1061 (6.7%)
[  3]  5.0- 6.0 sec   989 KBytes  8.10 Mbits/sec   0.720 ms    0/  989 (0%)
[  3]  6.0- 7.0 sec  1012 KBytes  8.29 Mbits/sec   0.991 ms    0/ 1012 (0%)
[  3]  7.0- 8.0 sec   978 KBytes  8.01 Mbits/sec   0.738 ms    0/  978 (0%)
[  3]  8.0- 9.0 sec   984 KBytes  8.06 Mbits/sec   0.673 ms    0/  984 (0%)
[  3]  9.0-10.0 sec   994 KBytes  8.14 Mbits/sec   0.676 ms    0/  994 (0%)
[  3] 10.0-11.0 sec   988 KBytes  8.09 Mbits/sec   0.653 ms    0/  988 (0%)
[  3] 11.0-12.0 sec  1012 KBytes  8.29 Mbits/sec   0.705 ms    0/ 1012 (0%)
[  3] 12.0-13.0 sec  1010 KBytes  8.27 Mbits/sec   0.698 ms    0/ 1010 (0%)
[  3] 13.0-14.0 sec  1012 KBytes  8.29 Mbits/sec   0.501 ms    0/ 1012 (0%)
[  3] 14.0-15.0 sec  1013 KBytes  8.30 Mbits/sec   0.641 ms    0/ 1013 (0%)
[  3] 15.0-16.0 sec   966 KBytes  7.91 Mbits/sec   0.948 ms    0/  966 (0%)
[  3] 16.0-17.0 sec   993 KBytes  8.13 Mbits/sec   0.689 ms    0/  993 (0%)
[  3] 17.0-18.0 sec   995 KBytes  8.15 Mbits/sec   0.645 ms    0/  995 (0%)
[  3] 18.0-19.0 sec   978 KBytes  8.01 Mbits/sec   0.668 ms    0/  978 (0%)
[  3]  0.0-19.9 sec  19.3 MBytes  8.13 Mbits/sec   0.848 ms   78/19841 (0.39%)
Terminal

Note

The Wi-Fi performance is affected by various factors, such as the distance to the access point and its placement, the Wi-Fi adapter on the PC running the iPerf server, frequency band, noise/interference, and more. Therefore, results will vary from one testing environment to the other.

v2.6.0 – v2.5.0

Measuring the throughput of a Wi-Fi connection

In this exercise, we will measure the throughput of the Wi-Fi connection between your device and an iPerf server that we will create in the exercise. .

The device will connect to the iPerf server running on a test PC, and will send and receive packets during the test duration. When the test is finished, the result of the throughput test will be printed to the terminal.

Exercise steps

In the GitHub repository for this course, go to the base code for this exercise, found in l3/l3_e2.

1. Set the necessary networking configurations

1.1 Enable zperf

Enable zperf shell utility through CONFIG_NET_ZPERF in the prj.conf file.

CONFIG_NET_ZPERF=y
Kconfig

1.2 Configure the relevant network configurations.

Enable network settings through CONFIG_NET_CONFIG_SETTINGS and set the networking timeout to 0 through CONFIG_NET_CONFIG_INIT_TIMEOUT.

Set the peer IPv4 address using CONFIG_NET_CONFIG_PEER_IPV4_ADDR. This is the IPv4 address of the computer you are running the zperf server on. You can find the IPv4 of your computer by typing “ipconfig” in your command prompt on Windows or “ifconfig” in your terminal on Linux and macOS.

CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_INIT_TIMEOUT=0
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="<IP address of PC running the iPerf server>"
Kconfig

1.3 Increase the heap memory pool size.

Depending on the board you are using, open either boards/nrf7002dk_nrf5340_cpuapp.conf or boards/nrf5340dk_nrf5340_cpuapp.conf, and increase the heap memory pool size by adding the following Kconfig line.

CONFIG_HEAP_MEM_POOL_SIZE=200000
Kconfig

2. Include the header files for the zperf API and nrfx clock.

#include <zephyr/net/zperf.h>
#include <nrfx_clock.h>
C

3. Define the zperf server and test parameters

3.1 Define the port for the zperf server

#define PEER_PORT 5001
C

3.2 Define packet size, rate and test duration

#define WIFI_ZPERF_PKT_SIZE 1024
#define WIFI_ZPERF_RATE 10000
#define WIFI_TEST_DURATION 20000
C

4. Create a socket address struct for the server address

static struct sockaddr_in in4_addr_my = {
	.sin_family = AF_INET,
	.sin_port = htons(PEER_PORT),
};
C

5. Fill in the zperf upload parameters for the zperf client

5.1 Initialize a struct for storing the zperf upload parameters.

struct zperf_upload_params params;
C

5.2 Configure packet size, rate and duration from the defines created earlier.

params.packet_size = WIFI_ZPERF_PKT_SIZE;
params.rate_kbps = WIFI_ZPERF_RATE;
params.duration_ms = WIFI_TEST_DURATION;
C

5.3 Convert the server address from a string to IP address.

ret = net_addr_pton(AF_INET, CONFIG_NET_CONFIG_PEER_IPV4_ADDR, &in4_addr_my.sin_addr);
if (ret < 0) {
	LOG_ERR("Invalid IPv4 address %s\n", CONFIG_NET_CONFIG_PEER_IPV4_ADDR);
	return -EINVAL;
}
C

5.4 Add the zperf server address to the zperf_upload_params struct.

memcpy(&params.peer_addr, &in4_addr_my, sizeof(in4_addr_my));
C

6. Call zperf_udp_upload_async() to start the asynchronous UDP upload.

ret = zperf_udp_upload_async(&params, udp_upload_results_cb, NULL);
if (ret != 0) {
	LOG_ERR("Failed to start Wi-Fi benchmark: %d\n", ret);
	return ret;
}
C

7. Retrieve and print the results from the throughput test.

Handle the three zperf session statuses: started, finished, and error inside the zperf_status switch.

7.1 Inform the user that the UDP session has started

LOG_INF("New UDP session started");
C

7.2 If client_time_in_us is not zero, calculate the throughput rate in kilobit per second. Otherwise, set it to zero.

if (result->client_time_in_us != 0U) {
	client_rate_in_kbps = (uint32_t)
		(((uint64_t)result->nb_packets_sent *
		  (uint64_t)result->packet_size * (uint64_t)8 *
		  (uint64_t)USEC_PER_SEC) /
		 ((uint64_t)result->client_time_in_us * 1024U));
} else {
	client_rate_in_kbps = 0U;
}
C

7.3 Print the results of the throughput test

LOG_INF("Upload results:");
LOG_INF("%u bytes in %u ms",
		(result->nb_packets_sent * result->packet_size),
		(result->client_time_in_us / USEC_PER_MSEC));
LOG_INF("%u packets sent", result->nb_packets_sent);
LOG_INF("%u packets lost", result->nb_packets_lost);
LOG_INF("%u packets received", result->nb_packets_rcvd);
LOG_INF("%u kbps throughput",client_rate_in_kbps);
C

7.4 Inform the user that there is an error with the UDP session

LOG_ERR("UDP session error");
C

8. Install iPerf

Running this example requires the Wi-Fi iPerf application. Download and install iPerf by following the guide for your operating system.

8.1 Install chocolatey.

8.2 Add chocolatey to path.

8.3 Open a Windows Command Prompt window as Administrator. To do so, press the Windows key, type “cmd”, right-click the result, and choose Run as Administrator.

8.4 Use choco to install iPerf. Make sure to install version 2.0.8.

choco install iperf2 --version=2.0.8

8.1 Install iPerf using apt.

sudo apt install iperf

9. Start the iPerf server

Important

This exercise assumes a local connection. Therefore, you must ensure your Nordic device and the device you are running the iPerf server from are connected to the same network.

9.1 Run the following command in the command prompt to start the iPerf server in UDP mode.

iperf2 -s -i 1 -u

Allow iperf2 to communicate on public and private network if you get a pop up from Windows Defender Firewall.

9.1 Run the following command in a terminal to start the iPerf server in UDP mode.

iperf -s -i 1 -u

10. With the iPerf server running, build and flash the application to your board.

Due to the memory requirements of the exercise, you need to build the application without TF-M.

BoardBuild without TF-MExtra CMake arguments
nRF7002 DKnrf7002dk_nrf5340_cpuappN/A
nRF5340 DK + nRF7002 EKnrf5340dk_nrf5340_cpuapp-DSHIELD=nrf7002ek

If the application does not connect to the network, initiate a Wi-Fi connection by issuing the following commands

wifi_cred add "<your_network_SSID>" WPA2-PSK "<your_network_password>"
wifi_cred auto_connect

Please note it can take a minute while messages are printed on the terminal.

*** Booting nRF Connect SDK 2.6.1-3758bcbfa5cd ***
[00:00:30.482,421] <inf> Lesson3_Exercise2: Starting nrf7002dk_nrf5340_cpuapp with CPU frequency: 128 MHz
[00:00:32.482,574] <inf> Lesson3_Exercise2: Waiting to connect to Wi-Fi
[00:00:37.485,260] <inf> Lesson3_Exercise2: Network connected
[00:00:37.498,229] <inf> Lesson3_Exercise2: IPv4 address 192.168.50.64
[00:00:37.498,229] <inf> Lesson3_Exercise2: Starting Wi-Fi throughput test: Zperf client
[00:00:37.498,504] <inf> Lesson3_Exercise2: New UDP session started
Terminal

11. The results of the throughput test will be printed after the test has finished

[00:00:28.882,141] <inf> Lesson3_Exercise2: Wi-Fi throughput test: Upload completed!
[00:00:28.882,171] <inf> Lesson3_Exercise2: Upload results:
[00:00:28.882,171] <inf> Lesson3_Exercise2: 20317184 bytes in 20001 ms
[00:00:28.882,171] <inf> Lesson3_Exercise2: 19841 packets sent
[00:00:28.882,171] <inf> Lesson3_Exercise2: 78 packets lost
[00:00:28.882,202] <inf> Lesson3_Exercise2: 19841 packets received
[00:00:28.882,202] <inf> Lesson3_Exercise2: 7935 kbps throughput
Terminal

You should see output similar to this from the iPerf server

------------------------------------------------------------
Server listening on UDP port 5001
Receiving 1470 byte datagrams
UDP buffer size:  208 KByte (default)
------------------------------------------------------------
[  3] local 192.168.10.102 port 5001 connected with 192.168.10.176 port 63316
[ ID] Interval       Transfer     Bandwidth        Jitter   Lost/Total Datagrams
[  3]  0.0- 1.0 sec   971 KBytes  7.95 Mbits/sec   0.656 ms    7/  978 (0.72%)
[  3]  1.0- 2.0 sec  1000 KBytes  8.19 Mbits/sec   0.718 ms    0/ 1000 (0%)
[  3]  2.0- 3.0 sec   995 KBytes  8.15 Mbits/sec   0.869 ms    0/  995 (0%)
[  3]  3.0- 4.0 sec   992 KBytes  8.13 Mbits/sec   0.948 ms    0/  992 (0%)
[  3]  4.0- 5.0 sec   990 KBytes  8.11 Mbits/sec   0.862 ms   71/ 1061 (6.7%)
[  3]  5.0- 6.0 sec   989 KBytes  8.10 Mbits/sec   0.720 ms    0/  989 (0%)
[  3]  6.0- 7.0 sec  1012 KBytes  8.29 Mbits/sec   0.991 ms    0/ 1012 (0%)
[  3]  7.0- 8.0 sec   978 KBytes  8.01 Mbits/sec   0.738 ms    0/  978 (0%)
[  3]  8.0- 9.0 sec   984 KBytes  8.06 Mbits/sec   0.673 ms    0/  984 (0%)
[  3]  9.0-10.0 sec   994 KBytes  8.14 Mbits/sec   0.676 ms    0/  994 (0%)
[  3] 10.0-11.0 sec   988 KBytes  8.09 Mbits/sec   0.653 ms    0/  988 (0%)
[  3] 11.0-12.0 sec  1012 KBytes  8.29 Mbits/sec   0.705 ms    0/ 1012 (0%)
[  3] 12.0-13.0 sec  1010 KBytes  8.27 Mbits/sec   0.698 ms    0/ 1010 (0%)
[  3] 13.0-14.0 sec  1012 KBytes  8.29 Mbits/sec   0.501 ms    0/ 1012 (0%)
[  3] 14.0-15.0 sec  1013 KBytes  8.30 Mbits/sec   0.641 ms    0/ 1013 (0%)
[  3] 15.0-16.0 sec   966 KBytes  7.91 Mbits/sec   0.948 ms    0/  966 (0%)
[  3] 16.0-17.0 sec   993 KBytes  8.13 Mbits/sec   0.689 ms    0/  993 (0%)
[  3] 17.0-18.0 sec   995 KBytes  8.15 Mbits/sec   0.645 ms    0/  995 (0%)
[  3] 18.0-19.0 sec   978 KBytes  8.01 Mbits/sec   0.668 ms    0/  978 (0%)
[  3]  0.0-19.9 sec  19.3 MBytes  8.13 Mbits/sec   0.848 ms   78/19841 (0.39%)
Terminal

Note

The Wi-Fi performance is affected by various factors, such as the distance to the access point and its placement, the Wi-Fi adapter on the PC running the iPerf server, frequency band, noise/interference, and more. Therefore, results will vary from one testing environment to the other.

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

  • 8 or more characters
  • Upper and lower case letters
  • At least one number or special character

Forgot your password?
Enter the email associated with your account, and we will send you a link to reset your password.