Enable or disable TWT with notification

I am studying Lesson 6/wifi fund_1ess6uexer2 and I can run the provided code normally.

(academy.nordicsemi.com/.../)
But I still want to control whether to send packets by pressing button 2 when turning off the TWT function.
As shown in step 3 of the diagram, when I turn off TWT, I press button 2 to enable the send package, but no data packet is sent. I tried to modify the code as shown in step 4 of the diagram, but couldn't. Could you please provide some help and guidance? Thank you very much!

Parents
  • Hi,

    This is due to how the exercise is implemented in such a way that it only sends a message during the awake state when TWT is enabled:

    static void twt_mgmt_event_handler(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
    				   struct net_if *iface)
    {
    	switch (mgmt_event) {
    	/* STEP 3.2.1 - Upon a TWT event, call handle_wifi_twt_event() to handle the response */
    	case NET_EVENT_WIFI_TWT:
    		handle_wifi_twt_event(cb);
    		break;
    	/* STEP 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;
    	}
    }

    If you want to be able to send packets when TWT is not enabled, you can call the function to send the packet directly in the button_handler() or schedule that it should be called. This code snippet shows how to call it directly in the button_handler():

    static void button_handler(uint32_t button_state, uint32_t has_changed)
    {
    	uint32_t button = button_state & has_changed;
    
    	/* STEP 5.1 - Call wifi_set_twt() to enable or disable TWT when button 1 is pressed. */
    	if (button & DK_BTN1_MSK) {
    		wifi_set_twt();
    	}
    
    	/* STEP 5.2 - Enable or disable sending packets during TWT awake when button 2 is pressed.
    	 */
    	if (button & DK_BTN2_MSK) {
    		send_packet();
    	}
    }

    If you want to schedule it, you can take a look at the TWT example here: https://github.com/martelmy/NCS_examples/blob/main/wifi/twt_provisioning_demo. In this example, pressing button 2 will schedule a ping that will either be sent immediately when TWT is not enabled or during the next wakeup when TWT is enabled. You can use work queue to schedule the UDP packet in the DevAcademy exercise in a similar way.
    Please note that this example is not official and should be considered provided “as-is”.

    Best regards,
    Marte

  • Thank you, . Your guidance is detailed and easy to understand.

    I still have questions, as shown in the picture, TWT mode and DTIM mode. How is the peak in the image caused in TWT mode? 

  • Oh, i am so sorry  that i modify the twt interval 100ms.

    The nRF Connect SDK is v2.7.0.

    nordic TWT.pcapng

  • Hi,

    Why are you setting the TWT interval to 100 ms? With your current configurations, the device will only be asleep for 35 ms for each TWT service period since the wake interval is 65 ms and the TWT interval (wake + sleep) is 100 ms. This will result in a higher current consumption than when using legacy power save mode. Can you try increasing the TWT interval to see if you get the same behavior?

    Best regards,
    Marte

  • I want to compare the power consumption of TWT and legacy power save mode under the same conditions, as the legacy power save mode is 100 ms.

    As you said, the legacy power save mode is 1.78mA, and TWT is 40.95mA, as shown in picture 1 and picture 2.

    But when I send a package the legacy power save mode consumes more power than TWT.

    as shown in picture 3 and picture 4.  TWT average is 44.35mA, and the legacy power save mode is 58.31mA  in 100ms.

    So, why does the legacy power save mode consume more power than the TWT mode when sending a package?

    picture 1 (the legacy power mode ):

    picture 2 (TWT mode ):

    picture 3 (the legacy power mode with send a package):

    picture 4 (the TWT mode with send a package):

    The code is from lesson 6/wifi fund_1ess6uexer2, and put the button 2 to send a "hello"

    package. the high peak power in Figure 3 and Figure 4 is caused by the package. the code change is as:

     

    static void button_handler(uint32_t button_state, uint32_t has_changed)
    {
    	uint32_t button = button_state & has_changed;
    
    	/* STEP 5.1 - Call wifi_set_twt() to enable or disable TWT when button 1 is pressed. */
    	if (button & DK_BTN1_MSK) {
    		wifi_set_twt();
    	}
    
    	/* STEP 5.2 - Enable or disable sending packets during TWT awake when button 2 is pressed.
    	 */
    	if (button & DK_BTN2_MSK) {
    		send_packet();
    	}
    }

  • Hi,

    ChengQing Zhao said:
    So, why does the legacy power save mode consume more power than the TWT mode when sending a package?

    Because the radio is on for a longer period of time with legacy power save mode. As is explained in the Power save modes topic in DevAcademy, with legacy power save mode, the device sends a PS poll to the AP to inform the AP that it is ready to receive data. The STA will be awake until it receives a frame from the AP with the More Data subfield set to 0, indicating that the AP does not have any more buffered data to send to the STA. In your case, this interaction seems to take 100 ms.

    When it comes to TWT, you have configured the TWT interval to be 100 ms and the wake interval to be 65 ms. This means that for each TWT period, which is 100 ms, the device will be awake for 65 ms and asleep for 35 ms. It does not wait to receive a frame from the AP with the More Data subfield set to 0 before going to sleep, it goes to sleep after being awake for 65 ms, as is agreed upon with the AP. If more data is buffered on the AP, this will be sent during the next wake-up instead.

    The nominal current consumption for RX is 60 mA for 2.4GHz and 56 mA for 5GHz, so the extra 35 ms that the device is awake for with legacy power save will greatly impact the average current consumption.

    The best improvement of TWT is that the device can be asleep for a longer period of time than with legacy power save mode, so comparing the current consumption under the same conditions does not really show the advantages of using TWT. However, for a fairer comparison, I would recommend reducing the TWT wake interval when having such a short TWT interval, as 65 ms wake interval is rather long when the total TWT interval is 100 ms. You can change this by changing the TWT_WAKE_INTERVAL_MS or directly changing params.setup.twt_wake_interval in wifi_set_twt() instead.

    I also recommend checking out the Online Power Profiler for Wi-Fi. Here, you can set the power save mode and other configurations and easily see how legacy power save mode (DTIM) compares with TWT when using different settings.

    Best regards,
    Marte

  • Thank you, . The last question, as you subscribe that, in legacy power save mode, after sta receives the data, the sta will be awake to send "ps-poll", as shown in the figure, so it keeps awake, does not go to sleep, still there is no data, sta then go to sleep, right?

Reply Children
No Data
Related