Skip to content

Commit

Permalink
Made wifi sleep mode configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
gskjold committed Oct 27, 2022
1 parent c38c305 commit 69d8fa9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/AmsConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bool AmsConfiguration::getWiFiConfig(WiFiConfig& config) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_WIFI_START, config);
EEPROM.end();
if(config.sleep > 2) config.sleep = 0xFF;
return true;
} else {
clearWifi(config);
Expand All @@ -33,6 +34,7 @@ bool AmsConfiguration::getWiFiConfig(WiFiConfig& config) {

bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) {
WiFiConfig existing;
if(config.sleep > 2) config.sleep = 0xFF;
if(getWiFiConfig(existing)) {
wifiChanged |= strcmp(config.ssid, existing.ssid) != 0;
wifiChanged |= strcmp(config.psk, existing.psk) != 0;
Expand All @@ -45,6 +47,7 @@ bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) {
}
wifiChanged |= strcmp(config.hostname, existing.hostname) != 0;
wifiChanged |= config.power != existing.power;
wifiChanged |= config.sleep != existing.sleep;
} else {
wifiChanged = true;
}
Expand All @@ -70,6 +73,7 @@ void AmsConfiguration::clearWifi(WiFiConfig& config) {
#endif
strcpy(config.hostname, (String("ams-") + String(chipId, HEX)).c_str());
config.mdns = true;
config.sleep = 0xFF;
}

void AmsConfiguration::clearWifiIp(WiFiConfig& config) {
Expand Down
3 changes: 2 additions & 1 deletion src/AmsConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ struct WiFiConfig {
char hostname[32];
bool mdns;
uint8_t power;
}; // 210
uint8_t sleep;
}; // 211

struct MqttConfig86 {
char host[128];
Expand Down
4 changes: 3 additions & 1 deletion src/AmsToMqttBridge.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,6 @@ void WiFi_connect() {
}
#endif
WiFi.mode(WIFI_STA);
WiFi.setSleep(WIFI_PS_MAX_MODEM);
#if defined(ESP32)
if(wifi.power >= 195)
WiFi.setTxPower(WIFI_POWER_19_5dBm);
Expand Down Expand Up @@ -1115,6 +1114,9 @@ void WiFi_connect() {
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
if(WiFi.begin(wifi.ssid, wifi.psk)) {
if(wifi.sleep <= 2) {
WiFi.setSleep(wifi.sleep);
}
yield();
} else {
if (Debug.isActive(RemoteDebug::ERROR)) debugI("Unable to start WiFi");
Expand Down
4 changes: 4 additions & 0 deletions src/web/AmsWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ void AmsWebServer::configWifiHtml() {
html.replace(F("{h}"), wifi.hostname);
html.replace(F("{m}"), wifi.mdns ? F("checked") : F(""));
html.replace(F("{w}"), String(wifi.power / 10.0, 1));
html.replace(F("{z0}"), wifi.sleep == 0 ? "selected" : "");
html.replace(F("{z1}"), wifi.sleep == 1 ? "selected" : "");
html.replace(F("{z2}"), wifi.sleep == 2 ? "selected" : "");
#if defined(ESP32)
html.replace(F("{wm}"), "19.5");
#elif defined(ESP8266)
Expand Down Expand Up @@ -1256,6 +1259,7 @@ void AmsWebServer::handleSave() {
strcpy(wifi.hostname, server.arg(F("h")).c_str());
}
wifi.power = server.arg(F("w")).toFloat() * 10;
wifi.sleep = server.arg(F("z")).toInt();
config->setWiFiConfig(wifi);
}

Expand Down
26 changes: 20 additions & 6 deletions web/wifi.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,39 @@ <h6>WiFi</h6>
</div>
</div>
<div class="row" id="i">
<div class="col-xl-3 col-lg-4 form-group">
<div class="col-xl-3 col-lg-4 col-md-6 form-group">
<div class="input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">IP</span>
</div>
<input type="text" name="i" class="form-control sip" pattern="\d?\d?\d.\d?\d?\d.\d?\d?\d.\d?\d?\d" value="{i}"/>
</div>
</div>
<div class="col-xl-3 col-lg-4 form-group">
<div class="col-xl-3 col-lg-4 col-md-6 form-group">
<div class="input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">Netmask</span>
</div>
<input type="text" name="sn" class="form-control sip" pattern="\d?\d?\d.\d?\d?\d.\d?\d?\d.\d?\d?\d" value="{sn}"/>
</div>
</div>
<div class="col-xl-3 col-lg-4 form-group">
<div class="col-xl-3 col-lg-4 col-md-6 form-group">
<div class="input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">Gateway</span>
</div>
<input type="text" name="g" class="form-control sip" pattern="\d?\d?\d.\d?\d?\d.\d?\d?\d.\d?\d?\d" value="{g}"/>
</div>
</div>
<div class="col-xl-4 col-lg-5 form-group">
<div class="col-xl-4 col-lg-5 col-md-6 form-group">
<div class="input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">DNS 1</span>
</div>
<input type="text" name="d1" class="form-control sip" pattern="\d?\d?\d.\d?\d?\d.\d?\d?\d.\d?\d?\d" value="{d1}"/>
</div>
</div>
<div class="col-xl-4 col-lg-5 form-group">
<div class="col-xl-4 col-lg-5 col-md-6 form-group">
<div class="input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">DNS 2</span>
Expand All @@ -89,7 +89,21 @@ <h6>WiFi</h6>
</div>
</div>
</div>
</div>

<div class="col-lg-3 col-md-4 col-sm-6 form-group">
<div class="input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">Power saving</span>
</div>
<select name="z" class="form-control">
<option value="255">Default</option>
<option value="0" {z0}>Off</option>
<option value="1" {z1}>Min</option>
<option value="2" {z2}>Max</option>
</select>
</div>
</div>
</div>
</div>
<hr/>
<div class="row form-group">
Expand Down

0 comments on commit 69d8fa9

Please sign in to comment.