Skip to content

Commit 96d95b9

Browse files
committed
Start improving documentation to describe newly added and modified functionallity
(cherry picked from commit b25ab4f)
1 parent 9c94ce4 commit 96d95b9

File tree

2 files changed

+17
-96
lines changed

2 files changed

+17
-96
lines changed

README.md

Lines changed: 17 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Provides many of the features required for IoT projects:
1515
* Configurable WiFi - Network scanner and WiFi configuration screen
1616
* Configurable Access Point - Can be continuous or automatically enabled when WiFi connection fails
1717
* Network Time - Synchronization with NTP
18+
* MQTT - Connection to an MQTT broker for automation and monitoring
1819
* Remote Firmware Updates - Enable secured OTA updates
1920
* Security - Protected RESTful endpoints and a secured user interface
2021

@@ -154,11 +155,12 @@ The config files can be found in the ['data/config'](data/config) directory:
154155

155156
File | Description
156157
---- | -----------
157-
[apSettings.json](data/config/apSettings.json) | Access point settings
158-
[ntpSettings.json](data/config/ntpSettings.json) | NTP synchronization settings
159-
[otaSettings.json](data/config/otaSettings.json) | OTA update configuration
158+
[apSettings.json](data/config/apSettings.json) | Access point settings
159+
[mqttSettings.json](data/config/mqttSettings.json) | MQTT connection settings
160+
[ntpSettings.json](data/config/ntpSettings.json) | NTP synchronization settings
161+
[otaSettings.json](data/config/otaSettings.json) | OTA update configuration
160162
[securitySettings.json](data/config/securitySettings.json) | Security settings and user credentials
161-
[wifiSettings.json](data/config/wifiSettings.json) | WiFi connection settings
163+
[wifiSettings.json](data/config/wifiSettings.json) | WiFi connection settings
162164

163165
### Access point settings
164166

@@ -283,12 +285,11 @@ The framework's source is split up by feature, for example [WiFiScanner.h](lib/f
283285

284286
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.
285287

286-
The following code creates the web server, esp8266React framework and the demo project instance:
288+
The following code creates the web server and esp8266React framework:
287289

288290
```cpp
289291
AsyncWebServer server(80);
290292
ESP8266React esp8266React(&server, &SPIFFS);
291-
DemoProject demoProject = DemoProject(&server, &SPIFFS, esp8266React.getSecurityManager());
292293
```
293294
294295
Now in the `setup()` function the initialization is performed:
@@ -308,23 +309,17 @@ void setup() {
308309
// start the framework and demo project
309310
esp8266React.begin();
310311
311-
// start the demo project
312-
demoProject.begin();
313-
314312
// start the server
315313
server.begin();
316314
}
317315
```
318316

319-
Finally the loop calls the framework's loop function to service the frameworks features. You can add your own code in here, as shown with the demo project:
317+
Finally the loop calls the framework's loop function to service the frameworks features.
320318

321319
```cpp
322320
void loop() {
323321
// run the framework's loop function
324322
esp8266React.loop();
325-
326-
// run the demo project's loop function
327-
demoProject.loop();
328323
}
329324
```
330325

@@ -460,94 +455,17 @@ NONE_REQUIRED | No authentication is required.
460455
IS_AUTHENTICATED | Any authenticated principal is permitted.
461456
IS_ADMIN | The authenticated principal must be an admin.
462457

463-
You can use the security manager to wrap any web handler with an authentication predicate:
458+
You can use the security manager to wrap any request handler function with an authentication predicate:
464459

465460
```cpp
466461
server->on("/rest/someService", HTTP_GET,
467462
_securityManager->wrapRequest(std::bind(&SomeService::someService, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
468463
);
469464
```
470465

471-
Alternatively you can extend [AdminSettingsService.h](lib/framework/AdminSettingsService.h) and optionally override `getAuthenticationPredicate()` to secure an endpoint.
472-
473-
## Extending the framework
474-
475-
It is recommend that you explore the framework code to gain a better understanding of how to use it's features. The framework provides APIs so you can add your own services or features or, if required, directly configure or observe changes to core framework features. Some of these capabilities are detailed below.
476-
477-
### Adding a service with persistant settings
478-
479-
The following code demonstrates how you might extend the framework with a feature which requires a username and password to be configured to drive an unspecified feature.
480-
481-
```cpp
482-
#include <SettingsService.h>
483-
484-
class ExampleSettings {
485-
public:
486-
String username;
487-
String password;
488-
};
489-
490-
class ExampleSettingsService : public SettingsService<ExampleSettings> {
491-
492-
public:
493-
494-
ExampleSettingsService(AsyncWebServer* server, FS* fs)
495-
: SettingsService(server, fs, "/exampleSettings", "/config/exampleSettings.json") {}
496-
497-
~ExampleSettingsService(){}
498-
499-
protected:
500-
501-
void readFromJsonObject(JsonObject& root) {
502-
_settings.username = root["username"] | "";
503-
_settings.password = root["password"] | "";
504-
}
505-
506-
void writeToJsonObject(JsonObject& root) {
507-
root["username"] = _settings.username;
508-
root["password"] = _settings.password;
509-
}
510-
511-
};
512-
```
513-
514-
Now this can be constructed, added to the server, and started as such:
515-
516-
```cpp
517-
ExampleSettingsService exampleSettingsService = ExampleSettingsService(&server, &SPIFFS);
518-
519-
exampleSettingsService.begin();
520-
```
521-
522-
There will now be a REST service exposed on "/exampleSettings" for reading and writing (GET/POST) the settings. Any modifications will be persisted in SPIFFS, in this case to "/config/exampleSettings.json"
523-
524-
Sometimes you need to perform an action when the settings are updated, you can achieve this by overriding the onConfigUpdated() function which gets called every time the settings are updated. You can also perform an action when the service starts by overriding the begin() function, being sure to call SettingsService::begin(). You can also provide a "loop" function in order to allow your service class continuously perform an action, calling this from the main loop.
525-
526-
```cpp
527-
528-
void begin() {
529-
// make sure we call super, so the settings get read!
530-
SettingsService::begin();
531-
reconfigureTheService();
532-
}
533-
534-
void onConfigUpdated() {
535-
reconfigureTheService();
536-
}
537-
538-
void reconfigureTheService() {
539-
// do whatever is required to react to the new settings
540-
}
541-
542-
void loop() {
543-
// execute somthing as part of the main loop
544-
}
545-
546-
```
547-
548466
### Accessing settings and services
549467

550-
The framework supplies access to it's SettingsService instances and the SecurityManager via getter functions:
468+
The framework supplies access to various features via getter functions:
551469

552470
SettingsService | Description
553471
---------------------------- | ----------------------------------------------
@@ -557,8 +475,13 @@ getWiFiSettingsService() | Configures and manages the WiFi network connectio
557475
getAPSettingsService() | Configures and manages the Access Point
558476
getNTPSettingsService() | Configures and manages the network time
559477
getOTASettingsService() | Configures and manages the Over-The-Air update feature
478+
getMQTTSettingsService() | Configures and manages the MQTT connection
479+
getMQTTClient() | Provides direct access to the MQTT client instance
480+
481+
These can be used to observe changes to settings. They can also be used to fetch or update settings.
560482

561-
These can be used to observe changes to settings. They can also be used to fetch or update settings directly via objects, JSON strings and JsonObjects. Here are some examples of how you may use this.
483+
------ TODO -----
484+
Fix documentation, provide serialization examples
562485

563486
Inspect the current WiFi settings:
564487

@@ -592,3 +515,4 @@ esp8266React.getWiFiSettingsService()->addUpdateHandler([]() {
592515
* [notistack](https://github.com/iamhosseindhv/notistack)
593516
* [ArduinoJson](https://github.com/bblanchon/ArduinoJson)
594517
* [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer)
518+
* [AsyncMqttClient](https://github.com/marvinroger/async-mqtt-client)

data/config/demoSettings.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)