WordClockESP/README.md

605 lines
24 KiB
Markdown
Raw Normal View History

# ESP8266 React
[![Build Status](https://travis-ci.org/rjwats/esp8266-react.svg?branch=master)](https://travis-ci.org/rjwats/esp8266-react)
2019-06-02 22:52:29 +00:00
A simple, secure and extensible framework for IoT projects built on ESP8266/ESP32 platforms with responsive React front-end.
2019-06-02 22:52:29 +00:00
Designed to work with the PlatformIO IDE with [limited setup](#getting-started). Please read below for setup, build and upload instructions.
2019-06-01 09:39:57 +00:00
![Screenshots](/media/screenshots.png?raw=true "Screenshots")
2018-02-26 22:57:09 +00:00
2019-05-31 22:28:39 +00:00
## Features
2018-02-26 22:57:09 +00:00
2019-05-31 22:28:39 +00:00
Provides many of the features required for IoT projects:
2018-02-26 22:57:09 +00:00
2019-06-02 22:52:29 +00:00
* Configurable WiFi - Network scanner and WiFi configuration screen
2019-05-31 22:28:39 +00:00
* Configurable Access Point - Can be continuous or automatically enabled when WiFi connection fails
* Network Time - Synchronization with NTP
* MQTT - Connection to an MQTT broker for automation and monitoring
2019-05-31 22:28:39 +00:00
* Remote Firmware Updates - Enable secured OTA updates
* Security - Protected RESTful endpoints and a secured user interface
2018-02-26 22:57:09 +00:00
The back end is provided by a set of RESTful endpoints and the responsive React based front end is built using [Material-UI](https://material-ui.com/).
2019-06-01 17:47:37 +00:00
The front end has the prerequisite manifest file and icon, so it can be added to the home screen of a mobile device if required.
## Getting Started
### Prerequisites
You will need the following before you can get started.
* [PlatformIO](https://platformio.org/) - IDE for development
2019-05-31 22:28:39 +00:00
* [Node.js](https://nodejs.org) - For building the interface with npm
2019-06-01 18:27:08 +00:00
### Building and uploading the firmware
2019-05-31 22:28:39 +00:00
Pull the project and open it in PlatformIO. PlatformIO should download the ESP8266 platform and the project library dependencies automatically.
2019-06-01 18:27:08 +00:00
The project structure is as follows:
2019-05-31 22:43:02 +00:00
Resource | Description
-------------------------------- | ----------------------------------------------------------------------
[data/](data) | The file system image directory
[interface/](interface) | React based front end
[lib/framework/](lib/framework) | C++ back end for the ESP8266/ESP32 device
[src/](src) | The main.cpp and demo project to get you started
[scripts/](scripts) | Scripts that build the React interface as part of the platformio build
2019-06-01 17:49:53 +00:00
[platformio.ini](platformio.ini) | PlatformIO project configuration file
2019-05-31 22:43:02 +00:00
2019-06-02 22:52:29 +00:00
### Building the firmware
2019-06-01 18:27:08 +00:00
2019-06-03 21:18:59 +00:00
Once the platform and libraries are downloaded the back end should successfully build within PlatformIO.
The firmware may be built by pressing the "Build" button:
![build](/media/build.png?raw=true "build")
Alternatively type the run command:
```bash
platformio run
```
2019-06-01 09:39:57 +00:00
2019-06-01 18:27:08 +00:00
#### Uploading the firmware
2019-06-02 22:52:29 +00:00
The project is configured to upload over a serial connection by default. You can change this to use OTA updates by uncommenting the relevant lines in ['platformio.ini'](platformio.ini).
2019-06-01 18:27:08 +00:00
2019-06-02 22:52:29 +00:00
The firmware may be uploaded to the device by pressing the "Upload" button:
2019-06-01 18:27:08 +00:00
2019-06-02 22:52:29 +00:00
![uploadfw](/media/uploadfw.png?raw=true "uploadfw")
Alternatively run the 'upload' target:
2019-06-01 18:27:08 +00:00
```bash
platformio run -t upload
```
### Building & uploading the interface
The interface has been configured with create-react-app and react-app-rewired so the build can customized for the target device. The large artefacts are gzipped and source maps and service worker are excluded from the production build. This reduces the production build to around ~150k, which easily fits on the device.
The interface will be automatically built by PlatformIO before it builds the firmware. The project can be configured to serve the interface from either PROGMEM or SPIFFS as your project requires. The default configuration is to serve the content from PROGMEM, serving from SPIFFS requires an additional upload step which is documented below.
#### Serving the interface from PROGMEM
2018-02-26 22:57:09 +00:00
By default, the project is configured to serve the interface from PROGMEM. This can be disabled by removing the -D PROGMEM_WWW build flag in ['platformio.ini'](platformio.ini) and re-building the firmware. If this your desired approach you must manually [upload the file system image](#uploading-the-file-system-image) to the device.
2019-06-02 22:52:29 +00:00
The interface will consume ~150k of program space which can be problematic if you already have a large binary artefact or if you have added large dependencies to the interface. The ESP32 binaries are fairly large in there simplest form so the addition of the interface resources requires us to use special partitioning for the ESP32.
2019-06-02 22:52:29 +00:00
When building using the "node32s" profile, the project uses the custom [min_spiffs.csv](https://github.com/espressif/arduino-esp32/blob/master/tools/partitions/min_spiffs.csv) partitioning mode. You may want to disable this if you are manually uploading the file system image:
2019-06-02 22:52:29 +00:00
```yaml
[env:node32s]
board_build.partitions = min_spiffs.csv
platform = espressif32
board = node32s
2019-06-02 22:52:29 +00:00
```
#### Uploading the file system image
If service content from SPIFFS, disable the PROGMEM_WWW build flag and build the project. The compiled interface will be copied to [data/](data) by the build process and may now be uploaded to the device by pressing the "Upload File System image" button:
![uploadfs](/media/uploadfs.png?raw=true "uploadfs")
Alternatively run the 'uploadfs' target:
```bash
platformio run -t uploadfs
```
2019-06-01 18:27:08 +00:00
### Running the interface locally
2018-02-26 22:57:09 +00:00
You can run a development server locally to allow you preview changes to the front end without the need to upload a file system image to the device after each change.
Change to the ['interface'](interface) directory with your bash shell (or Git Bash) and use the standard commands you would with any react app built with create-react-app:
```bash
cd interface
```
Install the npm dependencies, if required and start the development server:
2019-05-31 22:43:02 +00:00
2018-02-26 22:57:09 +00:00
```bash
npm install
2018-02-26 22:57:09 +00:00
npm start
```
> **Tip**: You can (optionally) speed up the build by commenting out the call to build_interface.py under "extra scripts" during local development. This will prevent the npm process from building the production release every time the firmware is compiled significantly decreasing the build time.
2018-02-26 22:57:09 +00:00
2019-06-01 18:27:08 +00:00
#### Changing the endpoint root
The interface has a development environment which is enabled when running the development server using `npm start`. The environment file can be found in ['interface/.env.development'](interface/.env.development) and contains the HTTP root URL and the WebSocket root URL:
```properties
REACT_APP_HTTP_ROOT=http://192.168.0.99
REACT_APP_WEB_SOCKET_ROOT=ws://192.168.0.99
```
2018-02-26 23:10:27 +00:00
The `REACT_APP_HTTP_ROOT` and `REACT_APP_WEB_SOCKET_ROOT` properties can be modified to point a ESP device running the back end firmware.
> **Tip**: You must restart the development server for changes to the environment file to come into effect.
2019-06-01 18:27:08 +00:00
#### Enabling CORS
You can enable CORS on the back end by uncommenting the -D ENABLE_CORS build flag in ['platformio.ini'](platformio.ini) then re-building and uploading the firmware to the device. The default settings assume you will be accessing the development server on the default port on [http://localhost:3000](http://localhost:3000) this can also be changed if required:
```properties
2019-06-01 18:27:08 +00:00
-D ENABLE_CORS
-D CORS_ORIGIN=\"http://localhost:3000\"
```
## Factory settings
2019-06-01 18:27:08 +00:00
The firmware has built-in factory settings which act as default values for the various configurable services where settings are not saved on the file system. These settings can be overridden using the build flags defined in [factory_settings.ini](factory_settings.ini).
Customize the settings as you see fit, for example you might configure your home WiFi network as the factory default:
2019-06-01 18:27:08 +00:00
```ini
-D FACTORY_WIFI_SSID=\"My Awesome WiFi Network\"
-D FACTORY_WIFI_PASSWORD=\"secret\"
-D FACTORY_WIFI_HOSTNAME=\"awesome_light_controller\"
```
2019-06-01 18:27:08 +00:00
### Default access point settings
2019-06-03 20:58:07 +00:00
By default, the factory settings configure the device to bring up an access point on start up which can be used to configure the device:
2019-06-01 18:27:08 +00:00
* SSID: ESP8266-React
* Password: esp-react
2019-06-03 20:58:07 +00:00
### Security settings and user credentials
By default, the factory settings configure two user accounts with the following credentials:
2019-06-03 20:58:07 +00:00
Username | Password
-------- | --------
admin | admin
guest | guest
It is recommended that you change the user credentials from their defaults better protect your device. You can do this in the user interface, or by modifying [factory_settings.ini](factory_settings.ini) as mentioned above.
### Customizing the factory time zone setting
Changing factory time zone setting is a common requirement. This requires a little effort because the time zone name and POSIX format are stored as separate values for the moment. The time zone names and POSIX formats are contained in the UI code in [TZ.ts](interface/src/ntp/TZ.ts). Take the appropriate pair of values from there, for example, for Los Angeles you would use:
```ini
-D FACTORY_NTP_TIME_ZONE_LABEL=\"America/Los_Angeles\"
-D FACTORY_NTP_TIME_ZONE_FORMAT=\"PST8PDT,M3.2.0,M11.1.0\"
```
### Device ID factory defaults
If not overridden with a build flag, the firmware will use the device ID to generate factory defaults for settings such as the JWT secret and MQTT client ID.
> **Tip**: Random values are generally better defaults for these settings, so it is recommended you leave these flags undefined.
2019-06-03 20:58:07 +00:00
## Building for different devices
2019-06-01 18:27:08 +00:00
This project supports ESP8266 and ESP32 platforms. To support OTA programming, enough free space to upload the new sketch and file system image will be required. It is recommended that a board with at least 2mb of flash is used.
The pre-configured environments are "esp12e" and "node32s". These are common ESP8266/ESP32 variants with 4mb of flash:
2019-06-01 18:27:08 +00:00
![ESP12E](/media/esp12e.jpg?raw=true "ESP12E") ![ESP32](/media/esp32.jpg?raw=true "ESP32")
2019-06-01 18:27:08 +00:00
The settings file ['platformio.ini'](platformio.ini) configures the supported environments. Modify these, or add new environments for the devides you need to support. The default environments are as follows:
2019-06-01 18:27:08 +00:00
```yaml
2019-06-01 18:27:08 +00:00
[env:esp12e]
platform = espressif8266
board = esp12e
board_build.f_cpu = 160000000L
2019-06-01 18:27:08 +00:00
[env:node32s]
platform = espressif32
board = node32s
```
If you want to build for a different device, all you need to do is re-configure ['platformio.ini'](platformio.ini) and select an alternative environment by modifying the default_envs variable. Building for the common esp32 "node32s" board for example:
```yaml
[platformio]
;default_envs = esp12e
default_envs = node32s
```
2019-06-01 13:57:00 +00:00
## Customizing and theming
2019-06-03 21:40:11 +00:00
The framework, and MaterialUI allows for a reasonable degree of customization with little effort.
2019-06-01 13:57:00 +00:00
### Theming the app
The app can be easily themed by editing the [MaterialUI theme](https://material-ui.com/customization/theming/). Edit the theme in ['interface/src/CustomMuiTheme.tsx'](interface/src/CustomMuiTheme.tsx) as you desire. For example, here is a dark theme:
2019-06-01 13:57:00 +00:00
```js
const theme = createMuiTheme({
palette: {
type:"dark",
primary: {
main: '#222',
},
secondary: {
main: '#666',
},
info: {
main: blueGrey[900]
},
warning: {
main: orange[500]
},
error: {
main: red[500]
},
success: {
main: green[500]
}
}
2019-06-01 13:57:00 +00:00
});
```
![Dark Theme](/media/dark.png?raw=true "Dark Theme")
2019-06-01 13:57:00 +00:00
### Changing the app icon
2019-06-01 18:27:08 +00:00
You can replace the app icon is located at ['interface/public/app/icon.png'](interface/public/app/icon.png) with one of your preference. A 256 x 256 PNG is recommended for best compatibility.
2019-06-01 09:39:57 +00:00
2019-06-01 13:57:00 +00:00
### Changing the app name
2019-06-01 18:27:08 +00:00
The app name displayed on the login page and on the menu bar can be modified by editing the REACT_APP_NAME property in ['interface/.env'](interface/.env)
2019-06-01 13:57:00 +00:00
```properties
2019-06-01 13:57:00 +00:00
REACT_APP_NAME=Funky IoT Project
2019-06-01 09:39:57 +00:00
```
2019-06-01 13:57:00 +00:00
2019-06-01 18:27:08 +00:00
There is also a manifest file which contains the app name to use when adding the app to a mobile device, so you may wish to also edit ['interface/public/app/manifest.json'](interface/public/app/manifest.json):
2019-06-01 13:57:00 +00:00
```json
{
"name":"Funky IoT Project",
"icons":[
{
"src":"/app/icon.png",
"sizes":"48x48 72x72 96x96 128x128 256x256"
}
],
"start_url":"/",
"display":"fullscreen",
"orientation":"any"
}
2019-06-01 09:39:57 +00:00
```
## Back end
2018-03-03 11:45:02 +00:00
2019-08-10 11:35:26 +00:00
The back end is a set of REST endpoints hosted by a [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer) instance. The ['lib/framework'](lib/framework) directory contains the majority of the back end code. The framework contains of a number of useful utility classes which you can use when extending it. The project also comes with a demo project to give you some help getting started.
2018-03-03 11:45:02 +00:00
2019-08-10 11:35:26 +00:00
The framework's source is split up by feature, for example [WiFiScanner.h](lib/framework/WiFiScanner.h) implements the end points for scanning for available networks where as [WiFiSettingsService.h](lib/framework/WiFiSettingsService.h) handles configuring the WiFi settings and managing the WiFi connection.
2019-08-10 11:35:26 +00:00
### Initializing the framework
The ['src/main.cpp'](src/main.cpp) file constructs the webserver and initializes the framework. You can add endpoints to the server here to support your IoT project. The main loop is also accessable so you can run your own code easily.
The following code creates the web server and esp8266React framework:
2019-08-10 11:35:26 +00:00
```cpp
AsyncWebServer server(80);
ESP8266React esp8266React(&server, &SPIFFS);
2019-08-10 11:35:26 +00:00
```
Now in the `setup()` function the initialization is performed:
```cpp
void setup() {
// start serial and filesystem
Serial.begin(SERIAL_BAUD_RATE);
// start the file system (must be done before starting the framework)
#ifdef ESP32
SPIFFS.begin(true);
#elif defined(ESP8266)
2019-08-10 11:35:26 +00:00
SPIFFS.begin();
#endif
2019-08-10 11:35:26 +00:00
// start the framework and demo project
esp8266React.begin();
2019-08-10 11:35:26 +00:00
// start the server
server.begin();
}
```
Finally the loop calls the framework's loop function to service the frameworks features.
2019-08-10 11:35:26 +00:00
```cpp
void loop() {
// run the framework's loop function
esp8266React.loop();
2019-08-10 11:35:26 +00:00
}
```
### Developing with the framework
2019-08-10 11:35:26 +00:00
The framework promotes a modular design and exposes features you may re-use to speed up the development of your project. Where possible it is recommended that you use the features the frameworks supplies. These are documented in this section and a comprehensive example is provided by the demo project.
2019-08-10 11:35:26 +00:00
The following diagram visualises how the framework's modular components fit together, each feature is described in detail below.
2019-08-10 11:35:26 +00:00
![framework diagram](/media/framework.png?raw=true "framework diagram")
2019-08-10 11:35:26 +00:00
#### Stateful service
2019-08-10 11:35:26 +00:00
The [StatefulService.h](lib/framework/StatefulService.h) class is a responsible for managing state and interfacing with code which wants to change or respond to changes in that state. You can define a data class to hold some state, then build a StatefulService class to manage its state:
2019-08-10 11:35:26 +00:00
```cpp
class LightState {
public:
bool on = false;
uint8_t brightness = 255;
};
2019-08-10 11:35:26 +00:00
class LightStateService : public StatefulService<LightState> {
};
```
2019-08-10 11:35:26 +00:00
You may listen for changes to state by registering an update handler callback. It is possible to remove an update handler later if required.
2019-08-10 11:35:26 +00:00
```cpp
// register an update handler
update_handler_id_t myUpdateHandler = lightStateService.addUpdateHandler(
[&](const String& originId) {
Serial.println("The light's state has been updated");
}
2019-08-10 11:35:26 +00:00
);
// remove the update handler
lightStateService.removeUpdateHandler(myUpdateHandler);
```
An "originId" is passed to the update handler which may be used to identify the origin of the update. The default origin values the framework provides are:
Origin | Description
--------------------- | -----------
http | An update sent over REST (HttpEndpoint)
mqtt | An update sent over MQTT (MqttPubSub)
websocket:{clientId} | An update sent over WebSocket (WebSocketRxTx)
StatefulService exposes a read function which you may use to safely read the state. This function takes care of protecting against parallel access to the state in multi-core enviornments such as the ESP32.
```cpp
lightStateService.read([&](LightState& state) {
digitalWrite(LED_PIN, state.on ? HIGH : LOW); // apply the state update to the LED_PIN
});
2019-08-10 11:35:26 +00:00
```
StatefulService also exposes an update function which allows the caller to update the state with a callback. This approach automatically calls the registered update handlers when complete. The example below turns on the lights using the arbitrary origin "timer":
2019-08-10 11:35:26 +00:00
```cpp
lightStateService.update([&](LightState& state) {
state.on = true; // turn on the lights!
}, "timer");
```
2018-03-03 11:45:02 +00:00
#### Serialization
When transmitting state over HTTP, WebSockets, or MQTT it must to be marshalled into a serializable form (JSON). The framework uses ArduinoJson for serialization and the functions defined in [JsonSerializer.h](lib/framework/JsonSerializer.h) and [JsonDeserializer.h](lib/framework/JsonDeserializer.h) facilitate this.
The static functions below can be used to facilitate the serialization/deserialization of the light state:
2018-03-03 11:45:02 +00:00
```cpp
class LightState {
public:
bool on = false;
uint8_t brightness = 255;
static void serialize(LightState& state, JsonObject& root) {
root["on"] = state.on;
root["brightness"] = state.brightness;
}
2018-03-03 11:45:02 +00:00
static void deserialize(JsonObject& root, LightState& state) {
state.on = root["on"] | false;
state.brightness = root["brightness"] | 255;
}
};
```
For convenience, the StatefulService class provides overloads of its `update` and `read` functions which utilize these functions.
2018-03-03 11:45:02 +00:00
Copy the state to a JsonObject using a serializer:
2018-03-03 11:45:02 +00:00
```cpp
JsonObject jsonObject = jsonDocument.to<JsonObject>();
lightStateService->read(jsonObject, serializer);
```
2018-03-03 12:09:03 +00:00
Update the state from a JsonObject using a deserializer:
```cpp
JsonObject jsonObject = jsonDocument.as<JsonObject>();
lightStateService->update(jsonObject, deserializer, "timer");
```
2018-03-03 11:45:02 +00:00
#### Endpoints
2018-03-03 11:45:02 +00:00
The framework provides an [HttpEndpoint.h](lib/framework/HttpEndpoint.h) class which may be used to register GET and POST handlers to read and update the state over HTTP. You may construct an HttpEndpoint as a part of the StatefulService or separately if you prefer.
2018-03-03 11:45:02 +00:00
The code below demonstrates how to extend the LightStateService class to provide an unsecured endpoint:
2018-03-03 11:45:02 +00:00
```cpp
class LightStateService : public StatefulService<LightState> {
public:
LightStateService(AsyncWebServer* server) :
_httpEndpoint(LightState::serialize, LightState::deserialize, this, server, "/rest/lightState") {
}
private:
HttpEndpoint<LightState> _httpEndpoint;
2018-03-03 11:45:02 +00:00
};
```
Endpoint security is provided by authentication predicates which are [documented below](#security-features). The SecurityManager and authentication predicate may be provided if a secure endpoint is required. The demo project shows how endpoints can be secured.
#### Persistence
[FSPersistence.h](lib/framework/FSPersistence.h) allows you to save state to the filesystem. FSPersistence automatically writes changes to the file system when state is updated. This feature can be disabled by calling `disableUpdateHandler()` if manual control of persistence is required.
The code below demonstrates how to extend the LightStateService class to provide persistence:
2018-03-03 12:09:03 +00:00
```cpp
class LightStateService : public StatefulService<LightState> {
public:
LightStateService(FS* fs) :
_fsPersistence(LightState::serialize, LightState::deserialize, this, fs, "/config/lightState.json") {
}
2018-03-03 12:09:03 +00:00
private:
FSPersistence<LightState> _fsPersistence;
};
2018-03-03 12:09:03 +00:00
```
#### WebSockets
2018-03-03 12:09:03 +00:00
[WebSocketTxRx.h](lib/framework/WebSocketTxRx.h) allows you to read and update state over a WebSocket connection. WebSocketTxRx automatically pushes changes to all connected clients when state is updated.
The code below demonstrates how to extend the LightStateService class to provide an unsecured WebSocket:
2018-03-03 12:09:03 +00:00
```cpp
class LightStateService : public StatefulService<LightState> {
public:
LightStateService(AsyncWebServer* server) :
_webSocket(LightState::serialize, LightState::deserialize, this, server, "/ws/lightState"), {
}
2018-03-03 12:09:03 +00:00
private:
WebSocketTxRx<LightState> _webSocket;
};
```
2018-03-03 12:09:03 +00:00
WebSocket security is provided by authentication predicates which are [documented below](#security-features). The SecurityManager and authentication predicate may be provided if a secure WebSocket is required. The demo project shows how WebSockets can be secured.
2018-03-03 12:09:03 +00:00
#### MQTT
2018-03-03 12:09:03 +00:00
The framework includes an MQTT client which can be configured via the UI. MQTT requirements will differ from project to project so the framework exposes the client for you to use as you see fit. The framework does however provide a utility to interface StatefulService to a pair of pub/sub (state/set) topics. This utility can be used to synchronize state with software such as Home Assistant.
[MqttPubSub.h](lib/framework/MqttPubSub.h) allows you to publish and subscribe to synchronize state over a pair of MQTT topics. MqttPubSub automatically pushes changes to the "pub" topic and reads updates from the "sub" topic.
The code below demonstrates how to extend the LightStateService class to interface with MQTT:
```cpp
class LightStateService : public StatefulService<LightState> {
public:
LightStateService(AsyncMqttClient* mqttClient) :
_mqttPubSub(LightState::serialize,
LightState::deserialize,
this,
mqttClient,
"homeassistant/light/my_light/set",
"homeassistant/light/my_light/state") {
}
private:
MqttPubSub<LightState> _mqttPubSub;
};
```
You can re-configure the pub/sub topics at runtime as required:
```cpp
_mqttPubSub.configureBroker("homeassistant/light/desk_lamp/set", "homeassistant/light/desk_lamp/state");
```
The demo project allows the user to modify the MQTT topics via the UI so they can be changed without re-flashing the firmware.
### Security features
The framework has security features to prevent unauthorized use of the device. This is driven by [SecurityManager.h](lib/framework/SecurityManager.h).
On successful authentication, the /rest/signIn endpoint issues a [JSON Web Token (JWT)](https://jwt.io/) which is then sent using Bearer Authentication. The framework come with built-in predicates for verifying a users access privileges. The built in AuthenticationPredicates can be found in [SecurityManager.h](lib/framework/SecurityManager.h) and are as follows:
Predicate | Description
-------------------- | -----------
NONE_REQUIRED | No authentication is required.
IS_AUTHENTICATED | Any authenticated principal is permitted.
IS_ADMIN | The authenticated principal must be an admin.
You can use the security manager to wrap any request handler function with an authentication predicate:
```cpp
server->on("/rest/someService", HTTP_GET,
_securityManager->wrapRequest(std::bind(&SomeService::someService, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
);
```
### Accessing settings and services
The framework supplies access to various features via getter functions:
SettingsService | Description
---------------------------- | ----------------------------------------------
getSecurityManager() | The security manager - detailed above
getSecuritySettingsService() | Configures the users and other security settings
getWiFiSettingsService() | Configures and manages the WiFi network connection
getAPSettingsService() | Configures and manages the Access Point
getNTPSettingsService() | Configures and manages the network time
getOTASettingsService() | Configures and manages the Over-The-Air update feature
getMqttSettingsService() | Configures and manages the MQTT connection
getMqttClient() | Provides direct access to the MQTT client instance
The core features use the [StatefulService.h](lib/framework/StatefulService.h) class and can therefore you can change settings or observe changes to settings through the read/update API.
Inspect the current WiFi settings:
```cpp
esp8266React.getWiFiSettingsService()->read([&](WiFiSettings& wifiSettings) {
Serial.print("The ssid is:");
Serial.println(wifiSettings.ssid);
});
```
Configure the WiFi SSID and password manually:
```cpp
esp8266React.getWiFiSettingsService()->update([&](WiFiSettings& wifiSettings) {
wifiSettings.ssid = "MyNetworkSSID";
wifiSettings.password = "MySuperSecretPassword";
}, "myapp");
```
Observe changes to the WiFiSettings:
```cpp
esp8266React.getWiFiSettingsService()->addUpdateHandler(
[&](const String& originId) {
Serial.println("The WiFi Settings were updated!");
}
);
2018-03-03 12:09:03 +00:00
```
## Libraries Used
* [React](https://reactjs.org/)
* [Material-UI](https://material-ui.com/)
2019-08-10 11:35:26 +00:00
* [notistack](https://github.com/iamhosseindhv/notistack)
* [ArduinoJson](https://github.com/bblanchon/ArduinoJson)
* [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer)
* [AsyncMqttClient](https://github.com/marvinroger/async-mqtt-client)