Skip to content

Commit

Permalink
[WiFi] Allow force to B/G to improve WiFi stability.
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Feb 1, 2019
1 parent 4890d13 commit 6643eb4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/ESPEasy-Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#define DEFAULT_IP_BLOCK_LEVEL 1 // 0: ALL_ALLOWED 1: LOCAL_SUBNET_ALLOWED 2: ONLY_IP_RANGE_ALLOWED

#define DEFAULT_WIFI_CONNECTION_TIMEOUT 10000 // minimum timeout in ms for WiFi to be connected.
#define DEFAULT_WIFI_FORCE_BG_MODE false // when set, only allow to connect in 802.11B or G mode (not N)

// --- Default Controller ------------------------------------------------------------------------------
#define DEFAULT_CONTROLLER false // true or false enabled or disabled, set 1st controller defaults
Expand Down Expand Up @@ -550,6 +551,7 @@ bool showSettingsFileLayout = false;
#include <WebServer.h>
#include "SPIFFS.h"
#include <rom/rtc.h>
#include "esp_wifi.h" // Needed to call ESP-IDF functions like esp_wifi_....
WebServer WebServer(80);
#ifdef FEATURE_MDNS
#include <ESPmDNS.h>
Expand Down Expand Up @@ -725,6 +727,11 @@ struct SettingsStruct
bool OldRulesEngine() { return !getBitFromUL(VariousBits1, 3); }
void OldRulesEngine(bool value) { setBitToUL(VariousBits1, 3, !value); }

bool ForceWiFi_bg_mode() { return getBitFromUL(VariousBits1, 4); }
void ForceWiFi_bg_mode(bool value) { setBitToUL(VariousBits1, 4, value); }




void validate() {
if (UDPPort > 65535) UDPPort = 0;
Expand Down Expand Up @@ -827,6 +834,7 @@ struct SettingsStruct
MQTTUseUnitNameAsClientId = 0;
VariousBits1 = 0;
OldRulesEngine(DEFAULT_RULES_OLDENGINE);
ForceWiFi_bg_mode(DEFAULT_WIFI_FORCE_BG_MODE);
}

void clearAll() {
Expand Down
29 changes: 28 additions & 1 deletion src/ESPEasyWifi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,33 @@ bool wifiConnectTimeoutReached() {
return timeOutReached(last_wifi_connect_attempt_moment + DEFAULT_WIFI_CONNECTION_TIMEOUT + randomOffset_in_sec);
}

void setConnectionSpeed() {
#ifdef ESP8266
if (Settings.ForceWiFi_bg_mode() || wifi_connect_attempt > 10) {
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
} else {
WiFi.setPhyMode(WIFI_PHY_MODE_11N);
}
#endif

// Does not (yet) work, so commented out.
#ifdef ESP32
uint8_t protocol = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G; // Default to BG
if (!Settings.ForceWiFi_bg_mode() || wifi_connect_attempt > 10) {
// Set to use BGN
protocol |= WIFI_PROTOCOL_11N;
}
if (WifiIsSTA(WiFi.getMode())) {
esp_wifi_set_protocol(WIFI_IF_STA, protocol);
}
if (WifiIsAP(WiFi.getMode())) {
esp_wifi_set_protocol(WIFI_IF_AP, protocol);
}
#endif


}

void setupStaticIPconfig() {
setUseStaticIP(useStaticIP());
if (!useStaticIP()) return;
Expand Down Expand Up @@ -641,7 +668,7 @@ bool tryConnectWiFi() {
addLog(LOG_LEVEL_INFO, log);
}
setupStaticIPconfig();
// WiFi.setPhyMode(WIFI_PHY_MODE_11G); // SMY: uncomment to force 802.11g for use with MikroTik AP's.
setConnectionSpeed();
last_wifi_connect_attempt_moment = millis();
switch (wifi_connect_attempt) {
case 0:
Expand Down
12 changes: 12 additions & 0 deletions src/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3363,6 +3363,10 @@ void html_B(const String& text) {
wrap_html_tag("b", text);
}

void html_I(const String& text) {
wrap_html_tag("i", text);
}

void html_U(const String& text) {
wrap_html_tag("u", text);
}
Expand Down Expand Up @@ -4677,6 +4681,7 @@ void handle_advanced() {
Settings.Latitude = getFormItemFloat(F("latitude"));
Settings.Longitude = getFormItemFloat(F("longitude"));
Settings.OldRulesEngine(isFormItemChecked(F("oldrulesengine")));
Settings.ForceWiFi_bg_mode(isFormItemChecked(F("forcewifi_bg")));

addHtmlError(SaveSettings());
if (systemTimePresent())
Expand Down Expand Up @@ -4756,6 +4761,13 @@ void handle_advanced() {
addFormCheckBox_disabled(F("Use SSDP"), F("usessdp"), Settings.UseSSDP);

addFormNumericBox(F("Connection Failure Threshold"), F("cft"), Settings.ConnectionFailuresThreshold, 0, 100);
#ifdef ESP8266
addFormCheckBox(F("Force WiFi B/G"), F("forcewifi_bg"), Settings.ForceWiFi_bg_mode());
#endif
#ifdef ESP32
// Disabled for now, since it is not working properly.
addFormCheckBox_disabled(F("Force WiFi B/G"), F("forcewifi_bg"), Settings.ForceWiFi_bg_mode());
#endif

addFormNumericBox(F("I2C ClockStretchLimit"), F("wireclockstretchlimit"), Settings.WireClockStretchLimit); //TODO define limits
#if defined(FEATURE_ARDUINO_OTA)
Expand Down

0 comments on commit 6643eb4

Please sign in to comment.