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

(Optional) Enabling TWT with notification

In this exercise, we will use Target Wake Time (TWT) to reduce power consumption further. Additionally, we will use the TWT sleep status notification to be notified of when the device goes to sleep and when it wakes up, and use this to send and check for received packets during the awake time. To show that the device can send and receive packets, the device connects to a simple echo UDP server that we will run on a PC.

When the device starts up, it will be configured to use legacy power saving mode. Pressing button 1 on the board will enable TWT, or if TWT is already enabled it will instead be disabled. Pressing button 2 will enable sending packets. When this is enabled, the device will send a packet every TWT period when it wakes up.

Important

To test TWT, you need a Wi-Fi 6 router that supports TWT.

Exercise steps

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

1. Define macros and global variables

1.1 Define the port and IPv4 address for the server.

#define SERVER_PORT 7777
#define SERVER_IPV4_ADDR "192.168.32.119"
C

The IPv4 address is the address of the PC running the UDP server, so this address must be changed to reflect the IPv4 address of your PC.

1.2 Define macros for wake up time and interval for TWT.

#define TWT_WAKE_INTERVAL_MS 65
#define TWT_INTERVAL_MS      7000
C

These variables will be used when configuring TWT later. The TWT interval is the total interval between TWT wake periods: TWT interval = wake up time + sleeping time.

1.3 Create variables to keep track of TWT status, flow ID, and if sending packets is enabled.

bool nrf_wifi_twt_enabled = 0;
static uint32_t twt_flow_id = 1;
bool sending_packets_enabled = 0;
C

2. Configure the TWT request.

2.1 Define the TWT parameters struct wifi_twt_params and fill the parameters that are common for both TWT setup and TWT teardown.

struct wifi_twt_params params = { 0 };

params.negotiation_type = WIFI_TWT_INDIVIDUAL;
params.flow_id = twt_flow_id;
params.dialog_token = 1;
C

2.2 Fill in the rest of the wifi_twt_params parameters.

When TWT is not enabled we want to send a TWT setup request, and if it is enabled we want to send a TWT teardown request.

2.2.1 Fill in the TWT setup specific parameters of the wifi_twt_params struct.

params.operation = WIFI_TWT_SETUP;
params.setup_cmd = WIFI_TWT_SETUP_CMD_REQUEST;
params.setup.responder = 0;
params.setup.trigger = 0;
params.setup.implicit = 1;
params.setup.announce = 0;
params.setup.twt_wake_interval = TWT_WAKE_INTERVAL_MS * USEC_PER_MSEC;
params.setup.twt_interval = TWT_INTERVAL_MS * USEC_PER_MSEC;
C

We are using the TWT setup operation to enable TWT. Since our device is the one requesting to setup TWT we send a Request TWT command to the AP with the responder parameter indicating that we are a requester.

We want to reduce the power consumption as much as possible, which we do by reducing the amount of messages sent. Therefore, we disable trigger frames, and configure TWT to be implicit and unannounced. With this configuration, the device does not have to send a trigger frame to the AP to inform that it is awake and ready to receive packets.

2.2.2 Fill in the TWT teardown specific parameters of the wifi_twt_params struct.

params.operation = WIFI_TWT_TEARDOWN;
params.setup_cmd = WIFI_TWT_TEARDOWN;
twt_flow_id = twt_flow_id<WIFI_MAX_TWT_FLOWS ? twt_flow_id+1 : 1;
nrf_wifi_twt_enabled = 0;
C

We are using the TWT teardown operation to disable TWT by sending a TWT teardown command to the AP.

2.3 Send the TWT request with net_mgmt.

if (net_mgmt(NET_REQUEST_WIFI_TWT, iface, &params, sizeof(params))) {
	LOG_ERR("%s with %s failed, reason : %s", wifi_twt_operation_txt(params.operation),
		wifi_twt_negotiation_type_txt(params.negotiation_type),
		wifi_twt_get_err_code_str(params.fail_reason));
	return -1;
}
C

3. Add the TWT events.

3.1 Define a mask for the TWT events we are interested in.

#define TWT_MGMT_EVENTS (NET_EVENT_WIFI_TWT | NET_EVENT_WIFI_TWT_SLEEP_STATE)
C

3.2 Handle the two events in the event handler.

3.2.1 Upon a TWT event, call handle_wifi_twt_event() to handle the response.

In twt_mgmt_event_handler(), add the following code to handle the TWT event

case NET_EVENT_WIFI_TWT:
	handle_wifi_twt_event(cb);
	break;
C

3.2.2 Upon TWT sleep state event, inform the user of the current sleep state. When the device is in the awake state, send a packet to the server and check for any received packets if sending packets is enabled.

case NET_EVENT_WIFI_TWT_SLEEP_STATE:
	int *twt_state;
	twt_state = (int *)(cb->info);
	LOG_INF("TWT sleep state: %s", *twt_state ? "awake" : "sleeping" );
	if ((*twt_state == WIFI_TWT_STATE_AWAKE) & sending_packets_enabled) {
		send_packet();
		receive_packet();
	}
	break;
C

3.3 Initialize and add the TWT event handler

net_mgmt_init_event_callback(&twt_mgmt_cb, twt_mgmt_event_handler, TWT_MGMT_EVENTS);
net_mgmt_add_event_callback(&twt_mgmt_cb);
C

4. Implement the TWT event callback function function.

4.1 Create a wifi_twt_params struct for the received TWT event and fill it with the event information.

const struct wifi_twt_params *resp = (const struct wifi_twt_params *)cb->info;
C

4.2 If the event was a TWT teardown initiated by the AP, set change the value of nrf_wifi_twt_enabled and exit the function.

if (resp->operation == WIFI_TWT_TEARDOWN) {
	LOG_INF("TWT teardown received for flow ID %d\n",
	      resp->flow_id);
	nrf_wifi_twt_enabled = 0;
	return;
}
C

4.3 Update twt_flow_id to reflect the flow ID received in the TWT response.

twt_flow_id = resp->flow_id;
C

4.4 Check if a TWT response was received. If not, the TWT request timed out.

if (resp->resp_status == WIFI_TWT_RESP_RECEIVED) {
	LOG_INF("TWT response: %s",
	      wifi_twt_setup_cmd_txt(resp->setup_cmd));		
} 
else {
	LOG_INF("TWT response timed out\n");
	return;
}
C

4.5 If the TWT setup was accepted, change the value of nrf_wifi_twt_enabled and print the negotiated parameters.

if (resp->setup_cmd == WIFI_TWT_SETUP_CMD_ACCEPT) {
	nrf_wifi_twt_enabled = 1;

	LOG_INF("== TWT negotiated parameters ==");
	LOG_INF("TWT Dialog token: %d",
	      resp->dialog_token);
	LOG_INF("TWT flow ID: %d",
	      resp->flow_id);
	LOG_INF("TWT negotiation type: %s",
	      wifi_twt_negotiation_type_txt(resp->negotiation_type));
	LOG_INF("TWT responder: %s",
	       resp->setup.responder ? "true" : "false");
	LOG_INF("TWT implicit: %s",
	      resp->setup.implicit ? "true" : "false");
	LOG_INF("TWT announce: %s",
	      resp->setup.announce ? "true" : "false");
	LOG_INF("TWT trigger: %s",
	      resp->setup.trigger ? "true" : "false");
	LOG_INF("TWT wake interval: %d ms (%d us)",
	      resp->setup.twt_wake_interval/USEC_PER_MSEC,
		  resp->setup.twt_wake_interval);
	LOG_INF("TWT interval: %lld s (%lld us)",
		  resp->setup.twt_interval/USEC_PER_SEC,
	      resp->setup.twt_interval);
	LOG_INF("===============================");
}
C

5. Modify the button handler.

5.1 Call wifi_set_twt() to enable or disable TWT when button 1 is pressed.

if (button & DK_BTN1_MSK) {
	wifi_set_twt();
}
C

5.2 Enable or disable sending packets during TWT awake when button 2 is pressed.

if (button & DK_BTN2_MSK) {
	sending_packets_enabled = !sending_packets_enabled;
	LOG_INF("Sending packets %s", sending_packets_enabled ? "enabled" : "disabled" );
}
C

6. Testing

6.1 Start the UDP server.

The UDP server is a Python script located in wififund_less6_exer2/scripts. Open the scripts directory in a terminal or command-line windows and enter the following command to start the server

python3 simple_udp_server.py

You should see Starting UDP server in the window running the server.

6.2 Build and flash the application to your device.

This exercise uses the PSA backend for storing the Wi-Fi credentials. Therefore, you must build with TF-M.

BoardBuild with TF-M
nRF7002 DKnrf7002dk_nrf5340_cpuapp_ns
nRF5340 DK + nRF7002 EKnrf5340dk_nrf5340_cpuapp_ns

If necessary, input the commands to connect to Wi-Fi, as we have done in previous exercises.

You should see the following log when the device has started

*** Booting nRF Connect SDK 2.6.1-3758bcbfa5cd ***
[00:00:01.490,509] <inf> Lesson6_Exercise2: Waiting to connect to Wi-Fi
[00:00:08.100,097] <inf> Lesson6_Exercise2: Network connected
[00:00:08.153,991] <inf> Lesson6_Exercise2: IPv4 address acquired
[00:00:08.154,968] <inf> Lesson6_Exercise2: Connected to server
[00:00:08.154,968] <inf> Lesson6_Exercise2: Press button 1 on your DK to enable or disable TWT
[00:00:08.156,280] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 0
[00:00:09.653,778] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 0)
Terminal

6.3 Press button one on the board to enable TWT.

You should see a log that TWT setup has been requested. If the router accepts the TWT setup request the device will print the negotiated TWT parameters.

[00:00:15.703,460] <inf> Lesson6_Exercise2: -------------------------------
[00:00:15.703,491] <inf> Lesson6_Exercise2: TWT operation TWT setup requested
[00:00:15.703,491] <inf> Lesson6_Exercise2: -------------------------------
[00:00:15.799,896] <inf> Lesson6_Exercise2: TWT response: TWT accept
[00:00:15.799,896] <inf> Lesson6_Exercise2: == TWT negotiated parameters ==
[00:00:15.799,896] <inf> Lesson6_Exercise2: TWT Dialog token: 1
[00:00:15.799,926] <inf> Lesson6_Exercise2: TWT flow ID: 0
[00:00:15.799,957] <inf> Lesson6_Exercise2: TWT negotiation type: TWT individual negotiation
[00:00:15.799,957] <inf> Lesson6_Exercise2: TWT responder: true
[00:00:15.799,987] <inf> Lesson6_Exercise2: TWT implicit: true
[00:00:15.800,018] <inf> Lesson6_Exercise2: TWT announce: false
[00:00:15.800,048] <inf> Lesson6_Exercise2: TWT trigger: false
[00:00:15.800,048] <inf> Lesson6_Exercise2: TWT wake interval: 65 ms (65024 us)
[00:00:15.800,079] <inf> Lesson6_Exercise2: TWT interval: 7 s (7004000 us)
[00:00:15.800,079] <inf> Lesson6_Exercise2: ===============================
Terminal

Once TWT is enabled, the sleep state will be printed to the log.

[00:00:15.800,720] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:18.435,821] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:18.501,220] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:25.440,124] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:25.506,378] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
Terminal

6.4 Press button two to enable sending packets during TWT awake periods.

The device will start sending and receiving UDP packets, which will be printed to logs.

[00:00:28.442,779] <inf> Lesson6_Exercise2: Sending packets enabled
[00:00:32.443,939] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:32.446,105] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 1
[00:00:39.451,538] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 1)
[00:00:39.451,568] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:39.451,599] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:39.452,392] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 2
[00:00:46.454,589] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 2)
[00:00:46.454,620] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:46.454,681] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:46.455,444] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 3
[00:00:53.458,801] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 3)
[00:00:53.458,831] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:53.458,892] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:53.459,655] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 4
[00:01:00.463,195] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 4)
Terminal

The UDP server will print the packets it receives from the device.

Starting UDP server
Data received from client: Hello from nRF70 Series! 1
Data received from client: Hello from nRF70 Series! 2
Data received from client: Hello from nRF70 Series! 3
Data received from client: Hello from nRF70 Series! 4
Terminal

6.5 Press button one on the board once more to disable TWT.

[00:01:04.898,895] <inf> Lesson6_Exercise2: -------------------------------
[00:01:04.898,956] <inf> Lesson6_Exercise2: TWT operation TWT teardown requested
[00:01:04.898,956] <inf> Lesson6_Exercise2: -------------------------------
Terminal

7. Testing with power measurement

7.1 Connect the PPK2 to the device as shown below

PPK2DK
VOUTP23 VBAT
GNDP21

Note

The current is measured over only the nRF7002 IC in this exercise, not including the nRF5340 SoC. Guides for how to measure the total current or measuring the nRF7002 IC and nRF5340 SoC separately can be found at nRF7002 DK – Measuring current.

7.2 Open the Power Profiler app and select the PPK2 in the drop-down menu.

7.3 Select source meter mode, set the supply voltage to 3.6 volts, and enable power output.

7.4 Build and program the example to the board.

7.5 Press ‘Start’ in the Power Profiler app to start the measurement.

7.6 Test the example as in step 6 and look at the power measurement.

When enabling TWT we see that the time between the wake up spikes increases compared to with legacy power saving. One of the big advantages of TWT is that it enables the device to be asleep for longer periods of time, which decreases the average power consumption.

Legacy power saving compared to TWT.

8. Play around with different TWT periods.

Modify TWT_INTERVAL_MS to see how the TWT interval affects the average power consumption.

The default TWT interval in the exercise is 7 seconds. Below we show the power consumption when the interval is 7 seconds and 20 seconds. The first image shows the 7 second interval where the device is only listening for packets during wake up, and the seconds shows the spikes when it is also sending a packet each time it wakes up. The second image shows when the TWT interval is 20 seconds.

TWT with 7 second TWT interval.
TWT with 7 second TWT interval and sending packets enabled.
TWT with 20 second TWT interval.

Increasing the TWT interval will increase the time the device is asleep, thus decreasing the average power consumption.

This is the complete log

*** Booting nRF Connect SDK 2.6.1-3758bcbfa5cd ***
[00:00:01.490,509] <inf> Lesson6_Exercise2: Connecting to Wi-Fi
[00:00:08.100,097] <inf> Lesson6_Exercise2: Connected to Wi-Fi Network: <SSID>
[00:00:08.153,991] <inf> Lesson6_Exercise2: IPv4 address acquired
[00:00:08.154,968] <inf> Lesson6_Exercise2: Connected to server
[00:00:08.154,968] <inf> Lesson6_Exercise2: Press button 1 on your DK to enable or disable TWT
[00:00:08.156,280] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 0
[00:00:09.653,778] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 0)
[00:00:15.703,460] <inf> Lesson6_Exercise2: -------------------------------
[00:00:15.703,491] <inf> Lesson6_Exercise2: TWT operation TWT setup requested
[00:00:15.703,491] <inf> Lesson6_Exercise2: -------------------------------
[00:00:15.799,896] <inf> Lesson6_Exercise2: TWT response: TWT accept
[00:00:15.799,896] <inf> Lesson6_Exercise2: == TWT negotiated parameters ==
[00:00:15.799,896] <inf> Lesson6_Exercise2: TWT Dialog token: 1
[00:00:15.799,926] <inf> Lesson6_Exercise2: TWT flow ID: 0
[00:00:15.799,957] <inf> Lesson6_Exercise2: TWT negotiation type: TWT individual negotiation
[00:00:15.799,957] <inf> Lesson6_Exercise2: TWT responder: true
[00:00:15.799,987] <inf> Lesson6_Exercise2: TWT implicit: true
[00:00:15.800,018] <inf> Lesson6_Exercise2: TWT announce: false
[00:00:15.800,048] <inf> Lesson6_Exercise2: TWT trigger: false
[00:00:15.800,048] <inf> Lesson6_Exercise2: TWT wake interval: 65 ms (65024 us)
[00:00:15.800,079] <inf> Lesson6_Exercise2: TWT interval: 7 s (7004000 us)
[00:00:15.800,079] <inf> Lesson6_Exercise2: ===============================
[00:00:15.800,720] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:18.435,821] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:18.501,220] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:25.440,124] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:25.506,378] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:28.442,779] <inf> Lesson6_Exercise2: Sending packets enabled
[00:00:32.443,939] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:32.446,105] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 1
[00:00:39.451,538] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 1)
[00:00:39.451,568] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:39.451,599] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:39.452,392] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 2
[00:00:46.454,589] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 2)
[00:00:46.454,620] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:46.454,681] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:46.455,444] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 3
[00:00:53.458,801] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 3)
[00:00:53.458,831] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:00:53.458,892] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:00:53.459,655] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 4
[00:01:00.463,195] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 4)
[00:01:00.463,256] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:01:00.463,287] <inf> Lesson6_Exercise2: TWT sleep state: awake
[00:01:00.464,080] <inf> Lesson6_Exercise2: Successfully sent message: Hello from nRF70 Series! 5
[00:01:00.472,167] <inf> Lesson6_Exercise2: Data received from the server: (Hello from nRF70 Series! 5)
[00:01:00.532,867] <inf> Lesson6_Exercise2: TWT sleep state: sleeping
[00:01:04.898,895] <inf> Lesson6_Exercise2: -------------------------------
[00:01:04.898,956] <inf> Lesson6_Exercise2: TWT operation TWT teardown requested
[00:01:04.898,956] <inf> Lesson6_Exercise2: -------------------------------
Terminal
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.