#include "wifi.h" #include #include #include #include #include #include "config.h" #include "logger.h" #define MAX_RETRY_ATTEMPTS 20 // static int s_ap_creds_num = 0; static int s_retry_num = 0; EventGroupHandle_t s_wifi_event_group; esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT) { switch (event_id) { case WIFI_EVENT_WIFI_READY: LOG_I("wifi: WiFi ready"); break; case WIFI_EVENT_SCAN_DONE: LOG_I("wifi: Finished scanning AP"); break; case WIFI_EVENT_STA_START: LOG_I("wifi: Station start"); esp_wifi_connect(); break; case WIFI_EVENT_STA_STOP: LOG_I("wifi: Station stop"); break; case WIFI_EVENT_STA_CONNECTED: LOG_I("wifi: Station connected to AP"); break; case WIFI_EVENT_STA_DISCONNECTED: LOG_W("wifi: Station disconnected from AP"); if (s_retry_num < MAX_RETRY_ATTEMPTS) { s_retry_num++; LOG_I("wifi: retry num %d", s_retry_num); esp_wifi_connect(); } else { xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } break; break; case WIFI_EVENT_STA_AUTHMODE_CHANGE: LOG_I("wifi: the auth mode of AP connected by device's station changed"); break; case WIFI_EVENT_STA_WPS_ER_SUCCESS: LOG_I("wifi: Station wps succeeds in enrollee mode"); break; case WIFI_EVENT_STA_WPS_ER_FAILED: LOG_E("wifi: Station wps fails in enrollee mode"); break; case WIFI_EVENT_STA_WPS_ER_TIMEOUT: LOG_E("wifi: Station wps timeout in enrollee mode"); break; case WIFI_EVENT_STA_WPS_ER_PIN: LOG_I("wifi: Station wps pin code in enrollee mode"); break; case WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP: LOG_I("wifi: Station wps overlap in enrollee mode"); break; case WIFI_EVENT_AP_START: LOG_I("wifi: Soft-AP start"); break; case WIFI_EVENT_AP_STOP: LOG_I("wifi: Soft-AP stop"); break; case WIFI_EVENT_AP_STACONNECTED: LOG_I("wifi: a station connected to Soft-AP"); break; case WIFI_EVENT_AP_STADISCONNECTED: LOG_W("wifi: a station disconnected from Soft-AP"); break; case WIFI_EVENT_AP_PROBEREQRECVED: LOG_I("wifi: Receive probe request packet in soft-AP interface"); break; case WIFI_EVENT_FTM_REPORT: LOG_I("wifi: Receive report of FTM procedure"); break; case WIFI_EVENT_STA_BSS_RSSI_LOW: LOG_I("wifi: AP's RSSI crossed configured threshold"); break; case WIFI_EVENT_ACTION_TX_STATUS: LOG_I("wifi: Status indication of Action Tx operation"); break; case WIFI_EVENT_ROC_DONE: LOG_I("wifi: Remain-on-Channel operation complete"); break; case WIFI_EVENT_STA_BEACON_TIMEOUT: LOG_E("wifi: Station beacon timeout"); break; case WIFI_EVENT_CONNECTIONLESS_MODULE_WAKE_INTERVAL_START: LOG_I("wifi: Connectionless module wake interval start"); break; case WIFI_EVENT_AP_WPS_RG_SUCCESS: LOG_I("wifi: Soft-AP wps succeeds in registrar mode"); break; case WIFI_EVENT_AP_WPS_RG_FAILED: LOG_E("wifi: Soft-AP wps fails in registrar mode"); break; case WIFI_EVENT_AP_WPS_RG_TIMEOUT: LOG_E("wifi: Soft-AP wps timeout in registrar mode"); break; case WIFI_EVENT_AP_WPS_RG_PIN: LOG_I("wifi: Soft-AP wps pin code in registrar mode"); break; case WIFI_EVENT_AP_WPS_RG_PBC_OVERLAP: LOG_I("wifi: Soft-AP wps overlap in registrar mode"); break; case WIFI_EVENT_ITWT_SETUP: LOG_I("wifi: iTWT setup"); break; case WIFI_EVENT_ITWT_TEARDOWN: LOG_I("wifi: iTWT teardown"); break; case WIFI_EVENT_ITWT_PROBE: LOG_I("wifi: iTWT probe"); break; case WIFI_EVENT_ITWT_SUSPEND: LOG_I("wifi: iTWT suspend"); break; case WIFI_EVENT_NAN_STARTED: LOG_I("wifi: NAN Discovery has started"); break; case WIFI_EVENT_NAN_STOPPED: LOG_I("wifi: NAN Discovery has stopped"); break; case WIFI_EVENT_NAN_SVC_MATCH: LOG_I("wifi: NAN Service Discovery match found"); break; case WIFI_EVENT_NAN_REPLIED: LOG_I("wifi: Replied to a NAN peer with Service Discovery match"); break; case WIFI_EVENT_NAN_RECEIVE: LOG_I("wifi: Received a Follow-up message"); break; case WIFI_EVENT_NDP_INDICATION: LOG_I("wifi: Received NDP Request from a NAN Peer"); break; case WIFI_EVENT_NDP_CONFIRM: LOG_I("wifi: NDP Confirm Indication"); break; case WIFI_EVENT_NDP_TERMINATED: LOG_I("wifi: NAN Datapath terminated indication"); break; case WIFI_EVENT_HOME_CHANNEL_CHANGE: LOG_I("wifi: WiFi home channel change,doesn't occur when scanning"); break; case WIFI_EVENT_MAX: LOG_I("wifi: Invalid WiFi event ID"); break; } } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; LOG_I("wifi: got ip: " IPSTR "", IP2STR(&event->ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } } void wifiInit() { // init network interface ESP_ERROR_CHECK(esp_netif_init()); // create event group s_wifi_event_group = xEventGroupCreate(); assert(s_wifi_event_group); ESP_ERROR_CHECK(esp_event_loop_create_default()); // init wifi as default station esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); assert(sta_netif); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // register events ESP_ERROR_CHECK(esp_event_handler_instance_register( WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id)); ESP_ERROR_CHECK(esp_event_handler_instance_register( IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip)); LOG_I("wifi: wifi_init_sta finished."); } void wifi_connect() { // set wifi connection config wifi_config_t wifi_config = { .sta = { .ssid = WIFI_SSID, .password = WIFI_PASS, /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8). * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards. */ .threshold.authmode = WIFI_AUTH, .sae_pwe_h2e = WPA3_SAE_PWE_HUNT_AND_PECK, .sae_h2e_identifier = "", }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); }