Hi,
I'm developing an application to send data via UDP. In order to minimize the power consumption of my application, my idea is to send data during the wake time of TWT.
I start by connecting to the AP. To do this, I based myself on the Wi-Fi: Station sample where I get the following output (Image 1) :
(Image 1 )
After this, I set up the TWT based on the Wi-Fi: TWT sample with the TWT_WAKE_INTERVAL and TWT_INTERVAL set to 20000us and 5000000us, respectively. I'm obtaining the following output (Image 2):
( Image 2 )
I was expecting that I would get an event when the wake up time and sleep time started. However, from what we can see in Image 2, it appears that I'm not obtaining any NET_EVENT_WIFI_TWT_SLEEP_STATE event. For this reason, I thought that my TWT application was not working (since there were no active or sleeping events), but after a quick test with the Joulescope JS110, I was able to verify that these events were being generated as we can see in the following image (Image 3) and the TWT_WAKE_INTERVAL and TWT_INTERVAL are correct (Image 4):
( Image 3 )
( Image 4 )
At this moment, my wifi_mgmt_event_handler looks like:
static void wifi_mgmt_event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event, struct net_if *iface)
{
switch (mgmt_event)
{
case NET_EVENT_WIFI_CONNECT_RESULT:
LOG_INF(" ========================= NET_EVENT_WIFI_CONNECT_RESULT event ========================= " );
handle_wifi_connect_result(cb);
break;
case NET_EVENT_WIFI_DISCONNECT_RESULT:
LOG_INF(" ========================= NET_EVENT_WIFI_DISCONNECT_RESULT ========================= ");
handle_wifi_disconnect_result(cb);
break;
case NET_EVENT_WIFI_TWT:
LOG_INF(" ========================= NET_EVENT_WIFI_TWT event ========================= ");
handle_wifi_twt_event(cb);
break;
case NET_EVENT_WIFI_TWT_SLEEP_STATE:
LOG_INF(" ========================= NET_EVENT_WIFI_TWT_SLEEP_STATE event ========================= ");
handle_wifi_twt_state_event(cb);
break;
default:
LOG_ERR("(WIFI) Unknown event: %d ", mgmt_event);
break;
}
}
My main function looks like:
int main(void)
{
int i, ret;
memset(&context, 0, sizeof(context));
net_mgmt_init_event_callback(&wifi_shell_mgmt_cb,
wifi_mgmt_event_handler,
WIFI_SHELL_MGMT_EVENTS);
net_mgmt_add_event_callback(&wifi_shell_mgmt_cb);
net_mgmt_init_event_callback(&net_shell_mgmt_cb,
net_mgmt_event_handler,
NET_EVENT_IPV4_DHCP_BOUND);
net_mgmt_add_event_callback(&net_shell_mgmt_cb);
LOG_INF("Starting %s with CPU frequency: %d MHz", CONFIG_BOARD, SystemCoreClock / MHZ(1));
k_sleep(K_SECONDS(1));
LOG_INF("Static IP address (overridable): %s/%s -> %s",
CONFIG_NET_CONFIG_MY_IPV4_ADDR,
CONFIG_NET_CONFIG_MY_IPV4_NETMASK,
CONFIG_NET_CONFIG_MY_IPV4_GW);
while (1)
{
wifi_connect();
for (i = 0; i < CONNECTION_TIMEOUT; i++)
{
k_sleep(K_MSEC(STATUS_POLLING_MS));
cmd_wifi_status();
if (context.connect_result)
{
break;
}
}
if (context.connected)
{
LOG_INF("============ Connected to wifi ============");
break;
}
else if (!context.connect_result)
{
LOG_ERR("Connection Timed Out");
}
k_sleep(K_MSEC(300));
}
k_sleep(K_SECONDS(2));
if (!twt_supported)
{
LOG_INF("AP is not TWT capable, exiting the sample\n");
return 1;
}
else
{
LOG_INF("AP is TWT capable, establishing TWT");
}
ret = setup_twt(true);
if (ret)
{
LOG_ERR("Failed to establish TWT flow: %d\n", ret);
return 1;
}
k_sleep(K_FOREVER);
return 0;
}
And my setup_twt function looks like:
static int setup_twt(bool enable)
{
struct net_if *iface = net_if_get_default();
struct wifi_twt_params params = { };
int ret;
params.operation = WIFI_TWT_SETUP;
params.negotiation_type = WIFI_TWT_INDIVIDUAL;
params.setup_cmd = WIFI_TWT_SETUP_CMD_REQUEST;
params.dialog_token = 1;
params.flow_id = 1;
params.setup.responder = 0;
params.setup.implicit = 1;
params.setup.trigger = IS_ENABLED(CONFIG_TWT_TRIGGER_ENABLE);
params.setup.announce = IS_ENABLED(CONFIG_TWT_ANNOUNCED_MODE);
params.setup.twt_wake_interval = CONFIG_TWT_WAKE_INTERVAL;
params.setup.twt_interval = CONFIG_TWT_INTERVAL;
ret = net_mgmt(NET_REQUEST_WIFI_TWT, iface, ¶ms, sizeof(params));
if (ret)
{
LOG_INF("TWT setup failed: %d", ret);
return ret;
}
else
{
LOG_INF("TWT setup requested");
}
return 0;
}
What I want to know is:
- What am I doing wrong in the wake up and sleep events detection (in order to send data via UDP during the wake time)?
- How can I fix the problem?
P.S.:
Sometimes looks like the TWT is not set (Image 5):

Thanks a lot!





