From 44045ae658fc94b8dbe6d78f260c906257fc4fc7 Mon Sep 17 00:00:00 2001 From: proddy Date: Mon, 18 Jan 2021 19:38:22 +0100 Subject: [PATCH] add Ethernet --- CHANGELOG_LATEST.md | 1 + debug_esp32_pio_local.ini | 29 ++- example_esp8266_pio_local.ini | 23 --- interface/.env.development | 4 +- interface/src/AppRouting.tsx | 9 +- interface/src/api/Endpoints.ts | 4 +- interface/src/components/MenuAppBar.tsx | 4 +- interface/src/network/NetworkConnection.tsx | 62 ++++++ .../src/network/NetworkConnectionContext.tsx | 13 ++ .../src/network/NetworkSettingsController.tsx | 29 +++ .../NetworkSettingsForm.tsx} | 37 ++-- interface/src/network/NetworkStatus.ts | 48 +++++ .../src/network/NetworkStatusController.tsx | 29 +++ .../NetworkStatusForm.tsx} | 22 ++- .../{wifi => network}/WiFiNetworkScanner.tsx | 0 .../{wifi => network}/WiFiNetworkSelector.tsx | 6 +- .../{wifi => network}/WiFiSecurityModes.ts | 0 interface/src/{wifi => network}/types.ts | 10 +- interface/src/system/SystemStatusForm.tsx | 21 -- interface/src/system/types.ts | 2 - interface/src/wifi/WiFiConnection.tsx | 62 ------ interface/src/wifi/WiFiConnectionContext.tsx | 13 -- interface/src/wifi/WiFiSettingsController.tsx | 29 --- interface/src/wifi/WiFiStatus.ts | 41 ---- interface/src/wifi/WiFiStatusController.tsx | 29 --- lib/framework/APSettingsService.cpp | 14 +- lib/framework/APSettingsService.h | 6 +- lib/framework/ESP8266React.cpp | 48 +---- lib/framework/ESP8266React.h | 56 ++---- lib/framework/MqttSettingsService.cpp | 5 - lib/framework/MqttSettingsService.h | 8 - lib/framework/NTPSettingsService.cpp | 62 +++--- lib/framework/NTPSettingsService.h | 17 +- lib/framework/NetworkSettingsService.cpp | 90 +++++++++ ...ingsService.h => NetworkSettingsService.h} | 56 +++--- lib/framework/NetworkStatus.cpp | 57 ++++++ lib/framework/NetworkStatus.h | 25 +++ lib/framework/SystemStatus.cpp | 41 ++-- lib/framework/WiFiScanner.cpp | 45 +---- lib/framework/WiFiSettingsService.cpp | 111 ----------- lib/framework/WiFiStatus.cpp | 72 ------- lib/framework/WiFiStatus.h | 45 ----- lib_standalone/Arduino.cpp | 5 + lib_standalone/Arduino.h | 12 +- lib_standalone/AsyncMqttClient.h | 12 +- lib_standalone/ESP8266React.h | 11 +- lib_standalone/Features.h | 1 - lib_standalone/Network.h | 180 ++++++++++++++++++ lib_standalone/WString.h | 1 - platformio.ini | 51 +---- src/WebStatusService.cpp | 100 +++++++--- src/WebStatusService.h | 13 +- src/console.cpp | 2 +- src/containers.h | 1 + src/emsesp.cpp | 32 +--- src/emsesp.h | 1 - src/locale_EN.h | 2 +- src/mqtt.cpp | 2 +- src/system.cpp | 165 ++++++++-------- src/system.h | 2 +- 60 files changed, 905 insertions(+), 973 deletions(-) delete mode 100644 example_esp8266_pio_local.ini create mode 100644 interface/src/network/NetworkConnection.tsx create mode 100644 interface/src/network/NetworkConnectionContext.tsx create mode 100644 interface/src/network/NetworkSettingsController.tsx rename interface/src/{wifi/WiFiSettingsForm.tsx => network/NetworkSettingsForm.tsx} (82%) create mode 100644 interface/src/network/NetworkStatus.ts create mode 100644 interface/src/network/NetworkStatusController.tsx rename interface/src/{wifi/WiFiStatusForm.tsx => network/NetworkStatusForm.tsx} (84%) rename interface/src/{wifi => network}/WiFiNetworkScanner.tsx (100%) rename interface/src/{wifi => network}/WiFiNetworkSelector.tsx (88%) rename interface/src/{wifi => network}/WiFiSecurityModes.ts (100%) rename interface/src/{wifi => network}/types.ts (83%) delete mode 100644 interface/src/wifi/WiFiConnection.tsx delete mode 100644 interface/src/wifi/WiFiConnectionContext.tsx delete mode 100644 interface/src/wifi/WiFiSettingsController.tsx delete mode 100644 interface/src/wifi/WiFiStatus.ts delete mode 100644 interface/src/wifi/WiFiStatusController.tsx create mode 100644 lib/framework/NetworkSettingsService.cpp rename lib/framework/{WiFiSettingsService.h => NetworkSettingsService.h} (63%) create mode 100644 lib/framework/NetworkStatus.cpp create mode 100644 lib/framework/NetworkStatus.h delete mode 100644 lib/framework/WiFiSettingsService.cpp delete mode 100644 lib/framework/WiFiStatus.cpp delete mode 100644 lib/framework/WiFiStatus.h create mode 100644 lib_standalone/Network.h diff --git a/CHANGELOG_LATEST.md b/CHANGELOG_LATEST.md index cbea0cf03..8d1d545a8 100644 --- a/CHANGELOG_LATEST.md +++ b/CHANGELOG_LATEST.md @@ -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 diff --git a/debug_esp32_pio_local.ini b/debug_esp32_pio_local.ini index b32b1e3f0..0b2d7b7ca 100644 --- a/debug_esp32_pio_local.ini +++ b/debug_esp32_pio_local.ini @@ -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 - diff --git a/example_esp8266_pio_local.ini b/example_esp8266_pio_local.ini deleted file mode 100644 index efa8e404a..000000000 --- a/example_esp8266_pio_local.ini +++ /dev/null @@ -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 diff --git a/interface/.env.development b/interface/.env.development index 8f0b64ff5..874a0b994 100644 --- a/interface/.env.development +++ b/interface/.env.development @@ -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 diff --git a/interface/src/AppRouting.tsx b/interface/src/AppRouting.tsx index f17d5b5a7..12c280bf3 100644 --- a/interface/src/AppRouting.tsx +++ b/interface/src/AppRouting.tsx @@ -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 { componentDidMount() { @@ -38,10 +37,10 @@ class AppRouting extends Component { {features.project && ( )} - + {features.ntp && ( - + )} {features.mqtt && ( diff --git a/interface/src/api/Endpoints.ts b/interface/src/api/Endpoints.ts index a0be3d46e..b4dbdeb40 100644 --- a/interface/src/api/Endpoints.ts +++ b/interface/src/api/Endpoints.ts @@ -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"; diff --git a/interface/src/components/MenuAppBar.tsx b/interface/src/components/MenuAppBar.tsx index 2a3dabc24..9ff87465e 100644 --- a/interface/src/components/MenuAppBar.tsx +++ b/interface/src/components/MenuAppBar.tsx @@ -144,11 +144,11 @@ class MenuAppBar extends React.Component { )} - + - + diff --git a/interface/src/network/NetworkConnection.tsx b/interface/src/network/NetworkConnection.tsx new file mode 100644 index 000000000..ff96978de --- /dev/null +++ b/interface/src/network/NetworkConnection.tsx @@ -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 { + + 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 ( + + + + + + + + + + + + + + + + ) + } +} + +export default withAuthenticatedContext(NetworkConnection); diff --git a/interface/src/network/NetworkConnectionContext.tsx b/interface/src/network/NetworkConnectionContext.tsx new file mode 100644 index 000000000..914b8954a --- /dev/null +++ b/interface/src/network/NetworkConnectionContext.tsx @@ -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 +); diff --git a/interface/src/network/NetworkSettingsController.tsx b/interface/src/network/NetworkSettingsController.tsx new file mode 100644 index 000000000..109e18d8a --- /dev/null +++ b/interface/src/network/NetworkSettingsController.tsx @@ -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; + +class NetworkSettingsController extends Component { + + componentDidMount() { + this.props.loadData(); + } + + render() { + return ( + + } + /> + + ); + } + +} + +export default restController(NETWORK_SETTINGS_ENDPOINT, NetworkSettingsController); \ No newline at end of file diff --git a/interface/src/wifi/WiFiSettingsForm.tsx b/interface/src/network/NetworkSettingsForm.tsx similarity index 82% rename from interface/src/wifi/WiFiSettingsForm.tsx rename to interface/src/network/NetworkSettingsForm.tsx index 839c3813e..c83e10034 100644 --- a/interface/src/wifi/WiFiSettingsForm.tsx +++ b/interface/src/network/NetworkSettingsForm.tsx @@ -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; +type NetworkStatusFormProps = RestFormProps; -class WiFiSettingsForm extends React.Component { +class NetworkSettingsForm extends React.Component { - static contextType = WiFiConnectionContext; - context!: React.ContextType; + static contextType = NetworkConnectionContext; + context!: React.ContextType; - 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 { const { selectedNetwork, deselectNetwork } = this.context; const { data, handleValueChange, saveData } = this.props; return ( - + { selectedNetwork ? @@ -117,6 +119,17 @@ class WiFiSettingsForm extends React.Component { onChange={handleValueChange('hostname')} margin="normal" /> + + None (wifi only) + Profile 1 (LAN8720) + Profile 2 (TLK110) + { } } -export default WiFiSettingsForm; \ No newline at end of file +export default NetworkSettingsForm; \ No newline at end of file diff --git a/interface/src/network/NetworkStatus.ts b/interface/src/network/NetworkStatus.ts new file mode 100644 index 000000000..fa83dd7b0 --- /dev/null +++ b/interface/src/network/NetworkStatus.ts @@ -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"; + } +} diff --git a/interface/src/network/NetworkStatusController.tsx b/interface/src/network/NetworkStatusController.tsx new file mode 100644 index 000000000..46b286a64 --- /dev/null +++ b/interface/src/network/NetworkStatusController.tsx @@ -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; + +class NetworkStatusController extends Component { + + componentDidMount() { + this.props.loadData(); + } + + render() { + return ( + + } + /> + + ); + } + +} + +export default restController(NETWORK_STATUS_ENDPOINT, NetworkStatusController); diff --git a/interface/src/wifi/WiFiStatusForm.tsx b/interface/src/network/NetworkStatusForm.tsx similarity index 84% rename from interface/src/wifi/WiFiStatusForm.tsx rename to interface/src/network/NetworkStatusForm.tsx index 7d5307cc6..77a097998 100644 --- a/interface/src/wifi/WiFiStatusForm.tsx +++ b/interface/src/network/NetworkStatusForm.tsx @@ -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 & WithTheme; +type NetworkStatusFormProps = RestFormProps & WithTheme; -class WiFiStatusForm extends Component { +class NetworkStatusForm extends Component { - dnsServers(status: WiFiStatus) { + dnsServers(status: NetworkStatus) { if (!status.dns_ip_1) { return "none"; } @@ -31,15 +31,15 @@ class WiFiStatusForm extends Component { - + - + { - isConnected(data) && + isWiFi(data) && @@ -50,6 +50,10 @@ class WiFiStatusForm extends Component { + + } + { isConnected(data) && + IP @@ -114,4 +118,4 @@ class WiFiStatusForm extends Component { } -export default withTheme(WiFiStatusForm); +export default withTheme(NetworkStatusForm); diff --git a/interface/src/wifi/WiFiNetworkScanner.tsx b/interface/src/network/WiFiNetworkScanner.tsx similarity index 100% rename from interface/src/wifi/WiFiNetworkScanner.tsx rename to interface/src/network/WiFiNetworkScanner.tsx diff --git a/interface/src/wifi/WiFiNetworkSelector.tsx b/interface/src/network/WiFiNetworkSelector.tsx similarity index 88% rename from interface/src/wifi/WiFiNetworkSelector.tsx rename to interface/src/network/WiFiNetworkSelector.tsx index 204365195..04aa7d660 100644 --- a/interface/src/wifi/WiFiNetworkSelector.tsx +++ b/interface/src/network/WiFiNetworkSelector.tsx @@ -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 { - static contextType = WiFiConnectionContext; - context!: React.ContextType; + static contextType = NetworkConnectionContext; + context!: React.ContextType; renderNetwork = (network: WiFiNetwork) => { return ( diff --git a/interface/src/wifi/WiFiSecurityModes.ts b/interface/src/network/WiFiSecurityModes.ts similarity index 100% rename from interface/src/wifi/WiFiSecurityModes.ts rename to interface/src/network/WiFiSecurityModes.ts diff --git a/interface/src/wifi/types.ts b/interface/src/network/types.ts similarity index 83% rename from interface/src/wifi/types.ts rename to interface/src/network/types.ts index d631051e6..22097a97d 100644 --- a/interface/src/wifi/types.ts +++ b/interface/src/network/types.ts @@ -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; diff --git a/interface/src/system/SystemStatusForm.tsx b/interface/src/system/SystemStatusForm.tsx index 9be36e79f..2ccf7b274 100644 --- a/interface/src/system/SystemStatusForm.tsx +++ b/interface/src/system/SystemStatusForm.tsx @@ -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) } - - - - - - - - - - - - - - - - - - diff --git a/interface/src/system/types.ts b/interface/src/system/types.ts index bad8765d3..b45cfc9ed 100644 --- a/interface/src/system/types.ts +++ b/interface/src/system/types.ts @@ -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; diff --git a/interface/src/wifi/WiFiConnection.tsx b/interface/src/wifi/WiFiConnection.tsx deleted file mode 100644 index c6cef06f3..000000000 --- a/interface/src/wifi/WiFiConnection.tsx +++ /dev/null @@ -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 { - - 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 ( - - - - - - - - - - - - - - - - ) - } -} - -export default withAuthenticatedContext(WiFiConnection); diff --git a/interface/src/wifi/WiFiConnectionContext.tsx b/interface/src/wifi/WiFiConnectionContext.tsx deleted file mode 100644 index 85b0c17dd..000000000 --- a/interface/src/wifi/WiFiConnectionContext.tsx +++ /dev/null @@ -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 -); diff --git a/interface/src/wifi/WiFiSettingsController.tsx b/interface/src/wifi/WiFiSettingsController.tsx deleted file mode 100644 index bac67ea7c..000000000 --- a/interface/src/wifi/WiFiSettingsController.tsx +++ /dev/null @@ -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; - -class WiFiSettingsController extends Component { - - componentDidMount() { - this.props.loadData(); - } - - render() { - return ( - - } - /> - - ); - } - -} - -export default restController(WIFI_SETTINGS_ENDPOINT, WiFiSettingsController); \ No newline at end of file diff --git a/interface/src/wifi/WiFiStatus.ts b/interface/src/wifi/WiFiStatus.ts deleted file mode 100644 index 2d3574fc0..000000000 --- a/interface/src/wifi/WiFiStatus.ts +++ /dev/null @@ -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"; - } -} diff --git a/interface/src/wifi/WiFiStatusController.tsx b/interface/src/wifi/WiFiStatusController.tsx deleted file mode 100644 index 222059546..000000000 --- a/interface/src/wifi/WiFiStatusController.tsx +++ /dev/null @@ -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; - -class WiFiStatusController extends Component { - - componentDidMount() { - this.props.loadData(); - } - - render() { - return ( - - } - /> - - ); - } - -} - -export default restController(WIFI_STATUS_ENDPOINT, WiFiStatusController); diff --git a/lib/framework/APSettingsService.cpp b/lib/framework/APSettingsService.cpp index b847748f3..6642a36a0 100644 --- a/lib/framework/APSettingsService.cpp +++ b/lib/framework/APSettingsService.cpp @@ -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); } diff --git a/lib/framework/APSettingsService.h b/lib/framework/APSettingsService.h index abc6aca9a..9f8800511 100644 --- a/lib/framework/APSettingsService.h +++ b/lib/framework/APSettingsService.h @@ -7,8 +7,10 @@ #include #include -#include +#include + +#include #define MANAGE_NETWORK_DELAY 10000 @@ -111,7 +113,7 @@ class APSettingsService : public StatefulService { // for the captive portal DNSServer * _dnsServer; - // for the mangement delay loop + // for the management delay loop volatile unsigned long _lastManaged; volatile boolean _reconfigureAp; diff --git a/lib/framework/ESP8266React.cpp b/lib/framework/ESP8266React.cpp index 378cd349d..4364b5ee9 100644 --- a/lib/framework/ESP8266React.cpp +++ b/lib/framework/ESP8266React.cpp @@ -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 } diff --git a/lib/framework/ESP8266React.h b/lib/framework/ESP8266React.h index 3698298fb..2abf17d19 100644 --- a/lib/framework/ESP8266React.h +++ b/lib/framework/ESP8266React.h @@ -3,13 +3,9 @@ #include -#ifdef ESP32 #include #include -#elif defined(ESP8266) -#include -#include -#endif +#include #include #include @@ -26,8 +22,8 @@ #include #include #include -#include -#include +#include +#include #ifdef PROGMEM_WWW #include @@ -44,33 +40,26 @@ class ESP8266React { return &_securitySettingsService; } -#if FT_ENABLED(FT_SECURITY) StatefulService * getSecuritySettingsService() { return &_securitySettingsService; } -#endif - StatefulService * getWiFiSettingsService() { - return &_wifiSettingsService; + StatefulService * getNetworkSettingsService() { + return &_networkSettingsService; } StatefulService * getAPSettingsService() { return &_apSettingsService; } -#if FT_ENABLED(FT_NTP) StatefulService * getNTPSettingsService() { return &_ntpSettingsService; } -#endif -#if FT_ENABLED(FT_OTA) StatefulService * getOTASettingsService() { return &_otaSettingsService; } -#endif -#if FT_ENABLED(FT_MQTT) StatefulService * getMqttSettingsService() { return &_mqttSettingsService; } @@ -78,7 +67,6 @@ class ESP8266React { AsyncMqttClient * getMqttClient() { return _mqttSettingsService.getMqttClient(); } -#endif void factoryReset() { _factoryResetService.factoryReset(); @@ -87,31 +75,21 @@ 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; + NTPSettingsService _ntpSettingsService; + NTPStatus _ntpStatus; + OTASettingsService _otaSettingsService; + UploadFirmwareService _uploadFirmwareService; + MqttSettingsService _mqttSettingsService; + MqttStatus _mqttStatus; + AuthenticationService _authenticationService; + RestartService _restartService; + FactoryResetService _factoryResetService; + SystemStatus _systemStatus; }; #endif diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index de1c2ad0c..a6c7379bc 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -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); diff --git a/lib/framework/MqttSettingsService.h b/lib/framework/MqttSettingsService.h index f2a5ef92b..36a52b0bb 100644 --- a/lib/framework/MqttSettingsService.h +++ b/lib/framework/MqttSettingsService.h @@ -140,16 +140,8 @@ class MqttSettingsService : public StatefulService { // 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(); diff --git a/lib/framework/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp index f2bc468f5..ef1b4312a 100644 --- a/lib/framework/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -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(); -} +// 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(); + break; -void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { - // Serial.println(F("WiFi connection dropped, stopping NTP.")); - configureNTP(); -} -#elif defined(ESP8266) -void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP & event) { - // Serial.println(F("Got IP address, starting NTP Synchronization")); - configureNTP(); -} + 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; -void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) { - // Serial.println(F("WiFi connection dropped, stopping NTP.")); - configureNTP(); + + + 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(); } } diff --git a/lib/framework/NTPSettingsService.h b/lib/framework/NTPSettingsService.h index efd4ee3d0..1bc42c76b 100644 --- a/lib/framework/NTPSettingsService.h +++ b/lib/framework/NTPSettingsService.h @@ -5,11 +5,8 @@ #include #include -#ifdef ESP32 #include -#elif defined(ESP8266) -#include -#endif +#include #ifndef FACTORY_NTP_ENABLED #define FACTORY_NTP_ENABLED true @@ -67,16 +64,8 @@ class NTPSettingsService : public StatefulService { FSPersistence _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); }; diff --git a/lib/framework/NetworkSettingsService.cpp b/lib/framework/NetworkSettingsService.cpp new file mode 100644 index 000000000..9deb1d6d8 --- /dev/null +++ b/lib/framework/NetworkSettingsService.cpp @@ -0,0 +1,90 @@ +#include + +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; + } +} diff --git a/lib/framework/WiFiSettingsService.h b/lib/framework/NetworkSettingsService.h similarity index 63% rename from lib/framework/WiFiSettingsService.h rename to lib/framework/NetworkSettingsService.h index eeafae4d8..64b24e001 100644 --- a/lib/framework/WiFiSettingsService.h +++ b/lib/framework/NetworkSettingsService.h @@ -1,13 +1,15 @@ -#ifndef WiFiSettingsService_h -#define WiFiSettingsService_h +#ifndef NetworkSettingsService_h +#define NetworkSettingsService_h #include #include #include #include -#define WIFI_SETTINGS_FILE "/config/wifiSettings.json" -#define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings" +#include + +#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; + 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) { - 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; + 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 { +class NetworkSettingsService : public StatefulService { public: - WiFiSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager); + NetworkSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager); void begin(); void loop(); private: - HttpEndpoint _httpEndpoint; - FSPersistence _fsPersistence; - unsigned long _lastConnectionAttempt; + HttpEndpoint _httpEndpoint; + FSPersistence _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 diff --git a/lib/framework/NetworkStatus.cpp b/lib/framework/NetworkStatus.cpp new file mode 100644 index 000000000..5ae030209 --- /dev/null +++ b/lib/framework/NetworkStatus.cpp @@ -0,0 +1,57 @@ +#include + +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); +} diff --git a/lib/framework/NetworkStatus.h b/lib/framework/NetworkStatus.h new file mode 100644 index 000000000..1bf77704a --- /dev/null +++ b/lib/framework/NetworkStatus.h @@ -0,0 +1,25 @@ +#ifndef NetworkStatus_h +#define NetworkStatus_h + +#include +#include +#include + +#include +#include +#include +#include +#include + +#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 diff --git a/lib/framework/SystemStatus.cpp b/lib/framework/SystemStatus.cpp index 0bedb9ce0..6c89bbcc9 100644 --- a/lib/framework/SystemStatus.cpp +++ b/lib/framework/SystemStatus.cpp @@ -1,44 +1,25 @@ #include 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(); + root["esp_platform"] = "esp32"; + root["max_alloc_heap"] = ESP.getMaxAllocHeap(); + root["psram_size"] = ESP.getPsramSize(); + root["free_psram"] = ESP.getFreePsram(); + root["cpu_freq_mhz"] = ESP.getCpuFreqMHz(); + root["free_heap"] = ESP.getFreeHeap(); + 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); diff --git a/lib/framework/WiFiScanner.cpp b/lib/framework/WiFiScanner.cpp index 85f532a44..764db6fef 100644 --- a/lib/framework/WiFiScanner.cpp +++ b/lib/framework/WiFiScanner.cpp @@ -1,12 +1,8 @@ #include 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) { @@ -24,16 +20,12 @@ void WiFiScanner::listNetworks(AsyncWebServerRequest * request) { JsonObject root = response->getRoot(); JsonArray networks = root.createNestedArray("networks"); for (int i = 0; i < numNetworks; i++) { - JsonObject network = networks.createNestedObject(); - network["rssi"] = WiFi.RSSI(i); - network["ssid"] = WiFi.SSID(i); - network["bssid"] = WiFi.BSSIDstr(i); - network["channel"] = WiFi.channel(i); -#ifdef ESP32 + JsonObject network = networks.createNestedObject(); + network["rssi"] = WiFi.RSSI(i); + network["ssid"] = WiFi.SSID(i); + network["bssid"] = WiFi.BSSIDstr(i); + network["channel"] = WiFi.channel(i); 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 diff --git a/lib/framework/WiFiSettingsService.cpp b/lib/framework/WiFiSettingsService.cpp deleted file mode 100644 index af1667d81..000000000 --- a/lib/framework/WiFiSettingsService.cpp +++ /dev/null @@ -1,111 +0,0 @@ -#include - -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 diff --git a/lib/framework/WiFiStatus.cpp b/lib/framework/WiFiStatus.cpp deleted file mode 100644 index 84e6a1c84..000000000 --- a/lib/framework/WiFiStatus.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include - -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); -} diff --git a/lib/framework/WiFiStatus.h b/lib/framework/WiFiStatus.h deleted file mode 100644 index a8ceacf0c..000000000 --- a/lib/framework/WiFiStatus.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef WiFiStatus_h -#define WiFiStatus_h - -#ifdef ESP32 -#include -#include -#elif defined(ESP8266) -#include -#include -#endif - -#include -#include -#include -#include -#include - -#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 diff --git a/lib_standalone/Arduino.cpp b/lib_standalone/Arduino.cpp index d8ed7f47a..33c951517 100644 --- a/lib_standalone/Arduino.cpp +++ b/lib_standalone/Arduino.cpp @@ -21,8 +21,13 @@ #include +#include + NativeConsole Serial; +ETHClass ETH; +WiFiClass WiFi; + /* millis() on C++ native could be for non-Arduino millis(): diff --git a/lib_standalone/Arduino.h b/lib_standalone/Arduino.h index a82b210a9..3fae3b209 100644 --- a/lib_standalone/Arduino.h +++ b/lib_standalone/Arduino.h @@ -53,10 +53,10 @@ int digitalRead(uint8_t pin); #define PROGMEM #define PGM_P const char * -#define PSTR(s) \ - (__extension__({ \ - static const char __c[] = (s); \ - &__c[0]; \ +#define PSTR(s) \ + (__extension__({ \ + static const char __c[] = (s); \ + &__c[0]; \ })) class __FlashStringHelper; @@ -193,7 +193,11 @@ class NativeConsole : public Stream { unsigned char peek_data_; }; +#include + extern NativeConsole Serial; +extern ETHClass ETH; +extern WiFiClass WiFi; unsigned long millis(); diff --git a/lib_standalone/AsyncMqttClient.h b/lib_standalone/AsyncMqttClient.h index f1f36c209..1a7e6d29f 100644 --- a/lib_standalone/AsyncMqttClient.h +++ b/lib_standalone/AsyncMqttClient.h @@ -26,12 +26,12 @@ struct AsyncMqttClientMessageProperties { namespace AsyncMqttClientInternals { -typedef std::function OnConnectUserCallback; -typedef std::function OnDisconnectUserCallback; -typedef std::function OnSubscribeUserCallback; -typedef std::function OnUnsubscribeUserCallback; +typedef std::function OnConnectUserCallback; +typedef std::function OnDisconnectUserCallback; +typedef std::function OnSubscribeUserCallback; +typedef std::function OnUnsubscribeUserCallback; typedef std::function OnMessageUserCallback; -typedef std::function OnPublishUserCallback; +typedef std::function OnPublishUserCallback; }; // namespace AsyncMqttClientInternals class AsyncMqttClient { @@ -115,4 +115,4 @@ class AsyncMqttClient { bool _willRetain; }; -#endif \ No newline at end of file +#endif diff --git a/lib_standalone/ESP8266React.h b/lib_standalone/ESP8266React.h index e78922e4c..b0f1599df 100644 --- a/lib_standalone/ESP8266React.h +++ b/lib_standalone/ESP8266React.h @@ -10,6 +10,7 @@ #include #include #include +#include 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 { 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 * getWiFiSettingsService() { + StatefulService * getNetworkSettingsService() { return &_settings; } @@ -113,10 +113,7 @@ class ESP8266React { class EMSESPSettingsService { public: EMSESPSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager); - void begin(); - - private: }; class JsonUtils { diff --git a/lib_standalone/Features.h b/lib_standalone/Features.h index d2172972b..f51037050 100644 --- a/lib_standalone/Features.h +++ b/lib_standalone/Features.h @@ -33,5 +33,4 @@ #define FT_UPLOAD_FIRMWARE 0 #endif - #endif diff --git a/lib_standalone/Network.h b/lib_standalone/Network.h new file mode 100644 index 000000000..b4a24be19 --- /dev/null +++ b/lib_standalone/Network.h @@ -0,0 +1,180 @@ +#ifndef Network_h +#define Network_h + +#include +#include + +#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 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 diff --git a/lib_standalone/WString.h b/lib_standalone/WString.h index fe4b2dfe0..98cb2dcbb 100644 --- a/lib_standalone/WString.h +++ b/lib_standalone/WString.h @@ -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); diff --git a/platformio.ini b/platformio.ini index dd88cea1d..5643e36e9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 @@ -15,20 +14,11 @@ core_build_flags = -Wno-deprecated-declarations -Wreturn-type -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 diff --git a/src/WebStatusService.cpp b/src/WebStatusService.cpp index 0e387b8fa..b65a0b9f1 100644 --- a/src/WebStatusService.cpp +++ b/src/WebStatusService.cpp @@ -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) { - EMSESP::logger().info(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason); -} -void WebStatusService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { - 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 -} +// 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); + + 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()); #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(); diff --git a/src/WebStatusService.h b/src/WebStatusService.h index ae9bb3593..c9d4f448c 100644 --- a/src/WebStatusService.h +++ b/src/WebStatusService.h @@ -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 diff --git a/src/console.cpp b/src/console.cpp index 7658f3ea5..253a6e83c 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -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'); diff --git a/src/containers.h b/src/containers.h index 40a96a4e2..072f1f1ce 100644 --- a/src/containers.h +++ b/src/containers.h @@ -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 diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 1cef1d5da..afc081c39 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -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 diff --git a/src/emsesp.h b/src/emsesp.h index 1c4ab10c3..a3b384b52 100644 --- a/src/emsesp.h +++ b/src/emsesp.h @@ -40,7 +40,6 @@ #include "WebSettingsService.h" #include "WebAPIService.h" -// #include "containers.h" #include "emsdevice.h" #include "emsfactory.h" #include "telegram.h" diff --git a/src/locale_EN.h b/src/locale_EN.h index 7584c960a..9337cc424 100644 --- a/src/locale_EN.h +++ b/src/locale_EN.h @@ -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") diff --git a/src/mqtt.cpp b/src/mqtt.cpp index da44a5669..84c7f75a9 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -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) { diff --git a/src/system.cpp b/src/system.cpp index 9d7583c20..797afab13 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -107,9 +107,9 @@ void System::restart() { void System::wifi_reconnect() { LOG_INFO(F("Wifi reconnecting...")); 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 + delay(1000); // wait a second + EMSESP::webSettingsService.save(); // local settings + 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 & 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 & 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 & 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; diff --git a/src/system.h b/src/system.h index 3551eefe1..a5b8e07e3 100644 --- a/src/system.h +++ b/src/system.h @@ -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();