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 Jan 24, 2019
1 parent 9194357 commit e2d05ed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
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 @@ -548,6 +549,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 @@ -723,6 +725,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 @@ -825,6 +832,7 @@ struct SettingsStruct
MQTTUseUnitNameAsClientId = 0;
VariousBits1 = 0;
OldRulesEngine(DEFAULT_RULES_OLDENGINE);
ForceWiFi_bg_mode(DEFAULT_WIFI_FORCE_BG_MODE);
}

void clearAll() {
Expand Down
28 changes: 28 additions & 0 deletions src/ESPEasyWifi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,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 @@ -637,6 +664,7 @@ bool tryConnectWiFi() {
addLog(LOG_LEVEL_INFO, log);
}
setupStaticIPconfig();
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 @@ -3359,6 +3359,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 @@ -4671,6 +4675,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 @@ -4750,6 +4755,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

2 comments on commit e2d05ed

@clumsy-stefan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TD-er hmm... actually WiFi.setPhyMode() understands 3 modes (B/G/N). Not sure if there is a "combined" mode B/G though... or do you think the setting is always "highest available"? Why not making all three available to select?

@TD-er
Copy link
Owner Author

@TD-er TD-er commented on e2d05ed Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For esp8266 the defines are 1, 2 and 3.
For esp32 these are bits which must be OR'ed
But esp32 only supports

  • B
  • BG
  • BGN

Please sign in to comment.