Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitb97a64e8dff8d6b90a9171bb968e0807b3a35c46 (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-05-23 20:43:52 +0300
Committer
 Syndamia <kamen@syndamia.com>
2024-05-23 20:43:52 +0300
Filesbc54df1a54299cb7caad47fa26bce17236bdbc94
uni-projects-b97a64e8dff8d6b90a9171bb968e0807b3a35c46.tar
uni-projects-b97a64e8dff8d6b90a9171bb968e0807b3a35c46.tar.gz
uni-projects-b97a64e8dff8d6b90a9171bb968e0807b3a35c46.zip
Parent60c4f949d1913dbed3bebe8912d4eeb9838237ff (diff)
[iot/goam] Implemented open network wifi scanning
1 files changed, 60 insertions, 58 deletions
-rw-r--r--Year 3/Internet of things/goam/main/wifi.c118
diff --git a/Year 3/Internet of things/goam/main/wifi.c b/Year 3/Internet of things/goam/main/wifi.c
index 3b58f82..09b62ea 100644
--- a/Year 3/Internet of things/goam/main/wifi.c
+++ b/Year 3/Internet of things/goam/main/wifi.c
@@ -1,3 +1,4 @@
+#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
@@ -28,79 +29,77 @@ static void netif_init() {
}
/*
- * WiFi events
+ * WiFi connectivity
*/
-static int connect_tries = 0;
+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));
-static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
-{
- if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
- esp_wifi_connect();
- return;
- }
+ wifi_config_t wifi_config = {
+ .sta.threshold.authmode = auth
+ };
- if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
- if (connect_tries < ESP_MAXIMUM_RETRY) {
- esp_wifi_connect();
- connect_tries++;
- ESP_LOGW(TAG, "Retrying to connect... Try %d/%d", connect_tries, ESP_MAXIMUM_RETRY);
- }
- else {
- ESP_LOGE(TAG, "Failed to connect to SSID:%s, password:%s", ESP_WIFI_SSID, ESP_WIFI_PASS);
- }
- return;
+ strcpy((char*)wifi_config.sta.ssid, ssid);
+ if (pass != NULL) {
+ strcpy((char*)wifi_config.sta.password, pass);
}
- if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
- connect_tries = 0;
+ ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
+ esp_wifi_connect();
+}
+
+static unsigned short MAX_AP = 16;
+
+static bool connect_to_open() {
+ ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true));
- 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_ap_record_t ap_info[MAX_AP];
+ unsigned short ap_count = 0;
+ ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
+ ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&MAX_AP, ap_info));
- char ip[16];
- sprintf(ip, IPSTR, IP2STR(&event->ip_info.ip));
- display_printon(ip, 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) {
+ wifi_connect_station((const char*)ap_info[i].ssid, WIFI_AUTH_OPEN, NULL);
+ return true;
+ }
}
+ return false;
}
+/*
+ * WiFi events
+ */
-static esp_event_handler_instance_t instance_any_id;
-static esp_event_handler_instance_t instance_got_ip;
+static int connect_tries = 0;
-static void wifi_init_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));
+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();
+ connect_tries++;
+ ESP_LOGW(TAG, "Retrying to connect... Try %d/%d", connect_tries, ESP_MAXIMUM_RETRY);
+ }
+ 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();
+ }
}
-/*
- * Config
- */
+static void ip_got_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
+ connect_tries = 0;
-static void wifi_init_static_station() {
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
+ 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 wifi_config = {
- .sta = {
- .ssid = ESP_WIFI_SSID,
- .password = ESP_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 = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
- },
- };
- ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
+ char ip[16];
+ sprintf(ip, IPSTR, IP2STR(&event->ip_info.ip));
+ display_printon(ip, 2);
+}
+
+static void wifi_init_events() {
+ 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));
}
/*
@@ -116,10 +115,13 @@ void wifi_init() {
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_init_events();
-
- wifi_init_static_station();
}
void wifi_start() {
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);
+ }
}