1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-13 05:53:06 +01:00

allow to set system name, auto shutdown AP, usb baud

This commit is contained in:
andreas
2021-10-22 15:20:40 +02:00
parent 35ff689cd9
commit 6e449ca4dc
6 changed files with 83 additions and 16 deletions

View File

@@ -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());

View File

@@ -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

View File

@@ -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

View File

@@ -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();
}