Skip to content

Commit b8f5d31

Browse files
committed
simplify ESP32_NVS to one funcn call
1 parent 81f0f9c commit b8f5d31

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

src/Wippersnapper.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,8 @@ void Wippersnapper::provision() {
8484
#elif defined(USE_NVS)
8585
// init esp32 nvs partition namespace
8686
_nvs = new Wippersnapper_ESP32_nvs();
87-
// validate esp32 has been programmed with credentials
88-
if (!_nvs->validateNVSConfig()) {
89-
WS_DEBUG_PRINTLN(
90-
"ERROR: NVS partition or credentials not found - was NVS flashed?");
91-
while (1)
92-
yield();
93-
}
94-
// pull values out of NVS configuration
95-
_nvs->setNVSConfig();
87+
// parse out secrets
88+
_nvs->parseSecrets();
9689
#endif
9790
// Set credentials
9891
set_user_key();

src/network_interfaces/Wippersnapper_ESP32.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ class Wippersnapper_ESP32 : public Wippersnapper {
100100
*/
101101
/********************************************************/
102102
void setupMQTTClient(const char *clientID) {
103+
WS_DEBUG_PRINT("BROKER URL: ");
104+
WS_DEBUG_PRINT(WS._mqttBrokerURL);
105+
106+
WS_DEBUG_PRINT("USERNAME: ");
107+
WS_DEBUG_PRINT(WS._username);
108+
WS_DEBUG_PRINT("KEY: ");
109+
WS_DEBUG_PRINT(WS._key);
110+
103111
if (WS._mqttBrokerURL == nullptr) {
104112
WS._mqttBrokerURL = "io.adafruit.com";
105113
_mqtt_client->setCACert(_aio_root_ca_prod);

src/provisioning/Wippersnapper_ESP32_nvs.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
*/
2323
/****************************************************************************/
2424
Wippersnapper_ESP32_nvs::Wippersnapper_ESP32_nvs() {
25-
// init. nvs, read-only
26-
nvs.begin("wsNamespace", false);
25+
// Attempt to initialize NVS partition
26+
if (!nvs.begin("wsNamespace", false)) {
27+
WS.setStatusLEDColor(RED);
28+
while (1)
29+
;
30+
}
2731
}
2832

2933
/****************************************************************************/
@@ -35,39 +39,31 @@ Wippersnapper_ESP32_nvs::~Wippersnapper_ESP32_nvs() { nvs.end(); }
3539

3640
/****************************************************************************/
3741
/*!
38-
@brief Reads and validates credentials from nvs' "wsNamespace"
42+
@brief Reads, validates, and sets credentials from nvs' "wsNamespace"
3943
namespace.
40-
@returns True if credentials were found, False otherwise.
4144
*/
4245
/****************************************************************************/
43-
bool Wippersnapper_ESP32_nvs::validateNVSConfig() {
46+
void Wippersnapper_ESP32_nvs::parseSecrets() {
47+
// parsey
4448
_ssid = nvs.getString("wsNetSSID", "");
4549
_ssidPass = nvs.getString("wsNetPass", "");
4650
_aioUser = nvs.getString("wsAIOUser", "");
4751
_aioPass = nvs.getString("wsAIOKey", "");
4852
_aioURL = nvs.getString("wsAIOURL", "");
4953

50-
// validate config properly set in partition
54+
// Validate configuration was set within the partition
5155
if (_ssid == "" || _ssidPass == "" || _aioUser == "" || _aioPass == "") {
52-
// TODO: Possibly LED blink/some external error handling around this
53-
return false;
56+
WS.setStatusLEDColor(RED);
57+
while (1)
58+
;
5459
}
55-
return true;
56-
}
5760

58-
/****************************************************************************/
59-
/*!
60-
@brief Sets Wippersnapper configuration using nvs configuration
61-
@returns True if credentials set successfully, False otherwise.
62-
*/
63-
/****************************************************************************/
64-
bool Wippersnapper_ESP32_nvs::setNVSConfig() {
61+
// Set global configuration strings
6562
WS._network_ssid = _ssid.c_str();
6663
WS._network_pass = _ssidPass.c_str();
6764
WS._username = _aioUser.c_str();
6865
WS._key = _aioPass.c_str();
6966
WS._mqttBrokerURL = _aioURL.c_str();
70-
return true;
7167
}
7268

7369
#endif // ARDUINO_ARCH_ESP32

src/provisioning/Wippersnapper_ESP32_nvs.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ class Wippersnapper_ESP32_nvs {
2929
public:
3030
Wippersnapper_ESP32_nvs();
3131
~Wippersnapper_ESP32_nvs();
32-
33-
bool validateNVSConfig();
34-
bool setNVSConfig();
32+
void parseSecrets();
3533

3634
Preferences nvs; ///< Provides access to ESP32's Non-Volatile Storage
3735

0 commit comments

Comments
 (0)