Skip to content

Commit

Permalink
Merge pull request #484 from sidoh/improvement/bgn_mode
Browse files Browse the repository at this point in the history
Improvement: Change wifi mode setting to allow for selecting 802.11 b, g, or n.
  • Loading branch information
sidoh authored Jul 3, 2019
2 parents 17eb0fd + 7f3d8b5 commit 47662b1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
4 changes: 2 additions & 2 deletions dist/index.html.gz.h

Large diffs are not rendered by default.

29 changes: 27 additions & 2 deletions lib/Settings/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ void Settings::patch(JsonObject parsedSettings) {
this->setIfPresent(parsedSettings, "wifi_static_ip_netmask", wifiStaticIPNetmask);
this->setIfPresent(parsedSettings, "packet_repeats_per_loop", packetRepeatsPerLoop);
this->setIfPresent(parsedSettings, "home_assistant_discovery_prefix", homeAssistantDiscoveryPrefix);
this->setIfPresent(parsedSettings, "wifi_force_b_mode", wifiForceBMode);

if (parsedSettings.containsKey("wifi_mode")) {
this->wifiMode = wifiModeFromString(parsedSettings["wifi_mode"]);
}

if (parsedSettings.containsKey("rf24_channels")) {
JsonArray arr = parsedSettings["rf24_channels"];
Expand Down Expand Up @@ -284,7 +287,7 @@ void Settings::serialize(Print& stream, const bool prettyPrint) {
root["wifi_static_ip_netmask"] = this->wifiStaticIPNetmask;
root["packet_repeats_per_loop"] = this->packetRepeatsPerLoop;
root["home_assistant_discovery_prefix"] = this->homeAssistantDiscoveryPrefix;
root["wifi_force_b_mode"] = this->wifiForceBMode;
root["wifi_mode"] = wifiModeToString(this->wifiMode);

JsonArray channelArr = root.createNestedArray("rf24_channels");
JsonHelpers::vectorToJsonArr<RF24Channel, String>(channelArr, rf24Channels, RF24ChannelHelpers::nameFromValue);
Expand Down Expand Up @@ -349,4 +352,26 @@ String Settings::typeToString(RadioInterfaceType type) {
default:
return "nRF24";
}
}

WifiMode Settings::wifiModeFromString(const String& mode) {
if (mode.equalsIgnoreCase("b")) {
return WifiMode::B;
} else if (mode.equalsIgnoreCase("g")) {
return WifiMode::G;
} else {
return WifiMode::N;
}
}

String Settings::wifiModeToString(WifiMode mode) {
switch (mode) {
case WifiMode::B:
return "b";
case WifiMode::G:
return "g";
case WifiMode::N:
default:
return "n";
}
}
11 changes: 9 additions & 2 deletions lib/Settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ enum RadioInterfaceType {
LT8900 = 1,
};

enum class WifiMode {
B, G, N
};

static const std::vector<GroupStateField> DEFAULT_GROUP_STATE_FIELDS({
GroupStateField::STATE,
GroupStateField::BRIGHTNESS,
Expand Down Expand Up @@ -113,7 +117,7 @@ class Settings {
groupStateFields(DEFAULT_GROUP_STATE_FIELDS),
rf24ListenChannel(RF24Channel::RF24_LOW),
packetRepeatsPerLoop(10),
wifiForceBMode(false),
wifiMode(WifiMode::N),
_autoRestartPeriod(0)
{ }

Expand Down Expand Up @@ -186,14 +190,17 @@ class Settings {
std::map<String, BulbId> groupIdAliases;
std::map<uint32_t, BulbId> deletedGroupIdAliases;
String homeAssistantDiscoveryPrefix;
bool wifiForceBMode;
WifiMode wifiMode;

protected:
size_t _autoRestartPeriod;

void parseGroupIdAliases(JsonObject json);
void dumpGroupIdAliases(JsonObject json);

static WifiMode wifiModeFromString(const String& mode);
static String wifiModeToString(WifiMode mode);

template <typename T>
void setIfPresent(JsonObject obj, const char* key, T& var) {
if (obj.containsKey(key)) {
Expand Down
16 changes: 12 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,19 @@ void applySettings() {

WiFi.hostname(settings.hostname);

if (settings.wifiForceBMode) {
WiFi.setPhyMode(WIFI_PHY_MODE_11B);
} else {
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
WiFiPhyMode_t wifiMode;
switch (settings.wifiMode) {
case WifiMode::B:
wifiMode = WIFI_PHY_MODE_11B;
break;
case WifiMode::G:
wifiMode = WIFI_PHY_MODE_11G;
break;
case WifiMode::N:
wifiMode = WIFI_PHY_MODE_11N;
break;
}
WiFi.setPhyMode(wifiMode);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions web/src/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ var UI_FIELDS = [ {
type: "string",
tab: "tab-wifi"
}, {
tag: "wifi_force_b_mode",
friendly: "Force 802.11b Mode",
help: "If true, force the ESP8266 into 802.11b mode. Try this if you're having wifi stability problems.",
tag: "wifi_mode",
friendly: "WiFi Mode",
help: "Try using G mode if you're having stability problems",
type: "option_buttons",
options: {
'true': 'True',
'false': 'False'
'b': 'B',
'g': 'G',
'n': 'N'
},
tab: "tab-wifi"
}, {
Expand Down

0 comments on commit 47662b1

Please sign in to comment.