diff options
| Commit | c248e73e01a5a8d95f26b96ff429964e5f9be696 (patch) | |
|---|---|---|
| Author | 2024-05-24 13:32:58 +0300 | |
| Committer | 2024-05-24 13:32:58 +0300 | |
| Files | cba4c3cc226633107a87d9ca9a97e1852ff24503 | |
| uni-projects-c248e73e01a5a8d95f26b96ff429964e5f9be696.tar uni-projects-c248e73e01a5a8d95f26b96ff429964e5f9be696.tar.gz uni-projects-c248e73e01a5a8d95f26b96ff429964e5f9be696.zip | ||
| Parent | 2beeab8a7a5992b2276da5197169dc68e836f6c9 (diff) | |
(iot/goam/wifi) Reworked, significantly more robust connection "algorithm"
- Connecting is done with two tasks, one taking up close to no resources, while the second does the connecting
- Added a more robust mechanism for reconnecting and retrying
- Fixed crashing related to task-init of ptc06
2 files changed, 67 insertions, 16 deletions
| -rw-r--r-- | Year 3/Internet of things/goam/main/wifi.c | 81 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/wifi.h | 2 |
diff --git a/Year 3/Internet of things/goam/main/wifi.c b/Year 3/Internet of things/goam/main/wifi.c index 09b62ea..fc7b141 100644 --- a/Year 3/Internet of things/goam/main/wifi.c +++ b/Year 3/Internet of things/goam/main/wifi.c @@ -24,7 +24,8 @@ static const char *TAG = "wifi_station"; static void netif_init() { ESP_ERROR_CHECK(esp_netif_init()); - esp_netif_create_default_wifi_sta(); + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); ESP_LOGI(TAG, "NETIF initialized"); } @@ -33,8 +34,6 @@ static void netif_init() { */ static void wifi_connect_station(const char* ssid, wifi_auth_mode_t auth, const char* pass) { - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - wifi_config_t wifi_config = { .sta.threshold.authmode = auth }; @@ -45,27 +44,74 @@ static void wifi_connect_station(const char* ssid, wifi_auth_mode_t auth, const } ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); - esp_wifi_connect(); + ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_connect()); } static unsigned short MAX_AP = 16; -static bool connect_to_open() { +#define TO_CONNECT_BIT 0b01 +#define CONNECTED_BIT 0b10 + +static TaskHandle_t launcher_handle = NULL; + +static void scan_and_connect_task() { + display_printon("---.---.---.---", 2); 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 stored_ind = -2; for (int i = 0; i < ap_count; i++) { ESP_LOGD(TAG, "Found network \"%s\"", (const char*)ap_info[i].ssid); - if (ap_info[i].authmode == WIFI_AUTH_OPEN) { + + if (strcmp((const char*)ap_info[i].ssid, ESP_WIFI_SSID) == 0) { + stored_ind = i; + } + + if (ap_info[i].authmode == WIFI_AUTH_OPEN && ap_info[i].rssi >= -70) { + ESP_LOGI(TAG, "Connecting to \"%s\"", (const char*)ap_info[i].ssid); wifi_connect_station((const char*)ap_info[i].ssid, WIFI_AUTH_OPEN, NULL); - return true; + stored_ind = -2; + break; } } - return false; + + if (stored_ind > -1) { + ESP_LOGI(TAG, "Couldn't find a good open network, connecting to configured net"); + wifi_connect_station(ESP_WIFI_SSID, ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD, ESP_WIFI_PASS); + } + + xTaskNotify(launcher_handle, CONNECTED_BIT, eSetBits); + if (stored_ind == -2) { + ESP_LOGW(TAG, "Couldn't find a good open network or the configured net, will retry in 2 seconds"); + vTaskDelay(2000 / portTICK_PERIOD_MS); + 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, 100 / portTICK_PERIOD_MS); + } while (!(notification & TO_CONNECT_BIT)); + + xTaskCreate(scan_and_connect_task, "scan_and_connect_task", 4096, NULL, 10, NULL); + + do { + xTaskNotifyWait(ULONG_MAX, 0, ¬ification, 100 / portTICK_PERIOD_MS); + } while (!(notification & CONNECTED_BIT)); + } } /* @@ -79,10 +125,11 @@ static void sta_disconnect_handler(void* arg, esp_event_base_t event_base, int32 esp_wifi_connect(); connect_tries++; ESP_LOGW(TAG, "Retrying to connect... Try %d/%d", connect_tries, ESP_MAXIMUM_RETRY); + vTaskDelay(100 / portTICK_PERIOD_MS); } else { - ESP_LOGW(TAG, "Failed to connect to SSID:%s, password:%s, will now try to connect to open...", ESP_WIFI_SSID, ESP_WIFI_PASS); - connect_to_open(); + ESP_LOGW(TAG, "Failed to connect to SSID:%s, password:%s, rescanning connections", ESP_WIFI_SSID, ESP_WIFI_PASS); + xTaskNotify(launcher_handle, TO_CONNECT_BIT, eSetBits); } } @@ -112,16 +159,20 @@ void wifi_init() { netif_init(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + // It seems that ptc06 also uses nvs, and since both are in different tasks during init, we get a crash, + // which I think is caused by both trying to access/modify nvs at the "same" time + // I couldn't figure out a good/proper mechanism for interlocking, since I don't entirely know when and why + // both access nvs, but this will do + cfg.nvs_enable = false; ESP_ERROR_CHECK(esp_wifi_init(&cfg)); wifi_init_events(); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); } -void wifi_start() { +void wifi_launch() { ESP_ERROR_CHECK(esp_wifi_start()); - if (!connect_to_open()) { - ESP_LOGI(TAG, "Couldn't find open network, connecting to configured net"); - wifi_connect_station(ESP_WIFI_SSID, ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD, ESP_WIFI_PASS); - } + xTaskCreate(launcher_scan_and_connect_task, "launcher_scan_and_connect_task", 768, NULL, 10, &launcher_handle); + xTaskNotify(launcher_handle, TO_CONNECT_BIT, eSetBits); } diff --git a/Year 3/Internet of things/goam/main/wifi.h b/Year 3/Internet of things/goam/main/wifi.h index 9c23266..f4e312a 100644 --- a/Year 3/Internet of things/goam/main/wifi.h +++ b/Year 3/Internet of things/goam/main/wifi.h @@ -1,4 +1,4 @@ #pragma once void wifi_init(); -void wifi_start(); +void wifi_launch(); |
