Skip to content

Commit 02ba38c

Browse files
committed
more improvements to documentation
fix demo led state on esp32
1 parent 2a52c78 commit 02ba38c

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

README.md

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ platform = espressif32
109109
board = node32s
110110
```
111111

112-
This is largley left as an exersise for the reader as everyone's requirements will vary.
112+
This is left as an exersise for the reader as everyone's requirements will vary.
113113

114114
### Running the interface locally
115115

@@ -325,15 +325,15 @@ void loop() {
325325

326326
### Developing with the framework
327327

328-
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 below.
328+
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 comprenensive example is provided by the demo project.
329329

330-
The following diagram visualises how the framework's modular components fit together. They are described in detail below.
330+
The following diagram visualises how the framework's modular components fit together, each feature is described in detail below.
331331

332332
![framework diagram](/media/framework.png?raw=true "framework diagram")
333333

334334
#### Settings service
335335

336-
The [SettingsService.h](lib/framework/SettingsService.h) class is a responsible for managing settings and interfacing with code which wants to control or respond to changes in those settings. You can define a data class to hold settings then build a SettingsService instance to manage them:
336+
The [SettingsService.h](lib/framework/SettingsService.h) class is a responsible for managing settings and interfacing with code which wants to change or respond to changes in them. You can define a data class to hold settings then build a SettingsService instance to manage them:
337337

338338
```cpp
339339
class LightSettings {
@@ -346,7 +346,7 @@ class LightSettingsService : public SettingsService<LightSettings> {
346346
};
347347
```
348348
349-
You may listen for changes to settings by registering an update handler callback. It is possible to remove an update handler later if required. An "origin" pointer is passed to the update handler which may point to the client or object which made the update.
349+
You may listen for changes to settings by registering an update handler callback. It is possible to remove an update handler later if required.
350350
351351
```cpp
352352
// register an update handler
@@ -360,6 +360,14 @@ update_handler_id_t myUpdateHandler = lightSettingsService.addUpdateHandler(
360360
lightSettingsService.removeUpdateHandler(myUpdateHandler);
361361
```
362362

363+
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:
364+
365+
Origin | Description
366+
----------------- | -----------
367+
endpoint | An update over REST (SettingsEndpoint)
368+
broker | An update sent over MQTT (SettingsBroker)
369+
socket:{clientId} | An update sent over WebSocket (SettingsSocket)
370+
363371
SettingsService exposes a read function which you may use to safely read the settings. This function takes care of protecting against parallel access to the settings in multi-core enviornments such as the ESP32.
364372

365373
```cpp
@@ -368,12 +376,12 @@ lightSettingsService.read([&](LightSettings& settings) {
368376
});
369377
```
370378

371-
SettingsService also exposes an update function which allows the caller to update the settings. The update function takes care of calling the registered update handler callbacks once the update is complete.
379+
SettingsService also exposes an update function which allows the caller to update the settings 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":
372380

373381
```cpp
374382
lightSettingsService.update([&](LightSettings& settings) {
375383
settings.on = true; // turn on the lights!
376-
});
384+
}, "timer");
377385
```
378386

379387
#### Serialization
@@ -447,7 +455,7 @@ class LightSettingsService : public SettingsService<LightSettings> {
447455

448456
The framework has security features to prevent unauthorized use of the device. This is driven by [SecurityManager.h](lib/framework/SecurityManager.h).
449457

450-
On successful authentication, the /rest/signIn endpoint issues a JWT which is then sent using Bearer Authentication. The framework come with built in predicates for verifying a users access level. The built in AuthenticationPredicates can be found in [SecurityManager.h](lib/framework/SecurityManager.h):
458+
On successful authentication, the /rest/signIn endpoint issues a JWT 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:
451459

452460
Predicate | Description
453461
-------------------- | -----------
@@ -480,32 +488,32 @@ getMQTTClient() | Provides direct access to the MQTT client instanc
480488

481489
These can be used to observe changes to settings. They can also be used to fetch or update settings.
482490

483-
------ TODO -----
484-
Fix documentation, provide serialization examples
485-
486491
Inspect the current WiFi settings:
487492

488493
```cpp
489-
WiFiSettings wifiSettings = esp8266React.getWiFiSettingsService()->fetch();
490-
Serial.print("The ssid is:");
491-
Serial.println(wifiSettings.ssid);
494+
esp8266React.getWiFiSettingsService()->read([&](WiFiSettings& settings) {
495+
Serial.print("The ssid is:");
496+
Serial.println(wifiSettings.ssid);
497+
});
492498
```
493499
494-
Configure the SSID and password:
500+
Configure the WiFi SSID and password manually:
495501
496502
```cpp
497-
WiFiSettings wifiSettings = esp8266React.getWiFiSettingsService()->fetch();
498-
wifiSettings.ssid = "MyNetworkSSID";
499-
wifiSettings.password = "MySuperSecretPassword";
500-
esp8266React.getWiFiSettingsService()->update(wifiSettings);
503+
esp8266React.getWiFiSettingsService()->update([&](WiFiSettings& settings) {
504+
wifiSettings.ssid = "MyNetworkSSID";
505+
wifiSettings.password = "MySuperSecretPassword";
506+
}, "myapp");
501507
```
502508

503509
Observe changes to the WiFiSettings:
504510

505511
```cpp
506-
esp8266React.getWiFiSettingsService()->addUpdateHandler([]() {
507-
Serial.println("The WiFi Settings were updated!");
508-
});
512+
esp8266React.getWiFiSettingsService()->addUpdateHandler(
513+
[&](String originId) {
514+
Serial.println("The WiFi Settings were updated!");
515+
}
516+
);
509517
```
510518

511519
## Libraries Used

lib/framework/SettingsSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SettingsSocket {
4343
_server->addHandler(&_webSocket);
4444
_server->on(socketPath, HTTP_GET, std::bind(&SettingsSocket::forbidden, this, std::placeholders::_1));
4545
}
46-
46+
4747
SettingsSocket(SettingsSerializer<T>* settingsSerializer,
4848
SettingsDeserializer<T>* settingsDeserializer,
4949
SettingsService<T>* settingsService,

platformio.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ default_envs = esp12e
66
build_flags=
77
-D NO_GLOBAL_ARDUINOOTA
88
; Uncomment ENABLE_CORS to enable Cross-Origin Resource Sharing (required for local React development)
9-
;-D ENABLE_CORS
9+
-D ENABLE_CORS
1010
-D CORS_ORIGIN=\"http://localhost:3000\"
1111
; Uncomment PROGMEM_WWW to enable the storage of the WWW data in PROGMEM
1212
;-D PROGMEM_WWW
@@ -24,7 +24,7 @@ framework = arduino
2424
monitor_speed = 115200
2525

2626
extra_scripts =
27-
pre:scripts/build_interface.py
27+
pre:scripts/build_interface.py
2828

2929
lib_deps =
3030
ArduinoJson@>=6.0.0,<7.0.0
@@ -37,6 +37,7 @@ board = esp12e
3737
board_build.f_cpu = 160000000L
3838

3939
[env:node32s]
40+
; Uncomment the min_spiffs.csv setting if using PROGMEM_WWW with ESP32
4041
;board_build.partitions = min_spiffs.csv
4142
platform = espressif32
4243
board = node32s

src/LightSettingsService.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@
1313
#define DEFAULT_LED_STATE false
1414
#define OFF_STATE "OFF"
1515
#define ON_STATE "ON"
16-
#define LED_ON 0x0
17-
#define LED_OFF 0x1
16+
17+
// Note that the built-in LED is on when the pin is low on most NodeMCU boards.
18+
// This is because the anode is tied to VCC and the cathode to the GPIO 4 (Arduino pin 2).
19+
#ifdef ESP32
20+
#define LED_ON 0x1
21+
#define LED_OFF 0x0
22+
#elif defined(ESP8266)
23+
#define LED_ON 0x0
24+
#define LED_OFF 0x1
25+
#endif
26+
1827

1928
#define LIGHT_SETTINGS_ENDPOINT_PATH "/rest/lightSettings"
2029
#define LIGHT_SETTINGS_SOCKET_PATH "/ws/lightSettings"

0 commit comments

Comments
 (0)