add Ethernet

This commit is contained in:
proddy
2021-01-18 19:38:22 +01:00
parent b81435e713
commit 44045ae658
60 changed files with 905 additions and 973 deletions

View File

@@ -5,6 +5,7 @@ See https://github.com/proddy/EMS-ESP/issues/632
### Added
- Power settings for ESP32
- Rx and Tx counts to Heartbeat MQTT payload
- Ethernet support (ESP32)
### Fixed
- telegrams matched to masterthermostat 0x18

View File

@@ -1,28 +1,21 @@
; ESP32 with debugger
; do not build web
; builds with DEBUG and TEST
[platformio]
default_envs = esp32
[env]
upload_port = COM3
; upload_protocol = espota
; upload_flags =
; --port=8266
; --auth=ems-esp-neo
; upload_port = ems-esp.local
; upload_port = COM3
[common]
debug_flags = -DEMSESP_DEBUG -DEMSESP_TEST -DEMSESP_FORCE_SERIAL
; debug_flags = -DENABLE_CORS -DEMSESP_DEBUG -DEMSESP_TEST
debug_flags = -DEMSESP_DEBUG -DEMSESP_TEST
; debug_flags = -DEMSESP_TEST
upload_protocol = espota
upload_flags =
--port=8266
--auth=ems-esp-neo
upload_port = ems-esp.local
[env:esp32]
monitor_filters = esp32_exception_decoder
debug_tool = esp-prog
debug_init_break = tbreak setup
build_type = debug
extra_scripts =
; pre:scripts/build_interface.py
; pre:scripts/build_interface.py
scripts/rename_fw.py

View File

@@ -1,23 +0,0 @@
; ESP8266
; local example the does not build the web UI
[platformio]
default_envs = esp8266
[common]
; debug_flags = -DENABLE_CORS -DEMSESP_TEST
[env]
; upload_port = COM3
upload_protocol = espota
upload_flags =
--port=8266
--auth=ems-esp-neo
upload_port = ems-esp.local
[env:esp8266]
extra_scripts =
; pre:scripts/build_interface.py
scripts/main_script.py
scripts/rename_fw.py

View File

@@ -2,8 +2,8 @@
# Remember to also enable CORS in platformio.ini before uploading the code to the device.
# ESP32 dev
REACT_APP_HTTP_ROOT=http://10.10.10.194
REACT_APP_WEB_SOCKET_ROOT=ws://10.10.10.194
REACT_APP_HTTP_ROOT=http://192.168.1.32
REACT_APP_WEB_SOCKET_ROOT=ws://192.168.1.32
# ESP8266 dev
#REACT_APP_HTTP_ROOT=http://10.10.10.140

View File

@@ -8,7 +8,7 @@ import AuthenticatedRoute from './authentication/AuthenticatedRoute';
import SignIn from './SignIn';
import ProjectRouting from './project/ProjectRouting';
import WiFiConnection from './wifi/WiFiConnection';
import NetworkConnection from './network/NetworkConnection';
import AccessPoint from './ap/AccessPoint';
import NetworkTime from './ntp/NetworkTime';
import Security from './security/Security';
@@ -19,8 +19,7 @@ import Mqtt from './mqtt/Mqtt';
import { withFeatures, WithFeaturesProps } from './features/FeaturesContext';
import { Features } from './features/types';
export const getDefaultRoute = (features: Features) => features.project ? `/${PROJECT_PATH}/` : "/wifi/";
export const getDefaultRoute = (features: Features) => features.project ? `/${PROJECT_PATH}/` : "/network/";
class AppRouting extends Component<WithFeaturesProps> {
componentDidMount() {
@@ -38,7 +37,7 @@ class AppRouting extends Component<WithFeaturesProps> {
{features.project && (
<AuthenticatedRoute exact path={`/${PROJECT_PATH}/*`} component={ProjectRouting} />
)}
<AuthenticatedRoute exact path="/wifi/*" component={WiFiConnection} />
<AuthenticatedRoute exact path="/network/*" component={NetworkConnection} />
<AuthenticatedRoute exact path="/ap/*" component={AccessPoint} />
{features.ntp && (
<AuthenticatedRoute exact path="/ntp/*" component={NetworkTime} />

View File

@@ -8,8 +8,8 @@ export const AP_SETTINGS_ENDPOINT = ENDPOINT_ROOT + "apSettings";
export const AP_STATUS_ENDPOINT = ENDPOINT_ROOT + "apStatus";
export const SCAN_NETWORKS_ENDPOINT = ENDPOINT_ROOT + "scanNetworks";
export const LIST_NETWORKS_ENDPOINT = ENDPOINT_ROOT + "listNetworks";
export const WIFI_SETTINGS_ENDPOINT = ENDPOINT_ROOT + "wifiSettings";
export const WIFI_STATUS_ENDPOINT = ENDPOINT_ROOT + "wifiStatus";
export const NETWORK_SETTINGS_ENDPOINT = ENDPOINT_ROOT + "networkSettings";
export const NETWORK_STATUS_ENDPOINT = ENDPOINT_ROOT + "networkStatus";
export const OTA_SETTINGS_ENDPOINT = ENDPOINT_ROOT + "otaSettings";
export const UPLOAD_FIRMWARE_ENDPOINT = ENDPOINT_ROOT + "uploadFirmware";
export const MQTT_SETTINGS_ENDPOINT = ENDPOINT_ROOT + "mqttSettings";

View File

@@ -144,11 +144,11 @@ class MenuAppBar extends React.Component<MenuAppBarProps, MenuAppBarState> {
)}
<List>
<ListItem to='/wifi/' selected={path.startsWith('/wifi/')} button component={Link}>
<ListItem to='/network/' selected={path.startsWith('/network/')} button component={Link}>
<ListItemIcon>
<WifiIcon />
</ListItemIcon>
<ListItemText primary="WiFi Connection" />
<ListItemText primary="Network Connection" />
</ListItem>
<ListItem to='/ap/' selected={path.startsWith('/ap/')} button component={Link}>
<ListItemIcon>

View File

@@ -0,0 +1,62 @@
import React, { Component } from 'react';
import { Redirect, Switch, RouteComponentProps } from 'react-router-dom'
import { Tabs, Tab } from '@material-ui/core';
import { withAuthenticatedContext, AuthenticatedContextProps, AuthenticatedRoute } from '../authentication';
import { MenuAppBar } from '../components';
import NetworkStatusController from './NetworkStatusController';
import NetworkSettingsController from './NetworkSettingsController';
import WiFiNetworkScanner from './WiFiNetworkScanner';
import { NetworkConnectionContext } from './NetworkConnectionContext';
import { WiFiNetwork } from './types';
type NetworkConnectionProps = AuthenticatedContextProps & RouteComponentProps;
class NetworkConnection extends Component<NetworkConnectionProps, NetworkConnectionContext> {
constructor(props: NetworkConnectionProps) {
super(props);
this.state = {
selectNetwork: this.selectNetwork,
deselectNetwork: this.deselectNetwork
};
}
selectNetwork = (network: WiFiNetwork) => {
this.setState({ selectedNetwork: network });
this.props.history.push('/network/settings');
}
deselectNetwork = () => {
this.setState({ selectedNetwork: undefined });
}
handleTabChange = (event: React.ChangeEvent<{}>, path: string) => {
this.props.history.push(path);
};
render() {
const { authenticatedContext } = this.props;
return (
<NetworkConnectionContext.Provider value={this.state}>
<MenuAppBar sectionTitle="Network Connection">
<Tabs value={this.props.match.url} onChange={this.handleTabChange} variant="fullWidth">
<Tab value="/network/status" label="Network Status" />
<Tab value="/network/scan" label="Scan Networks" disabled={!authenticatedContext.me.admin} />
<Tab value="/network/settings" label="Network Settings" disabled={!authenticatedContext.me.admin} />
</Tabs>
<Switch>
<AuthenticatedRoute exact path="/network/status" component={NetworkStatusController} />
<AuthenticatedRoute exact path="/network/scan" component={WiFiNetworkScanner} />
<AuthenticatedRoute exact path="/network/settings" component={NetworkSettingsController} />
<Redirect to="/network/status" />
</Switch>
</MenuAppBar>
</NetworkConnectionContext.Provider>
)
}
}
export default withAuthenticatedContext(NetworkConnection);

View File

@@ -0,0 +1,13 @@
import React from 'react';
import { WiFiNetwork } from './types';
export interface NetworkConnectionContext {
selectedNetwork?: WiFiNetwork;
selectNetwork: (network: WiFiNetwork) => void;
deselectNetwork: () => void;
}
const NetworkConnectionContextDefaultValue = {} as NetworkConnectionContext
export const NetworkConnectionContext = React.createContext(
NetworkConnectionContextDefaultValue
);

View File

@@ -0,0 +1,29 @@
import React, { Component } from 'react';
import { restController, RestControllerProps, RestFormLoader, SectionContent } from '../components';
import NetworkSettingsForm from './NetworkSettingsForm';
import { NETWORK_SETTINGS_ENDPOINT } from '../api';
import { NetworkSettings } from './types';
type NetworkSettingsControllerProps = RestControllerProps<NetworkSettings>;
class NetworkSettingsController extends Component<NetworkSettingsControllerProps> {
componentDidMount() {
this.props.loadData();
}
render() {
return (
<SectionContent title="Network Settings">
<RestFormLoader
{...this.props}
render={formProps => <NetworkSettingsForm {...formProps} />}
/>
</SectionContent>
);
}
}
export default restController(NETWORK_SETTINGS_ENDPOINT, NetworkSettingsController);

View File

@@ -1,5 +1,5 @@
import React, { Fragment } from 'react';
import { TextValidator, ValidatorForm } from 'react-material-ui-form-validator';
import { TextValidator, SelectValidator, ValidatorForm } from 'react-material-ui-form-validator';
import { Checkbox, List, ListItem, ListItemText, ListItemAvatar, ListItemSecondaryAction } from '@material-ui/core';
@@ -9,33 +9,35 @@ import LockIcon from '@material-ui/icons/Lock';
import LockOpenIcon from '@material-ui/icons/LockOpen';
import DeleteIcon from '@material-ui/icons/Delete';
import SaveIcon from '@material-ui/icons/Save';
import MenuItem from '@material-ui/core/MenuItem';
import { RestFormProps, PasswordValidator, BlockFormControlLabel, FormActions, FormButton } from '../components';
import { isIP, isHostname, optional } from '../validators';
import { WiFiConnectionContext } from './WiFiConnectionContext';
import { NetworkConnectionContext } from './NetworkConnectionContext';
import { isNetworkOpen, networkSecurityMode } from './WiFiSecurityModes';
import { WiFiSettings } from './types';
import { NetworkSettings } from './types';
type WiFiStatusFormProps = RestFormProps<WiFiSettings>;
type NetworkStatusFormProps = RestFormProps<NetworkSettings>;
class WiFiSettingsForm extends React.Component<WiFiStatusFormProps> {
class NetworkSettingsForm extends React.Component<NetworkStatusFormProps> {
static contextType = WiFiConnectionContext;
context!: React.ContextType<typeof WiFiConnectionContext>;
static contextType = NetworkConnectionContext;
context!: React.ContextType<typeof NetworkConnectionContext>;
constructor(props: WiFiStatusFormProps, context: WiFiConnectionContext) {
constructor(props: NetworkStatusFormProps, context: NetworkConnectionContext) {
super(props);
const { selectedNetwork } = context;
if (selectedNetwork) {
const wifiSettings: WiFiSettings = {
const networkSettings: NetworkSettings = {
ssid: selectedNetwork.ssid,
password: "",
hostname: props.data.hostname,
ethernet_profile: 0,
static_ip_config: false,
}
props.setData(wifiSettings);
props.setData(networkSettings);
}
}
@@ -58,7 +60,7 @@ class WiFiSettingsForm extends React.Component<WiFiStatusFormProps> {
const { selectedNetwork, deselectNetwork } = this.context;
const { data, handleValueChange, saveData } = this.props;
return (
<ValidatorForm onSubmit={saveData} ref="WiFiSettingsForm">
<ValidatorForm onSubmit={saveData} ref="NetworkSettingsForm">
{
selectedNetwork ?
<List>
@@ -117,6 +119,17 @@ class WiFiSettingsForm extends React.Component<WiFiStatusFormProps> {
onChange={handleValueChange('hostname')}
margin="normal"
/>
<SelectValidator name="ems_bus_id"
label="Ethernet Profile"
value={data.ethernet_profile}
fullWidth
variant="outlined"
onChange={handleValueChange('ethernet_profile')}
margin="normal">
<MenuItem value={0}>None (wifi only)</MenuItem>
<MenuItem value={1}>Profile 1 (LAN8720)</MenuItem>
<MenuItem value={2}>Profile 2 (TLK110)</MenuItem>
</SelectValidator>
<BlockFormControlLabel
control={
<Checkbox
@@ -197,4 +210,4 @@ class WiFiSettingsForm extends React.Component<WiFiStatusFormProps> {
}
}
export default WiFiSettingsForm;
export default NetworkSettingsForm;

View File

@@ -0,0 +1,48 @@
import { Theme } from '@material-ui/core';
import { NetworkStatus, NetworkConnectionStatus } from './types';
export const isConnected = ({ status }: NetworkStatus) => {
return ((status === NetworkConnectionStatus.WIFI_STATUS_CONNECTED) || (status === NetworkConnectionStatus.ETHERNET_STATUS_CONNECTED));
}
export const isWiFi = ({ status }: NetworkStatus) => (status === NetworkConnectionStatus.WIFI_STATUS_CONNECTED)
export const networkStatusHighlight = ({ status }: NetworkStatus, theme: Theme) => {
switch (status) {
case NetworkConnectionStatus.WIFI_STATUS_IDLE:
case NetworkConnectionStatus.WIFI_STATUS_DISCONNECTED:
case NetworkConnectionStatus.WIFI_STATUS_NO_SHIELD:
return theme.palette.info.main;
case NetworkConnectionStatus.WIFI_STATUS_CONNECTED:
case NetworkConnectionStatus.ETHERNET_STATUS_CONNECTED:
return theme.palette.success.main;
case NetworkConnectionStatus.WIFI_STATUS_CONNECT_FAILED:
case NetworkConnectionStatus.WIFI_STATUS_CONNECTION_LOST:
return theme.palette.error.main;
default:
return theme.palette.warning.main;
}
}
export const networkStatus = ({ status }: NetworkStatus) => {
switch (status) {
case NetworkConnectionStatus.WIFI_STATUS_NO_SHIELD:
return "Inactive";
case NetworkConnectionStatus.WIFI_STATUS_IDLE:
return "Idle";
case NetworkConnectionStatus.WIFI_STATUS_NO_SSID_AVAIL:
return "No SSID Available";
case NetworkConnectionStatus.WIFI_STATUS_CONNECTED:
return "Connected (WiFi)";
case NetworkConnectionStatus.ETHERNET_STATUS_CONNECTED:
return "Connected (Ethernet)";
case NetworkConnectionStatus.WIFI_STATUS_CONNECT_FAILED:
return "Connection Failed";
case NetworkConnectionStatus.WIFI_STATUS_CONNECTION_LOST:
return "Connection Lost";
case NetworkConnectionStatus.WIFI_STATUS_DISCONNECTED:
return "Disconnected";
default:
return "Unknown";
}
}

View File

@@ -0,0 +1,29 @@
import React, { Component } from 'react';
import {restController, RestControllerProps, RestFormLoader, SectionContent } from '../components';
import NetworkStatusForm from './NetworkStatusForm';
import { NETWORK_STATUS_ENDPOINT } from '../api';
import { NetworkStatus } from './types';
type NetworkStatusControllerProps = RestControllerProps<NetworkStatus>;
class NetworkStatusController extends Component<NetworkStatusControllerProps> {
componentDidMount() {
this.props.loadData();
}
render() {
return (
<SectionContent title="Network Status">
<RestFormLoader
{...this.props}
render={formProps => <NetworkStatusForm {...formProps} />}
/>
</SectionContent>
);
}
}
export default restController(NETWORK_STATUS_ENDPOINT, NetworkStatusController);

View File

@@ -11,14 +11,14 @@ import DeviceHubIcon from '@material-ui/icons/DeviceHub';
import RefreshIcon from '@material-ui/icons/Refresh';
import { RestFormProps, FormActions, FormButton, HighlightAvatar } from '../components';
import { wifiStatus, wifiStatusHighlight, isConnected } from './WiFiStatus';
import { WiFiStatus } from './types';
import { networkStatus, networkStatusHighlight, isConnected, isWiFi } from './NetworkStatus';
import { NetworkStatus } from './types';
type WiFiStatusFormProps = RestFormProps<WiFiStatus> & WithTheme;
type NetworkStatusFormProps = RestFormProps<NetworkStatus> & WithTheme;
class WiFiStatusForm extends Component<WiFiStatusFormProps> {
class NetworkStatusForm extends Component<NetworkStatusFormProps> {
dnsServers(status: WiFiStatus) {
dnsServers(status: NetworkStatus) {
if (!status.dns_ip_1) {
return "none";
}
@@ -31,15 +31,15 @@ class WiFiStatusForm extends Component<WiFiStatusFormProps> {
<Fragment>
<ListItem>
<ListItemAvatar>
<HighlightAvatar color={wifiStatusHighlight(data, theme)}>
<HighlightAvatar color={networkStatusHighlight(data, theme)}>
<WifiIcon />
</HighlightAvatar>
</ListItemAvatar>
<ListItemText primary="Status" secondary={wifiStatus(data)} />
<ListItemText primary="Status" secondary={networkStatus(data)} />
</ListItem>
<Divider variant="inset" component="li" />
{
isConnected(data) &&
isWiFi(data) &&
<Fragment>
<ListItem>
<ListItemAvatar>
@@ -50,6 +50,10 @@ class WiFiStatusForm extends Component<WiFiStatusFormProps> {
<ListItemText primary="SSID" secondary={data.ssid} />
</ListItem>
<Divider variant="inset" component="li" />
</Fragment>
}
{ isConnected(data) &&
<Fragment>
<ListItem>
<ListItemAvatar>
<Avatar>IP</Avatar>
@@ -114,4 +118,4 @@ class WiFiStatusForm extends Component<WiFiStatusFormProps> {
}
export default withTheme(WiFiStatusForm);
export default withTheme(NetworkStatusForm);

View File

@@ -8,7 +8,7 @@ import LockIcon from '@material-ui/icons/Lock';
import LockOpenIcon from '@material-ui/icons/LockOpen';
import { isNetworkOpen, networkSecurityMode } from './WiFiSecurityModes';
import { WiFiConnectionContext } from './WiFiConnectionContext';
import { NetworkConnectionContext } from './NetworkConnectionContext';
import { WiFiNetwork, WiFiNetworkList } from './types';
interface WiFiNetworkSelectorProps {
@@ -17,8 +17,8 @@ interface WiFiNetworkSelectorProps {
class WiFiNetworkSelector extends Component<WiFiNetworkSelectorProps> {
static contextType = WiFiConnectionContext;
context!: React.ContextType<typeof WiFiConnectionContext>;
static contextType = NetworkConnectionContext;
context!: React.ContextType<typeof NetworkConnectionContext>;
renderNetwork = (network: WiFiNetwork) => {
return (

View File

@@ -1,10 +1,11 @@
export enum WiFiConnectionStatus {
export enum NetworkConnectionStatus {
WIFI_STATUS_IDLE = 0,
WIFI_STATUS_NO_SSID_AVAIL = 1,
WIFI_STATUS_CONNECTED = 3,
WIFI_STATUS_CONNECT_FAILED = 4,
WIFI_STATUS_CONNECTION_LOST = 5,
WIFI_STATUS_DISCONNECTED = 6,
ETHERNET_STATUS_CONNECTED = 10,
WIFI_STATUS_NO_SHIELD = 255
}
@@ -17,8 +18,8 @@ export enum WiFiEncryptionType {
WIFI_AUTH_WPA2_ENTERPRISE = 5
}
export interface WiFiStatus {
status: WiFiConnectionStatus;
export interface NetworkStatus {
status: NetworkConnectionStatus;
local_ip: string;
mac_address: string;
rssi: number;
@@ -31,10 +32,11 @@ export interface WiFiStatus {
dns_ip_2: string;
}
export interface WiFiSettings {
export interface NetworkSettings {
ssid: string;
password: string;
hostname: string;
ethernet_profile: number;
static_ip_config: boolean;
local_ip?: string;
gateway_ip?: string;

View File

@@ -6,14 +6,11 @@ import { List, ListItem, ListItemAvatar, ListItemText } from '@material-ui/core'
import DevicesIcon from '@material-ui/icons/Devices';
import MemoryIcon from '@material-ui/icons/Memory';
import ShowChartIcon from '@material-ui/icons/ShowChart';
import SdStorageIcon from '@material-ui/icons/SdStorage';
import FolderIcon from '@material-ui/icons/Folder';
import DataUsageIcon from '@material-ui/icons/DataUsage';
import AppsIcon from '@material-ui/icons/Apps';
import PowerSettingsNewIcon from '@material-ui/icons/PowerSettingsNew';
import RefreshIcon from '@material-ui/icons/Refresh';
import SettingsBackupRestoreIcon from '@material-ui/icons/SettingsBackupRestore';
import BatteryUnknownIcon from "@material-ui/icons/BatteryUnknown";
import TimerIcon from "@material-ui/icons/Timer";
import { redirectingAuthorizedFetch, AuthenticatedContextProps, withAuthenticatedContext } from '../authentication';
@@ -96,24 +93,6 @@ class SystemStatusForm extends Component<SystemStatusFormProps, SystemStatusForm
</Fragment>)
}
<Divider variant="inset" component="li" />
<ListItem >
<ListItemAvatar>
<Avatar>
<DataUsageIcon />
</Avatar>
</ListItemAvatar>
<ListItemText primary="Sketch (Size / Free)" secondary={formatNumber(data.sketch_size) + ' / ' + formatNumber(data.free_sketch_space) + ' bytes'} />
</ListItem>
<Divider variant="inset" component="li" />
<ListItem >
<ListItemAvatar>
<Avatar>
<SdStorageIcon />
</Avatar>
</ListItemAvatar>
<ListItemText primary="Flash Chip (Size / Speed)" secondary={formatNumber(data.flash_chip_size) + ' bytes / ' + (data.flash_chip_speed / 1000000).toFixed(0) + ' MHz'} />
</ListItem>
<Divider variant="inset" component="li" />
<ListItem >
<ListItemAvatar>
<Avatar>

View File

@@ -8,8 +8,6 @@ interface ESPSystemStatus {
max_alloc_heap: number;
cpu_freq_mhz: number;
free_heap: number;
sketch_size: number;
free_sketch_space: number;
sdk_version: string;
flash_chip_size: number;
flash_chip_speed: number;

View File

@@ -1,62 +0,0 @@
import React, { Component } from 'react';
import { Redirect, Switch, RouteComponentProps } from 'react-router-dom'
import { Tabs, Tab } from '@material-ui/core';
import { withAuthenticatedContext, AuthenticatedContextProps, AuthenticatedRoute } from '../authentication';
import { MenuAppBar } from '../components';
import WiFiStatusController from './WiFiStatusController';
import WiFiSettingsController from './WiFiSettingsController';
import WiFiNetworkScanner from './WiFiNetworkScanner';
import { WiFiConnectionContext } from './WiFiConnectionContext';
import { WiFiNetwork } from './types';
type WiFiConnectionProps = AuthenticatedContextProps & RouteComponentProps;
class WiFiConnection extends Component<WiFiConnectionProps, WiFiConnectionContext> {
constructor(props: WiFiConnectionProps) {
super(props);
this.state = {
selectNetwork: this.selectNetwork,
deselectNetwork: this.deselectNetwork
};
}
selectNetwork = (network: WiFiNetwork) => {
this.setState({ selectedNetwork: network });
this.props.history.push('/wifi/settings');
}
deselectNetwork = () => {
this.setState({ selectedNetwork: undefined });
}
handleTabChange = (event: React.ChangeEvent<{}>, path: string) => {
this.props.history.push(path);
};
render() {
const { authenticatedContext } = this.props;
return (
<WiFiConnectionContext.Provider value={this.state}>
<MenuAppBar sectionTitle="WiFi Connection">
<Tabs value={this.props.match.url} onChange={this.handleTabChange} variant="fullWidth">
<Tab value="/wifi/status" label="WiFi Status" />
<Tab value="/wifi/scan" label="Scan Networks" disabled={!authenticatedContext.me.admin} />
<Tab value="/wifi/settings" label="WiFi Settings" disabled={!authenticatedContext.me.admin} />
</Tabs>
<Switch>
<AuthenticatedRoute exact path="/wifi/status" component={WiFiStatusController} />
<AuthenticatedRoute exact path="/wifi/scan" component={WiFiNetworkScanner} />
<AuthenticatedRoute exact path="/wifi/settings" component={WiFiSettingsController} />
<Redirect to="/wifi/status" />
</Switch>
</MenuAppBar>
</WiFiConnectionContext.Provider>
)
}
}
export default withAuthenticatedContext(WiFiConnection);

View File

@@ -1,13 +0,0 @@
import React from 'react';
import { WiFiNetwork } from './types';
export interface WiFiConnectionContext {
selectedNetwork?: WiFiNetwork;
selectNetwork: (network: WiFiNetwork) => void;
deselectNetwork: () => void;
}
const WiFiConnectionContextDefaultValue = {} as WiFiConnectionContext
export const WiFiConnectionContext = React.createContext(
WiFiConnectionContextDefaultValue
);

View File

@@ -1,29 +0,0 @@
import React, { Component } from 'react';
import { restController, RestControllerProps, RestFormLoader, SectionContent } from '../components';
import WiFiSettingsForm from './WiFiSettingsForm';
import { WIFI_SETTINGS_ENDPOINT } from '../api';
import { WiFiSettings } from './types';
type WiFiSettingsControllerProps = RestControllerProps<WiFiSettings>;
class WiFiSettingsController extends Component<WiFiSettingsControllerProps> {
componentDidMount() {
this.props.loadData();
}
render() {
return (
<SectionContent title="WiFi Settings">
<RestFormLoader
{...this.props}
render={formProps => <WiFiSettingsForm {...formProps} />}
/>
</SectionContent>
);
}
}
export default restController(WIFI_SETTINGS_ENDPOINT, WiFiSettingsController);

View File

@@ -1,41 +0,0 @@
import { Theme } from '@material-ui/core';
import { WiFiStatus, WiFiConnectionStatus } from './types';
export const isConnected = ({ status }: WiFiStatus) => status === WiFiConnectionStatus.WIFI_STATUS_CONNECTED;
export const wifiStatusHighlight = ({ status }: WiFiStatus, theme: Theme) => {
switch (status) {
case WiFiConnectionStatus.WIFI_STATUS_IDLE:
case WiFiConnectionStatus.WIFI_STATUS_DISCONNECTED:
case WiFiConnectionStatus.WIFI_STATUS_NO_SHIELD:
return theme.palette.info.main;
case WiFiConnectionStatus.WIFI_STATUS_CONNECTED:
return theme.palette.success.main;
case WiFiConnectionStatus.WIFI_STATUS_CONNECT_FAILED:
case WiFiConnectionStatus.WIFI_STATUS_CONNECTION_LOST:
return theme.palette.error.main;
default:
return theme.palette.warning.main;
}
}
export const wifiStatus = ({ status }: WiFiStatus) => {
switch (status) {
case WiFiConnectionStatus.WIFI_STATUS_NO_SHIELD:
return "Inactive";
case WiFiConnectionStatus.WIFI_STATUS_IDLE:
return "Idle";
case WiFiConnectionStatus.WIFI_STATUS_NO_SSID_AVAIL:
return "No SSID Available";
case WiFiConnectionStatus.WIFI_STATUS_CONNECTED:
return "Connected";
case WiFiConnectionStatus.WIFI_STATUS_CONNECT_FAILED:
return "Connection Failed";
case WiFiConnectionStatus.WIFI_STATUS_CONNECTION_LOST:
return "Connection Lost";
case WiFiConnectionStatus.WIFI_STATUS_DISCONNECTED:
return "Disconnected";
default:
return "Unknown";
}
}

View File

@@ -1,29 +0,0 @@
import React, { Component } from 'react';
import {restController, RestControllerProps, RestFormLoader, SectionContent } from '../components';
import WiFiStatusForm from './WiFiStatusForm';
import { WIFI_STATUS_ENDPOINT } from '../api';
import { WiFiStatus } from './types';
type WiFiStatusControllerProps = RestControllerProps<WiFiStatus>;
class WiFiStatusController extends Component<WiFiStatusControllerProps> {
componentDidMount() {
this.props.loadData();
}
render() {
return (
<SectionContent title="WiFi Status">
<RestFormLoader
{...this.props}
render={formProps => <WiFiStatusForm {...formProps} />}
/>
</SectionContent>
);
}
}
export default restController(WIFI_STATUS_ENDPOINT, WiFiStatusController);

View File

@@ -20,6 +20,10 @@ void APSettingsService::reconfigureAP() {
}
void APSettingsService::loop() {
// if we have an ETH connection, quit
if (ETH.linkUp()) {
return;
}
unsigned long currentMillis = uuid::get_uptime();
unsigned long manageElapsed = (uint32_t)(currentMillis - _lastManaged);
if (manageElapsed >= MANAGE_NETWORK_DELAY) {
@@ -42,13 +46,13 @@ void APSettingsService::manageAP() {
}
void APSettingsService::startAP() {
// Serial.println(F("Starting software access point"));
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());
if (!_dnsServer) {
IPAddress apIp = WiFi.softAPIP();
// Serial.print(F("Starting captive portal on "));
// Serial.println(apIp);
Serial.print(F("Starting captive portal on "));
Serial.println(apIp);
_dnsServer = new DNSServer;
_dnsServer->start(DNS_PORT, "*", apIp);
}
@@ -56,12 +60,12 @@ void APSettingsService::startAP() {
void APSettingsService::stopAP() {
if (_dnsServer) {
// Serial.println(F("Stopping captive portal"));
Serial.println(F("Stopping captive portal"));
_dnsServer->stop();
delete _dnsServer;
_dnsServer = nullptr;
}
// Serial.println(F("Stopping software access point"));
Serial.println(F("Stopping software access point"));
WiFi.softAPdisconnect(true);
}

View File

@@ -7,8 +7,10 @@
#include <DNSServer.h>
#include <IPAddress.h>
#include <uuid/common.h>
#include <ETH.h>
#include <uuid/common.h>
#define MANAGE_NETWORK_DELAY 10000
@@ -111,7 +113,7 @@ class APSettingsService : public StatefulService<APSettings> {
// for the captive portal
DNSServer * _dnsServer;
// for the mangement delay loop
// for the management delay loop
volatile unsigned long _lastManaged;
volatile boolean _reconfigureAp;

View File

@@ -3,35 +3,19 @@
ESP8266React::ESP8266React(AsyncWebServer * server, FS * fs)
: _featureService(server)
, _securitySettingsService(server, fs)
, _wifiSettingsService(server, fs, &_securitySettingsService)
, _networkSettingsService(server, fs, &_securitySettingsService)
, _wifiScanner(server, &_securitySettingsService)
, _wifiStatus(server, &_securitySettingsService)
, _networkStatus(server, &_securitySettingsService)
, _apSettingsService(server, fs, &_securitySettingsService)
, _apStatus(server, &_securitySettingsService, &_apSettingsService)
,
#if FT_ENABLED(FT_NTP)
_ntpSettingsService(server, fs, &_securitySettingsService)
, _ntpSettingsService(server, fs, &_securitySettingsService)
, _ntpStatus(server, &_securitySettingsService)
,
#endif
#if FT_ENABLED(FT_OTA)
_otaSettingsService(server, fs, &_securitySettingsService)
,
#endif
#if FT_ENABLED(FT_UPLOAD_FIRMWARE)
_uploadFirmwareService(server, &_securitySettingsService)
,
#endif
#if FT_ENABLED(FT_MQTT)
_mqttSettingsService(server, fs, &_securitySettingsService)
, _otaSettingsService(server, fs, &_securitySettingsService)
, _uploadFirmwareService(server, &_securitySettingsService)
, _mqttSettingsService(server, fs, &_securitySettingsService)
, _mqttStatus(server, &_mqttSettingsService, &_securitySettingsService)
,
#endif
#if FT_ENABLED(FT_SECURITY)
_authenticationService(server, &_securitySettingsService)
,
#endif
_restartService(server, &_securitySettingsService)
, _authenticationService(server, &_securitySettingsService)
, _restartService(server, &_securitySettingsService)
, _factoryResetService(server, fs, &_securitySettingsService)
, _systemStatus(server, &_securitySettingsService) {
#ifdef PROGMEM_WWW
@@ -91,29 +75,17 @@ ESP8266React::ESP8266React(AsyncWebServer * server, FS * fs)
}
void ESP8266React::begin() {
_wifiSettingsService.begin();
_networkSettingsService.begin();
_apSettingsService.begin();
#if FT_ENABLED(FT_NTP)
_ntpSettingsService.begin();
#endif
#if FT_ENABLED(FT_OTA)
_otaSettingsService.begin();
#endif
#if FT_ENABLED(FT_MQTT)
_mqttSettingsService.begin();
#endif
#if FT_ENABLED(FT_SECURITY)
_securitySettingsService.begin();
#endif
}
void ESP8266React::loop() {
_wifiSettingsService.loop();
_networkSettingsService.loop();
_apSettingsService.loop();
#if FT_ENABLED(FT_OTA)
_otaSettingsService.loop();
#endif
#if FT_ENABLED(FT_MQTT)
_mqttSettingsService.loop();
#endif
}

View File

@@ -3,13 +3,9 @@
#include <Arduino.h>
#ifdef ESP32
#include <AsyncTCP.h>
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ETH.h>
#include <FeaturesService.h>
#include <APSettingsService.h>
@@ -26,8 +22,8 @@
#include <SecuritySettingsService.h>
#include <SystemStatus.h>
#include <WiFiScanner.h>
#include <WiFiSettingsService.h>
#include <WiFiStatus.h>
#include <NetworkSettingsService.h>
#include <NetworkStatus.h>
#ifdef PROGMEM_WWW
#include <WWWData.h>
@@ -44,33 +40,26 @@ class ESP8266React {
return &_securitySettingsService;
}
#if FT_ENABLED(FT_SECURITY)
StatefulService<SecuritySettings> * getSecuritySettingsService() {
return &_securitySettingsService;
}
#endif
StatefulService<WiFiSettings> * getWiFiSettingsService() {
return &_wifiSettingsService;
StatefulService<NetworkSettings> * getNetworkSettingsService() {
return &_networkSettingsService;
}
StatefulService<APSettings> * getAPSettingsService() {
return &_apSettingsService;
}
#if FT_ENABLED(FT_NTP)
StatefulService<NTPSettings> * getNTPSettingsService() {
return &_ntpSettingsService;
}
#endif
#if FT_ENABLED(FT_OTA)
StatefulService<OTASettings> * getOTASettingsService() {
return &_otaSettingsService;
}
#endif
#if FT_ENABLED(FT_MQTT)
StatefulService<MqttSettings> * getMqttSettingsService() {
return &_mqttSettingsService;
}
@@ -78,7 +67,6 @@ class ESP8266React {
AsyncMqttClient * getMqttClient() {
return _mqttSettingsService.getMqttClient();
}
#endif
void factoryReset() {
_factoryResetService.factoryReset();
@@ -87,28 +75,18 @@ class ESP8266React {
private:
FeaturesService _featureService;
SecuritySettingsService _securitySettingsService;
WiFiSettingsService _wifiSettingsService;
NetworkSettingsService _networkSettingsService;
WiFiScanner _wifiScanner;
WiFiStatus _wifiStatus;
NetworkStatus _networkStatus;
APSettingsService _apSettingsService;
APStatus _apStatus;
#if FT_ENABLED(FT_NTP)
NTPSettingsService _ntpSettingsService;
NTPStatus _ntpStatus;
#endif
#if FT_ENABLED(FT_OTA)
OTASettingsService _otaSettingsService;
#endif
#if FT_ENABLED(FT_UPLOAD_FIRMWARE)
UploadFirmwareService _uploadFirmwareService;
#endif
#if FT_ENABLED(FT_MQTT)
MqttSettingsService _mqttSettingsService;
MqttStatus _mqttStatus;
#endif
#if FT_ENABLED(FT_SECURITY)
AuthenticationService _authenticationService;
#endif
RestartService _restartService;
FactoryResetService _factoryResetService;
SystemStatus _systemStatus;

View File

@@ -40,13 +40,8 @@ MqttSettingsService::MqttSettingsService(AsyncWebServer * server, FS * fs, Secur
, _disconnectedAt(0)
, _disconnectReason(AsyncMqttClientDisconnectReason::TCP_DISCONNECTED)
, _mqttClient() {
#ifdef ESP32
WiFi.onEvent(std::bind(&MqttSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent(std::bind(&MqttSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#elif defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&MqttSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&MqttSettingsService::onStationModeGotIP, this, std::placeholders::_1));
#endif
_mqttClient.onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, std::placeholders::_1));
_mqttClient.onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, std::placeholders::_1));
addUpdateHandler([&](const String & originId) { onConfigUpdated(); }, false);

View File

@@ -140,16 +140,8 @@ class MqttSettingsService : public StatefulService<MqttSettings> {
// the MQTT client instance
AsyncMqttClient _mqttClient;
#ifdef ESP32
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
#elif defined(ESP8266)
WiFiEventHandler _onStationModeDisconnectedHandler;
WiFiEventHandler _onStationModeGotIPHandler;
void onStationModeGotIP(const WiFiEventStationModeGotIP & event);
void onStationModeDisconnected(const WiFiEventStationModeDisconnected & event);
#endif
void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void configureMqtt();

View File

@@ -3,20 +3,13 @@
NTPSettingsService::NTPSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
: _httpEndpoint(NTPSettings::read, NTPSettings::update, this, server, NTP_SETTINGS_SERVICE_PATH, securityManager)
, _fsPersistence(NTPSettings::read, NTPSettings::update, this, fs, NTP_SETTINGS_FILE)
, _timeHandler(TIME_PATH,
securityManager->wrapCallback(std::bind(&NTPSettingsService::configureTime, this, std::placeholders::_1, std::placeholders::_2),
AuthenticationPredicates::IS_ADMIN)) {
, _timeHandler(TIME_PATH, securityManager->wrapCallback(std::bind(&NTPSettingsService::configureTime, this, std::placeholders::_1, std::placeholders::_2), AuthenticationPredicates::IS_ADMIN)) {
_timeHandler.setMethod(HTTP_POST);
_timeHandler.setMaxContentLength(MAX_TIME_SIZE);
server->addHandler(&_timeHandler);
#ifdef ESP32
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#elif defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
#endif
WiFi.onEvent(std::bind(&NTPSettingsService::WiFiEvent, this, std::placeholders::_1));
addUpdateHandler([&](const String & originId) { configureNTP(); }, false);
}
@@ -25,43 +18,36 @@ void NTPSettingsService::begin() {
configureNTP();
}
#ifdef ESP32
void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
// Serial.println(F("Got IP address, starting NTP Synchronization"));
configureNTP();
}
void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
// handles both WiFI and Ethernet
void NTPSettingsService::WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_STA_DISCONNECTED:
// Serial.println(F("WiFi connection dropped, stopping NTP."));
connected_ = false;
configureNTP();
}
#elif defined(ESP8266)
void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP & event) {
// Serial.println(F("Got IP address, starting NTP Synchronization"));
configureNTP();
}
break;
void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) {
// Serial.println(F("WiFi connection dropped, stopping NTP."));
case SYSTEM_EVENT_STA_GOT_IP:
case SYSTEM_EVENT_ETH_GOT_IP:
// Serial.println(F("Got IP address, starting NTP Synchronization"));
connected_ = true;
configureNTP();
break;
default:
break;
}
}
#endif
void NTPSettingsService::configureNTP() {
if (WiFi.isConnected() && _state.enabled) {
if (connected_ && _state.enabled) {
// Serial.println(F("Starting NTP..."));
#ifdef ESP32
configTzTime(_state.tzFormat.c_str(), _state.server.c_str());
#elif defined(ESP8266)
configTime(_state.tzFormat.c_str(), _state.server.c_str());
#endif
} else {
#ifdef ESP32
setenv("TZ", _state.tzFormat.c_str(), 1);
tzset();
#elif defined(ESP8266)
setTZ(_state.tzFormat.c_str());
#endif
sntp_stop();
}
}

View File

@@ -5,11 +5,8 @@
#include <FSPersistence.h>
#include <time.h>
#ifdef ESP32
#include <lwip/apps/sntp.h>
#elif defined(ESP8266)
#include <sntp.h>
#endif
#include <ETH.h>
#ifndef FACTORY_NTP_ENABLED
#define FACTORY_NTP_ENABLED true
@@ -67,16 +64,8 @@ class NTPSettingsService : public StatefulService<NTPSettings> {
FSPersistence<NTPSettings> _fsPersistence;
AsyncCallbackJsonWebHandler _timeHandler;
#ifdef ESP32
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
#elif defined(ESP8266)
WiFiEventHandler _onStationModeDisconnectedHandler;
WiFiEventHandler _onStationModeGotIPHandler;
void onStationModeGotIP(const WiFiEventStationModeGotIP & event);
void onStationModeDisconnected(const WiFiEventStationModeDisconnected & event);
#endif
bool connected_ = false;
void WiFiEvent(WiFiEvent_t event);
void configureNTP();
void configureTime(AsyncWebServerRequest * request, JsonVariant & json);
};

View File

@@ -0,0 +1,90 @@
#include <NetworkSettingsService.h>
NetworkSettingsService::NetworkSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
: _httpEndpoint(NetworkSettings::read, NetworkSettings::update, this, server, NETWORK_SETTINGS_SERVICE_PATH, securityManager)
, _fsPersistence(NetworkSettings::read, NetworkSettings::update, this, fs, NETWORK_SETTINGS_FILE)
, _lastConnectionAttempt(0) {
// We want the device to come up in opmode=0 (WIFI_OFF), when erasing the flash this is not the default.
// If needed, we save opmode=0 before disabling persistence so the device boots with WiFi disabled in the future.
if (WiFi.getMode() != WIFI_OFF) {
WiFi.mode(WIFI_OFF);
}
// Disable WiFi config persistance and auto reconnect
WiFi.persistent(false);
WiFi.setAutoReconnect(false);
WiFi.mode(WIFI_MODE_MAX);
WiFi.mode(WIFI_MODE_NULL);
WiFi.onEvent(std::bind(&NetworkSettingsService::WiFiEvent, this, std::placeholders::_1));
addUpdateHandler([&](const String & originId) { reconfigureWiFiConnection(); }, false);
}
void NetworkSettingsService::begin() {
_fsPersistence.readFromFS();
reconfigureWiFiConnection();
}
void NetworkSettingsService::reconfigureWiFiConnection() {
// reset last connection attempt to force loop to reconnect immediately
_lastConnectionAttempt = 0;
// disconnect and de-configure wifi
if (WiFi.disconnect(true)) {
_stopping = true;
}
}
void NetworkSettingsService::loop() {
unsigned long currentMillis = millis();
if (!_lastConnectionAttempt || (uint32_t)(currentMillis - _lastConnectionAttempt) >= WIFI_RECONNECTION_DELAY) {
_lastConnectionAttempt = currentMillis;
manageSTA();
}
}
void NetworkSettingsService::manageSTA() {
// Abort if already connected, or if we have no SSID
if (WiFi.isConnected() || _state.ssid.length() == 0) {
return;
}
// Connect or reconnect as required
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
} else {
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); // configure for DHCP
}
WiFi.setHostname(_state.hostname.c_str()); // set hostname
WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network
}
}
// handles both WiFI and Ethernet
void NetworkSettingsService::WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_STA_DISCONNECTED:
WiFi.disconnect(true);
break;
case SYSTEM_EVENT_STA_STOP:
if (_stopping) {
_lastConnectionAttempt = 0;
_stopping = false;
}
break;
case SYSTEM_EVENT_ETH_START:
if (_state.staticIPConfig) {
ETH.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2); // configure for static IP
}
break;
default:
break;
}
}

View File

@@ -1,13 +1,15 @@
#ifndef WiFiSettingsService_h
#define WiFiSettingsService_h
#ifndef NetworkSettingsService_h
#define NetworkSettingsService_h
#include <StatefulService.h>
#include <FSPersistence.h>
#include <HttpEndpoint.h>
#include <JsonUtils.h>
#define WIFI_SETTINGS_FILE "/config/wifiSettings.json"
#define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings"
#include <ETH.h>
#define NETWORK_SETTINGS_FILE "/config/networkSettings.json"
#define NETWORK_SETTINGS_SERVICE_PATH "/rest/networkSettings"
#define WIFI_RECONNECTION_DELAY 1000 * 30
#ifndef FACTORY_WIFI_SSID
@@ -22,13 +24,14 @@
#define FACTORY_WIFI_HOSTNAME ""
#endif
class WiFiSettings {
class NetworkSettings {
public:
// core wifi configuration
String ssid;
String password;
String hostname;
bool staticIPConfig;
uint8_t ethernet_profile;
// optional configuration for static IP address
IPAddress localIP;
@@ -37,12 +40,13 @@ class WiFiSettings {
IPAddress dnsIP1;
IPAddress dnsIP2;
static void read(WiFiSettings & settings, JsonObject & root) {
static void read(NetworkSettings & settings, JsonObject & root) {
// connection settings
root["ssid"] = settings.ssid;
root["password"] = settings.password;
root["hostname"] = settings.hostname;
root["static_ip_config"] = settings.staticIPConfig;
root["ethernet_profile"] = settings.ethernet_profile;
// extended settings
JsonUtils::writeIP(root, "local_ip", settings.localIP);
@@ -52,11 +56,12 @@ class WiFiSettings {
JsonUtils::writeIP(root, "dns_ip_2", settings.dnsIP2);
}
static StateUpdateResult update(JsonObject & root, WiFiSettings & settings) {
static StateUpdateResult update(JsonObject & root, NetworkSettings & settings) {
settings.ssid = root["ssid"] | FACTORY_WIFI_SSID;
settings.password = root["password"] | FACTORY_WIFI_PASSWORD;
settings.hostname = root["hostname"] | FACTORY_WIFI_HOSTNAME;
settings.staticIPConfig = root["static_ip_config"] | false;
settings.ethernet_profile = root["ethernet_profile"] | 0; // no ethernet
// extended settings
JsonUtils::readIP(root, "local_ip", settings.localIP);
@@ -77,33 +82,28 @@ class WiFiSettings {
if (settings.staticIPConfig && (settings.localIP == INADDR_NONE || settings.gatewayIP == INADDR_NONE || settings.subnetMask == INADDR_NONE)) {
settings.staticIPConfig = false;
}
return StateUpdateResult::CHANGED;
}
};
class WiFiSettingsService : public StatefulService<WiFiSettings> {
class NetworkSettingsService : public StatefulService<NetworkSettings> {
public:
WiFiSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
NetworkSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
void begin();
void loop();
private:
HttpEndpoint<WiFiSettings> _httpEndpoint;
FSPersistence<WiFiSettings> _fsPersistence;
HttpEndpoint<NetworkSettings> _httpEndpoint;
FSPersistence<NetworkSettings> _fsPersistence;
unsigned long _lastConnectionAttempt;
#ifdef ESP32
bool _stopping;
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
void onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info);
#elif defined(ESP8266)
WiFiEventHandler _onStationModeDisconnectedHandler;
void onStationModeDisconnected(const WiFiEventStationModeDisconnected & event);
#endif
void WiFiEvent(WiFiEvent_t event);
void reconfigureWiFiConnection();
void manageSTA();
};
#endif // end WiFiSettingsService_h
#endif

View File

@@ -0,0 +1,57 @@
#include <NetworkStatus.h>
NetworkStatus::NetworkStatus(AsyncWebServer * server, SecurityManager * securityManager) {
server->on(NETWORK_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&NetworkStatus::networkStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED));
}
void NetworkStatus::networkStatus(AsyncWebServerRequest * request) {
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_NETWORK_STATUS_SIZE);
JsonObject root = response->getRoot();
bool ethernet_connected = ETH.linkUp();
wl_status_t wifi_status = WiFi.status();
// see if Ethernet is connected
if (ethernet_connected) {
root["status"] = 10; // custom code #10 - ETHERNET_STATUS_CONNECTED
} else {
root["status"] = (uint8_t)wifi_status;
}
// for Wifi
if (wifi_status == WL_CONNECTED) {
root["local_ip"] = WiFi.localIP().toString();
root["mac_address"] = WiFi.macAddress();
root["rssi"] = WiFi.RSSI();
root["ssid"] = WiFi.SSID();
root["bssid"] = WiFi.BSSIDstr();
root["channel"] = WiFi.channel();
root["subnet_mask"] = WiFi.subnetMask().toString();
root["gateway_ip"] = WiFi.gatewayIP().toString();
IPAddress dnsIP1 = WiFi.dnsIP(0);
IPAddress dnsIP2 = WiFi.dnsIP(1);
if (dnsIP1 != INADDR_NONE) {
root["dns_ip_1"] = dnsIP1.toString();
}
if (dnsIP2 != INADDR_NONE) {
root["dns_ip_2"] = dnsIP2.toString();
}
} else if (ETH.linkUp()) {
// Ethernet
root["local_ip"] = ETH.localIP().toString();
root["mac_address"] = ETH.macAddress();
root["subnet_mask"] = ETH.subnetMask().toString();
root["gateway_ip"] = ETH.gatewayIP().toString();
IPAddress dnsIP1 = ETH.dnsIP(0);
IPAddress dnsIP2 = ETH.dnsIP(1);
if (dnsIP1 != INADDR_NONE) {
root["dns_ip_1"] = dnsIP1.toString();
}
if (dnsIP2 != INADDR_NONE) {
root["dns_ip_2"] = dnsIP2.toString();
}
}
response->setLength();
request->send(response);
}

View File

@@ -0,0 +1,25 @@
#ifndef NetworkStatus_h
#define NetworkStatus_h
#include <WiFi.h>
#include <ETH.h>
#include <AsyncTCP.h>
#include <ArduinoJson.h>
#include <AsyncJson.h>
#include <ESPAsyncWebServer.h>
#include <IPAddress.h>
#include <SecurityManager.h>
#define MAX_NETWORK_STATUS_SIZE 1024
#define NETWORK_STATUS_SERVICE_PATH "/rest/networkStatus"
class NetworkStatus {
public:
NetworkStatus(AsyncWebServer * server, SecurityManager * securityManager);
private:
void networkStatus(AsyncWebServerRequest * request);
};
#endif

View File

@@ -1,44 +1,25 @@
#include <SystemStatus.h>
SystemStatus::SystemStatus(AsyncWebServer * server, SecurityManager * securityManager) {
server->on(SYSTEM_STATUS_SERVICE_PATH,
HTTP_GET,
securityManager->wrapRequest(std::bind(&SystemStatus::systemStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED));
server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&SystemStatus::systemStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED));
}
void SystemStatus::systemStatus(AsyncWebServerRequest * request) {
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_ESP_STATUS_SIZE);
JsonObject root = response->getRoot();
#ifdef ESP32
root["esp_platform"] = "esp32";
root["max_alloc_heap"] = ESP.getMaxAllocHeap();
root["psram_size"] = ESP.getPsramSize();
root["free_psram"] = ESP.getFreePsram();
#elif defined(ESP8266)
root["esp_platform"] = "esp8266";
root["max_alloc_heap"] = ESP.getMaxFreeBlockSize();
root["heap_fragmentation"] = ESP.getHeapFragmentation();
#endif
root["cpu_freq_mhz"] = ESP.getCpuFreqMHz();
root["free_heap"] = ESP.getFreeHeap();
root["sketch_size"] = ESP.getSketchSize();
root["free_sketch_space"] = ESP.getFreeSketchSpace();
root["sdk_version"] = ESP.getSdkVersion();
root["flash_chip_size"] = ESP.getFlashChipSize();
root["flash_chip_speed"] = ESP.getFlashChipSpeed();
// ESP8266 and ESP32 do not have feature parity in FS.h which currently makes that difficult.
#ifdef ESP32
root["fs_total"] = SPIFFS.totalBytes();
root["fs_used"] = SPIFFS.usedBytes();
#elif defined(ESP8266)
FSInfo fs_info;
LittleFS.info(fs_info); // // proddy added
root["fs_total"] = fs_info.totalBytes;
root["fs_used"] = fs_info.usedBytes;
#endif
root["uptime"] = uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3); // proddy added
root["uptime"] = uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3);
response->setLength();
request->send(response);

View File

@@ -1,12 +1,8 @@
#include <WiFiScanner.h>
WiFiScanner::WiFiScanner(AsyncWebServer * server, SecurityManager * securityManager) {
server->on(SCAN_NETWORKS_SERVICE_PATH,
HTTP_GET,
securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN));
server->on(LIST_NETWORKS_SERVICE_PATH,
HTTP_GET,
securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN));
server->on(SCAN_NETWORKS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN));
server->on(LIST_NETWORKS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN));
};
void WiFiScanner::scanNetworks(AsyncWebServerRequest * request) {
@@ -29,11 +25,7 @@ void WiFiScanner::listNetworks(AsyncWebServerRequest * request) {
network["ssid"] = WiFi.SSID(i);
network["bssid"] = WiFi.BSSIDstr(i);
network["channel"] = WiFi.channel(i);
#ifdef ESP32
network["encryption_type"] = (uint8_t)WiFi.encryptionType(i);
#elif defined(ESP8266)
network["encryption_type"] = convertEncryptionType(WiFi.encryptionType(i));
#endif
}
response->setLength();
request->send(response);
@@ -43,26 +35,3 @@ void WiFiScanner::listNetworks(AsyncWebServerRequest * request) {
scanNetworks(request);
}
}
#ifdef ESP8266
/*
* Convert encryption type to standard used by ESP32 rather than the translated form which the esp8266 libaries expose.
*
* This allows us to use a single set of mappings in the UI.
*/
uint8_t WiFiScanner::convertEncryptionType(uint8_t encryptionType) {
switch (encryptionType) {
case ENC_TYPE_NONE:
return AUTH_OPEN;
case ENC_TYPE_WEP:
return AUTH_WEP;
case ENC_TYPE_TKIP:
return AUTH_WPA_PSK;
case ENC_TYPE_CCMP:
return AUTH_WPA2_PSK;
case ENC_TYPE_AUTO:
return AUTH_WPA_WPA2_PSK;
}
return -1;
}
#endif

View File

@@ -1,111 +0,0 @@
#include <WiFiSettingsService.h>
WiFiSettingsService::WiFiSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
: _httpEndpoint(WiFiSettings::read, WiFiSettings::update, this, server, WIFI_SETTINGS_SERVICE_PATH, securityManager)
, _fsPersistence(WiFiSettings::read, WiFiSettings::update, this, fs, WIFI_SETTINGS_FILE)
, _lastConnectionAttempt(0) {
// We want the device to come up in opmode=0 (WIFI_OFF), when erasing the flash this is not the default.
// If needed, we save opmode=0 before disabling persistence so the device boots with WiFi disabled in the future.
if (WiFi.getMode() != WIFI_OFF) {
WiFi.mode(WIFI_OFF);
}
// Disable WiFi config persistance and auto reconnect
WiFi.persistent(false);
WiFi.setAutoReconnect(false);
#ifdef ESP32
// Init the wifi driver on ESP32
WiFi.mode(WIFI_MODE_MAX);
WiFi.mode(WIFI_MODE_NULL);
WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeStop, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_STOP);
#elif defined(ESP8266)
// proddy added
#if defined(ESP8266)
// WiFi.setSleepMode(WIFI_NONE_SLEEP); // added to possibly fix wifi dropouts in arduino core 2.5.0
// ref: https://github.com/esp8266/Arduino/issues/6471
// ref: https://github.com/esp8266/Arduino/issues/6366
// high tx power causing weird behavior, slightly lowering from 20.5 to 20.0 may help stability
// WiFi.setOutputPower(20.0); // in dBm
#endif
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
#endif
addUpdateHandler([&](const String & originId) { reconfigureWiFiConnection(); }, false);
}
void WiFiSettingsService::begin() {
_fsPersistence.readFromFS();
reconfigureWiFiConnection();
}
void WiFiSettingsService::reconfigureWiFiConnection() {
// reset last connection attempt to force loop to reconnect immediately
_lastConnectionAttempt = 0;
// disconnect and de-configure wifi
#ifdef ESP32
if (WiFi.disconnect(true)) {
_stopping = true;
}
#elif defined(ESP8266)
WiFi.disconnect(true);
#endif
}
void WiFiSettingsService::loop() {
unsigned long currentMillis = millis();
if (!_lastConnectionAttempt || (uint32_t)(currentMillis - _lastConnectionAttempt) >= WIFI_RECONNECTION_DELAY) {
_lastConnectionAttempt = currentMillis;
manageSTA();
}
}
void WiFiSettingsService::manageSTA() {
// Abort if already connected, or if we have no SSID
if (WiFi.isConnected() || _state.ssid.length() == 0) {
return;
}
// Connect or reconnect as required
if ((WiFi.getMode() & WIFI_STA) == 0) {
// Serial.println(F("Connecting to WiFi."));
if (_state.staticIPConfig) {
// configure for static IP
WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2);
} else {
// configure for DHCP
#ifdef ESP32
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
#elif defined(ESP8266)
WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY);
#endif
}
// set hostname
#ifdef ESP32
WiFi.setHostname(_state.hostname.c_str());
#elif defined(ESP8266)
WiFi.hostname(_state.hostname);
#endif
// attempt to connect to the network
WiFi.begin(_state.ssid.c_str(), _state.password.c_str());
}
}
#ifdef ESP32
void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
WiFi.disconnect(true);
}
void WiFiSettingsService::onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info) {
if (_stopping) {
_lastConnectionAttempt = 0;
_stopping = false;
}
}
#elif defined(ESP8266)
void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) {
WiFi.disconnect(true);
}
#endif

View File

@@ -1,72 +0,0 @@
#include <WiFiStatus.h>
WiFiStatus::WiFiStatus(AsyncWebServer * server, SecurityManager * securityManager) {
server->on(WIFI_STATUS_SERVICE_PATH,
HTTP_GET,
securityManager->wrapRequest(std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED));
#ifdef ESP32
WiFi.onEvent(onStationModeConnected, WiFiEvent_t::SYSTEM_EVENT_STA_CONNECTED);
WiFi.onEvent(onStationModeDisconnected, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent(onStationModeGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#elif defined(ESP8266)
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP);
#endif
}
#ifdef ESP32
void WiFiStatus::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
// Serial.println(F("WiFi Connected."));
}
void WiFiStatus::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
// Serial.print(F("WiFi Disconnected. Reason code="));
// Serial.println(info.disconnected.reason);
}
void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
// Serial.printf_P(PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), WiFi.localIP().toString().c_str(), WiFi.getHostname());
}
#elif defined(ESP8266)
void WiFiStatus::onStationModeConnected(const WiFiEventStationModeConnected & event) {
// Serial.print(F("WiFi Connected. SSID="));
// Serial.println(event.ssid);
}
void WiFiStatus::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) {
// Serial.print(F("WiFi Disconnected. Reason code="));
// Serial.println(event.reason);
}
void WiFiStatus::onStationModeGotIP(const WiFiEventStationModeGotIP & event) {
// Serial.printf_P(PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), event.ip.toString().c_str(), WiFi.hostname().c_str());
}
#endif
void WiFiStatus::wifiStatus(AsyncWebServerRequest * request) {
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_WIFI_STATUS_SIZE);
JsonObject root = response->getRoot();
wl_status_t status = WiFi.status();
root["status"] = (uint8_t)status;
if (status == WL_CONNECTED) {
root["local_ip"] = WiFi.localIP().toString();
root["mac_address"] = WiFi.macAddress();
root["rssi"] = WiFi.RSSI();
root["ssid"] = WiFi.SSID();
root["bssid"] = WiFi.BSSIDstr();
root["channel"] = WiFi.channel();
root["subnet_mask"] = WiFi.subnetMask().toString();
root["gateway_ip"] = WiFi.gatewayIP().toString();
IPAddress dnsIP1 = WiFi.dnsIP(0);
IPAddress dnsIP2 = WiFi.dnsIP(1);
if (dnsIP1 != INADDR_NONE) {
root["dns_ip_1"] = dnsIP1.toString();
}
if (dnsIP2 != INADDR_NONE) {
root["dns_ip_2"] = dnsIP2.toString();
}
}
response->setLength();
request->send(response);
}

View File

@@ -1,45 +0,0 @@
#ifndef WiFiStatus_h
#define WiFiStatus_h
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ArduinoJson.h>
#include <AsyncJson.h>
#include <ESPAsyncWebServer.h>
#include <IPAddress.h>
#include <SecurityManager.h>
#define MAX_WIFI_STATUS_SIZE 1024
#define WIFI_STATUS_SERVICE_PATH "/rest/wifiStatus"
class WiFiStatus {
public:
WiFiStatus(AsyncWebServer * server, SecurityManager * securityManager);
private:
#ifdef ESP32
// static functions for logging WiFi events to the UART
static void onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info);
static void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
static void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
#elif defined(ESP8266)
// handler refrences for logging important WiFi events over serial
WiFiEventHandler _onStationModeConnectedHandler;
WiFiEventHandler _onStationModeDisconnectedHandler;
WiFiEventHandler _onStationModeGotIPHandler;
// static functions for logging WiFi events to the UART
static void onStationModeConnected(const WiFiEventStationModeConnected & event);
static void onStationModeDisconnected(const WiFiEventStationModeDisconnected & event);
static void onStationModeGotIP(const WiFiEventStationModeGotIP & event);
#endif
void wifiStatus(AsyncWebServerRequest * request);
};
#endif // end WiFiStatus_h

View File

@@ -21,8 +21,13 @@
#include <string>
#include <Network.h>
NativeConsole Serial;
ETHClass ETH;
WiFiClass WiFi;
/* millis() on C++ native could be
for non-Arduino
millis():

View File

@@ -193,7 +193,11 @@ class NativeConsole : public Stream {
unsigned char peek_data_;
};
#include <Network.h>
extern NativeConsole Serial;
extern ETHClass ETH;
extern WiFiClass WiFi;
unsigned long millis();

View File

@@ -10,6 +10,7 @@
#include <SecurityManager.h>
#include <SecuritySettingsService.h>
#include <StatefulService.h>
#include <Network.h>
class DummySettings {
public:
@@ -44,6 +45,7 @@ class DummySettings {
String staticIPConfig = "";
String dnsIP1 = "";
String dnsIP2 = "";
uint8_t ethernet_profile = 0;
uint16_t publish_time_boiler = 10;
uint16_t publish_time_thermostat = 10;
uint16_t publish_time_solar = 10;
@@ -66,11 +68,9 @@ class DummySettingsService : public StatefulService<DummySettings> {
void begin();
void loop();
private:
};
#define WiFiSettings DummySettings
#define NetworkSettings DummySettings
#define SecuritySettings DummySettings
#define MqttSettings DummySettings
@@ -91,7 +91,7 @@ class ESP8266React {
return _mqttClient;
}
StatefulService<DummySettings> * getWiFiSettingsService() {
StatefulService<DummySettings> * getNetworkSettingsService() {
return &_settings;
}
@@ -113,10 +113,7 @@ class ESP8266React {
class EMSESPSettingsService {
public:
EMSESPSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
void begin();
private:
};
class JsonUtils {

View File

@@ -33,5 +33,4 @@
#define FT_UPLOAD_FIRMWARE 0
#endif
#endif

180
lib_standalone/Network.h Normal file
View File

@@ -0,0 +1,180 @@
#ifndef Network_h
#define Network_h
#include <Arduino.h>
#include <functional>
#define WiFiMode_t wifi_mode_t
#define WIFI_OFF WIFI_MODE_NULL
#define WIFI_STA WIFI_MODE_STA
#define WIFI_AP WIFI_MODE_AP
#define WIFI_AP_STA WIFI_MODE_APSTA
typedef enum {
SYSTEM_EVENT_WIFI_READY = 0, /**< ESP32 WiFi ready */
SYSTEM_EVENT_SCAN_DONE, /**< ESP32 finish scanning AP */
SYSTEM_EVENT_STA_START, /**< ESP32 station start */
SYSTEM_EVENT_STA_STOP, /**< ESP32 station stop */
SYSTEM_EVENT_STA_CONNECTED, /**< ESP32 station connected to AP */
SYSTEM_EVENT_STA_DISCONNECTED, /**< ESP32 station disconnected from AP */
SYSTEM_EVENT_STA_AUTHMODE_CHANGE, /**< the auth mode of AP connected by ESP32 station changed */
SYSTEM_EVENT_STA_GOT_IP, /**< ESP32 station got IP from connected AP */
SYSTEM_EVENT_STA_LOST_IP, /**< ESP32 station lost IP and the IP is reset to 0 */
SYSTEM_EVENT_STA_WPS_ER_SUCCESS, /**< ESP32 station wps succeeds in enrollee mode */
SYSTEM_EVENT_STA_WPS_ER_FAILED, /**< ESP32 station wps fails in enrollee mode */
SYSTEM_EVENT_STA_WPS_ER_TIMEOUT, /**< ESP32 station wps timeout in enrollee mode */
SYSTEM_EVENT_STA_WPS_ER_PIN, /**< ESP32 station wps pin code in enrollee mode */
SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP, /*!< ESP32 station wps overlap in enrollee mode */
SYSTEM_EVENT_AP_START, /**< ESP32 soft-AP start */
SYSTEM_EVENT_AP_STOP, /**< ESP32 soft-AP stop */
SYSTEM_EVENT_AP_STACONNECTED, /**< a station connected to ESP32 soft-AP */
SYSTEM_EVENT_AP_STADISCONNECTED, /**< a station disconnected from ESP32 soft-AP */
SYSTEM_EVENT_AP_STAIPASSIGNED, /**< ESP32 soft-AP assign an IP to a connected station */
SYSTEM_EVENT_AP_PROBEREQRECVED, /**< Receive probe request packet in soft-AP interface */
SYSTEM_EVENT_GOT_IP6, /**< ESP32 station or ap or ethernet interface v6IP addr is preferred */
SYSTEM_EVENT_ETH_START, /**< ESP32 ethernet start */
SYSTEM_EVENT_ETH_STOP, /**< ESP32 ethernet stop */
SYSTEM_EVENT_ETH_CONNECTED, /**< ESP32 ethernet phy link up */
SYSTEM_EVENT_ETH_DISCONNECTED, /**< ESP32 ethernet phy link down */
SYSTEM_EVENT_ETH_GOT_IP, /**< ESP32 ethernet got IP from connected AP */
SYSTEM_EVENT_MAX
} system_event_id_t;
typedef enum {
WIFI_AUTH_OPEN = 0, /**< authenticate mode : open */
WIFI_AUTH_WEP, /**< authenticate mode : WEP */
WIFI_AUTH_WPA_PSK, /**< authenticate mode : WPA_PSK */
WIFI_AUTH_WPA2_PSK, /**< authenticate mode : WPA2_PSK */
WIFI_AUTH_WPA_WPA2_PSK, /**< authenticate mode : WPA_WPA2_PSK */
WIFI_AUTH_WPA2_ENTERPRISE, /**< authenticate mode : WPA2_ENTERPRISE */
WIFI_AUTH_MAX
} wifi_auth_mode_t;
typedef struct {
uint32_t status; /**< status of scanning APs */
uint8_t number;
uint8_t scan_id;
} system_event_sta_scan_done_t;
typedef struct {
uint8_t ssid[32]; /**< SSID of connected AP */
uint8_t ssid_len; /**< SSID length of connected AP */
uint8_t bssid[6]; /**< BSSID of connected AP*/
uint8_t channel; /**< channel of connected AP*/
wifi_auth_mode_t authmode;
} system_event_sta_connected_t;
typedef struct {
uint8_t ssid[32]; /**< SSID of disconnected AP */
uint8_t ssid_len; /**< SSID length of disconnected AP */
uint8_t bssid[6]; /**< BSSID of disconnected AP */
uint8_t reason; /**< reason of disconnection */
} system_event_sta_disconnected_t;
typedef struct {
wifi_auth_mode_t old_mode; /**< the old auth mode of AP */
wifi_auth_mode_t new_mode; /**< the new auth mode of AP */
} system_event_sta_authmode_change_t;
typedef struct {
uint8_t pin_code[8]; /**< PIN code of station in enrollee mode */
} system_event_sta_wps_er_pin_t;
typedef struct {
uint8_t mac[6]; /**< MAC address of the station connected to ESP32 soft-AP */
uint8_t aid; /**< the aid that ESP32 soft-AP gives to the station connected to */
} system_event_ap_staconnected_t;
typedef struct {
uint8_t mac[6]; /**< MAC address of the station disconnects to ESP32 soft-AP */
uint8_t aid; /**< the aid that ESP32 soft-AP gave to the station disconnects to */
} system_event_ap_stadisconnected_t;
typedef struct {
int rssi; /**< Received probe request signal strength */
uint8_t mac[6]; /**< MAC address of the station which send probe request */
} system_event_ap_probe_req_rx_t;
typedef enum {
WPS_FAIL_REASON_NORMAL = 0, /**< ESP32 WPS normal fail reason */
WPS_FAIL_REASON_RECV_M2D, /**< ESP32 WPS receive M2D frame */
WPS_FAIL_REASON_MAX
} system_event_sta_wps_fail_reason_t;
typedef union {
system_event_sta_connected_t connected; /**< ESP32 station connected to AP */
system_event_sta_disconnected_t disconnected; /**< ESP32 station disconnected to AP */
system_event_sta_scan_done_t scan_done; /**< ESP32 station scan (APs) done */
system_event_sta_authmode_change_t auth_change; /**< the auth mode of AP ESP32 station connected to changed */
system_event_sta_wps_fail_reason_t sta_er_fail_reason; /**< ESP32 station WPS enrollee mode failed reason code received */
system_event_ap_staconnected_t sta_connected; /**< a station connected to ESP32 soft-AP */
system_event_ap_stadisconnected_t sta_disconnected; /**< a station disconnected to ESP32 soft-AP */
system_event_ap_probe_req_rx_t ap_probereqrecved; /**< ESP32 soft-AP receive probe request packet */
} system_event_info_t;
typedef struct {
system_event_id_t event_id; /**< event ID */
system_event_info_t event_info; /**< event information */
} system_event_t;
#define WiFiEvent_t system_event_id_t
#define WiFiEventInfo_t system_event_info_t
#define WiFiEventId_t wifi_event_id_t
typedef enum {
WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library
WL_IDLE_STATUS = 0,
WL_NO_SSID_AVAIL = 1,
WL_SCAN_COMPLETED = 2,
WL_CONNECTED = 3,
WL_CONNECT_FAILED = 4,
WL_CONNECTION_LOST = 5,
WL_DISCONNECTED = 6
} wl_status_t;
typedef void (*WiFiEventCb)(system_event_id_t event);
typedef std::function<void(system_event_id_t event, system_event_info_t info)> WiFiEventFuncCb;
typedef void (*WiFiEventSysCb)(system_event_t * event);
typedef size_t wifi_event_id_t;
class WiFiClass {
public:
wifi_event_id_t onEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX) {
return 0;
};
wifi_event_id_t onEvent(WiFiEventFuncCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX) {
return 0;
};
wifi_event_id_t onEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX) {
return 0;
};
char * getHostname() {
return nullptr;
}
char * localIP() {
return nullptr;
}
};
class ETHClass {
public:
bool begin() {
return false;
};
void setHostname(const char * s){};
char * getHostname() {
return nullptr;
}
char * localIP() {
return nullptr;
}
int linkSpeed() {
return 100;
}
};
#endif

View File

@@ -56,7 +56,6 @@ inline bool operator==(const std::string & lhs, const ::String & rhs) {
return lhs == rhs.c_str();
}
size_t strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize);
size_t strlcat(char * dst, const char * src, size_t siz);

View File

@@ -1,7 +1,6 @@
; PlatformIO Project Configuration File for EMS-ESP
[platformio]
; default_envs = esp8266
default_envs = esp32
# override any settings with your own local ones in pio_local.ini
@@ -16,19 +15,10 @@ core_build_flags = -Wno-deprecated-declarations
-DCORE_DEBUG_LEVEL=0
-DNDEBUG
esp8266_build_flags = -free
-mtarget-align
-fipa-pta
-Wreturn-type
-DFP_IN_IROM
-DBEARSSL_SSL_BASIC
-DVTABLES_IN_FLASH
-DPSTR_ALIGN=1 ; remove the 4-bytes alignment for PSTR()
-std=c17 -std=c++17 -std=gnu++17
esp32_build_flags = -DARDUINO_ARCH_ESP32=1
-DESP32=1
-DBOARD_HAS_PSRAM
; -std=c17 -std=c++17 -std=gnu++17
build_flags =
${common.core_build_flags}
@@ -48,9 +38,8 @@ build_flags =
build_unflags = -Wall
-Wdeprecated-declarations
esp8266_build_unflags = -std=gnu++11
esp32_build_unflags =
; -std=gnu++11
debug_flags =
; -D EMSESP_DEBUG
@@ -73,18 +62,6 @@ check_flags =
cppcheck: --std=c++11 -v
clangtidy: --checks=-*,clang-analyzer-*,performance-*
; build for GitHub Actions CI
[env:esp8266-ci]
extra_scripts =
scripts/main_script.py
scripts/rename_fw.py
board = esp12e
platform = espressif8266
board_build.filesystem = littlefs
board_build.f_cpu = 160000000L
build_flags = ${common.build_flags} ${common.esp8266_build_flags}
build_unflags = ${common.build_unflags} ${common.esp8266_build_unflags}
; build for GitHub Actions CI
[env:esp32-ci]
extra_scripts =
@@ -95,30 +72,6 @@ board_build.partitions = min_spiffs.csv
build_flags = ${common.build_flags} ${common.esp32_build_flags}
build_unflags = ${common.build_unflags} ${common.esp32_build_unflags}
[env:esp8266]
extra_scripts =
pre:scripts/build_interface.py
scripts/main_script.py
scripts/rename_fw.py
board = esp12e ; https://github.com/platformio/platform-espressif8266/tree/master/boards
platform = espressif8266 ; https://github.com/platformio/platform-espressif8266/releases
platform_packages = platformio/framework-arduinoespressif8266 @ https://github.com/esp8266/Arduino.git
; toolchain-xtensa @ ~2.100100.200706
; toolchain-xtensa @ 2.40802.200502
; toolchain-xtensa @ https://github.com/earlephilhower/esp-quick-toolchain/releases/download/3.0.0-gnu12/x86_64-linux-gnu.xtensa-lx106-elf-0474ae9.200706.tar.gz
; platformio/tool-esptool @ 1.413.0
; platformio/tool-esptoolpy @ ~1.30000.0
mcspr/toolchain-xtensa @ 5.100200.201223
board_build.filesystem = littlefs
board_build.f_cpu = 160000000L ; 160MHz
; eagle.flash.4m1m.ld = 1019 KB sketch, 1000 KB SPIFFS. 4KB EEPROM, 4KB RFCAL, 12KB WIFI stack, 2052 KB OTA & buffer
; eagle.flash.4m2m.ld = 1019 KB sketch, 2024 KB SPIFFS. 4KB EEPROM, 4KB RFCAL, 12KB WIFI stack, 1028 KB OTA & buffer
; board_build.ldscript = eagle.flash.4m2m.ld
build_flags = ${common.build_flags} ${common.esp8266_build_flags} ${common.debug_flags}
build_unflags = ${common.build_unflags} ${common.esp8266_build_unflags}
lib_ignore =
AsyncTCP
[env:esp32]
extra_scripts =
pre:scripts/build_interface.py

View File

@@ -22,39 +22,81 @@ namespace emsesp {
WebStatusService::WebStatusService(AsyncWebServer * server, SecurityManager * securityManager) {
// rest endpoint for web page
server->on(EMSESP_STATUS_SERVICE_PATH,
HTTP_GET,
securityManager->wrapRequest(std::bind(&WebStatusService::webStatusService, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED));
// trigger on wifi connects/disconnects
#ifdef ESP32
WiFi.onEvent(onStationModeDisconnected, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent(onStationModeGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#elif defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP);
#endif
server->on(EMSESP_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WebStatusService::webStatusService, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED));
WiFi.onEvent(std::bind(&WebStatusService::WiFiEvent, this, std::placeholders::_1, std::placeholders::_2));
}
#ifdef ESP32
void WebStatusService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
// handles both WiFI and Ethernet
void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
switch (event) {
case SYSTEM_EVENT_STA_DISCONNECTED:
EMSESP::logger().info(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason);
}
void WebStatusService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
break;
case SYSTEM_EVENT_STA_GOT_IP:
#ifndef EMSESP_STANDALONE
EMSESP::logger().info(F("WiFi Connected with IP=%s, hostname=%s"), WiFi.localIP().toString().c_str(), WiFi.getHostname());
EMSESP::system_.init_wifi(); // send out heartbeat MQTT as soon as we have a connection
}
#elif defined(ESP8266)
void WebStatusService::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) {
EMSESP::logger().info(F("WiFi Disconnected. Reason code=%d"), event.reason);
}
void WebStatusService::onStationModeGotIP(const WiFiEventStationModeGotIP & event) {
EMSESP::logger().info(F("WiFi Connected with IP=%s, hostname=%s"), event.ip.toString().c_str(), WiFi.hostname().c_str());
EMSESP::system_.init_wifi(); // send out heartbeat MQTT as soon as we have a connection
}
#endif
// Lower WiFi tx power to free up current for bus-powered
/*
WIFI_POWER_19_5dBm = 78,// 19.5dBm <-- default
WIFI_POWER_19dBm = 76,// 19dBm
WIFI_POWER_18_5dBm = 74,// 18.5dBm
WIFI_POWER_17dBm = 68,// 17dBm
WIFI_POWER_15dBm = 60,// 15dBm
WIFI_POWER_13dBm = 52,// 13dBm
WIFI_POWER_11dBm = 44,// 11dBm
WIFI_POWER_8_5dBm = 34,// 8.5dBm
WIFI_POWER_7dBm = 28,// 7dBm
WIFI_POWER_5dBm = 20,// 5dBm
WIFI_POWER_2dBm = 8,// 2dBm
WIFI_POWER_MINUS_1dBm = -4// -1dBm
*/
/*
wifi_power_t a1 = WiFi.getTxPower();
// bool ok = WiFi.setTxPower(WIFI_POWER_17dBm); // lower power
bool ok = WiFi.setTxPower(WIFI_POWER_19_5dBm);
wifi_power_t a2 = WiFi.getTxPower();
LOG_INFO("Adjusting Wifi Tx power from %d to %d (%s)", a1, a2, ok ? "ok" : "failed");
*/
EMSESP::system_.init_network(); // send out heartbeat MQTT as soon as we have a connection
break;
case SYSTEM_EVENT_ETH_START:
EMSESP::logger().info(F("Ethernet Started"));
ETH.setHostname(emsesp::System::hostname().c_str());
break;
case SYSTEM_EVENT_ETH_GOT_IP:
// prevent double calls
if (!connected_) {
#ifndef EMSESP_STANDALONE
EMSESP::logger().info(F("Ethernet Connected with IP=%s, speed %d Mbps"), ETH.localIP().toString().c_str(), ETH.linkSpeed());
#endif
EMSESP::system_.init_network(); // send out heartbeat MQTT as soon as we have a connection
connected_ = true;
}
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
EMSESP::logger().info(F("Ethernet Disconnected"));
connected_ = false;
break;
case SYSTEM_EVENT_ETH_STOP:
EMSESP::logger().info(F("Ethernet Stopped"));
connected_ = false;
break;
default:
break;
}
}
void WebStatusService::webStatusService(AsyncWebServerRequest * request) {
AsyncJsonResponse * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_MEDIUM_DYN);
JsonObject root = response->getRoot();

View File

@@ -34,18 +34,9 @@ class WebStatusService {
WebStatusService(AsyncWebServer * server, SecurityManager * securityManager);
private:
bool connected_ = false;
void webStatusService(AsyncWebServerRequest * request);
#ifdef ESP32
static void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
static void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
#elif defined(ESP8266)
WiFiEventHandler _onStationModeDisconnectedHandler;
WiFiEventHandler _onStationModeGotIPHandler;
static void onStationModeDisconnected(const WiFiEventStationModeDisconnected & event);
static void onStationModeGotIP(const WiFiEventStationModeGotIP & event);
#endif
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info);
};
} // namespace emsesp

View File

@@ -74,7 +74,7 @@ void EMSESPShell::display_banner() {
println();
// set console name
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { console_hostname_ = wifiSettings.hostname.c_str(); });
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { console_hostname_ = networkSettings.hostname.c_str(); });
if (console_hostname_.empty()) {
console_hostname_.resize(16, '\0');

View File

@@ -20,6 +20,7 @@
* Lightweight queue & array
* Based ideas from https://github.com/muwerk/ustd
* Limits to max 255 entries
* Was used in testing with ESP8266 to overcome heap memory issues - no longer used
*/
#ifndef EMSESP_CONTAINERS_H

View File

@@ -31,9 +31,6 @@ AsyncWebServer webServer(80);
#if defined(ESP32)
ESP8266React EMSESP::esp8266React(&webServer, &SPIFFS);
WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &SPIFFS, EMSESP::esp8266React.getSecurityManager());
#elif defined(ESP8266)
ESP8266React EMSESP::esp8266React(&webServer, &LittleFS);
WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
#elif defined(EMSESP_STANDALONE)
FS dummyFS;
ESP8266React EMSESP::esp8266React(&webServer, &dummyFS);
@@ -1027,11 +1024,9 @@ void EMSESP::send_raw_telegram(const char * data) {
// start all the core services
// the services must be loaded in the correct order
void EMSESP::start() {
// start the file system. We use LittleFS for ESP8266.
#ifdef ESP32
// start the file system
#ifndef EMSESP_STANDALONE
SPIFFS.begin(true);
#elif defined(ESP8266)
LittleFS.begin();
#endif
esp8266React.begin(); // loads system settings (wifi, mqtt, etc)
@@ -1039,25 +1034,11 @@ void EMSESP::start() {
system_.check_upgrade(); // do any upgrades
#if defined(EMSESP_DEBUG)
#ifndef EMSESP_STANDALONE
uint32_t tbefore = ESP.getFreeHeap();
#endif
#endif
// Load our library of known devices into stack mem. Names are stored in Flash memory
// Still it takes up about 960bytes
device_library_.reserve(80);
// Load our library of known devices into stack mem. Names are stored in Flash memory (take about 960bytes)
device_library_ = {
#include "device_library.h"
};
#if defined(EMSESP_DEBUG)
#ifndef EMSESP_STANDALONE
uint32_t tafter = ESP.getFreeHeap();
#endif
#endif
console_.start(); // telnet and serial console
mqtt_.start(); // mqtt init
system_.start(heap_start); // starts syslog, uart, sets version, initializes LED. Requires pre-loaded settings.
@@ -1069,13 +1050,6 @@ void EMSESP::start() {
LOG_INFO(F("EMS Device library loaded with %d records"), device_library_.size());
#if defined(EMSESP_DEBUG)
#ifndef EMSESP_STANDALONE
LOG_INFO(F("Used %d mem for devices"), tbefore - tafter);
System::show_mem("after start()");
#endif
#endif
#if defined(EMSESP_STANDALONE)
Mqtt::on_connect(); // simulate an MQTT connection
#endif

View File

@@ -40,7 +40,6 @@
#include "WebSettingsService.h"
#include "WebAPIService.h"
// #include "containers.h"
#include "emsdevice.h"
#include "emsfactory.h"
#include "telegram.h"

View File

@@ -96,7 +96,7 @@ MAKE_PSTR_WORD(unknown)
MAKE_PSTR(EMSESP, "EMS-ESP")
MAKE_PSTR(master_thermostat_fmt, "Master Thermostat Device ID = %s")
MAKE_PSTR(host_fmt, "Host = %s")
MAKE_PSTR(hostname_fmt, "WiFi Hostname = %s")
MAKE_PSTR(hostname_fmt, "Hostname = %s")
MAKE_PSTR(mark_interval_fmt, "Mark interval = %lus")
MAKE_PSTR(wifi_ssid_fmt, "WiFi SSID = %s")
MAKE_PSTR(wifi_password_fmt, "WiFi Password = %S")

View File

@@ -340,7 +340,7 @@ void Mqtt::start() {
mqttClient_ = EMSESP::esp8266React.getMqttClient();
// get the hostname, which we'll use to prefix to all topics
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { hostname_ = wifiSettings.hostname.c_str(); });
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { hostname_ = networkSettings.hostname.c_str(); });
// fetch MQTT settings, to see if MQTT is enabled
EMSESP::esp8266React.getMqttSettingsService()->read([&](MqttSettings & mqttSettings) {

View File

@@ -109,7 +109,7 @@ void System::wifi_reconnect() {
Shell::loop_all();
delay(1000); // wait a second
EMSESP::webSettingsService.save(); // local settings
EMSESP::esp8266React.getWiFiSettingsService()->callUpdateHandlers("local"); // in case we've changed ssid or password
EMSESP::esp8266React.getNetworkSettingsService()->callUpdateHandlers("local"); // in case we've changed ssid or password
}
// format fs
@@ -164,7 +164,7 @@ void System::syslog_init() {
syslog_.log_level((uuid::log::Level)syslog_level_);
syslog_.mark_interval(syslog_mark_interval_);
syslog_.destination(addr);
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { syslog_.hostname(wifiSettings.hostname.c_str()); });
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { syslog_.hostname(networkSettings.hostname.c_str()); });
EMSESP::logger().info(F("Syslog started"));
#endif
@@ -182,7 +182,7 @@ void System::start(uint32_t heap_start) {
#endif
// print boot message
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { LOG_INFO(F("System %s booted (EMS-ESP version %s)"), wifiSettings.hostname.c_str(), EMSESP_APP_VERSION); });
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { LOG_INFO(F("System %s booted (EMS-ESP version %s)"), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); });
// these commands respond to the topic "system" and take a payload like {cmd:"", data:"", id:""}
EMSESP::webSettingsService.read([&](WebSettings & settings) {
@@ -198,6 +198,42 @@ void System::start(uint32_t heap_start) {
#endif
});
// check ethernet profile, if we're using exclusive Ethernet then disabled wifi and AP/captive portal
uint8_t ethernet_profile;
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) { ethernet_profile = settings.ethernet_profile; });
#ifndef EMSESP_STANDALONE
uint8_t phy_addr = 0; // I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110)
int power = -1; // Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source)
int mdc = 23; // Pin# of the I²C clock signal for the Ethernet PHY
int mdio = 18; // Pin# of the I²C IO signal for the Ethernet PHY
eth_phy_type_t type = ETH_PHY_LAN8720; // Type of the Ethernet PHY (LAN8720 or TLK110)
eth_clock_mode_t clock_mode = ETH_CLOCK_GPIO0_IN; // ETH_CLOCK_GPIO0_IN or ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO17_OUT for 50Hz inverted clock
// see if we can start it using default settings
if (!ETH.begin(phy_addr, power, mdc, mdio, type, clock_mode)) {
// it failed. Now try again based on profile. 0 is the same as 1. 2 is for TLK110
if (ethernet_profile == 2) {
if (ETH.begin(31, power, mdc, mdio, ETH_PHY_TLK110, clock_mode)) {
EMSESP::esp8266React.getNetworkSettingsService()->update(
[&](NetworkSettings & settings) {
settings.ssid == ""; // remove SSID
return StateUpdateResult::CHANGED;
},
"local");
EMSESP::esp8266React.getAPSettingsService()->update(
[&](APSettings & settings) {
settings.provisionMode = AP_MODE_NEVER;
return StateUpdateResult::CHANGED;
},
"local");
}
}
}
#endif
// continue with init'ing services
init();
}
@@ -217,9 +253,9 @@ void System::init() {
syslog_init(); // init SysLog
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & settings) { hostname(settings.hostname.c_str()); });
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) { hostname(settings.hostname.c_str()); });
#if defined(ESP32)
#ifndef EMSESP_STANDALONE
// setCpuFrequencyMhz(160); // default is 240
// disable bluetooth
@@ -288,7 +324,7 @@ void System::loop() {
#ifndef EMSESP_STANDALONE
#if defined(EMSESP_DEBUG)
static uint32_t last_memcheck_ = 0;
if (currentMillis - last_memcheck_ > 5000) { // 5 seconds
if (currentMillis - last_memcheck_ > 10000) { // 10 seconds
last_memcheck_ = currentMillis;
show_mem("core");
}
@@ -391,32 +427,9 @@ void System::set_led_speed(uint32_t speed) {
led_monitor();
}
void System::init_wifi() {
void System::init_network() {
last_system_check_ = 0; // force the LED to go from fast flash to pulse
send_heartbeat();
#if defined(ESP32)
// TODO wifi tx power
/*
WIFI_POWER_19_5dBm = 78,// 19.5dBm
WIFI_POWER_19dBm = 76,// 19dBm
WIFI_POWER_18_5dBm = 74,// 18.5dBm
WIFI_POWER_17dBm = 68,// 17dBm
WIFI_POWER_15dBm = 60,// 15dBm
WIFI_POWER_13dBm = 52,// 13dBm
WIFI_POWER_11dBm = 44,// 11dBm
WIFI_POWER_8_5dBm = 34,// 8.5dBm
WIFI_POWER_7dBm = 28,// 7dBm
WIFI_POWER_5dBm = 20,// 5dBm
WIFI_POWER_2dBm = 8,// 2dBm
WIFI_POWER_MINUS_1dBm = -4// -1dBm
*/
// wifi_power_t a1 = WiFi.getTxPower();
// // bool ok = WiFi.setTxPower(WIFI_POWER_17dBm);
// bool ok = true;
// wifi_power_t a2 = WiFi.getTxPower();
// LOG_INFO("Wifi Tx power was %d, is now %d, ok=%s", a1, a2, ok ? "ok" : "failed");
#endif
}
// check health of system, done every few seconds
@@ -512,28 +525,9 @@ void System::show_system(uuid::console::Shell & shell) {
shell.printfln(F("Uptime: %s"), uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3).c_str());
#ifndef EMSESP_STANDALONE
#if defined(ESP8266)
shell.printfln(F("Chip ID: 0x%08x"), ESP.getChipId());
shell.printfln(F("SDK version: %s"), ESP.getSdkVersion());
shell.printfln(F("Core version: %s"), ESP.getCoreVersion().c_str());
shell.printfln(F("Full version: %s"), ESP.getFullVersion().c_str());
shell.printfln(F("Boot version: %u"), ESP.getBootVersion());
shell.printfln(F("Boot mode: %u"), ESP.getBootMode());
shell.printfln(F("CPU frequency: %u MHz"), ESP.getCpuFreqMHz());
shell.printfln(F("Flash chip: 0x%08X (%u bytes)"), ESP.getFlashChipId(), ESP.getFlashChipRealSize());
shell.printfln(F("Reset reason: %s"), ESP.getResetReason().c_str());
shell.printfln(F("Reset info: %s"), ESP.getResetInfo().c_str());
#elif defined(ESP32)
shell.printfln(F("SDK version: %s"), ESP.getSdkVersion());
shell.printfln(F("CPU frequency: %u MHz"), ESP.getCpuFreqMHz());
#endif
shell.printfln(F("Sketch size: %u bytes (%u bytes free)"), ESP.getSketchSize(), ESP.getFreeSketchSpace());
shell.printfln(F("Free heap: %lu bytes"), (uint32_t)ESP.getFreeHeap());
#if defined(ESP8266)
shell.printfln(F("Heap fragmentation: %u %%"), ESP.getHeapFragmentation());
shell.printfln(F("Maximum free block size: %lu bytes"), (uint32_t)ESP.getMaxFreeBlockSize());
shell.printfln(F("Free continuations stack: %lu bytes"), (uint32_t)ESP.getFreeContStack());
#endif
shell.println();
switch (WiFi.status()) {
@@ -555,12 +549,7 @@ void System::show_system(uuid::console::Shell & shell) {
shell.printfln(F("BSSID: %s"), WiFi.BSSIDstr().c_str());
shell.printfln(F("RSSI: %d dBm (%d %%)"), WiFi.RSSI(), wifi_quality());
shell.printfln(F("MAC address: %s"), WiFi.macAddress().c_str());
#if defined(ESP8266)
shell.printfln(F("Hostname: %s"), WiFi.hostname().c_str());
#elif defined(ESP32)
shell.printfln(F("Hostname: %s"), WiFi.getHostname());
#endif
shell.printfln(F("IPv4 address: %s/%s"), uuid::printable_to_string(WiFi.localIP()).c_str(), uuid::printable_to_string(WiFi.subnetMask()).c_str());
shell.printfln(F("IPv4 gateway: %s"), uuid::printable_to_string(WiFi.gatewayIP()).c_str());
shell.printfln(F("IPv4 nameserver: %s"), uuid::printable_to_string(WiFi.dnsIP()).c_str());
@@ -584,6 +573,20 @@ void System::show_system(uuid::console::Shell & shell) {
break;
}
shell.println();
// show Ethernet
if (ETH.linkUp()) {
shell.printfln(F("Ethernet: Connected"));
shell.printfln(F("MAC address: %s"), ETH.macAddress().c_str());
shell.printfln(F("Hostname: %s"), ETH.getHostname());
shell.printfln(F("IPv4 address: %s/%s"), uuid::printable_to_string(ETH.localIP()).c_str(), uuid::printable_to_string(ETH.subnetMask()).c_str());
shell.printfln(F("IPv4 gateway: %s"), uuid::printable_to_string(ETH.gatewayIP()).c_str());
shell.printfln(F("IPv4 nameserver: %s"), uuid::printable_to_string(ETH.dnsIP()).c_str());
} else {
shell.printfln(F("Ethernet: disconnected"));
}
EMSESP::webSettingsService.read([&](WebSettings & settings) {
shell.println();
@@ -658,15 +661,15 @@ void System::console_commands(Shell & shell, unsigned int context) {
EMSESPShell::commands->add_command(ShellContext::SYSTEM,
CommandFlags::ADMIN,
flash_string_vector{F_(set), F_(wifi), F_(hostname)},
flash_string_vector{F_(set), F_(hostname)},
flash_string_vector{F_(name_mandatory)},
[](Shell & shell __attribute__((unused)), const std::vector<std::string> & arguments) {
shell.println("The wifi connection will be reset...");
shell.println("The network connection will be reset...");
Shell::loop_all();
delay(1000); // wait a second
EMSESP::esp8266React.getWiFiSettingsService()->update(
[&](WiFiSettings & wifiSettings) {
wifiSettings.hostname = arguments.front().c_str();
EMSESP::esp8266React.getNetworkSettingsService()->update(
[&](NetworkSettings & networkSettings) {
networkSettings.hostname = arguments.front().c_str();
return StateUpdateResult::CHANGED;
},
"local");
@@ -677,8 +680,8 @@ void System::console_commands(Shell & shell, unsigned int context) {
flash_string_vector{F_(set), F_(wifi), F_(ssid)},
flash_string_vector{F_(name_mandatory)},
[](Shell & shell, const std::vector<std::string> & arguments) {
EMSESP::esp8266React.getWiFiSettingsService()->updateWithoutPropagation([&](WiFiSettings & wifiSettings) {
wifiSettings.ssid = arguments.front().c_str();
EMSESP::esp8266React.getNetworkSettingsService()->updateWithoutPropagation([&](NetworkSettings & networkSettings) {
networkSettings.ssid = arguments.front().c_str();
return StateUpdateResult::CHANGED;
});
shell.println("Use `wifi reconnect` to apply the new settings");
@@ -690,8 +693,8 @@ void System::console_commands(Shell & shell, unsigned int context) {
shell.enter_password(F_(new_password_prompt2), [password1](Shell & shell, bool completed, const std::string & password2) {
if (completed) {
if (password1 == password2) {
EMSESP::esp8266React.getWiFiSettingsService()->updateWithoutPropagation([&](WiFiSettings & wifiSettings) {
wifiSettings.password = password2.c_str();
EMSESP::esp8266React.getNetworkSettingsService()->updateWithoutPropagation([&](NetworkSettings & networkSettings) {
networkSettings.password = password2.c_str();
return StateUpdateResult::CHANGED;
});
shell.println("Use `wifi reconnect` to apply the new settings");
@@ -705,16 +708,16 @@ void System::console_commands(Shell & shell, unsigned int context) {
});
EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::USER, flash_string_vector{F_(set)}, [](Shell & shell, const std::vector<std::string> & arguments __attribute__((unused))) {
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) {
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
shell.print(F(" "));
shell.printfln(F_(hostname_fmt), wifiSettings.hostname.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : wifiSettings.hostname.c_str());
shell.printfln(F_(hostname_fmt), networkSettings.hostname.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : networkSettings.hostname.c_str());
});
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) {
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
shell.print(F(" "));
shell.printfln(F_(wifi_ssid_fmt), wifiSettings.ssid.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : wifiSettings.ssid.c_str());
shell.printfln(F_(wifi_ssid_fmt), networkSettings.ssid.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : networkSettings.ssid.c_str());
shell.print(F(" "));
shell.printfln(F_(wifi_password_fmt), wifiSettings.ssid.isEmpty() ? F_(unset) : F_(asterisks));
shell.printfln(F_(wifi_password_fmt), networkSettings.ssid.isEmpty() ? F_(unset) : F_(asterisks));
});
});
@@ -802,17 +805,17 @@ bool System::check_upgrade() {
EMSESP::esp8266React.begin();
EMSESP::webSettingsService.begin();
EMSESP::esp8266React.getWiFiSettingsService()->update(
[&](WiFiSettings & wifiSettings) {
wifiSettings.hostname = general["hostname"] | FACTORY_WIFI_HOSTNAME;
wifiSettings.ssid = network["ssid"] | FACTORY_WIFI_SSID;
wifiSettings.password = network["password"] | FACTORY_WIFI_PASSWORD;
EMSESP::esp8266React.getNetworkSettingsService()->update(
[&](NetworkSettings & networkSettings) {
networkSettings.hostname = general["hostname"] | FACTORY_WIFI_HOSTNAME;
networkSettings.ssid = network["ssid"] | FACTORY_WIFI_SSID;
networkSettings.password = network["password"] | FACTORY_WIFI_PASSWORD;
wifiSettings.staticIPConfig = false;
JsonUtils::readIP(network, "staticip", wifiSettings.localIP);
JsonUtils::readIP(network, "dnsip", wifiSettings.dnsIP1);
JsonUtils::readIP(network, "gatewayip", wifiSettings.gatewayIP);
JsonUtils::readIP(network, "nmask", wifiSettings.subnetMask);
networkSettings.staticIPConfig = false;
JsonUtils::readIP(network, "staticip", networkSettings.localIP);
JsonUtils::readIP(network, "dnsip", networkSettings.dnsIP1);
JsonUtils::readIP(network, "gatewayip", networkSettings.gatewayIP);
JsonUtils::readIP(network, "nmask", networkSettings.subnetMask);
return StateUpdateResult::CHANGED;
},
@@ -922,7 +925,7 @@ bool System::check_upgrade() {
// e.g. http://ems-esp/api?device=system&cmd=settings
// value and id are ignored
bool System::command_settings(const char * value, const int8_t id, JsonObject & json) {
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & settings) {
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) {
JsonObject node = json.createNestedObject("WIFI");
node["ssid"] = settings.ssid;
// node["password"] = settings.password;

View File

@@ -68,7 +68,7 @@ class System {
static void upload_status(bool in_progress);
static bool upload_status();
static void show_mem(const char * note);
void init_wifi();
void init_network();
static void init();
static void led_init();
static void syslog_init();