In this exercise, we will flash the Wi-Fi: Shell sample to your board. This sample allows you to send commands to the Wi-Fi chipset on your board through a command-line interface (see Supported CLI commands).
We will use the CLI to do various common Wi-Fi operations, such as a Wi-Fi scan, connection and disconnection. Afterwards, we will learn how to securely store your Wi-Fi credentials on your device. In the last section, we will test out the commands for managing Target Wake Time (TWT) on your device. This requires you to have a Wi-Fi 6 Access Point with TWT enabled, and this portion of the exercise is, therefore, optional.
Exercise steps
1. Build and flash the Wi-Fi Shell sample to your board.
1.1 In the nRF Connect window in Visual Studio Code, select Create a new application under the WELCOME menu.
1.2 Select Copy a sample, then select the nRF Connect SDK version you are using.
Search for and select the sample “Wi-Fi shell“.
In the next step, you will input the path to where you want to store this exercise. Save the application to wherever you cloned the code base for this course, as <wifi_fund_repo>\l2\l2_e1_sol.
1.3 Add a build configuration and select the board target for whichever board you are using.
Under Board, select , then click Build Configuration.nrf7002dk/nrf5340/cpuapp

Under Board, select . Then, under Extra CMake arguments, write the following line and click OK.nrf5340dk/nrf5340/cpuapp
Then select Build Configuration.

Alternatively, you can add the following line to the CMakeLists.txt file of the application you are building
1.4 Build and flash the application to your device.
Note
Make sure jumpers P22 and P23 are installed on your board or you will get an error while building.
![]()
1.5. View the terminal output
In VS Code, under Connected Devices, connect to the COM port of your board by clicking the electric plug icon as shown in the image below, then choose the default COM port settings 115200 8n1 rtscts:off

1. Install the Serial Terminal app.
Open nRF Connect for Desktop, navigate to the Serial Terminal app and click Install.
2. Launch the Serial Terminal app.
3. Connect to your board and find the COM ports.
- In the upper right hand corner, Select Device.
- Select your board, in the example below we are using the nRF7002 DK. This will automatically connect to one of the COM ports on the device.

Observe the following log output
[00:00:00.315,429] <inf> wifi_nrf: Management buffer offload enabled
[00:00:00.448,486] <inf> fs_nvs: 2 Sectors of 4096 bytes
[00:00:00.448,486] <inf> fs_nvs: alloc wra: 0, fe8
[00:00:00.448,516] <inf> fs_nvs: data wra: 0, 0
*** Booting nRF Connect SDK ***
*** Using Zephyr OS ***
Starting nrf7002dk with CPU frequency: 128 MHz
[00:00:00.449,951] <inf> wifi_supplicant: wpa_supplicant initializedTerminal2. Scan for access points in your vicinity.
Before we connect to a network, let’s do a passive scan to see all available access points.
Type the following command into the terminal.
wifi scanYou should see something similar to the following output (this might take a few seconds). This is the scan result list.

The Wi-Fi chip outputs the acquired information about each available access point. As you can see in the output, this information includes the SSID, the length of the SSID, the channel where the AP was broadcasting its beacon, the band the AP operates on, the RSSI as perceived by your device, the security protocol implemented by the AP, the BSSID, and finally the presence or absence of MFP security.
3. Connect to an access point.
Now we can use the command wifi connect to connect to an access point.
wifi connect takes many parameters, a few of them are listed below (run wifi connect -help in the terminal for the full list)
- -s
<SSID>: SSID of network - -c
<channel>: 0 for any channel - -b
<band>: 0 for any band - -p
<PSK>: Valid only for secured SSIDs - -k
<key-mgmt>: 0: None, 1: WPA2-PSK, 2: WPA2-PSK-256, 3: SAE-HNP, 4: SAE-H2E, 5: SAE-AUTO, 6: WAPI, 7: EAP-TLS, 8: WEP, 9: WPA-PSK, 10: WPA-Auto-Personal, 11: DPP, 12: EAP-PEAP-MSCHAPv2, 13: EAP-PEAP-GTC, 14: EAP-TTLS-MSCHAPv2, 15: EAP-PEAP-TLS - -w
<MFP>(optional): 0-Disable, 1-Optional, 2-Required - -m
<bssid>: MAC address of the AP (BSSID). - -t
<timeout>: Duration after which connection attempt needs to fail. - -h
<help>: Print out the help for the connect command.
Entering only the SSID, password (PSK), and key management of the access point you want to connect to will suffice. You can use the results from the wifi scan command to know which key management parameter to enter, see the Security column.
For the <key-mgmt> parameter, entering “0” only works with open APs.
Issue the following command in the terminal to connect to the AP.
wifi connect -s "<SSID>" -p "<psk>" -k <key-mgmt>If the connection is successful, you should see a similar log output
wifi connect -s "<SSID>" -p "<psk>" -k <key-mgmt>
wifi connect -s "<SSID>" -p "<psk>" -k <key-mgmt>
Connection requested
Connected
wpa_supp: wlan0: SME: Trying to authenticate with aa:aa:aa:aa:aa:aa (SSID='<SSID>' freq=5180 MHz)
<inf> wifi_nrf: wifi_nrf_wpa_supp_authenticate:Authentication request sent successfully
<inf> wpa_supp: wlan0: Trying to associate with aa:aa:aa:aa:aa:aa (SSID='<SSID>' freq=5180 MHz)
<inf> wifi_nrf: wifi_nrf_wpa_supp_associate: Association request sent successfully
<inf> wpa_supp: wlan0: Associated with aa:aa:aa:aa:aa:aa
<inf> wpa_supp: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
<inf> wpa_supp: wlan0: WPA: Key negotiation completed with aa:aa:aa:aa:aa:aa [PTK=CCMP GTK=CCMP]
<inf> wpa_supp: wlan0: CTRL-EVENT-CONNECTED - Connection to aa:aa:aa:aa:aa:aa completed [id=0 id_str=]
<inf> net_dhcpv4: Received: 192.00.00.00
Terminal4. Get the status of the network interface.
The command wifi status lets us read out extra information about the access point we are connected to that we didn’t get during the scan procedure, such as the beacon interval, DTIM and whether TWT is supported or not.
Issue the following command in the terminal
wifi statusObserve the following output:
Status: successful
==================
State: COMPLETED
Interface Mode: STATION
Link Mode: WIFI 6 (802.11ax/HE)
SSID: <SSID>
BSSID: AA:AA:AA:AA:AA:AA
Band: 5GHz
Channel: 36
Security: WPA2-PSK
MFP: Optional
RSSI: -86
Beacon Interval: 100
DTIM: 2
TWT: Supported
Current PHY TX rate (Mbps) : 0Terminal5. Get statistics about the Wi-Fi interface.
The command wifi statistics gives us additional information about the Wi-Fi connection, such as the bytes being sent and received, and the errors.
Issue the following command in the terminal
wifi statisticsObserve the following output
Statistics for Wi-Fi interface 0x200016a8 [1]
Bytes received : 109247
Bytes sent : 5458
Packets received : 346
Packets sent : 39
Receive errors : 0
Send errors : 0
Bcast received : 371
Bcast sent : 11
Mcast received : 218
Mcast sent : 8
Beacons received : 138
Beacons missed : 46
Unicast received : 50
Unicast sent : 17
Overrun count : 16Terminal6. Configure device wakeup mode
The command wifi ps_wakeup_mode is used to configure your Wi-Fi device wakeup mode when using power save mode. It takes the following parameters.
<dtim>: Wakeup mode for the DTIM interval<listen_interval>: Wakeup mode for the Listen interval
Issue the following command in the terminal to set it to wakeup for the DTIM interval for example. Power save modes, DTIM intervals and the Listen interval will be explained in detail in Lesson 6 of this course.
wifi ps_wakeup_mode dtim7. Storing the Wi-Fi credentials.
Using the wifi connect command mentioned above, you will have to re-enter your SSID and password upon device reset to re-establish the connection. To mitigate this, we can use the Wi-Fi credentials shell subcommands, which interact with the Wi-Fi credentials library to add the network credentials to credentials storage.
7.1 Store the Wi-Fi credentials using wifi cred add.
To store the credentials, we will be using the Wi-Fi credentials shell, which are identified as wifi cred, as specifically the subcommand add.
We will be using the following parameters from wifi cred add
<-s –ssid "<SSID>">: SSID.[-p, –passphrase]: Passphrase (valid only for secure SSIDs)[-k, –key-mgmt]: Key management type – [0-15] 0: None, 1: WPA2-PSK, 2: WPA2-PSK-256, 3: SAE-HNP, 4: SAE-H2E, 5: SAE-AUTO, 6: WAPI, 7: EAP-TLS, 8: WEP, 9: WPA-PSK, 10: WPA-Auto-Personal …
Enter the following CLI command to store your Wi-Fi credentials
wifi cred add -s "<your_network_SSID>" -p "<your_network_password>" -k <key_mgmt>For the last command, refer to the output from wifi scan, specifically the security column. Then replace <key-mgmt> with whichever number corresponds to the security for the Wi-Fi network you wish to connect to.
For a full list of key management types, you can use the following command
wifi cred add help7.2 Connect to Wi-Fi using the auto_connect subcommand.
Now that we have stored the credentials on the device, we will use the auto-connect subcommand which automatically connects to any stored network
wifi cred auto_connectThe auto_connect command is defined in <install_path>/zephyr/subsys/net/lib/wifi_credentials/wifi_credentials_shell.c, and uses the NET_REQUEST_WIFI_CONNECT_STORED command in the Network Management API. We will take a closer look at this command in Exercise 2 of this lesson.
7.3 The wifi cred command also has the subcommands list and delete.
list will list all the networks in credential storage and delete "<SSID>" will remove that network from credentials storage.
wifi cred list
wifi cred delete "<SSID>"(Optional) 8. Set up a Target Wake Time flow.
This step is optional as it only works if you are using a Wi-Fi 6 router that supports Target Wake Time. In some cases, you will need to enable TWT functionality in the router configurations. Consult your router’s user manual for more information.
The command wifi twt is used to manage TWT flows.
It has five different subcommands: quick_setup, setup, btwt_setup, teardown,and teardown_all.
To easily setup a TWT flow, we will use the quick_setup subcommand, which takes the following two parameters
<twt_wake_interval>: 1 – 262144 µs, TWT Wake Duration, for how long will the device be awake (in µs)<twt_interval>: 1 µs – 2^31 µs, TWT Wake Interval, the interval between successive TWT wake periods
Let’s say we want the device to wakeup for 20 ms, every minute. Then twt_wake_interval would be 20 ms (20000 µs) and the twt_interval will be 1 minute (60000000 µS).
Issue the following command in the terminal
wifi twt quick_setup 20000 60000000Terminaltwt quick_setup uses twt setup, but sets all the other parameters to default values.
twt setup takes the following parameters
<negotiation_type>: 0 – Individual, 1 – Broadcast, 2 – Wake TBTT
<setup_cmd>: 0 – Request, 1 – Suggest, 2 – Demand
<dialog_token>: 1-255, used to match action requests with action responses
<flow_id>: 0-7, gives an ID to the flow being created
<responder>: 0 – Requester, 1 – Responder
<trigger>: 0 – Trigger mode enabled, 1 – Trigger mode disabled
<implicit>: 0 – Explicit TWT operation, 1 – Implicit
<announce>: 0 – Announce mode enabled, 1 – Announce mode disabled
<twt_wake_interval>: 1 – 262144 µs, TWT Wake Duration, for how long will the device be awake (in µs)
<twt_interval>: 1 µs – 2^31 µs, TWT Wake Interval, the interval between successive TWT wake periods
<wake-ahead-duration>: 1 µs – 2^31 µs,
<info-disable>: 0 or 1
<exponent>: 0-31
<mantissa>: 1 – 2^16
Negotiation type is used to determine the target STA of this TWT schedule. When set to “Individual”, it means that this TWT schedule will be set to the STA individually. Whereas, when set to “Broadcast”, it means the AP broadcasts TWT schedules to multiple STAs.
(Optional) 9. Tear down the Target Wake Time flow.
To tear down the TWT flow, we will use the teardown subcommand, which takes the following parameters
<negotiation_type>: 0 – Individual, 1 – Broadcast, 2 – Wake TBTT (0 when using twt_<setup_cmd>: 0 – Request, 1 – Suggest, 2 – Demand<dialog_token>: 1-255, used to match action requests with action responses<flow_id>: 0-7, gives an ID to the flow being created
When using wifi twt quick_setup, the TWT flow
Use the flow_id parameter to indicate which TWT flow to tear down.
We want to tear down the flow that was set up in the previous step, which we assigned flow_id 2.
Issue the following command in the terminal
wifi twt teardown 0 0 1 0Alternatively, teardown_all can be used to tear down all TWT flows, by sending the following command.
wifi twt teardown_all