Skip to content

Commit

Permalink
Merge pull request #200 from adafruit/fix-esp32-connect-issue-b19
Browse files Browse the repository at this point in the history
Fix ESP32 Connection Issue on Beta 19
  • Loading branch information
brentru authored Jan 3, 2022
2 parents 81f0f9c + 6c125cc commit 21637dd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 57 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit WipperSnapper Beta
version=1.0.0-beta.19
version=1.0.0-beta.20
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino client for Adafruit.io WipperSnapper
Expand Down
34 changes: 4 additions & 30 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,14 @@ void Wippersnapper::provision() {
// init. LED for status signaling
statusLEDInit();
#ifdef USE_TINYUSB
// init new filesystem
_fileSystem = new Wippersnapper_FS();
// parse out secrets.json file
_fileSystem->parseSecrets();
#elif defined(USE_NVS)
// init esp32 nvs partition namespace
_nvs = new Wippersnapper_ESP32_nvs();
// validate esp32 has been programmed with credentials
if (!_nvs->validateNVSConfig()) {
WS_DEBUG_PRINTLN(
"ERROR: NVS partition or credentials not found - was NVS flashed?");
while (1)
yield();
}
// pull values out of NVS configuration
_nvs->setNVSConfig();
_nvs->parseSecrets();
#endif
// Set credentials
set_user_key();

// Set WiFi credentials within network interface
set_ssid_pass();
}

Expand All @@ -115,19 +104,6 @@ void Wippersnapper::set_user_key(const char *aio_username,
WS._key = aio_key;
}

/****************************************************************************/
/*!
@brief Configures the device's Adafruit IO credentials from the
secrets.json file.
*/
/****************************************************************************/
void Wippersnapper::set_user_key() {
#ifdef USE_TINYUSB
WS._username = _fileSystem->io_username;
WS._key = _fileSystem->io_key;
#endif
}

/**************************************************************************/
/*!
@brief Disconnects from Adafruit IO+ Wippersnapper.
Expand Down Expand Up @@ -1218,8 +1194,6 @@ void Wippersnapper::errorWriteHang(String error) {
/**************************************************************************/
void Wippersnapper::runNetFSM() {
WS.feedWDT();
// MQTT connack RC
int mqttRC;
// Initial state
fsm_net_t fsmNetwork;
fsmNetwork = FSM_NET_CHECK_MQTT;
Expand Down Expand Up @@ -1274,7 +1248,7 @@ void Wippersnapper::runNetFSM() {
maxAttempts = 10;
while (maxAttempts >= 0) {
setStatusLEDColor(LED_IO_CONNECT);
mqttRC = WS._mqtt->connect();
int8_t mqttRC = WS._mqtt->connect();
if (mqttRC == WS_MQTT_CONNECTED) {
fsmNetwork = FSM_NET_CHECK_MQTT;
break;
Expand Down
3 changes: 1 addition & 2 deletions src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#endif

#define WS_VERSION \
"1.0.0-beta.19" ///< WipperSnapper app. version (semver-formatted)
"1.0.0-beta.20" ///< WipperSnapper app. version (semver-formatted)

// Reserved Adafruit IO MQTT topics
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic
Expand Down Expand Up @@ -187,7 +187,6 @@ class Wippersnapper {
bool lockStatusLED = false; ///< True if status LED is using the built-in LED

void set_user_key(const char *aio_username, const char *aio_key);
void set_user_key();
virtual void set_ssid_pass(const char *ssid, const char *ssidPassword);
virtual void set_ssid_pass();

Expand Down
39 changes: 18 additions & 21 deletions src/provisioning/Wippersnapper_ESP32_nvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
*/
/****************************************************************************/
Wippersnapper_ESP32_nvs::Wippersnapper_ESP32_nvs() {
// init. nvs, read-only
nvs.begin("wsNamespace", false);
// Attempt to initialize NVS partition
if (!nvs.begin("wsNamespace", false)) {
WS.setStatusLEDColor(RED);
while (1)
;
}
}

/****************************************************************************/
Expand All @@ -35,39 +39,32 @@ Wippersnapper_ESP32_nvs::~Wippersnapper_ESP32_nvs() { nvs.end(); }

/****************************************************************************/
/*!
@brief Reads and validates credentials from nvs' "wsNamespace"
@brief Reads, validates, and sets credentials from nvs' "wsNamespace"
namespace.
@returns True if credentials were found, False otherwise.
*/
/****************************************************************************/
bool Wippersnapper_ESP32_nvs::validateNVSConfig() {
void Wippersnapper_ESP32_nvs::parseSecrets() {
// Parse from
// https://github.com/adafruit/Adafruit_WebSerial_NVMGenerator/blob/main/wsPartitions.csv
_ssid = nvs.getString("wsNetSSID", "");
_ssidPass = nvs.getString("wsNetPass", "");
_aioUser = nvs.getString("wsAIOUser", "");
_aioPass = nvs.getString("wsAIOKey", "");
_aioURL = nvs.getString("wsAIOURL", "");

// validate config properly set in partition
if (_ssid == "" || _ssidPass == "" || _aioUser == "" || _aioPass == "") {
// TODO: Possibly LED blink/some external error handling around this
return false;
// Validate getString() calls
if (_ssid.length() == 0 || _ssidPass.length() == 0 ||
_aioUser.length() == 0 || _aioPass.length() == 0) {
WS.setStatusLEDColor(RED);
while (1)
;
}
return true;
}

/****************************************************************************/
/*!
@brief Sets Wippersnapper configuration using nvs configuration
@returns True if credentials set successfully, False otherwise.
*/
/****************************************************************************/
bool Wippersnapper_ESP32_nvs::setNVSConfig() {
// Set global configuration strings
WS._network_ssid = _ssid.c_str();
WS._network_pass = _ssidPass.c_str();
WS._username = _aioUser.c_str();
WS._key = _aioPass.c_str();
WS._mqttBrokerURL = _aioURL.c_str();
return true;
WS._mqttBrokerURL = nullptr;
}

#endif // ARDUINO_ARCH_ESP32
4 changes: 1 addition & 3 deletions src/provisioning/Wippersnapper_ESP32_nvs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class Wippersnapper_ESP32_nvs {
public:
Wippersnapper_ESP32_nvs();
~Wippersnapper_ESP32_nvs();

bool validateNVSConfig();
bool setNVSConfig();
void parseSecrets();

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

Expand Down
2 changes: 2 additions & 0 deletions src/provisioning/tinyusb/Wippersnapper_FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ void Wippersnapper_FS::parseSecrets() {
"effect");
fsHalt();
}
WS._username = io_username;

// Get io key
io_key = doc["io_key"];
Expand All @@ -349,6 +350,7 @@ void Wippersnapper_FS::parseSecrets() {
writeErrorToBootOut("ERROR: invalid io_key value in secrets.json!");
fsHalt();
}
WS._key = io_key;

// next, we detect the network interface from the `secrets.json`
WS_DEBUG_PRINTLN("Attempting to find network interface...");
Expand Down

0 comments on commit 21637dd

Please sign in to comment.