Hello, I want to connect to a server through Wifi periodicaly, every n time I do the following steps:
1) Bring the interface up with `net_if_up(iface)`
2) Ask the wifi for a connection with `net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &connection_parameters, sizeof(struct wifi_connect_req_params))`
3) Wait for the wifi to be connected through some callback
4) Interact with the server, to test my problem I removed this part
5) Ask the wifi for a deconnection with `net_mgmt(NET_REQUEST_WIFI_DISCONNECT, iface, NULL, 0)`
6) Wait for the wifi to be disconnected through some callback
7) Bring the interface down with `net_if_down(iface)`
My problem is the following: The first time I try to connect myself to the WiFi, the card never fails to succeed (as long as the SSID and the password of the WiFi are correct), but the second time I try, it almost always fails to connect to the WiFi with a timeout error. After some retries and by changing the time elapsed between two tests, it manages to reconnect, but I don't manage to find the correlation of why I have those timeout errors. I also tried to not change the status of the interface, but it didn't seem to fix it.
Here are some part of my code to help you understand more how I handle my connection:
- How I bring the interface up and down:
int8_t hc_wifi_start(){
struct net_if *iface = net_if_get_default();
if (net_if_up(iface)){
printk("<Wrn> Couldn't wake up WiFi interface\n");
return -1;
}
k_msleep(100);
hc_status.is_wifi_on = 1; // this is a variable we use to keep track of the iface status
return 0;
}
int8_t hc_wifi_stop(){
struct net_if *iface = net_if_get_default();
if (net_if_down(iface)){
printk("<Wrn> Couldn't stop WiFi interface\n");
return -1;
}
k_msleep(100);
hc_status.is_wifi_on = 0; // this is a variable we use to keep track of the iface status
return 0;
}
- How I setup the Wifi:
static void hc_wifi_connect_events_handler(struct net_mgmt_event_callback *callback, uint32_t mgmt_event, struct net_if *interface)
{
switch (mgmt_event) {
case NET_EVENT_WIFI_CONNECT_RESULT:
hc_wifi_connect_handler(callback);
break;
case NET_EVENT_WIFI_DISCONNECT_RESULT:
hc_wifi_disconnect_handler(callback);
break;
case NET_EVENT_IPV4_DHCP_BOUND:
hc_status.is_dhcp_bound = 1;
break;
default:
break;
}
}
void hc_init_wifi_connect(void)
{
net_mgmt_init_event_callback(&wifi_connect_events_callback, hc_wifi_connect_events_handler, NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT);
net_mgmt_init_event_callback(&ipv4_connect_events_callback, hc_wifi_connect_events_handler, NET_EVENT_IPV4_DHCP_BOUND);
net_mgmt_add_event_callback(&wifi_connect_events_callback);
net_mgmt_add_event_callback(&ipv4_connect_events_callback);
}
- How I connect myself to the Wifi:
static void hc_wifi_connect_handler(struct net_mgmt_event_callback *callback)
{
const struct wifi_status *status = (const struct wifi_status *)callback->info;
if (status->status) {
printk("<Err> WiFi connection failed (%d)\n", status->status);
hc_status.is_wifi_connected = 0;
} else {
hc_status.is_wifi_connected = 1;
net_dhcpv4_start(net_if_get_default());
}
}
static void hc_wifi_configuration_reader(struct wifi_connect_req_params *parameters)
{
parameters->timeout = HC_WIFI_CONNECTION_TIMEOUT_S;
parameters->ssid = hc_config.wifi_ssid; // a variable we use to store the SSID
parameters->ssid_length = hc_config.wifi_ssid_len;
parameters->security = hc_config.wifi_security == 0 ? 1 : hc_config.wifi_security; // a variable we use to store the network security
parameters->psk = hc_config.wifi_psk; // a variable we use to store the password
parameters->psk_length = hc_config.wifi_psk_len;
parameters->channel = WIFI_CHANNEL_ANY;
parameters->mfp = WIFI_MFP_OPTIONAL;
}
int8_t hc_wifi_connect(void)
{
struct net_if *interface = net_if_get_default();
static struct wifi_connect_req_params connection_parameters;
printk("<Inf> Connection requested\n");
hc_wifi_configuration_reader(&connection_parameters);
if (net_mgmt(NET_REQUEST_WIFI_CONNECT, interface, &connection_parameters, sizeof(struct wifi_connect_req_params))) {
printk("<Err> Connection request failed\n");
return -ENOEXEC;
}
return 0;
}
- How I disconnect myself from the Wifi:
static void hc_wifi_disconnect_handler(struct net_mgmt_event_callback *callback)
{
const struct wifi_status *status = (const struct wifi_status *) callback->info;
if (status->status) {
printk("<Err> WiFi disconnection failed (%d)\n", status->status);
} else {
printk("<Inf> WiFi disconnected\n");
hc_status.is_wifi_connected = 0;
hc_status.is_dhcp_bound = 0;
}
if (hc_wifi_task_id == HC_WIFI_TASK_DISCONNECT)
k_thread_resume(wifi_thread);
}
int8_t hc_wifi_disconnect(void)
{
struct net_if *interface = net_if_get_default();
printk("<Inf> Disconnection requested\n");
if (net_mgmt(NET_REQUEST_WIFI_DISCONNECT, interface, NULL, 0)) {
printk("<Err> Disconnection request failed\n");
return -ENOEXEC;
}
return 0;
}I hope the information I provided about my connection/reconnection timeout issue is clear. Feel free to ask if you need more details. Have a great day!