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;i<getNumConfig();i++){ jdoc[configs[i]->getName()]=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;i<getNumConfig();i++){ String v=prefs.getString(configs[i]->getName().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{ <span class="label">connected</span> <span class="value" id="connected"></span> </div> +<div class="row"> + <span class="label">Access Point IP</span> + <span class="value" id="apIp">---</span> +</div> <div class="row"> <span class="label"># NMEA2000 messages</span> <span class="value" id="numcan">---</span> @@ -197,6 +201,10 @@ span#connected.ok{ </div> <button id="reset" >Reset</button> <div class="configForm"> + <div class="row"> + <span class="label">system name</span> + <input name="systemName" type="text"> + </div> <div class="row"> <span class="label">NMEAtoUSB</span> <select name="sendUsb"> @@ -204,6 +212,24 @@ span#connected.ok{ <option value="false" selected="selected">Off</option> </select> </div> + <!--"1200","2400","4800","9600","14400","19200","28800","38400","57600","115200","230400","460800"--> + <div class="row"> + <span class="label">USB baud rate</span> + <select name="usbBaud"> + <option value="1200">1200</option> + <option value="2400">2400</option> + <option value="4800">4800</option> + <option value="9600">9600</option> + <option value="14400">14400</option> + <option value="19200">19200</option> + <option value="28800">28800</option> + <option value="38400">38400</option> + <option value="57600">57600</option> + <option value="115200">115200</option> + <option value="230400">230400</option> + <option value="460800">460800</option> + </select> + </div> <div class="row"> <span class="label">TCP Port</span> <input name="serverPort" type="number"> @@ -226,6 +252,10 @@ span#connected.ok{ <option value="false" selected="selected">Off</option> </select> </div> + <div class="row"> + <span class="label">shutdown AP after (min)</span> + <input name="stopApTime" type="number"> + </div> <div class="row"> <span class="label">wifiClient</span> <select name="wifiClient">