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 GwConfigHandler::toJson() const{
String rt; String rt;
DynamicJsonDocument jdoc(300); DynamicJsonDocument jdoc(400);
for (int i=0;i<getNumConfig();i++){ for (int i=0;i<getNumConfig();i++){
jdoc[configs[i]->getName()]=configs[i]->asCString(); jdoc[configs[i]->getName()]=configs[i]->asCString();
} }
@ -58,7 +58,6 @@ GwConfigHandler::GwConfigHandler(GwLog *logger){
this->logger=logger; this->logger=logger;
} }
bool GwConfigHandler::loadConfig(){ bool GwConfigHandler::loadConfig(){
logger->logString("config load");
prefs.begin(PREF_NAME,true); prefs.begin(PREF_NAME,true);
for (int i=0;i<getNumConfig();i++){ for (int i=0;i<getNumConfig();i++){
String v=prefs.getString(configs[i]->getName().c_str(),configs[i]->getDefault()); 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 maxClients=F("maxClients");
const String sendTCP=F("sendTCP"); const String sendTCP=F("sendTCP");
const String sendSeasmart=F("sendSeasmart"); const String sendSeasmart=F("sendSeasmart");
const String usbBaud=F("usbBaud");
const String systemName=F("systemName");
const String stopApTime=F("stopApTime");
GwConfigHandler(GwLog *logger); GwConfigHandler(GwLog *logger);
bool loadConfig(); bool loadConfig();
bool saveConfig(); bool saveConfig();
@ -81,7 +84,7 @@ class GwConfigHandler{
GwConfigItem * findConfig(const String name, bool dummy=false); GwConfigItem * findConfig(const String name, bool dummy=false);
GwConfigInterface * getConfigItem(const String name, bool dummy=false) const; GwConfigInterface * getConfigItem(const String name, bool dummy=false) const;
private: private:
GwConfigItem* configs[9]={ GwConfigItem* configs[12]={
new GwConfigItem(sendUsb,"true"), new GwConfigItem(sendUsb,"true"),
new GwConfigItem (receiveUsb,"false"), new GwConfigItem (receiveUsb,"false"),
new GwConfigItem (wifiClient,"false"), new GwConfigItem (wifiClient,"false"),
@ -90,10 +93,13 @@ class GwConfigHandler{
new GwConfigItem (serverPort,"2222"), new GwConfigItem (serverPort,"2222"),
new GwConfigItem (maxClients, "10"), new GwConfigItem (maxClients, "10"),
new GwConfigItem (sendTCP,"true"), 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{ int getNumConfig() const{
return 9; return 12;
} }
}; };
#endif #endif

View File

@ -11,11 +11,15 @@ class GwWifi{
const GwConfigInterface *wifiPass; const GwConfigInterface *wifiPass;
bool connectInternal(); bool connectInternal();
long lastConnectStart=0; long lastConnectStart=0;
unsigned long lastApAccess=0;
unsigned long apShutdownTime=0;
bool apActive=false;
public: public:
GwWifi(const GwConfigHandler *config,GwLog *log); GwWifi(const GwConfigHandler *config,GwLog *log);
void setup(); void setup();
void loop(); void loop();
bool clientConnected(); bool clientConnected();
bool connectClient(); bool connectClient();
String apIP();
}; };
#endif #endif

View File

@ -1,8 +1,6 @@
#include "GWWifi.h" #include "GWWifi.h"
const char *AP_password = "esp32nmea2k";
const char *AP_ssid = "ESP32NMEA2K"; // ESP32 as AP
const char *AP_password = "esp32nmea2k"; //too short - so no pass
GwWifi::GwWifi(const GwConfigHandler *config,GwLog *log){ GwWifi::GwWifi(const GwConfigHandler *config,GwLog *log){
this->config=config; this->config=config;
@ -18,13 +16,20 @@ void GwWifi::setup(){
IPAddress AP_gateway(192, 168, 15, 1); IPAddress AP_gateway(192, 168, 15, 1);
IPAddress AP_subnet(255, 255, 255, 0); IPAddress AP_subnet(255, 255, 255, 0);
WiFi.mode(WIFI_MODE_APSTA); //enable both AP and client 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); delay(100);
WiFi.softAPConfig(AP_local_ip, AP_gateway, AP_subnet); WiFi.softAPConfig(AP_local_ip, AP_gateway, AP_subnet);
logger->logString("WifiAP created: ssid=%s,adress=%s", logger->logString("WifiAP created: ssid=%s,adress=%s",
AP_ssid, ssid,
WiFi.softAPIP().toString().c_str() 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(); connectInternal();
} }
bool GwWifi::connectInternal(){ bool GwWifi::connectInternal(){
@ -46,6 +51,16 @@ void GwWifi::loop(){
connectInternal(); 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(){ bool GwWifi::clientConnected(){
return WiFi.status() == WL_CONNECTED; return WiFi.status() == WL_CONNECTED;
@ -53,4 +68,9 @@ bool GwWifi::clientConnected(){
bool GwWifi::connectClient(){ bool GwWifi::connectClient(){
WiFi.disconnect(); WiFi.disconnect();
return connectInternal(); return connectInternal();
}
String GwWifi::apIP(){
if (! apActive) return String();
return WiFi.softAPIP().toString();
} }

View File

@ -108,6 +108,7 @@ void js_status(){
status["wifiConnected"]=gwWifi.clientConnected(); status["wifiConnected"]=gwWifi.clientConnected();
status["clientIP"]=WiFi.localIP().toString(); status["clientIP"]=WiFi.localIP().toString();
status["numClients"]=socketServer.numClients(); status["numClients"]=socketServer.numClients();
status["apIp"]=gwWifi.apIP();
nmea0183Converter->toJson(status); nmea0183Converter->toJson(status);
String buf; String buf;
serializeJson(status,buf); serializeJson(status,buf);
@ -170,19 +171,26 @@ void handleNotFound()
GwConfigInterface *sendUsb=NULL; GwConfigInterface *sendUsb=NULL;
GwConfigInterface *sendTCP=NULL; GwConfigInterface *sendTCP=NULL;
GwConfigInterface *sendSeasmart=NULL; GwConfigInterface *sendSeasmart=NULL;
GwConfigInterface *systemName=NULL;
void setup() { void setup() {
uint8_t chipid[6]; uint8_t chipid[6];
uint32_t id = 0; uint32_t id = 0;
// Init USB serial port
Serial.begin(115200);
Serial.println("Starting...");
config.loadConfig(); 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()); Serial.println(config.toString());
sendUsb=config.getConfigItem(config.sendUsb,true); sendUsb=config.getConfigItem(config.sendUsb,true);
sendTCP=config.getConfigItem(config.sendTCP,true); sendTCP=config.getConfigItem(config.sendTCP,true);
sendSeasmart=config.getConfigItem(config.sendSeasmart,true); sendSeasmart=config.getConfigItem(config.sendSeasmart,true);
systemName=config.getConfigItem(config.systemName,true);
gwWifi.setup(); gwWifi.setup();
@ -213,9 +221,9 @@ void setup() {
// Set product information // Set product information
NMEA2000.SetProductInformation("1", // Manufacturer's Model serial code NMEA2000.SetProductInformation("1", // Manufacturer's Model serial code
100, // Manufacturer's product code 100, // Manufacturer's product code
"NMEA 2000 WiFi Gateway", // Manufacturer's Model ID systemName->asCString(), // Manufacturer's Model ID
"1.0.2.25 (2019-07-07)", // Manufacturer's Software version code VERSION, // Manufacturer's Software version code
"1.0.2.0 (2019-07-07)" // Manufacturer's Model version VERSION // Manufacturer's Model version
); );
// Set device information // Set device information
NMEA2000.SetDeviceInformation(id, // Unique number. Use e.g. Serial number. Id is generated from MAC-Address NMEA2000.SetDeviceInformation(id, // Unique number. Use e.g. Serial number. Id is generated from MAC-Address

View File

@ -171,6 +171,10 @@ span#connected.ok{
<span class="label">connected</span> <span class="label">connected</span>
<span class="value" id="connected"></span> <span class="value" id="connected"></span>
</div> </div>
<div class="row">
<span class="label">Access Point IP</span>
<span class="value" id="apIp">---</span>
</div>
<div class="row"> <div class="row">
<span class="label"># NMEA2000 messages</span> <span class="label"># NMEA2000 messages</span>
<span class="value" id="numcan">---</span> <span class="value" id="numcan">---</span>
@ -197,6 +201,10 @@ span#connected.ok{
</div> </div>
<button id="reset" >Reset</button> <button id="reset" >Reset</button>
<div class="configForm"> <div class="configForm">
<div class="row">
<span class="label">system name</span>
<input name="systemName" type="text">
</div>
<div class="row"> <div class="row">
<span class="label">NMEAtoUSB</span> <span class="label">NMEAtoUSB</span>
<select name="sendUsb"> <select name="sendUsb">
@ -204,6 +212,24 @@ span#connected.ok{
<option value="false" selected="selected">Off</option> <option value="false" selected="selected">Off</option>
</select> </select>
</div> </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"> <div class="row">
<span class="label">TCP Port</span> <span class="label">TCP Port</span>
<input name="serverPort" type="number"> <input name="serverPort" type="number">
@ -226,6 +252,10 @@ span#connected.ok{
<option value="false" selected="selected">Off</option> <option value="false" selected="selected">Off</option>
</select> </select>
</div> </div>
<div class="row">
<span class="label">shutdown AP after (min)</span>
<input name="stopApTime" type="number">
</div>
<div class="row"> <div class="row">
<span class="label">wifiClient</span> <span class="label">wifiClient</span>
<select name="wifiClient"> <select name="wifiClient">