Skip to content

Commit

Permalink
Merge pull request #2 from soylentOrange/adjust-wifi-tx-power
Browse files Browse the repository at this point in the history
Make wifi tx power adjustable
  • Loading branch information
soylentOrange authored Oct 6, 2024
2 parents 9e8f180 + 63e2625 commit ec6e4df
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
int8_t _modbusRtsPin;
unsigned long _serialBaudRate;
uint32_t _serialConfig;
int8_t _WiFiTXPower;
String _hostname;
public:
Config();
void begin(Preferences *prefs);
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Config::Config()
,_modbusRtsPin(-1)
,_serialBaudRate(115200)
,_serialConfig(SERIAL_8N1)
,_WiFiTXPower(60)
,_hostname("na")
{}

void Config::begin(Preferences *prefs)
Expand All @@ -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(){
Expand Down Expand Up @@ -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);
}
10 changes: 10 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;});
Expand Down
51 changes: 49 additions & 2 deletions src/pages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
response->print("<table>");

// 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());
Expand Down Expand Up @@ -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("<form method=\"post\">");
response->print("<table>"
"<tr>"
"<td>"
"<label for=\"hn\">Hostname</label>"
"</td>"
"<td>");
response->printf("<input type=\"text\" minlength=\"2\" maxlength=\"63\" id=\"hn\" name=\"hn\" value=\"%s\">", config->getHostname().length() > 2 ? config->getHostname().c_str() : WiFi.getHostname());
response->print("</tr>"
"</tr>"
"<tr>"
"<td>"
"<label for=\"tx\">TX Power</label>"
"</td>"
"<td>");
response->printf("<select id=\"tx\" name=\"tx\" data-value=\"%d\">", config->getWiFiTXPower());
response->print("<option value=\"78\">19.5dBm</option>"
"<option value=\"76\">19dBm</option>"
"<option value=\"74\">18.5dBm</option>"
"<option value=\"68\">17dBm</option>"
"<option value=\"60\">15dBm</option>"
"<option value=\"52\">13dBm</option>"
"<option value=\"44\">11dBm</option>"
"<option value=\"34\">8.5dBm</option>"
"<option value=\"28\">7dBm</option>"
"<option value=\"20\">5dBm</option>"
"<option value=\"8\">2dBm</option>"
"</select>"
"</td>"
"</tr>"
"</table>"
"<h3>Modbus TCP</h3>"
"<table>"
"<tr>"
"<td>"
"<label for=\"tp\">TCP Port</label>"
Expand Down Expand Up @@ -191,7 +224,8 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
"</select>"
"</td>"
"</tr>"
"</table>");
"<tr><td>&nbsp;</td><td></td></tr>"
"</table>");
response->print("<button class=\"r\">Save</button>"
"</form>"
"<p></p>");
Expand Down Expand Up @@ -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){
Expand Down

0 comments on commit ec6e4df

Please sign in to comment.