diff --git a/main/Settings.cpp b/main/Settings.cpp index 2ed1629..8be8539 100644 --- a/main/Settings.cpp +++ b/main/Settings.cpp @@ -86,9 +86,9 @@ void Settings::setDefaults() m_data.led.brightness = 25; // 25 % strcpy(m_data.wifi.entry[0].ssid, "gumball"); - strcpy(m_data.wifi.entry[0].pwd, "mikelememb"); + strcpy(m_data.wifi.entry[0].pwd, "mikelemembee"); strcpy(m_data.wifi.entry[1].ssid, "miro"); - strcpy(m_data.wifi.entry[1].pwd, "mikelemembe"); + strcpy(m_data.wifi.entry[1].pwd, "mikelemembee"); m_data.wifi.num = 2; m_data.wifi.selected = 0xff; diff --git a/main/Wifi.cpp b/main/Wifi.cpp index feb68ae..2980883 100644 --- a/main/Wifi.cpp +++ b/main/Wifi.cpp @@ -8,7 +8,7 @@ #include "utilities.h" static const char *TAG = "Wifi"; -#define IGNORE_SSID_MINS 10 +#define IGNORE_SSID_MINS 20 using namespace std::placeholders; @@ -18,29 +18,30 @@ Wifi::Wifi() esp_log_level_set("wifi", ESP_LOG_WARN); esp_log_level_set("wifi_init", ESP_LOG_INFO); - memset(m_ignored, 0, SETTINGS_NUM_WIFI_ENTRIES); + // memset(m_ignored, 0, SETTINGS_NUM_WIFI_ENTRIES); WiFi.onEvent(std::bind(&Wifi::wifi_event, this, _1, _2)); - esp_timer_create_args_t timer; - timer.arg = this; - timer.callback = &timerCallback; - timer.dispatch_method = ESP_TIMER_TASK; - timer.skip_unhandled_events = true; - timer.name = "wifi countdown timer"; + // esp_timer_create_args_t timer; + // timer.arg = this; + // timer.callback = &timerCallback; + // timer.dispatch_method = ESP_TIMER_TASK; + // timer.skip_unhandled_events = true; + // timer.name = "wifi countdown timer"; - ESP_ERROR_CHECK(esp_timer_create(&timer, &m_timer)); - ESP_ERROR_CHECK(esp_timer_start_periodic(m_timer, 60000000)); // 1 min + // ESP_ERROR_CHECK(esp_timer_create(&timer, &m_timer)); + // ESP_ERROR_CHECK(esp_timer_start_periodic(m_timer, 60000000)); // 1 min + // m_task = TASK_FACTORY.createTask(std::bind(&Wifi::wifiTask, this), "wifi task", 2048, 5, 0); } -void Wifi::timerCallback(void* arg) -{ - for(int n = 0; n < SETTINGS_NUM_WIFI_ENTRIES; n++) - { - if(((Wifi*)arg)->m_ignored[n] > 0) - ((Wifi*)arg)->m_ignored[n]--; - } -} +// void Wifi::timerCallback(void* arg) +// { +// for(int n = 0; n < SETTINGS_NUM_WIFI_ENTRIES; n++) +// { +// if(((Wifi*)arg)->m_ignored[n] > 0) +// ((Wifi*)arg)->m_ignored[n]--; +// } +// } void Wifi::wifi_event(arduino_event_id_t event, arduino_event_info_t info) { @@ -52,6 +53,35 @@ void Wifi::wifi_event(arduino_event_id_t event, arduino_event_info_t info) } } +// void Wifi::wifiTask() +// { +// while(true) +// { +// delay(1000); +// ESP_LOGI(TAG, "in wifi task!"); +// } +// } + + +/// @brief Connect to provisioned wifi +/// @param index index of wifi in settings +/// @return ok/not connected +uint32_t Wifi::connectTo(int index) +{ + ESP_LOGI(TAG, "Connecting to %s...", SETTINGS.wifi.entry[index].ssid); + WiFi.begin(SETTINGS.wifi.entry[index].ssid, SETTINGS.wifi.entry[index].pwd); + int status = WiFi.waitForConnectResult(10000); + + if(status == WL_CONNECTED) + { + ESP_LOGI(TAG, "Connected"); + return WH_OK; + } + WiFi.disconnect(); + ESP_LOGW(TAG, "Failed to connect"); + return WH_ERR_WIFI_NOT_CONNECTED; +} + uint32_t Wifi::connect() { if(SETTINGS.wifi.num == 0) @@ -63,34 +93,71 @@ uint32_t Wifi::connect() if(WiFi.isConnected()) return WH_OK; - int ssid_ix = SETTINGS.wifi.selected; - - if((SETTINGS.wifi.always_scan_before_connect && SETTINGS.wifi.num > 1) || SETTINGS.wifi.selected >= SETTINGS_NUM_WIFI_ENTRIES) - ssid_ix = scan(); - - if(ssid_ix < 0) + if(SETTINGS.wifi.num == 1) { -// ESP_LOGE(TAG, "") - } + // only one wifi set up + //ssid_ix = 0; + SETTINGS.wifi.selected = 0; + for(int n = 0; n < 3; n++) + { + if(connectTo(SETTINGS.wifi.selected) == WH_OK) + return WH_OK; - ESP_LOGI(TAG, "Connecting to %s...", SETTINGS.wifi.entry[ssid_ix].ssid); - WiFi.begin(SETTINGS.wifi.entry[ssid_ix].ssid, SETTINGS.wifi.entry[ssid_ix].pwd); - int status = WiFi.waitForConnectResult(15000); + WiFi.disconnect(); + } - if(status == WL_CONNECTED) - { - ESP_LOGI(TAG, "Connected"); - return WH_OK; + ESP_LOGW(TAG, "Cannot connect, need provisioning"); + return WH_ERR_WIFI_NOT_PROVISIONED; } - WiFi.disconnect(); - m_ignored[ssid_ix] = IGNORE_SSID_MINS; - SETTINGS.wifi.selected = 0xff; + + memset(m_ignored, 0, SETTINGS_NUM_WIFI_ENTRIES); + int ssid_ix = SETTINGS.wifi.selected; - ESP_LOGE(TAG, "connect %d", status); - return WH_ERR_WIFI_NOT_CONNECTED; + if(SETTINGS.wifi.always_scan_before_connect || SETTINGS.wifi.selected >= SETTINGS_NUM_WIFI_ENTRIES) + ssid_ix = scan(); + + if(ssid_ix < 0) + return WH_ERR_WIFI_NOT_PROVISIONED; + + if(connectTo(ssid_ix) == WH_OK) + return WH_OK; + + m_ignored[ssid_ix] = IGNORE_SSID_MINS; + + // try to connect to highest rssi + do + { + ssid_ix = scan(); + if(ssid_ix >= 0) + { + if(connectTo(ssid_ix) == WH_OK) + return WH_OK; + else + m_ignored[ssid_ix] = IGNORE_SSID_MINS; + } + else + break; + + } while(true); + + // tried all provisioned ssid's, try round robin few times + + for(int m = 0; m < 3; m++) + { + for(int n = 0; n < SETTINGS.wifi.num; n++) + { + if(connectTo(n) == WH_OK) + return WH_OK; + } + } + + ESP_LOGW(TAG, "Cannot connect, need provisioning"); + return WH_ERR_WIFI_NOT_PROVISIONED; } +/// @brief Scans all visible ssid's and picks known ssid with highest rssi that is not ignored. +/// @return wifi index in Settings int Wifi::scan() { ESP_LOGI(TAG, "Scanning wifi networks..."); @@ -118,7 +185,11 @@ int Wifi::scan() } } } - ESP_LOGI(TAG, "Chosen network: '%s' based on RSSI", SETTINGS.wifi.entry[selected_index].ssid); + + if(selected_index >= 0) + ESP_LOGI(TAG, "Chosen network: '%s' based on RSSI", SETTINGS.wifi.entry[selected_index].ssid); + else + ESP_LOGI(TAG, "No suitable networks found at this time"); return selected_index; } \ No newline at end of file diff --git a/main/Wifi.h b/main/Wifi.h index 15da16b..ba3e9a6 100644 --- a/main/Wifi.h +++ b/main/Wifi.h @@ -6,14 +6,18 @@ #include #include #include "Settings.h" - +#include "TaskFactory.h" class Wifi { protected: uint8_t m_ignored[SETTINGS_NUM_WIFI_ENTRIES]; - esp_timer_handle_t m_timer; - static void timerCallback(void* arg); + // esp_timer_handle_t m_timer; + // static void timerCallback(void* arg); + // TaskHandle_t m_task = nullptr; + // void wifiTask(); + + uint32_t connectTo(int index); public: Wifi();