diff options
| Commit | 1b7010f0a1095591bbe59b29908a6524af239a32 (patch) | |
|---|---|---|
| Author | 2024-05-27 15:46:12 +0300 | |
| Committer | 2024-05-27 15:46:12 +0300 | |
| Files | 30b36e71ae5f84206b4c1aab3a282d053cf7232d | |
| uni-projects-1b7010f0a1095591bbe59b29908a6524af239a32.tar uni-projects-1b7010f0a1095591bbe59b29908a6524af239a32.tar.gz uni-projects-1b7010f0a1095591bbe59b29908a6524af239a32.zip | ||
| Parent | 1e958b6c995e1b8def17fa3f8ee508de8b99118e (diff) | |
[iot/goam] Static network login is done with nvs memory and now we first connect to static then to open
2 files changed, 47 insertions, 27 deletions
| -rw-r--r-- | Year 3/Internet of things/goam/main/webserver.c | 4 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/wifi.c | 70 |
diff --git a/Year 3/Internet of things/goam/main/webserver.c b/Year 3/Internet of things/goam/main/webserver.c index 4f4f94e..26a170f 100644 --- a/Year 3/Internet of things/goam/main/webserver.c +++ b/Year 3/Internet of things/goam/main/webserver.c @@ -90,6 +90,10 @@ static esp_err_t post_network_handler(httpd_req_t *req) { httpd_req_recv(req, buffer, req->content_len); buffer[req->content_len] = '\0'; // just in case + for (char* iter = buffer; *iter != '\0'; iter++) { + if (*iter == '+') *iter = ' '; + } + char* ssid_start = strstr(buffer, ssid_post_param) + sizeof(ssid_post_param); char* pass_start = strstr(buffer, pass_post_param) + sizeof(pass_post_param); char* separator = strchr(buffer, '&'); diff --git a/Year 3/Internet of things/goam/main/wifi.c b/Year 3/Internet of things/goam/main/wifi.c index c40c512..7efa8fe 100644 --- a/Year 3/Internet of things/goam/main/wifi.c +++ b/Year 3/Internet of things/goam/main/wifi.c @@ -21,6 +21,16 @@ 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 */ @@ -38,6 +48,8 @@ static void netif_init() { */ 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 }; @@ -73,29 +85,37 @@ static void scan_and_connect_task() { ESP_LOGI(TAG, "Found %d WiFi networks", ap_count); - int stored_ind = -2; + 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, ESP_WIFI_SSID) == 0) { - stored_ind = i; + 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 >= -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); - stored_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 (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); + + 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 (stored_ind == -2) { + if (open_ind == -1) { 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); @@ -138,7 +158,9 @@ static void sta_disconnect_handler(void* arg, esp_event_base_t event_base, int32 vTaskDelay(200 / portTICK_PERIOD_MS); } else { - ESP_LOGW(TAG, "Failed to connect to SSID:%s, password:%s, rescanning connections", ESP_WIFI_SSID, ESP_WIFI_PASS); + 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); } } @@ -147,12 +169,18 @@ static void ip_got_handler(void* arg, esp_event_base_t event_base, int32_t event connect_tries = 0; ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "Connected to SSID:%s password:%s, got ip:" IPSTR, ESP_WIFI_SSID, ESP_WIFI_PASS, IP2STR(&event->ip_info.ip)); + 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, IP_LINE); - display_printon(ESP_WIFI_SSID, 3); + display_printon((char*)config.sta.ssid, 3); } static void wifi_init_events() { @@ -162,16 +190,6 @@ static void wifi_init_events() { } /* - * WiFi settings - */ - -#define NVS_SSID_KEY "ssid" -static char* nvs_ssid = NULL; - -#define NVS_PASS_KEY "pass" -static char* nvs_pass = NULL; - -/* * Public interface */ @@ -203,8 +221,6 @@ void wifi_init() { ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); wifi_load_network(); - if (nvs_ssid != NULL && nvs_pass != NULL) - printf("%s %s\n", nvs_ssid, nvs_pass); } void wifi_launch() { |
