Add additional settings to AP configuration (#215)

* add wifi settings option to set AP visible/non visible

* add add channel and max client settings

Co-authored-by: lukas <lukas.heiligenbrunner@gmail.com>
This commit is contained in:
rjwats 2021-01-03 18:51:11 +00:00 committed by GitHub
parent e771ab134a
commit 3ecdc27550
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 7 deletions

View File

@ -15,6 +15,9 @@ build_flags =
-D FACTORY_AP_PROVISION_MODE=AP_MODE_DISCONNECTED
-D FACTORY_AP_SSID=\"ESP8266-React-#{unique_id}\" ; 1-64 characters, supports placeholders
-D FACTORY_AP_PASSWORD=\"esp-react\" ; 8-64 characters
-D FACTORY_AP_CHANNEL=1
-D FACTORY_AP_SSID_HIDDEN=false
-D FACTORY_AP_MAX_CLIENTS=4
-D FACTORY_AP_LOCAL_IP=\"192.168.4.1\"
-D FACTORY_AP_GATEWAY_IP=\"192.168.4.1\"
-D FACTORY_AP_SUBNET_MASK=\"255.255.255.0\"

View File

@ -1,14 +1,17 @@
import React, { Fragment } from 'react';
import { TextValidator, ValidatorForm, SelectValidator } from 'react-material-ui-form-validator';
import { TextValidator, ValidatorForm } from 'react-material-ui-form-validator';
import { range } from 'lodash';
import MenuItem from '@material-ui/core/MenuItem';
import Checkbox from '@material-ui/core/Checkbox';
import SaveIcon from '@material-ui/icons/Save';
import { PasswordValidator, RestFormProps, FormActions, FormButton } from '../components';
import { PasswordValidator, RestFormProps, FormActions, FormButton, BlockFormControlLabel } from '../components';
import { isAPEnabled } from './APModes';
import { APSettings, APProvisionMode } from './types';
import { isIP } from '../validators';
import { TextField } from '@material-ui/core';
type APSettingsFormProps = RestFormProps<APSettings>;
@ -22,17 +25,18 @@ class APSettingsForm extends React.Component<APSettingsFormProps> {
const { data, handleValueChange, saveData } = this.props;
return (
<ValidatorForm onSubmit={saveData} ref="APSettingsForm">
<SelectValidator name="provision_mode"
<TextField name="provision_mode"
label="Provide Access Point&hellip;"
value={data.provision_mode}
fullWidth
select
variant="outlined"
onChange={handleValueChange('provision_mode')}
margin="normal">
<MenuItem value={APProvisionMode.AP_MODE_ALWAYS}>Always</MenuItem>
<MenuItem value={APProvisionMode.AP_MODE_DISCONNECTED}>When WiFi Disconnected</MenuItem>
<MenuItem value={APProvisionMode.AP_NEVER}>Never</MenuItem>
</SelectValidator>
</TextField>
{
isAPEnabled(data) &&
<Fragment>
@ -58,6 +62,43 @@ class APSettingsForm extends React.Component<APSettingsFormProps> {
onChange={handleValueChange('password')}
margin="normal"
/>
<TextField name="channel"
label="Preferred Channel"
value={data.channel}
fullWidth
select
type="number"
variant="outlined"
onChange={handleValueChange('channel')}
margin="normal">
{
range(1, 14).map(i => <MenuItem value={i}>{i}</MenuItem>)
}
</TextField>
<BlockFormControlLabel
control={
<Checkbox
value="ssid_hidden"
checked={data.ssid_hidden}
onChange={handleValueChange("ssid_hidden")}
/>
}
label="Hide SSID?"
/>
<TextField name="max_clients"
label="Max Clients"
value={data.max_clients}
fullWidth
select
type="number"
variant="outlined"
onChange={handleValueChange('max_clients')}
margin="normal"
>
{
range(1, 9).map(i => <MenuItem value={i}>{i}</MenuItem>)
}
</TextField>
<TextValidator
validators={['required', 'isIP']}
errorMessages={['Local IP is required', 'Must be an IP address']}

View File

@ -21,6 +21,9 @@ export interface APSettings {
provision_mode: APProvisionMode;
ssid: string;
password: string;
channel: number;
ssid_hidden: boolean;
max_clients: number;
local_ip: string;
gateway_ip: string;
subnet_mask: string;

View File

@ -46,7 +46,7 @@ void APSettingsService::manageAP() {
void APSettingsService::startAP() {
Serial.println(F("Starting software access point"));
WiFi.softAPConfig(_state.localIP, _state.gatewayIP, _state.subnetMask);
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str());
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str(), _state.channel, _state.ssidHidden, _state.maxClients);
if (!_dnsServer) {
IPAddress apIp = WiFi.softAPIP();
Serial.print(F("Starting captive portal on "));

View File

@ -33,15 +33,26 @@
#define FACTORY_AP_SUBNET_MASK "255.255.255.0"
#endif
#ifndef FACTORY_AP_CHANNEL
#define FACTORY_AP_CHANNEL 1
#endif
#ifndef FACTORY_AP_SSID_HIDDEN
#define FACTORY_AP_SSID_HIDDEN false
#endif
#ifndef FACTORY_AP_MAX_CLIENTS
#define FACTORY_AP_MAX_CLIENTS 4
#endif
#define AP_SETTINGS_FILE "/config/apSettings.json"
#define AP_SETTINGS_SERVICE_PATH "/rest/apSettings"
#define MANAGE_NETWORK_DELAY 10000
#define AP_MODE_ALWAYS 0
#define AP_MODE_DISCONNECTED 1
#define AP_MODE_NEVER 2
#define MANAGE_NETWORK_DELAY 10000
#define DNS_PORT 53
enum APNetworkStatus { ACTIVE = 0, INACTIVE, LINGERING };
@ -51,12 +62,17 @@ class APSettings {
uint8_t provisionMode;
String ssid;
String password;
uint8_t channel;
bool ssidHidden;
uint8_t maxClients;
IPAddress localIP;
IPAddress gatewayIP;
IPAddress subnetMask;
bool operator==(const APSettings& settings) const {
return provisionMode == settings.provisionMode && ssid == settings.ssid && password == settings.password &&
channel == settings.channel && ssidHidden == settings.ssidHidden && maxClients == settings.maxClients &&
localIP == settings.localIP && gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask;
}
@ -64,6 +80,9 @@ class APSettings {
root["provision_mode"] = settings.provisionMode;
root["ssid"] = settings.ssid;
root["password"] = settings.password;
root["channel"] = settings.channel;
root["ssid_hidden"] = settings.ssidHidden;
root["max_clients"] = settings.maxClients;
root["local_ip"] = settings.localIP.toString();
root["gateway_ip"] = settings.gatewayIP.toString();
root["subnet_mask"] = settings.subnetMask.toString();
@ -82,6 +101,9 @@ class APSettings {
}
newSettings.ssid = root["ssid"] | SettingValue::format(FACTORY_AP_SSID);
newSettings.password = root["password"] | FACTORY_AP_PASSWORD;
newSettings.channel = root["channel"] | FACTORY_AP_CHANNEL;
newSettings.ssidHidden = root["ssid_hidden"] | FACTORY_AP_SSID_HIDDEN;
newSettings.maxClients = root["max_clients"] | FACTORY_AP_MAX_CLIENTS;
JsonUtils::readIP(root, "local_ip", newSettings.localIP, FACTORY_AP_LOCAL_IP);
JsonUtils::readIP(root, "gateway_ip", newSettings.gatewayIP, FACTORY_AP_GATEWAY_IP);