Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/ConfigHTML.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ const char CONFIG_HTML[] PROGMEM = R"rawliteral(
</div>
</div>
</div>
<div class="toggle-row" style="margin-top:10px">
<div>
<span class="toggle-label">Keep WiFi radio awake</span>
<div style="font-size:11px;color:#71717A;margin-top:4px">Disables modem sleep. Improves RSSI and response time on weak signals, uses slightly more power.</div>
</div>
<label class="toggle-switch">
<input type="checkbox" id="wifi_keep_awake" />
<span class="toggle-track"></span>
</label>
</div>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
</section>

<section>
Expand Down Expand Up @@ -284,6 +294,7 @@ const char CONFIG_HTML[] PROGMEM = R"rawliteral(
});
if (cfg.nfc_reader) document.getElementById('nfc_reader').value = cfg.nfc_reader;
document.getElementById('bambu_dashboard').checked = !!cfg.bambu_dashboard;
document.getElementById('wifi_keep_awake').checked = !!cfg.wifi_keep_awake;
maybeSetValue('moonraker_url', cfg.moonraker_url);
// Password placeholders
if (cfg.wifi_pass_set) document.getElementById('wifi_pass').placeholder = '(set) Leave blank to keep';
Expand Down Expand Up @@ -342,7 +353,8 @@ const char CONFIG_HTML[] PROGMEM = R"rawliteral(
prusalink_on: document.getElementById('prusalink_on').checked ? 1 : 0,
prusalink_url: document.getElementById('prusalink_url').value.trim(),
prusalink_api_key: document.getElementById('prusalink_api_key').value,
bambu_dashboard: document.getElementById('bambu_dashboard').checked ? 1 : 0
bambu_dashboard: document.getElementById('bambu_dashboard').checked ? 1 : 0,
wifi_keep_awake: document.getElementById('wifi_keep_awake').checked ? 1 : 0
};

fetch('/api/config', {
Expand Down
11 changes: 11 additions & 0 deletions src/ConfigurationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static const char* NVS_KEY_NFC_READER = "nfc_reader";
static const char* NVS_KEY_HOSTNAME = "hostname";
static const char* NVS_KEY_LOW_SPOOL = "low_spool_g";
static const char* NVS_KEY_BAMBU_DASH = "bambu_dash";
static const char* NVS_KEY_WIFI_AWAKE = "wifi_awake";

// Sanitize hostname: enforce mDNS naming constraints (lowercase alphanum + hyphens,
// no leading/trailing hyphens) and reject empty strings to avoid boot-time errors.
Expand Down Expand Up @@ -253,6 +254,10 @@ bool ConfigurationManager::loadFromNVS() {
_bambuDashboard = prefs.getBool(NVS_KEY_BAMBU_DASH, false);
anyOverride = true;
}
if (prefs.isKey(NVS_KEY_WIFI_AWAKE)) {
_wifiKeepAwake = prefs.getBool(NVS_KEY_WIFI_AWAKE, false);
anyOverride = true;
}

prefs.end();
return anyOverride;
Expand Down Expand Up @@ -359,6 +364,10 @@ bool ConfigurationManager::isBambuDashboardEnabled() const {
return _bambuDashboard;
}

bool ConfigurationManager::isWifiKeepAwakeEnabled() const {
return _wifiKeepAwake;
}

void ConfigurationManager::getCurrentConfig(ConfigUpdate& out) const {
memset(&out, 0, sizeof(out));
strncpy(out.wifi_ssid, _ssid, sizeof(out.wifi_ssid) - 1);
Expand All @@ -383,6 +392,7 @@ void ConfigurationManager::getCurrentConfig(ConfigUpdate& out) const {
strncpy(out.hostname, _hostname, sizeof(out.hostname) - 1);
out.low_spool_threshold_g = _lowSpoolThreshold;
out.bambu_dashboard = _bambuDashboard ? 1 : 0;
out.wifi_keep_awake = _wifiKeepAwake ? 1 : 0;
}

#ifndef NATIVE_TEST
Expand Down Expand Up @@ -427,6 +437,7 @@ bool ConfigurationManager::saveToNVS(const ConfigUpdate& update) {
prefs.putString(NVS_KEY_HOSTNAME, sanitizedHostname);
prefs.putUShort(NVS_KEY_LOW_SPOOL, update.low_spool_threshold_g);
prefs.putBool(NVS_KEY_BAMBU_DASH, update.bambu_dashboard != 0);
prefs.putBool(NVS_KEY_WIFI_AWAKE, update.wifi_keep_awake != 0);

// Invalidate Spoolman enrichment cache on config change to force re-fetch
// (config change could invalidate cached spool lookups)
Expand Down
6 changes: 6 additions & 0 deletions src/ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct ConfigUpdate {
char hostname[33]; // max 32 chars + null
uint16_t low_spool_threshold_g; // grams below which LED breathes (default 100)
uint8_t bambu_dashboard;
uint8_t wifi_keep_awake; // disable WiFi modem sleep (better RSSI, more power)
};

class ConfigurationManager {
Expand Down Expand Up @@ -89,6 +90,10 @@ class ConfigurationManager {

bool isBambuDashboardEnabled() const;

// When true, firmware calls WiFi.setSleep(false) after WiFi.begin so the
// radio stays fully powered. Trades idle current for better RSSI/latency.
bool isWifiKeepAwakeEnabled() const;

// Web config support
void getCurrentConfig(ConfigUpdate& out) const;
bool saveToNVS(const ConfigUpdate& update);
Expand Down Expand Up @@ -139,6 +144,7 @@ class ConfigurationManager {
char _tftDriver[8] = "st7789";
uint16_t _lowSpoolThreshold = 100; // grams
bool _bambuDashboard = false;
bool _wifiKeepAwake = false;

bool _initialized = false;
};
Expand Down
2 changes: 2 additions & 0 deletions src/WebServerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ void WebServerManager::handleApiGetConfig() {
doc["hostname"] = cfg.hostname;
doc["low_spool_threshold_g"] = cfg.low_spool_threshold_g;
doc["bambu_dashboard"] = cfg.bambu_dashboard;
doc["wifi_keep_awake"] = cfg.wifi_keep_awake;
doc["tft_enabled"] = cfg.tft_enabled;
doc["tft_driver"] = cfg.tft_driver;
doc["ap_mode"] = _apMode;
Expand Down Expand Up @@ -782,6 +783,7 @@ void WebServerManager::handleApiPostConfig() {
sanitizeHostname(update.hostname, sizeof(update.hostname));
update.low_spool_threshold_g = doc["low_spool_threshold_g"] | (uint16_t)100;
update.bambu_dashboard = doc["bambu_dashboard"] | 0;
update.wifi_keep_awake = doc["wifi_keep_awake"] | 0;

if (update.wifi_ssid[0] == '\0') {
sendError(400, "WiFi SSID is required");
Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ void initWiFi() {

WiFi.setHostname(config.getHostname());
WiFi.begin(config.getWiFiSSID(), config.getWiFiPassword());
// Keep-awake disables WIFI_PS_MIN_MODEM (Arduino default). Users on weak
// networks report ~10 dB better RSSI with modem sleep off; costs idle current.
WiFi.setSleep(!config.isWifiKeepAwakeEnabled());

int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 30) {
Expand Down