From 436dd3632b1a8d07b962d0d9ede6c1e1a9f6bce5 Mon Sep 17 00:00:00 2001 From: andreas Date: Sat, 6 Nov 2021 18:55:05 +0100 Subject: [PATCH] allow to set the AP password if we can do hardware reset --- lib/wifi/GWWifi.h | 3 ++- lib/wifi/GwWifi.cpp | 10 ++++++++-- src/main.cpp | 12 ++++++++++-- web/config.json | 9 +++++++++ web/index.html | 34 +++++++++++++++++++++++++++++++--- 5 files changed, 60 insertions(+), 8 deletions(-) diff --git a/lib/wifi/GWWifi.h b/lib/wifi/GWWifi.h index 854c2c5..bde0a2b 100644 --- a/lib/wifi/GWWifi.h +++ b/lib/wifi/GWWifi.h @@ -14,8 +14,9 @@ class GwWifi{ unsigned long lastApAccess=0; unsigned long apShutdownTime=0; bool apActive=false; + bool fixedApPass=true; public: - GwWifi(const GwConfigHandler *config,GwLog *log); + GwWifi(const GwConfigHandler *config,GwLog *log, bool fixedApPass=true); void setup(); void loop(); bool clientConnected(); diff --git a/lib/wifi/GwWifi.cpp b/lib/wifi/GwWifi.cpp index f610b22..726d7d9 100644 --- a/lib/wifi/GwWifi.cpp +++ b/lib/wifi/GwWifi.cpp @@ -2,12 +2,13 @@ const char *AP_password = "esp32nmea2k"; -GwWifi::GwWifi(const GwConfigHandler *config,GwLog *log){ +GwWifi::GwWifi(const GwConfigHandler *config,GwLog *log, bool fixedApPass){ this->config=config; this->logger=log; wifiClient=config->getConfigItem(config->wifiClient,true); wifiSSID=config->getConfigItem(config->wifiSSID,true); wifiPass=config->getConfigItem(config->wifiPass,true); + this->fixedApPass=fixedApPass; } void GwWifi::setup(){ logger->logString("Wifi setup"); @@ -17,7 +18,12 @@ void GwWifi::setup(){ IPAddress AP_subnet(255, 255, 255, 0); WiFi.mode(WIFI_MODE_APSTA); //enable both AP and client const char *ssid=config->getConfigItem(config->systemName)->asCString(); - WiFi.softAP(ssid,AP_password); + if (fixedApPass){ + WiFi.softAP(ssid,AP_password); + } + else{ + WiFi.softAP(ssid,config->getConfigItem(config->apPassword)->asCString()); + } delay(100); WiFi.softAPConfig(AP_local_ip, AP_gateway, AP_subnet); logger->logString("WifiAP created: ssid=%s,adress=%s", diff --git a/src/main.cpp b/src/main.cpp index ee30609..0e5a7d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,7 +62,12 @@ typedef std::map StringMap; GwLog logger(GwLog::DEBUG,NULL); GwConfigHandler config(&logger); -GwWifi gwWifi(&config,&logger); +#ifdef GWBUTTON_PIN +bool fixedApPass=false; +#else +bool fixedApPass=true; +#endif +GwWifi gwWifi(&config,&logger,fixedApPass); GwSocketServer socketServer(&config,&logger,MIN_TCP_CHANNEL_ID); GwBoatData boatData(&logger); @@ -223,13 +228,16 @@ class CapabilitiesRequest : public GwRequestMessage{ CapabilitiesRequest() : GwRequestMessage(F("application/json"),F("capabilities")){}; protected: virtual void processRequest(){ - DynamicJsonDocument json(JSON_OBJECT_SIZE(2)); + DynamicJsonDocument json(JSON_OBJECT_SIZE(6)); #ifdef GWSERIAL_MODE String serial(F(GWSERIAL_MODE)); #else String serial(F("NONE")); #endif json["serialmode"]=serial; + #ifdef GWBUTTON_PIN + json["hardwareReset"]="true"; + #endif serializeJson(json,result); } }; diff --git a/web/config.json b/web/config.json index f772808..97c7d27 100644 --- a/web/config.json +++ b/web/config.json @@ -25,6 +25,15 @@ "description": "stop the access point after that many minutes if not used", "category":"system" }, + { + "name": "apPassword", + "type": "password", + "default": "esp32nmea2k", + "check": "checkApPass", + "description": "set the password for the Wifi access point", + "category":"system", + "capabilities":{"hardwareReset":["true"]} + }, { "name": "usbBaud", "label": "USB baud rate", diff --git a/web/index.html b/web/index.html index 8a7915f..0c7cec3 100644 --- a/web/index.html +++ b/web/index.html @@ -83,6 +83,12 @@ if (allowed != v) return "contains invalid characters, only a-z, A-Z, 0-9"; if (v.length < 2 || v.length > 32) return "invalid length (2...32)"; } + function checkApPass(v){ + //min 8 characters + if (v.length < 8){ + return "password must be at least 8 characters"; + } + } function changeConfig(){ let url="/api/setConfig?"; let values=document.querySelectorAll('.configForm select , .configForm input'); @@ -96,7 +102,9 @@ if (typeof(self[check]) === 'function'){ let res=self[check](v.value); if (res){ - alert("invalid config for "+v.getAttribute('name')+"("+v.value+"):\n"+res); + let value=v.value; + if (v.type === 'password') value="******"; + alert("invalid config for "+v.getAttribute('name')+"("+value+"):\n"+res); return; } } @@ -254,8 +262,18 @@ } el=document.createElement('input'); el.setAttribute('name',configItem.name) + frame.appendChild(el); if (configItem.type === 'password'){ el.setAttribute('type','password'); + let vis=addEl('span','icon-eye',frame); + vis.addEventListener('click',function(v){ + if (vis.classList.toggle('active')){ + el.setAttribute('type','text'); + } + else{ + el.setAttribute('type','password'); + } + }); } else if (configItem.type === 'number'){ el.setAttribute('type','number'); @@ -263,7 +281,6 @@ else{ el.setAttribute('type','text'); } - frame.appendChild(el); return el; } let configDefinitions; @@ -488,7 +505,7 @@ div#overlayContent { padding: 0.5em; } div#overlayContent.text{ - white-space: pre; + white-space: pre-line; } .overlayButtons { border-top: 1px solid grey; @@ -525,6 +542,17 @@ button#reset{ h1{ margin-bottom: 0; } +.icon-eye{ + background-image: url("data:image/svg+xml;utf-8, Layer 1 "); + height: 1.5em; + width: 1.5em; + margin-left: 0.5em; + opacity: 0.3; +} +.icon-eye.active{ + opacity: 1; +} +