diff --git a/include/config.h b/include/config.h index 5ff8e51..bb00e89 100644 --- a/include/config.h +++ b/include/config.h @@ -16,6 +16,8 @@ int8_t _modbusRtsPin; unsigned long _serialBaudRate; uint32_t _serialConfig; + int8_t _WiFiTXPower; + String _hostname; public: Config(); void begin(Preferences *prefs); @@ -43,6 +45,10 @@ void setSerialParity(uint8_t value); uint8_t getSerialStopBits(); void setSerialStopBits(uint8_t value); + int8_t getWiFiTXPower(); + void setWiFiTXPower(int8_t value); + String getHostname(); + void setHostname(String value); }; #ifdef DEBUG #define dbg(x...) debugSerial.print(x); diff --git a/src/config.cpp b/src/config.cpp index 3c46754..b701c3c 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -9,6 +9,8 @@ Config::Config() ,_modbusRtsPin(-1) ,_serialBaudRate(115200) ,_serialConfig(SERIAL_8N1) + ,_WiFiTXPower(60) + ,_hostname("na") {} void Config::begin(Preferences *prefs) @@ -21,6 +23,8 @@ void Config::begin(Preferences *prefs) _modbusRtsPin = _prefs->getChar("modbusRtsPin", _modbusRtsPin); _serialBaudRate = _prefs->getULong("serialBaudRate", _serialBaudRate); _serialConfig = _prefs->getULong("serialConfig", _serialConfig); + _WiFiTXPower = _prefs->getChar("txPower", _WiFiTXPower); + _hostname = _prefs->getString("hostname", _hostname); } uint16_t Config::getTcpPort(){ @@ -153,4 +157,23 @@ void Config::setSerialStopBits(uint8_t value){ if (stopbits == value) return; _serialConfig = (_serialConfig & 0xffffffcf) | value; _prefs->putULong("serialConfig", _serialConfig); +} +String Config::getHostname(){ + return _hostname; +} + +void Config::setHostname(String value){ + if (_hostname == value) return; + _hostname = value; + _prefs->putString("hostname", _hostname); +} + +int8_t Config::getWiFiTXPower(){ + return _WiFiTXPower; +} + +void Config::setWiFiTXPower(int8_t value){ + if (_WiFiTXPower == value) return; + _WiFiTXPower = value; + _prefs->putChar("txPower", _WiFiTXPower); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 22a37b1..52ec666 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,7 +26,17 @@ void setup() { debugSerial.end(); debugSerial.begin(config.getSerialBaudRate(), config.getSerialConfig()); dbgln("[wifi] start"); + // Set Hostname + if(config.getHostname().length() > 2) { + WiFi.setHostname(config.getHostname().c_str()); + } + // Enable auto-reconnect + wm.setWiFiAutoReconnect(true); + // Set WiFi to station mode WiFi.mode(WIFI_STA); + // Set (reduced) WiFi TX Power + dbgln(String("[WiFi] TxPower: ") + ((float)config.getWiFiTXPower()) / 4 + "dBm") + WiFi.setTxPower((wifi_power_t) config.getWiFiTXPower()); wm.setClass("invert"); auto reboot = false; wm.setAPCallback([&reboot](WiFiManager *wifiManager){reboot = true;}); diff --git a/src/pages.cpp b/src/pages.cpp index 3847548..6d04359 100644 --- a/src/pages.cpp +++ b/src/pages.cpp @@ -22,6 +22,8 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi * response->print(""); // show ESP infos... + sendTableRow(response, "WiFi TX Power (dBm)", String(((float)WiFi.getTxPower())/4, 2)); + sendTableRow(response, "ESP Temperature (C)", String(temperatureRead(), 2)); sendTableRow(response, "ESP Uptime (sec)", esp_timer_get_time() / 1000000); sendTableRow(response, "ESP SSID", WiFi.SSID()); sendTableRow(response, "ESP RSSI", WiFi.RSSI()); @@ -63,9 +65,40 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi * server->on("/config", HTTP_GET, [config](AsyncWebServerRequest *request){ dbgln("[webserver] GET /config"); auto *response = request->beginResponseStream("text/html"); - sendResponseHeader(response, "Modbus TCP"); + sendResponseHeader(response, "Wifi Settings"); response->print(""); response->print("
" + "" + "" + "" + "" + "" + "" + "" + "" + "
" + "" + ""); + response->printf("", config->getHostname().length() > 2 ? config->getHostname().c_str() : WiFi.getHostname()); + response->print("
" + "" + ""); + response->printf("" + "
" + "

Modbus TCP

" + "" "" "" "" - "
" "" @@ -191,7 +224,8 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi * "" "
"); + " " + ""); response->print("" "" "

"); @@ -263,6 +297,19 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi * config->setSerialStopBits(stop); dbg("[webserver] saved serial stop bits: "); dbgln(stop); } + if (request->hasParam("hn", true)){ + auto hostname = request->getParam("hn", true)->value(); + config->setHostname(hostname); + dbg("[webserver] saved hostname: "); + dbgln(config->getHostname()); + } + if (request->hasParam("tx", true)){ + auto txPower = request->getParam("tx", true)->value().toInt(); + config->setWiFiTXPower((int8_t)txPower); + dbg("[webserver] saved TX Power: "); + dbg(((float)config->getWiFiTXPower())/4); + dbgln("dBm"); + } request->redirect("/"); }); server->on("/debug", HTTP_GET, [](AsyncWebServerRequest *request){