diff --git a/components/wifiManager/CMakeLists.txt b/components/wifiManager/CMakeLists.txt index eb1c1ad..37fcbe2 100644 --- a/components/wifiManager/CMakeLists.txt +++ b/components/wifiManager/CMakeLists.txt @@ -1,4 +1,4 @@ idf_component_register(SRCS "wifiManager/wifiManager.cpp" INCLUDE_DIRS "wifiManager" - REQUIRES esp_wifi nvs_flash esp_event esp_netif lwip StateManager + REQUIRES esp_wifi nvs_flash esp_event esp_netif lwip StateManager ProjectConfig ) \ No newline at end of file diff --git a/components/wifiManager/wifiManager/wifiManager.cpp b/components/wifiManager/wifiManager/wifiManager.cpp index a51606b..cfd8a8f 100644 --- a/components/wifiManager/wifiManager/wifiManager.cpp +++ b/components/wifiManager/wifiManager/wifiManager.cpp @@ -1,7 +1,7 @@ #include "wifiManager.hpp" -void event_handler(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) +void WiFiManagerHelpers::event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { ESP_LOGI(WIFI_MAMANGER_TAG, "Trying to connect, got event: %d", (int)event_id); if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) @@ -10,7 +10,6 @@ void event_handler(void *arg, esp_event_base_t event_base, if (err != ESP_OK) { ESP_LOGI(WIFI_MAMANGER_TAG, "esp_wifi_connect() failed: %s", esp_err_to_name(err)); - // xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) @@ -37,6 +36,8 @@ void event_handler(void *arg, esp_event_base_t event_base, } } +WiFiManager::WiFiManager(ProjectConfig &deviceConfig) : deviceConfig(deviceConfig) {} + void WiFiManager::SetCredentials(const char *ssid, const char *password) { memcpy(_wifi_cfg.sta.ssid, ssid, std::min(strlen(ssid), sizeof(_wifi_cfg.sta.ssid))); @@ -44,43 +45,13 @@ void WiFiManager::SetCredentials(const char *ssid, const char *password) memcpy(_wifi_cfg.sta.password, password, std::min(strlen(password), sizeof(_wifi_cfg.sta.password))); } -void WiFiManager::Begin() +void WiFiManager::ConnectWithHardcodedCredentials() { - s_wifi_event_group = xEventGroupCreate(); - - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - esp_netif_create_default_wifi_sta(); - - wifi_init_config_t esp_wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&esp_wifi_init_config)); - - 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)); - - _wifi_cfg = {}; - _wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WEP; - _wifi_cfg.sta.pmf_cfg.capable = true; - _wifi_cfg.sta.pmf_cfg.required = false; - - // we try to connect with the hardcoded credentials first - // todo, add support for more networks and setting up an AP this->SetCredentials(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSOWRD); - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &_wifi_cfg)); wifiStateManager.setState(WiFiState_e::WiFiState_ReadyToConect); esp_wifi_start(); - ESP_LOGI(WIFI_MAMANGER_TAG, "wifi_init_sta finished."); wifiStateManager.setState(WiFiState_e::WiFiState_Connecting); EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, @@ -110,3 +81,118 @@ void WiFiManager::Begin() ESP_LOGE(WIFI_MAMANGER_TAG, "UNEXPECTED EVENT"); } } + +void WiFiManager::ConnectWithStoredCredentials() +{ + auto networks = this->deviceConfig.getWifiConfigs(); + for (auto network : networks) + { + xEventGroupClearBits(s_wifi_event_group, WIFI_FAIL_BIT); + this->SetCredentials(network.ssid.c_str(), network.password.c_str()); + + // we need to update the config after every credentials change + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &_wifi_cfg)); + + wifiStateManager.setState(WiFiState_e::WiFiState_ReadyToConect); + + esp_wifi_start(); + wifiStateManager.setState(WiFiState_e::WiFiState_Connecting); + + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, + pdFALSE, + pdFALSE, + portMAX_DELAY); + if (bits & WIFI_CONNECTED_BIT) + { + ESP_LOGI(WIFI_MAMANGER_TAG, "connected to ap SSID:%s password:%s", + _wifi_cfg.sta.ssid, _wifi_cfg.sta.password); + + wifiStateManager.setState(WiFiState_e::WiFiState_Connected); + return; + } + ESP_LOGE(WIFI_MAMANGER_TAG, "Failed to connect to SSID:%s, password:%s, trying next stored network", + _wifi_cfg.sta.ssid, _wifi_cfg.sta.password); + } + + wifiStateManager.setState(WiFiState_e::WiFiState_Error); + ESP_LOGE(WIFI_MAMANGER_TAG, "Failed to connect to all saved networks"); +} + +void WiFiManager::SetupAccessPoint() +{ + ESP_LOGI(WIFI_MAMANGER_TAG, "Connection to stored credentials failed, starting AP"); + + esp_netif_create_default_wifi_ap(); + wifi_init_config_t esp_wifi_ap_init_config = WIFI_INIT_CONFIG_DEFAULT(); + + ESP_ERROR_CHECK(esp_wifi_init(&esp_wifi_ap_init_config)); + + wifi_config_t ap_wifi_config = { + .ap = { + .ssid = CONFIG_AP_WIFI_SSID, + .password = CONFIG_AP_WIFI_PASSWORD, + .max_connection = 1, + + }, + }; + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_LOGI(WIFI_MAMANGER_TAG, "AP started."); + wifiStateManager.setState(WiFiState_e::WiFiState_ADHOC); +} + +void WiFiManager::Begin() +{ + s_wifi_event_group = xEventGroupCreate(); + + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + auto netif = esp_netif_create_default_wifi_sta(); + + wifi_init_config_t esp_wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&esp_wifi_init_config)); + + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, + ESP_EVENT_ANY_ID, + &WiFiManagerHelpers::event_handler, + NULL, + &instance_any_id)); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, + IP_EVENT_STA_GOT_IP, + &WiFiManagerHelpers::event_handler, + NULL, + &instance_got_ip)); + + _wifi_cfg = {}; + _wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WEP; + _wifi_cfg.sta.pmf_cfg.capable = true; + _wifi_cfg.sta.pmf_cfg.required = false; + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + + ESP_LOGI(WIFI_MAMANGER_TAG, "Beginning setup"); + bool hasHardcodedCredentials = strlen(CONFIG_WIFI_SSID) > 0; + if (hasHardcodedCredentials) + { + ESP_LOGI(WIFI_MAMANGER_TAG, "Detected hardcoded credentials, trying them out"); + this->ConnectWithHardcodedCredentials(); + } + + if (wifiStateManager.getCurrentState() != WiFiState_e::WiFiState_Connected || !hasHardcodedCredentials) + { + ESP_LOGI(WIFI_MAMANGER_TAG, "Hardcoded credentials failed or missing, trying stored credentials"); + xEventGroupClearBits(s_wifi_event_group, WIFI_FAIL_BIT); + this->ConnectWithStoredCredentials(); + } + + if (wifiStateManager.getCurrentState() != WiFiState_e::WiFiState_Connected) + { + ESP_LOGI(WIFI_MAMANGER_TAG, "Stored netoworks failed or hardcoded credentials missing, starting AP"); + xEventGroupClearBits(s_wifi_event_group, WIFI_FAIL_BIT); + esp_netif_destroy(netif); + this->SetupAccessPoint(); + } +} diff --git a/components/wifiManager/wifiManager/wifiManager.hpp b/components/wifiManager/wifiManager/wifiManager.hpp index 43a89ba..a1bbd58 100644 --- a/components/wifiManager/wifiManager/wifiManager.hpp +++ b/components/wifiManager/wifiManager/wifiManager.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "esp_event.h" #include "esp_wifi.h" @@ -19,10 +20,17 @@ static int s_retry_num = 0; static EventGroupHandle_t s_wifi_event_group; static const char *WIFI_MAMANGER_TAG = "[WIFI_MANAGER]"; +namespace WiFiManagerHelpers +{ + void event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data); +} + class WiFiManager { private: uint8_t channel; + ProjectConfig &deviceConfig; wifi_init_config_t _wifi_init_cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_config_t _wifi_cfg = {}; @@ -30,13 +38,14 @@ private: esp_event_handler_instance_t instance_got_ip; int8_t power; - bool _enable_adhoc; - // static void event_handler(void *arg, esp_event_base_t event_base, - // int32_t event_id, void *event_data); + void SetCredentials(const char *ssid, const char *password); + void ConnectWithHardcodedCredentials(); + void ConnectWithStoredCredentials(); + void SetupAccessPoint(); public: - void SetCredentials(const char *ssid, const char *password); + WiFiManager(ProjectConfig &deviceConfig); void Begin(); }; diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 87d3dc8..c062338 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -57,4 +57,12 @@ menu "OpenIris basic configuration" config WIFI_PASSOWRD string "WiFi password" default "" + + config AP_WIFI_SSID + string "AP WiFi network name" + default "EyeTrackVR" + + config AP_WIFI_PASSWORD + string "AP WiFi password" + default "12345678" endmenu diff --git a/main/openiris_main.cpp b/main/openiris_main.cpp index f9e8320..3b85574 100644 --- a/main/openiris_main.cpp +++ b/main/openiris_main.cpp @@ -33,7 +33,7 @@ WebSocketLogger webSocketLogger; // TODO add this option // ProjectConfig deviceConfig("openiris", MDNS_HOSTNAME); ProjectConfig deviceConfig("openiris", "openiristracker"); -WiFiManager wifiManager; +WiFiManager wifiManager(deviceConfig); MDNSManager mdnsManager(deviceConfig); CameraManager cameraHandler(deviceConfig); StreamServer streamServer(80); @@ -76,20 +76,21 @@ extern "C" void app_main(void) // porting plan: // port the wifi manager first. - worky!!! - // get it connect to the network and setup an AP with hardcoded creds first -- connects. AP will be next // port the logo - done // port preferences lib - DONE; prolly temporary // then port the config - done, needs todos done // State Management - done // then port the led manager as this will be fairly easy - done // then port the mdns stuff - done - // then port the camera manager - in progress + // then port the camera manager - done // then port the streaming stuff (web and uvc) - done - // then add ADHOC and support for more networks in wifi manager + // then add ADHOC and support for more networks in wifi manager - done // then port the async web server // then port the Elegant OTA stuff - // then port the serial manager + // then port the serial manager - for wifi and mdns provisioning setup? + // finish todos, overhaul stuff a bit + // maybe swich websocket logging to udp logging Logo::printASCII(); initNVSStorage(); diff --git a/sdkconfig b/sdkconfig index 85f8619..2f43a92 100644 --- a/sdkconfig +++ b/sdkconfig @@ -539,9 +539,11 @@ CONFIG_BLINK_LED_GPIO=y # CONFIG_BLINK_LED_STRIP is not set CONFIG_BLINK_GPIO=21 CONFIG_BLINK_PERIOD=1000 -CONFIG_WIRED_MODE=y -CONFIG_WIFI_SSID="UPC7878684" -CONFIG_WIFI_PASSOWRD="j3ttQPpfvhep" +# CONFIG_WIRED_MODE is not set +CONFIG_WIFI_SSID="" +CONFIG_WIFI_PASSOWRD="" +CONFIG_AP_WIFI_SSID="EyeTrackVR" +CONFIG_AP_WIFI_PASSWORD="12345678" # end of OpenIris basic configuration # diff --git a/sdkconfig.old b/sdkconfig.old index 7f75294..60db9d8 100644 --- a/sdkconfig.old +++ b/sdkconfig.old @@ -539,9 +539,11 @@ CONFIG_BLINK_LED_GPIO=y # CONFIG_BLINK_LED_STRIP is not set CONFIG_BLINK_GPIO=21 CONFIG_BLINK_PERIOD=1000 -CONFIG_WIRED_MODE=y +# CONFIG_WIRED_MODE is not set CONFIG_WIFI_SSID="UPC7878684" CONFIG_WIFI_PASSOWRD="j3ttQPpfvhep" +CONFIG_AP_WIFI_SSID="EyeTrackVR" +CONFIG_AP_WIFI_PASSWORD="12345678" # end of OpenIris basic configuration # @@ -2142,8 +2144,8 @@ CONFIG_TUSB_SERIAL_NUM="12345678" CONFIG_FORMAT_MJPEG_CAM1=y # CONFIG_FORMAT_H264_CAM1 is not set # CONFIG_FORMAT_UNCOMPR_CAM1 is not set -CONFIG_UVC_MODE_ISOC_CAM1=y -# CONFIG_UVC_MODE_BULK_CAM1 is not set +# CONFIG_UVC_MODE_ISOC_CAM1 is not set +CONFIG_UVC_MODE_BULK_CAM1=y CONFIG_FRAMESIZE_QVGA=y # CONFIG_FRAMESIZE_HVGA is not set # CONFIG_FRAMESIZE_VGA is not set @@ -2188,9 +2190,9 @@ CONFIG_UVC_MULTI_FRAME_FPS_3=60 # # UVC Task Config # -CONFIG_UVC_TINYUSB_TASK_PRIORITY=5 +CONFIG_UVC_TINYUSB_TASK_PRIORITY=4 CONFIG_UVC_TINYUSB_TASK_CORE=-1 -CONFIG_UVC_CAM1_TASK_PRIORITY=4 +CONFIG_UVC_CAM1_TASK_PRIORITY=3 CONFIG_UVC_CAM1_TASK_CORE=-1 # end of UVC Task Config # end of USB Device UVC