From 6e449ca4dce77444fe531b3f673afcafbab49bef Mon Sep 17 00:00:00 2001 From: andreas Date: Fri, 22 Oct 2021 15:20:40 +0200 Subject: [PATCH] allow to set system name, auto shutdown AP, usb baud --- lib/config/GWConfig.cpp | 3 +-- lib/config/GWConfig.h | 12 +++++++++--- lib/wifi/GWWifi.h | 4 ++++ lib/wifi/GwWifi.cpp | 30 +++++++++++++++++++++++++----- src/main.cpp | 20 ++++++++++++++------ web/index.html | 30 ++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 16 deletions(-) diff --git a/lib/config/GWConfig.cpp b/lib/config/GWConfig.cpp index 8b92130..c2910f4 100644 --- a/lib/config/GWConfig.cpp +++ b/lib/config/GWConfig.cpp @@ -31,7 +31,7 @@ String GwConfigHandler::toString() const{ String GwConfigHandler::toJson() const{ String rt; - DynamicJsonDocument jdoc(300); + DynamicJsonDocument jdoc(400); for (int i=0;igetName()]=configs[i]->asCString(); } @@ -58,7 +58,6 @@ GwConfigHandler::GwConfigHandler(GwLog *logger){ this->logger=logger; } bool GwConfigHandler::loadConfig(){ - logger->logString("config load"); prefs.begin(PREF_NAME,true); for (int i=0;igetName().c_str(),configs[i]->getDefault()); diff --git a/lib/config/GWConfig.h b/lib/config/GWConfig.h index 122bdd9..b5125fe 100644 --- a/lib/config/GWConfig.h +++ b/lib/config/GWConfig.h @@ -67,6 +67,9 @@ class GwConfigHandler{ const String maxClients=F("maxClients"); const String sendTCP=F("sendTCP"); const String sendSeasmart=F("sendSeasmart"); + const String usbBaud=F("usbBaud"); + const String systemName=F("systemName"); + const String stopApTime=F("stopApTime"); GwConfigHandler(GwLog *logger); bool loadConfig(); bool saveConfig(); @@ -81,7 +84,7 @@ class GwConfigHandler{ GwConfigItem * findConfig(const String name, bool dummy=false); GwConfigInterface * getConfigItem(const String name, bool dummy=false) const; private: - GwConfigItem* configs[9]={ + GwConfigItem* configs[12]={ new GwConfigItem(sendUsb,"true"), new GwConfigItem (receiveUsb,"false"), new GwConfigItem (wifiClient,"false"), @@ -90,10 +93,13 @@ class GwConfigHandler{ new GwConfigItem (serverPort,"2222"), new GwConfigItem (maxClients, "10"), new GwConfigItem (sendTCP,"true"), - new GwConfigItem (sendSeasmart,"false") + new GwConfigItem (sendSeasmart,"false"), + new GwConfigItem (usbBaud,"115200"), + new GwConfigItem (systemName,"ESP32NMEA2K"), + new GwConfigItem (stopApTime,"0") }; int getNumConfig() const{ - return 9; + return 12; } }; #endif \ No newline at end of file diff --git a/lib/wifi/GWWifi.h b/lib/wifi/GWWifi.h index b823907..854c2c5 100644 --- a/lib/wifi/GWWifi.h +++ b/lib/wifi/GWWifi.h @@ -11,11 +11,15 @@ class GwWifi{ const GwConfigInterface *wifiPass; bool connectInternal(); long lastConnectStart=0; + unsigned long lastApAccess=0; + unsigned long apShutdownTime=0; + bool apActive=false; public: GwWifi(const GwConfigHandler *config,GwLog *log); void setup(); void loop(); bool clientConnected(); bool connectClient(); + String apIP(); }; #endif \ No newline at end of file diff --git a/lib/wifi/GwWifi.cpp b/lib/wifi/GwWifi.cpp index 32a4923..9c1945e 100644 --- a/lib/wifi/GwWifi.cpp +++ b/lib/wifi/GwWifi.cpp @@ -1,8 +1,6 @@ #include "GWWifi.h" - -const char *AP_ssid = "ESP32NMEA2K"; // ESP32 as AP -const char *AP_password = "esp32nmea2k"; //too short - so no pass +const char *AP_password = "esp32nmea2k"; GwWifi::GwWifi(const GwConfigHandler *config,GwLog *log){ this->config=config; @@ -18,13 +16,20 @@ void GwWifi::setup(){ IPAddress AP_gateway(192, 168, 15, 1); IPAddress AP_subnet(255, 255, 255, 0); WiFi.mode(WIFI_MODE_APSTA); //enable both AP and client - WiFi.softAP(AP_ssid,AP_password); + const char *ssid=config->getConfigItem(config->systemName)->asCString(); + WiFi.softAP(ssid,AP_password); delay(100); WiFi.softAPConfig(AP_local_ip, AP_gateway, AP_subnet); logger->logString("WifiAP created: ssid=%s,adress=%s", - AP_ssid, + ssid, WiFi.softAPIP().toString().c_str() ); + apActive=true; + lastApAccess=millis(); + apShutdownTime=config->getConfigItem(config->stopApTime)->asInt() * 60; + if (apShutdownTime < 120 && apShutdownTime != 0) apShutdownTime=120; //min 2 minutes + logger->logString("GWWIFI: AP auto shutdown %s (%ds)",apShutdownTime> 0?"enabled":"disabled",apShutdownTime); + apShutdownTime=apShutdownTime*1000; //ms connectInternal(); } bool GwWifi::connectInternal(){ @@ -46,6 +51,16 @@ void GwWifi::loop(){ connectInternal(); } } + if (apShutdownTime != 0 && apActive){ + if (WiFi.softAPgetStationNum()){ + lastApAccess=millis(); + } + if ((lastApAccess + apShutdownTime) < millis()){ + logger->logString("GWWIFI: shutdown AP"); + WiFi.softAPdisconnect(true); + apActive=false; + } + } } bool GwWifi::clientConnected(){ return WiFi.status() == WL_CONNECTED; @@ -53,4 +68,9 @@ bool GwWifi::clientConnected(){ bool GwWifi::connectClient(){ WiFi.disconnect(); return connectInternal(); +} + +String GwWifi::apIP(){ + if (! apActive) return String(); + return WiFi.softAPIP().toString(); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7dba46b..7e31f84 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,6 +108,7 @@ void js_status(){ status["wifiConnected"]=gwWifi.clientConnected(); status["clientIP"]=WiFi.localIP().toString(); status["numClients"]=socketServer.numClients(); + status["apIp"]=gwWifi.apIP(); nmea0183Converter->toJson(status); String buf; serializeJson(status,buf); @@ -170,19 +171,26 @@ void handleNotFound() GwConfigInterface *sendUsb=NULL; GwConfigInterface *sendTCP=NULL; GwConfigInterface *sendSeasmart=NULL; +GwConfigInterface *systemName=NULL; void setup() { uint8_t chipid[6]; uint32_t id = 0; - // Init USB serial port - Serial.begin(115200); - Serial.println("Starting..."); config.loadConfig(); + // Init USB serial port + GwConfigInterface *usbBaud=config.getConfigItem(config.usbBaud,false); + int baud=115200; + if (usbBaud){ + baud=usbBaud->asInt(); + } + Serial.begin(baud); + Serial.println("Starting..."); Serial.println(config.toString()); sendUsb=config.getConfigItem(config.sendUsb,true); sendTCP=config.getConfigItem(config.sendTCP,true); sendSeasmart=config.getConfigItem(config.sendSeasmart,true); + systemName=config.getConfigItem(config.systemName,true); gwWifi.setup(); @@ -213,9 +221,9 @@ void setup() { // Set product information NMEA2000.SetProductInformation("1", // Manufacturer's Model serial code 100, // Manufacturer's product code - "NMEA 2000 WiFi Gateway", // Manufacturer's Model ID - "1.0.2.25 (2019-07-07)", // Manufacturer's Software version code - "1.0.2.0 (2019-07-07)" // Manufacturer's Model version + systemName->asCString(), // Manufacturer's Model ID + VERSION, // Manufacturer's Software version code + VERSION // Manufacturer's Model version ); // Set device information NMEA2000.SetDeviceInformation(id, // Unique number. Use e.g. Serial number. Id is generated from MAC-Address diff --git a/web/index.html b/web/index.html index 18587e8..2cf0eb5 100644 --- a/web/index.html +++ b/web/index.html @@ -171,6 +171,10 @@ span#connected.ok{ connected +
+ Access Point IP + --- +
# NMEA2000 messages --- @@ -197,6 +201,10 @@ span#connected.ok{
+
+ system name + +
NMEAtoUSB
+ +
+ USB baud rate + +
TCP Port @@ -226,6 +252,10 @@ span#connected.ok{
+
+ shutdown AP after (min) + +
wifiClient