From 4692f103404b3d95b6fbec4596e94df8bea61641 Mon Sep 17 00:00:00 2001 From: Miroslav Pivovarsky Date: Fri, 10 May 2024 23:35:08 +0200 Subject: [PATCH] Added the option to disable the service AP after MCU startup --- ESP32_PrusaConnectCam/WebPage.h | 52 +++++++++++++++++++++++++++++ ESP32_PrusaConnectCam/cfg.cpp | 29 ++++++++++++++++ ESP32_PrusaConnectCam/cfg.h | 2 ++ ESP32_PrusaConnectCam/mcu_cfg.h | 8 +++-- ESP32_PrusaConnectCam/server.cpp | 7 ++++ ESP32_PrusaConnectCam/system.cpp | 7 ++-- ESP32_PrusaConnectCam/wifi_mngt.cpp | 45 +++++++++++++++++++++---- ESP32_PrusaConnectCam/wifi_mngt.h | 3 ++ webpage/page_wifi.html | 15 +++++++++ webpage/scripts.js | 14 ++++++++ webpage/styles.css | 23 +++++++++++++ 11 files changed, 192 insertions(+), 13 deletions(-) diff --git a/ESP32_PrusaConnectCam/WebPage.h b/ESP32_PrusaConnectCam/WebPage.h index ab12b85..79291fa 100644 --- a/ESP32_PrusaConnectCam/WebPage.h +++ b/ESP32_PrusaConnectCam/WebPage.h @@ -193,13 +193,28 @@ const char page_wifi_html[] PROGMEM = R"rawliteral( + + +
+
+ +
+
+
+ + + +
Advanced Wi-Fi settings
Enable service AP
+
+ )rawliteral"; @@ -629,6 +644,9 @@ cfg_bar li a:hover { table-layout: fixed; text-align: left; } +#wificfg_tb { + margin-left: 30%; +} /* wifi_ntw table */ #wifi_ntw { font: normal normal normal 12px/5px sans-serif; @@ -840,6 +858,26 @@ cfg_bar li a:hover { background-color: #FA6831; color: white; } + +/* advanced wifi cfg */ +.content_wifi { + display: none; +} + +.btn_collapsible_wifi { + width: 300px; + height: 24px; + text-align: center; + font: normal normal bold 14px/5px sans-serif; + color: #000000; + background-color: white; + border-radius: 5px; + border: 1px solid #343a40; +} +.btn_collapsible_wifi:hover { + background-color: #FA6831; + color: white; +} )rawliteral"; /* ------------------------------------------------------------------------------------------------------------ */ @@ -914,6 +952,8 @@ function get_data(val) { } if (val == "wifi") { + document.getElementById('serviceapid').checked = obj.serviceap; + $("#status_serviceap").text((obj.serviceap == "true") ? "On" : "Off"); $("#ssid").text(obj.ssid); $("#rssi").text(obj.rssi); $("#rssi_percentage").text(obj.rssi_percentage); @@ -1292,6 +1332,18 @@ function setupCollapsibleButtons() { } }); } + +function setupCollapsibleButtonsWiFi() { + $(".btn_collapsible_wifi").click(function(){ + $(this).toggleClass("active"); + var content_wifi = $(this).parent().next(); + if (content_wifi.css("display") === "block") { + content_wifi.css("display", "none"); + } else { + content_wifi.css("display", "block"); + } + }); +} )rawliteral"; /* ------------------------------------------------------------------------------------------------------------ */ diff --git a/ESP32_PrusaConnectCam/cfg.cpp b/ESP32_PrusaConnectCam/cfg.cpp index e63d6db..918c7d1 100644 --- a/ESP32_PrusaConnectCam/cfg.cpp +++ b/ESP32_PrusaConnectCam/cfg.cpp @@ -147,6 +147,7 @@ void Configuration::DefaultCfg() { SaveWifiCfgFlag(CFG_WIFI_SETTINGS_NOT_SAVED); SaveWifiPassword(""); SaveWifiSsid(""); + SaveEnableServiceAp(FACTORY_CFG_ENABLE_SERVICE_AP); SaveBasicAuthUsername(FACTORY_CFG_WEB_AUTH_USERNAME); SaveBasicAuthPassword(FACTORY_CFG_WEB_AUTH_PASSWORD); SaveBasicAuthFlag(FACTORY_CFG_WEB_AUTH_ENABLE); @@ -560,6 +561,17 @@ void Configuration::SaveWifiCfgFlag(uint8_t i_data) { Log->AddEvent(LogLevel_Verbose, "Save active wifi cfg flag: " + String(i_data)); SaveUint8(EEPROM_ADDR_WIFI_ACTIVE_FLAG_START, i_data); } + +/** + @info save enable/disable service AP to EEPROM + @param bool - status + @return none +*/ +void Configuration::SaveEnableServiceAp(bool i_data) { + Log->AddEvent(LogLevel_Verbose, "Save Enable/disable service AP: " + String(i_data)); + SaveBool(EEPROM_ADDR_SERVICE_AP_ENABLE_START, i_data); +} + /* @info save username fof BasicAuth to EEPROM @param string - username @@ -931,6 +943,23 @@ String Configuration::LoadWifiPassowrd() { return ret; } +/** + @info Load flag for enable/disable service AP from eeprom + @param none + @return bool - status +*/ +bool Configuration::LoadEnableServiceAp() { + bool ret = false; + int tmp = EEPROM.read(EEPROM_ADDR_SERVICE_AP_ENABLE_START); + + if ((255 == tmp) || (1 == tmp)) { + ret = true; + } + Log->AddEvent(LogLevel_Info, "Enable Service AP: " + String(ret)); + + return ret; +} + /** @info Read username for basic authentification from eeprom @param none diff --git a/ESP32_PrusaConnectCam/cfg.h b/ESP32_PrusaConnectCam/cfg.h index 5ca4965..65bcd28 100644 --- a/ESP32_PrusaConnectCam/cfg.h +++ b/ESP32_PrusaConnectCam/cfg.h @@ -52,6 +52,7 @@ public: void SaveWifiSsid(String); void SaveWifiPassword(String); void SaveWifiCfgFlag(uint8_t); + void SaveEnableServiceAp(bool); void SaveBasicAuthUsername(String); void SaveBasicAuthPassword(String); void SaveBasicAuthFlag(bool); @@ -86,6 +87,7 @@ public: bool LoadRawGama(); String LoadWifiSsid(); String LoadWifiPassowrd(); + bool LoadEnableServiceAp(); String LoadBasicAuthUsername(); String LoadBasicAuthPassword(); bool LoadBasicAuthFlag(); diff --git a/ESP32_PrusaConnectCam/mcu_cfg.h b/ESP32_PrusaConnectCam/mcu_cfg.h index 7df537c..b99db7c 100644 --- a/ESP32_PrusaConnectCam/mcu_cfg.h +++ b/ESP32_PrusaConnectCam/mcu_cfg.h @@ -14,7 +14,7 @@ #define _MCU_CFG_H_ /* ---------------- BASIC MCU CFG --------------*/ -#define SW_VERSION "1.0.2-beta" ///< SW version +#define SW_VERSION "1.0.2-rc1" ///< SW version #define SW_BUILD __DATE__ " " __TIME__ ///< build number #define CONSOLE_VERBOSE_DEBUG false ///< enable/disable verbose debug log level for console #define DEVICE_HOSTNAME "Prusa-ESP32cam" ///< device hostname @@ -125,6 +125,7 @@ #define FACTORY_CFG_GAIN_CTRL 1 ///< enable automatic gain #define FACTORY_CFG_AGC_GAIN 0 ///< automatic gain controll gain #define FACTORY_CFG_HOSTNAME "connect.prusa3d.com" ///< hostname for Prusa Connect +#define FACTORY_CFG_ENABLE_SERVICE_AP 1 ///< enable service AP mode /* ---------------- CFG FLAGS ------------------*/ #define CFG_WIFI_SETTINGS_SAVED 0x0A ///< flag saved config @@ -239,6 +240,9 @@ #define EEPROM_ADDR_HOSTNAME_START (EEPROM_ADDR_LOG_LEVEL + EEPROM_ADDR_LOG_LEVEL_LENGTH) #define EEPROM_ADDR_HOSTNAME_LENGTH 51 +#define EEPROM_ADDR_SERVICE_AP_ENABLE_START (EEPROM_ADDR_HOSTNAME_START + EEPROM_ADDR_HOSTNAME_LENGTH) +#define EEPROM_ADDR_SERVICE_AP_ENABLE_LENGTH 1 + #define EEPROM_SIZE (EEPROM_ADDR_REFRESH_INTERVAL_LENGTH + EEPROM_ADDR_FINGERPRINT_LENGTH + EEPROM_ADDR_TOKEN_LENGTH + \ EEPROM_ADDR_FRAMESIZE_LENGTH + EEPROM_ADDR_BRIGHTNESS_LENGTH + EEPROM_ADDR_CONTRAST_LENGTH + \ EEPROM_ADDR_SATURATION_LENGTH + EEPROM_ADDR_HMIRROR_LENGTH + EEPROM_ADDR_VFLIP_LENGTH + \ @@ -251,7 +255,7 @@ EEPROM_ADDR_AWB_MODE_ENABLE_LENGTH + EEPROM_ADDR_BPC_ENABLE_LENGTH + EEPROM_ADDR_WPC_ENABLE_LENGTH + \ EEPROM_ADDR_RAW_GAMA_ENABLE_LENGTH + EEPROM_ADDR_AEC2_LENGTH + EEPROM_ADDR_AE_LEVEL_LENGTH + \ EEPROM_ADDR_AEC_VALUE_LENGTH + EEPROM_ADDR_GAIN_CTRL_LENGTH + EEPROM_ADDR_AGC_GAIN_LENGTH + EEPROM_ADDR_LOG_LEVEL_LENGTH + \ - EEPROM_ADDR_HOSTNAME_LENGTH ) ///< how many bits do we need for eeprom memory + EEPROM_ADDR_HOSTNAME_LENGTH + EEPROM_ADDR_SERVICE_AP_ENABLE_LENGTH) ///< how many bits do we need for eeprom memory #endif diff --git a/ESP32_PrusaConnectCam/server.cpp b/ESP32_PrusaConnectCam/server.cpp index 1641a15..f0caf51 100644 --- a/ESP32_PrusaConnectCam/server.cpp +++ b/ESP32_PrusaConnectCam/server.cpp @@ -635,6 +635,12 @@ void Server_InitWebServer_Sets() { response = true; } + if (request->hasParam("serviceap_enable")) { + SystemLog.AddEvent(LogLevel_Verbose, F("Set service AP enable")); + SystemWifiMngt.SetEnableServiceAp(Server_TransfeStringToBool(request->getParam("serviceap_enable")->value())); + response = true; + } + if (true == response) { request->send_P(200, F("text/html"), MSG_SAVE_OK); } @@ -982,6 +988,7 @@ String Server_GetJsonData() { doc_json["wifi_mode"] = SystemWifiMngt.GetWiFiMode(); doc_json["mdns"] = SystemWifiMngt.GetMdns(); doc_json["service_ap_ssid"] = SystemWifiMngt.GetServiceApSsid(); + doc_json["serviceap"] = (SystemWifiMngt.GetEnableServiceAp() == true) ? "true" : ""; doc_json["auth"] = (WebBasicAuth.EnableAuth == true) ? "true" : ""; doc_json["auth_username"] = WebBasicAuth.UserName; doc_json["last_upload_status"] = Connect.GetBackendReceivedStatus(); diff --git a/ESP32_PrusaConnectCam/system.cpp b/ESP32_PrusaConnectCam/system.cpp index 218e710..a216e64 100644 --- a/ESP32_PrusaConnectCam/system.cpp +++ b/ESP32_PrusaConnectCam/system.cpp @@ -448,7 +448,9 @@ void System_TaskWifiManagement(void *pvParameters) { /* wifi reconnect after signal lost */ SystemWifiMngt.WiFiReconnect(); - + SystemLog.AddEvent(LogLevel_Info, "Free RAM: " + String(ESP.getFreeHeap()) + " bytes"); + SystemLog.AddEvent(LogLevel_Info, "Free SPIRAM: " + String(ESP.getFreePsram()) + " bytes"); + SystemLog.AddEvent(LogLevel_Info, "Temperature: " + String(temperatureRead()) + " *C"); SystemLog.AddEvent(LogLevel_Verbose, "WiFiManagement task. Stack free size: " + String(uxTaskGetStackHighWaterMark(NULL)) + " bytes"); /* reset wdg */ @@ -473,9 +475,6 @@ void System_TaskMain(void *pvParameters) { /* for ota update */ esp_task_wdt_reset(); System_Main(); - SystemLog.AddEvent(LogLevel_Info, "Free RAM: " + String(ESP.getFreeHeap()) + " bytes"); - SystemLog.AddEvent(LogLevel_Info, "Free SPIRAM: " + String(ESP.getFreePsram()) + " bytes"); - SystemLog.AddEvent(LogLevel_Info, "Temperature: " + String(temperatureRead()) + " *C"); SystemLog.AddEvent(LogLevel_Verbose, "System task. Stack free size: " + String(uxTaskGetStackHighWaterMark(NULL)) + " bytes"); /* reset wdg */ diff --git a/ESP32_PrusaConnectCam/wifi_mngt.cpp b/ESP32_PrusaConnectCam/wifi_mngt.cpp index 10c9327..c915ec8 100644 --- a/ESP32_PrusaConnectCam/wifi_mngt.cpp +++ b/ESP32_PrusaConnectCam/wifi_mngt.cpp @@ -43,6 +43,7 @@ void WiFiMngt::LoadCfgFromEeprom() { WifiSsid = config->LoadWifiSsid(); WifiPassword = config->LoadWifiPassowrd(); mDNS_record = config->LoadMdnsRecord(); + EnableServiceAp = config->LoadEnableServiceAp(); } /** @@ -53,20 +54,30 @@ void WiFiMngt::LoadCfgFromEeprom() { void WiFiMngt::Init() { /* check WI-FI mode */ system_led.setTimer(STATUS_LED_WIFI_AP); - ServiceMode = true; log->AddEvent(LogLevel_Info, "WiFi MAC: " + WiFi.macAddress()); /* Set Wi-Fi networks */ SetWifiEvents(); CreateApSsid(); - log->AddEvent(LogLevel_Warning, F("Set WiFi AP mode")); - WiFi.mode(WIFI_AP_STA); + + if (true == GetEnableServiceAp()) { + log->AddEvent(LogLevel_Info, F("Service AP mode enabled")); + WiFi.mode(WIFI_AP_STA); + ServiceMode = true; + WiFi.softAPConfig(Service_LocalIp, Service_Gateway, Service_Subnet); + WiFi.softAP(SericeApSsid.c_str(), SERVICE_WIFI_PASS, SERVICE_WIFI_CHANNEL); + WiFiMode = "AP + Client"; + log->AddEvent(LogLevel_Info, "Service IP Address: http://" + WiFi.softAPIP().toString()); + + } else { + log->AddEvent(LogLevel_Warning, F("Service AP mode disabled!")); + WiFi.mode(WIFI_STA); + ServiceMode = false; + WiFiMode = "Client"; + } + esp_wifi_set_ps(WIFI_PS_NONE); - WiFi.softAPConfig(Service_LocalIp, Service_Gateway, Service_Subnet); - WiFi.softAP(SericeApSsid.c_str(), SERVICE_WIFI_PASS, SERVICE_WIFI_CHANNEL); WiFi.setHostname(DEVICE_HOSTNAME); - WiFiMode = "AP + Client"; - log->AddEvent(LogLevel_Info, "Service IP Address: http://" + WiFi.softAPIP().toString()); FirstConnected = false; //WiFi.setTxPower(WIFI_POWER_18_5dBm); @@ -639,6 +650,16 @@ String WiFiMngt::GetMdns() { return mDNS_record; } +/** + * @brief function for get status about enable service AP mode + * + * @return true + * @return false + */ +bool WiFiMngt::GetEnableServiceAp() { + return EnableServiceAp; +} + /** @brief function for get first time NTP sync status @param none @@ -693,6 +714,16 @@ void WiFiMngt::SetStaPassword(String i_pass) { config->SaveWifiPassword(WifiPassword); } +/** + * @brief Set enable service AP mode after MCU boot + * + * @param i_data + */ +void WiFiMngt::SetEnableServiceAp(bool i_data) { + EnableServiceAp = i_data; + config->SaveEnableServiceAp(EnableServiceAp); +} + /** @brief function for connect to STA @param none diff --git a/ESP32_PrusaConnectCam/wifi_mngt.h b/ESP32_PrusaConnectCam/wifi_mngt.h index c5136e7..787c07d 100644 --- a/ESP32_PrusaConnectCam/wifi_mngt.h +++ b/ESP32_PrusaConnectCam/wifi_mngt.h @@ -51,6 +51,7 @@ private: String SericeApSsid; ///< Service AP SSID bool FirstConnected; ///< flag about first connecting to WiFi network status bool NtpFirstSync; ///< flag about first NTP sync status + bool EnableServiceAp; ///< flag about enable service AP mode uint8_t WiFiStaNetworkBssid[6]; ///< BSSID of the network @@ -104,6 +105,7 @@ public: String GetWiFiMode(); String GetWifiMac(); String GetMdns(); + bool GetEnableServiceAp(); bool GetkActifeWifiCfgFlag(); bool GetNtpFirstTimeSync(); bool GetFirstConnection(); @@ -111,6 +113,7 @@ public: void SetStaCredentials(String, String); void SetStaSsid(String); void SetStaPassword(String); + void SetEnableServiceAp(bool); void ConnectToSta(); void SetMdns(String); void SetFirstConnection(bool); diff --git a/webpage/page_wifi.html b/webpage/page_wifi.html index 3f5d755..e7e3186 100644 --- a/webpage/page_wifi.html +++ b/webpage/page_wifi.html @@ -48,11 +48,26 @@ + + +
+
+ +
+
+
+ + + +
Advanced Wi-Fi settings
Enable service AP
+
+ \ No newline at end of file diff --git a/webpage/scripts.js b/webpage/scripts.js index 728d688..e79b7cb 100644 --- a/webpage/scripts.js +++ b/webpage/scripts.js @@ -68,6 +68,8 @@ function get_data(val) { } if (val == "wifi") { + document.getElementById('serviceapid').checked = obj.serviceap; + $("#status_serviceap").text((obj.serviceap == "true") ? "On" : "Off"); $("#ssid").text(obj.ssid); $("#rssi").text(obj.rssi); $("#rssi_percentage").text(obj.rssi_percentage); @@ -446,3 +448,15 @@ function setupCollapsibleButtons() { } }); } + +function setupCollapsibleButtonsWiFi() { + $(".btn_collapsible_wifi").click(function(){ + $(this).toggleClass("active"); + var content_wifi = $(this).parent().next(); + if (content_wifi.css("display") === "block") { + content_wifi.css("display", "none"); + } else { + content_wifi.css("display", "block"); + } + }); +} diff --git a/webpage/styles.css b/webpage/styles.css index 7ad6680..32e3951 100644 --- a/webpage/styles.css +++ b/webpage/styles.css @@ -287,6 +287,9 @@ cfg_bar li a:hover { table-layout: fixed; text-align: left; } +#wificfg_tb { + margin-left: 30%; +} /* wifi_ntw table */ #wifi_ntw { font: normal normal normal 12px/5px sans-serif; @@ -498,3 +501,23 @@ cfg_bar li a:hover { background-color: #FA6831; color: white; } + +/* advanced wifi cfg */ +.content_wifi { + display: none; +} + +.btn_collapsible_wifi { + width: 300px; + height: 24px; + text-align: center; + font: normal normal bold 14px/5px sans-serif; + color: #000000; + background-color: white; + border-radius: 5px; + border: 1px solid #343a40; +} +.btn_collapsible_wifi:hover { + background-color: #FA6831; + color: white; +}