#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "sdkconfig.h" #include "wifi.h" #include "display.h" #include "nvs.h" #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK #define ESP_MAXIMUM_RETRY 3 static const char *TAG = "wifi_station"; /* * WiFi settings */ #define NVS_SSID_KEY "ssid" static char* nvs_ssid = NULL; #define NVS_PASS_KEY "pass" static char* nvs_pass = NULL; /* * NetIF */ // HAS to be ran after nvs_init static void netif_init() { ESP_ERROR_CHECK(esp_netif_init()); esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); assert(sta_netif); ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, CONFIG_NETWORK_HOSTNAME)); ESP_LOGI(TAG, "NETIF initialized"); } /* * WiFi connectivity */ static void wifi_connect_station(const char* ssid, wifi_auth_mode_t auth, const char* pass) { if (ssid == NULL) return; wifi_config_t wifi_config = { .sta.threshold.authmode = auth }; strcpy((char*)wifi_config.sta.ssid, ssid); if (pass != NULL) { strcpy((char*)wifi_config.sta.password, pass); } ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_connect()); } static unsigned short MAX_AP = 16; #define TO_CONNECT_BIT 0b01 #define CONNECTED_BIT 0b10 static TaskHandle_t launcher_handle = NULL; static void scan_and_connect_task() { display_printat(DCHAR_NO_WIFI, CONFIG_DISPLAY_ICONS_ROW, CONFIG_WIFI_COLUMN); display_printon("---.---.---.--- ", CONFIG_DISPLAY_IP_ROW); display_printon("-" LINE_CLEAR LINE_CLEAR, CONFIG_DISPLAY_SSID_ROW); ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true)); wifi_ap_record_t ap_info[MAX_AP]; unsigned short ap_count = 0; ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count)); if (ap_count > MAX_AP) { ap_count = MAX_AP; } ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&MAX_AP, ap_info)); ESP_LOGI(TAG, "Found %d WiFi networks", ap_count); int open_ind = -1, open_strength = -69; for (int i = 0; i < ap_count; i++) { ESP_LOGD(TAG, "Found network \"%s\"", (const char*)ap_info[i].ssid); if (strcmp((const char*)ap_info[i].ssid, nvs_ssid) == 0) { if (ap_info[i].rssi < -70) { ESP_LOGW(TAG, "Found static network, but it's signal is too weak!"); } else { ESP_LOGI(TAG, "Connecting to static network \"%s\"", nvs_ssid); wifi_connect_station(nvs_ssid, ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD, nvs_pass); open_ind = -2; break; } } if (ap_info[i].authmode == WIFI_AUTH_OPEN && ap_info[i].rssi > open_strength) { open_ind = i; open_strength = ap_info[i].rssi; } } if (open_ind > -1) { ESP_LOGW(TAG, "Couldn't find or connect to static network, connecting to an open network"); ESP_LOGI(TAG, "Connecting to \"%s\"", (const char*)ap_info[open_ind].ssid); wifi_connect_station((const char*)ap_info[open_ind].ssid, WIFI_AUTH_OPEN, NULL); } xTaskNotify(launcher_handle, CONNECTED_BIT, eSetBits); if (open_ind == -1) { ESP_LOGW(TAG, "Couldn't find a good open network or the configured net, will retry in 2 seconds"); vTaskDelay(pdMS_TO_TICKS(2000)); xTaskNotify(launcher_handle, TO_CONNECT_BIT, eSetBits); } vTaskDelete(NULL); } static void launcher_scan_and_connect_task(void*) { long unsigned int notification = 0; while (true) { do { xTaskNotifyWait(ULONG_MAX, 0, ¬ification, pdMS_TO_TICKS(100)); } while (!(notification & TO_CONNECT_BIT)); xTaskCreate(scan_and_connect_task, "scan_and_connect_task", 4096, NULL, 18, NULL); do { xTaskNotifyWait(ULONG_MAX, 0, ¬ification, pdMS_TO_TICKS(100)); } while (!(notification & CONNECTED_BIT)); } } /* * WiFi events */ static int connect_tries = 0; static void sta_connected_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { display_printat(DCHAR_WIFI, CONFIG_DISPLAY_ICONS_ROW, CONFIG_WIFI_COLUMN); } static void sta_disconnect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (connect_tries < ESP_MAXIMUM_RETRY) { esp_wifi_connect(); display_printat(DCHAR_WIFI_EXCLAM, CONFIG_DISPLAY_ICONS_ROW, CONFIG_WIFI_COLUMN); connect_tries++; ESP_LOGW(TAG, "Retrying to connect... Try %d/%d", connect_tries, ESP_MAXIMUM_RETRY); vTaskDelay(pdMS_TO_TICKS(200)); } else { wifi_config_t config; esp_wifi_get_config(WIFI_IF_STA, &config); ESP_LOGW(TAG, "Failed to reconnect with \"%s\", rescanning connections", (char*)config.sta.ssid); xTaskNotify(launcher_handle, TO_CONNECT_BIT, eSetBits); } } static void ip_got_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { connect_tries = 0; ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; wifi_config_t config; esp_wifi_get_config(WIFI_IF_STA, &config); ESP_LOGI(TAG, "Connected to \"%s\" with ip " IPSTR, (char*)config.sta.ssid, IP2STR(&event->ip_info.ip)); char ip[17]; ip[16] = '\0'; sprintf(ip, IPSTR, IP2STR(&event->ip_info.ip)); for (int i = strchr(ip, '\0') - ip; i < 16; i++) { ip[i] = ' '; } display_printon(ip, CONFIG_DISPLAY_IP_ROW); display_printon((char*)config.sta.ssid, CONFIG_DISPLAY_SSID_ROW); } static void wifi_init_events() { ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &sta_connected_handler, NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &sta_disconnect_handler, NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_got_handler, NULL, NULL)); } /* * Public interface */ void wifi_store_network(const char* valueSSID, const char* valuePass) { nvs_write(NVS_SSID_KEY, valueSSID); free(nvs_ssid); nvs_ssid = malloc((strlen(valueSSID) + 1) * sizeof(char)); strcpy(nvs_ssid, valueSSID); nvs_write(NVS_PASS_KEY, valuePass); free(nvs_pass); nvs_pass = malloc((strlen(valuePass) + 1) * sizeof(char)); strcpy(nvs_pass, valuePass); } void wifi_load_network() { nvs_read(NVS_SSID_KEY, &nvs_ssid); nvs_read(NVS_PASS_KEY, &nvs_pass); } void wifi_init() { ESP_ERROR_CHECK(esp_event_loop_create_default()); netif_init(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); wifi_init_events(); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); wifi_load_network(); } void wifi_launch() { ESP_ERROR_CHECK(esp_wifi_start()); xTaskCreate(launcher_scan_and_connect_task, "launcher_scan_and_connect_task", 768, NULL, 10, &launcher_handle); xTaskNotify(launcher_handle, TO_CONNECT_BIT, eSetBits); }