Skip to content

Commit

Permalink
add BSSID and Channel to network settings, full_scan
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Sep 18, 2023
1 parent 8d4b43e commit 09e2945
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 41 deletions.
44 changes: 40 additions & 4 deletions interface/src/framework/network/NetworkSettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ const WiFiSettingsForm: FC = () => {
if (selectedNetwork) {
updateState('networkSettings', (current_data) => ({
ssid: selectedNetwork.ssid,
password: '',
bssid: selectedNetwork.bssid,
channel: selectedNetwork.channel,
password: current_data ? current_data.password : '',
hostname: current_data?.hostname,
static_ip_config: false,
enableIPv6: false,
Expand Down Expand Up @@ -117,6 +119,12 @@ const WiFiSettingsForm: FC = () => {
} catch (errors: any) {
setFieldErrors(errors);
}
deselectNetwork();
};

const setCancel = async () => {
deselectNetwork();
await loadData();
};

const restart = async () => {
Expand All @@ -139,10 +147,17 @@ const WiFiSettingsForm: FC = () => {
</ListItemAvatar>
<ListItemText
primary={selectedNetwork.ssid}
secondary={'Security: ' + networkSecurityMode(selectedNetwork) + ', Ch: ' + selectedNetwork.channel}
secondary={
'Security: ' +
networkSecurityMode(selectedNetwork) +
', Ch: ' +
selectedNetwork.channel +
', bssid: ' +
selectedNetwork.bssid
}
/>
<ListItemSecondaryAction>
<IconButton onClick={deselectNetwork}>
<IconButton onClick={setCancel}>
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
Expand All @@ -160,6 +175,27 @@ const WiFiSettingsForm: FC = () => {
margin="normal"
/>
)}
<ValidatedTextField
fieldErrors={fieldErrors}
name="bssid"
label={'BSSID (leave blank for SSID only)'}
fullWidth
variant="outlined"
value={data.bssid}
onChange={updateFormValue}
margin="normal"
/>
<ValidatedTextField
fieldErrors={fieldErrors}
name="channel"
label="Channel (0=auto)"
fullWidth
variant="outlined"
value={numberValue(data.channel)}
onChange={updateFormValue}
type="number"
margin="normal"
/>
{(!selectedNetwork || !isNetworkOpen(selectedNetwork)) && (
<ValidatedPasswordField
fieldErrors={fieldErrors}
Expand Down Expand Up @@ -296,7 +332,7 @@ const WiFiSettingsForm: FC = () => {
</MessageBox>
)}

{!restartNeeded && dirtyFlags && dirtyFlags.length !== 0 && (
{!restartNeeded && (selectedNetwork || (dirtyFlags && dirtyFlags.length !== 0)) && (
<ButtonRow>
<Button
startIcon={<CancelIcon />}
Expand Down
4 changes: 3 additions & 1 deletion interface/src/framework/network/WiFiNetworkSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ const WiFiNetworkSelector: FC<WiFiNetworkSelectorProps> = ({ networkList }) => {
</ListItemAvatar>
<ListItemText
primary={network.ssid}
secondary={'Security: ' + networkSecurityMode(network) + ', Ch: ' + network.channel}
secondary={
'Security: ' + networkSecurityMode(network) + ', Ch: ' + network.channel + ', bssid: ' + network.bssid
}
/>
<ListItemIcon>
<Badge badgeContent={network.rssi + 'dBm'}>
Expand Down
2 changes: 2 additions & 0 deletions interface/src/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface NetworkStatus {

export interface NetworkSettings {
ssid: string;
bssid: string;
channel: number;
password: string;
hostname: string;
static_ip_config: boolean;
Expand Down
5 changes: 5 additions & 0 deletions interface/src/validators/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { NetworkSettings } from 'types';
export const createNetworkSettingsValidator = (networkSettings: NetworkSettings) =>
new Schema({
ssid: [{ type: 'string', max: 32, message: 'SSID must be 32 characters or less' }],
bssid: [{ type: 'string', max: 17, message: 'BSSID must be 17 characters or empty' }],
password: { type: 'string', max: 64, message: 'Password must be 64 characters or less' },
hostname: [{ required: true, message: 'Hostname is required' }, HOSTNAME_VALIDATOR],
...(networkSettings.static_ip_config && {
Expand All @@ -17,5 +18,9 @@ export const createNetworkSettingsValidator = (networkSettings: NetworkSettings)
tx_power: [
{ required: true, message: 'Tx Power is required' },
{ type: 'number', min: 0, max: 20, message: 'Tx Power must be between 0 and 20dBm' }
],
channel: [
{ required: true, message: 'Channel is required' },
{ type: 'number', min: 0, max: 13, message: 'Channel must be between 0 and 13' }
]
});
58 changes: 33 additions & 25 deletions lib/framework/NetworkSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ void NetworkSettingsService::begin() {

WiFi.mode(WIFI_MODE_MAX);
WiFi.mode(WIFI_MODE_NULL);
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); // default is FAST_SCAN
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); // is default, no need to set

WiFi.onEvent(std::bind(&NetworkSettingsService::WiFiEvent, this, _1));

Expand Down Expand Up @@ -54,36 +56,42 @@ void NetworkSettingsService::manageSTA() {
}

// Connect or reconnect as required
WiFi.disconnect(true); // turn radio off
WiFiMode_t currentWiFiMode = WiFi.getMode();
if (currentWiFiMode == WIFI_MODE_APSTA || currentWiFiMode == WIFI_MODE_AP) {
WiFi.mode(WIFI_MODE_AP);
} else {
WiFi.mode(WIFI_MODE_NULL);
}
if (_state.staticIPConfig) {
WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2); // configure for static IP
}
WiFi.setHostname(_state.hostname.c_str()); // set hostname
if ((WiFi.getMode() & WIFI_STA) == 0) {
if (_state.staticIPConfig) {
WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2); // configure for static IP
}
WiFi.setHostname(_state.hostname.c_str()); // set hostname

// www.esp32.com/viewtopic.php?t=12055
if (_state.bandwidth20) {
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT20);
} else {
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT40);
}
if (_state.nosleep) {
WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE
}
WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network
// www.esp32.com/viewtopic.php?t=12055
if (_state.bandwidth20) {
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT20);
} else {
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT40);
}
if (_state.nosleep) {
WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE
}
// attempt to connect to the network
uint mac[6];
if (!_state.bssid.isEmpty() && sscanf(_state.bssid.c_str(), "%X:%X:%X:%X:%X:%X", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) == 6) {
uint8_t mac1[6];
for (uint8_t i = 0; i < 6; i++) {
mac1[i] = (uint8_t)mac[i];
}
WiFi.begin(_state.ssid.c_str(), _state.password.c_str(), _state.channel, mac1);
} else {
WiFi.begin(_state.ssid.c_str(), _state.password.c_str(), _state.channel);
}

// set power after wifi is startet, fixed value for C3_V1
// set power after wifi is startet, fixed value for C3_V1
#ifdef BOARD_C3_MINI_V1
// v1 needs this value, see https://github.com/emsesp/EMS-ESP32/pull/620#discussion_r993173979
WiFi.setTxPower(WIFI_POWER_8_5dBm); // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
// v1 needs this value, see https://github.com/emsesp/EMS-ESP32/pull/620#discussion_r993173979
WiFi.setTxPower(WIFI_POWER_8_5dBm); // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
#else
esp_wifi_set_max_tx_power(_state.tx_power * 4);
// esp_wifi_set_max_tx_power(_state.tx_power * 4);
WiFi.setTxPower((wifi_power_t)(_state.tx_power * 4));
#endif
}
}

// handles if wifi stopped
Expand Down
28 changes: 17 additions & 11 deletions lib/framework/NetworkSettingsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@
class NetworkSettings {
public:
// core wifi configuration
String ssid;
String password;
String hostname;
bool staticIPConfig;
bool enableIPv6;
bool bandwidth20;
int8_t tx_power;
bool nosleep;
bool enableMDNS;
bool enableCORS;
String CORSOrigin;
String ssid;
String bssid;
String password;
String hostname;
uint8_t channel;
bool staticIPConfig;
bool enableIPv6;
bool bandwidth20;
int8_t tx_power;
bool nosleep;
bool enableMDNS;
bool enableCORS;
String CORSOrigin;

// optional configuration for static IP address
IPAddress localIP;
Expand All @@ -52,6 +54,8 @@ class NetworkSettings {
static void read(NetworkSettings & settings, JsonObject & root) {
// connection settings
root["ssid"] = settings.ssid;
root["bssid"] = settings.bssid;
root["channel"] = settings.channel;
root["password"] = settings.password;
root["hostname"] = settings.hostname;
root["static_ip_config"] = settings.staticIPConfig;
Expand All @@ -75,6 +79,8 @@ class NetworkSettings {
auto enableCORS = settings.enableCORS;
auto CORSOrigin = settings.CORSOrigin;
settings.ssid = root["ssid"] | FACTORY_WIFI_SSID;
settings.bssid = root["bssid"] | "";
settings.channel = root["channel"] | 0;
settings.password = root["password"] | FACTORY_WIFI_PASSWORD;
settings.hostname = root["hostname"] | FACTORY_WIFI_HOSTNAME;
settings.staticIPConfig = root["static_ip_config"] | false;
Expand Down

0 comments on commit 09e2945

Please sign in to comment.