Added WiFi signal status and settings

This commit is contained in:
Djuri Baars 2023-11-21 16:12:44 +01:00
parent e29d53574e
commit d6e9610721
5 changed files with 68 additions and 2 deletions

2
data

@ -1 +1 @@
Subproject commit d25284e3a47a9efe6c0a8877e8abeac098ced8af
Subproject commit b38dabec52a55d79e090d3db4b5f2432d075bc0f

View File

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

View File

@ -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<wifi_power_t>(preferences.getInt("txPower", 0)))) {
Serial.printf("WiFi max tx power set to %d\n", preferences.getInt("txPower", 0));
}
}
while (WiFi.status() != WL_CONNECTED)
{

View File

@ -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<int>();
if (txPower == 80) {
preferences.remove("txPower");
if (WiFi.getTxPower() != 80) {
ESP.restart();
}
} else if (static_cast<int>(wifi_power_t::WIFI_POWER_MINUS_1dBm) <= txPower &&
txPower <= static_cast<int>(wifi_power_t::WIFI_POWER_19_5dBm)) {
// is valid value
if(WiFi.setTxPower(static_cast<wifi_power_t>(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<int>(wifi_power_t::WIFI_POWER_MINUS_1dBm) <= txPower &&
txPower <= static_cast<int>(wifi_power_t::WIFI_POWER_19_5dBm)) {
// is valid value
String txPowerName = std::to_string(static_cast<std::underlying_type_t<wifi_power_t>>(txPower)).c_str();
Serial.printf("Set WiFi Tx power to: %s\n", txPowerName);
if(WiFi.setTxPower(static_cast<wifi_power_t>(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");

View File

@ -5,6 +5,7 @@
#include <LittleFS.h>
#include <ESPmDNS.h>
#include "AsyncJson.h"
#include <iostream>
#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);