From d6e961072102e9239b8f567823ceaef5a1b333ec Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Tue, 21 Nov 2023 16:12:44 +0100 Subject: [PATCH] Added WiFi signal status and settings --- data | 2 +- src/CMakeLists.txt | 2 +- src/lib/config.cpp | 8 +++++++ src/lib/webserver.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++ src/lib/webserver.hpp | 2 ++ 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/data b/data index d25284e..b38dabe 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit d25284e3a47a9efe6c0a8877e8abeac098ced8af +Subproject commit b38dabec52a55d79e090d3db4b5f2432d075bc0f diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d115ee0..fd810d4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) idf_component_register(SRCS ${app_sources}) -target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++11) +target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++17) diff --git a/src/lib/config.cpp b/src/lib/config.cpp index 7d427b3..23649b4 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -21,6 +21,7 @@ void setup() if (mcp.digitalRead(3) == LOW) { preferences.putBool("wifiConfigured", false); + preferences.remove("txPower"); WiFi.eraseAP(); queueLedEffect(LED_EFFECT_WIFI_ERASE_SETTINGS); @@ -51,6 +52,7 @@ void tryImprovSetup() { WiFi.onEvent(WiFiEvent); + if (!preferences.getBool("wifiConfigured", false)) { setFgColor(GxEPD_BLACK); @@ -143,6 +145,12 @@ void tryImprovSetup() WiFi.setAutoConnect(true); WiFi.setAutoReconnect(true); WiFi.begin(); + if(preferences.getInt("txPower", 0)) { + if(WiFi.setTxPower(static_cast(preferences.getInt("txPower", 0)))) { + Serial.printf("WiFi max tx power set to %d\n", preferences.getInt("txPower", 0)); + } + } + while (WiFi.status() != WL_CONNECTED) { diff --git a/src/lib/webserver.cpp b/src/lib/webserver.cpp index dd689f1..f0f135a 100644 --- a/src/lib/webserver.cpp +++ b/src/lib/webserver.cpp @@ -21,6 +21,8 @@ void setupWebserver() server.on("/api/status", HTTP_GET, onApiStatus); server.on("/api/system_status", HTTP_GET, onApiSystemStatus); + server.on("/api/wifi_set_tx_power", HTTP_GET, onApiSetWifiTxPower); + server.on("/api/full_refresh", HTTP_GET, onApiFullRefresh); server.on("/api/action/pause", HTTP_GET, onApiActionPause); @@ -104,6 +106,8 @@ StaticJsonDocument<768> getStatusObject() conStatus["price"] = isPriceNotifyConnected(); conStatus["blocks"] = isBlockNotifyConnected(); + root["rssi"] = WiFi.RSSI(); + return root; } @@ -344,6 +348,27 @@ void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json) } } + if (settings.containsKey("txPower")) { + int txPower = settings["txPower"].as(); + + if (txPower == 80) { + preferences.remove("txPower"); + if (WiFi.getTxPower() != 80) { + ESP.restart(); + } + } else if (static_cast(wifi_power_t::WIFI_POWER_MINUS_1dBm) <= txPower && + txPower <= static_cast(wifi_power_t::WIFI_POWER_19_5dBm)) { + // is valid value + + + if(WiFi.setTxPower(static_cast(txPower))) { + Serial.printf("Set WiFi Tx power to: %d\n", txPower); + preferences.putInt("txPower", txPower); + settingsChanged = true; + } + } + } + request->send(200); if (settingsChanged) { @@ -393,6 +418,7 @@ void onApiSettingsGet(AsyncWebServerRequest *request) root["hostnamePrefix"] = preferences.getString("hostnamePrefix", "btclock"); root["hostname"] = getMyHostname(); root["ip"] = WiFi.localIP(); + root["txPower"] = WiFi.getTxPower(); #ifdef GIT_REV root["gitRev"] = String(GIT_REV); @@ -672,12 +698,42 @@ void onApiSystemStatus(AsyncWebServerRequest *request) root["espHeapSize"] = ESP.getHeapSize(); root["espFreePsram"] = ESP.getFreePsram(); root["espPsramSize"] = ESP.getPsramSize(); + root["rssi"] = WiFi.RSSI(); + root["txPower"] = WiFi.getTxPower(); serializeJson(root, *response); request->send(response); } +#define STRINGIFY(x) #x +#define ENUM_TO_STRING(x) STRINGIFY(x) + + +void onApiSetWifiTxPower(AsyncWebServerRequest *request) +{ + if (request->hasParam("txPower")) { + AsyncWebParameter *txPowerParam = request->getParam("txPower"); + int txPower = txPowerParam->value().toInt(); + if (static_cast(wifi_power_t::WIFI_POWER_MINUS_1dBm) <= txPower && + txPower <= static_cast(wifi_power_t::WIFI_POWER_19_5dBm)) { + // is valid value + String txPowerName = std::to_string(static_cast>(txPower)).c_str(); + + Serial.printf("Set WiFi Tx power to: %s\n", txPowerName); + + if(WiFi.setTxPower(static_cast(txPower))) { + preferences.putInt("txPower", txPower); + request->send(200, "application/json", "{\"setTxPower\": \"ok\"}"); + return; + } + } + } + + return request->send(400); +} + + void onApiLightsStatus(AsyncWebServerRequest *request) { AsyncResponseStream *response = request->beginResponseStream("application/json"); diff --git a/src/lib/webserver.hpp b/src/lib/webserver.hpp index 82e89cc..ca7bc46 100644 --- a/src/lib/webserver.hpp +++ b/src/lib/webserver.hpp @@ -5,6 +5,7 @@ #include #include #include "AsyncJson.h" +#include #include "lib/block_notify.hpp" #include "lib/price_notify.hpp" @@ -21,6 +22,7 @@ bool processEpdColorSettings(AsyncWebServerRequest *request); void onApiStatus(AsyncWebServerRequest *request); void onApiSystemStatus(AsyncWebServerRequest *request); +void onApiSetWifiTxPower(AsyncWebServerRequest *request); void onApiShowScreen(AsyncWebServerRequest *request); void onApiShowText(AsyncWebServerRequest *request);