Skip to content
Snippets Groups Projects
Commit 519e117b authored by thiebolt's avatar thiebolt
Browse files

update

parent 7617c063
Branches
Tags
No related merge requests found
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
* --- * ---
* TODO: * TODO:
* - add support for JSON structure to embedds several WiFi credentials * - add support for JSON structure to embedds several WiFi credentials
* e.g NVS-WiFi namespace --> key("credentials") = [ ["ssid1","pass1"], ["ssid2","pass2"], ... ]
* - add encryption with MAC addr or APpassword for example
* --- * ---
* F.Thiebolt may.23 initial release * F.Thiebolt may.23 initial release
*/ */
...@@ -71,6 +73,9 @@ void endLoop( void ) { ...@@ -71,6 +73,9 @@ void endLoop( void ) {
// --- MAIN -------------------------------------------------------------------- // --- MAIN --------------------------------------------------------------------
void setup() { void setup() {
char _answer, _tmp[256];
String _str;
delay(5000); delay(5000);
Serial.begin(115200); // debug link Serial.begin(115200); // debug link
Serial.println(F("\n\n\n[NVS] namespace demo ..."));Serial.flush(); Serial.println(F("\n\n\n[NVS] namespace demo ..."));Serial.flush();
...@@ -79,27 +84,121 @@ void setup() { ...@@ -79,27 +84,121 @@ void setup() {
#ifdef ESP32 #ifdef ESP32
/* NVS @ ESP32 */ /* NVS @ ESP32 */
Preferences nvs_wifi; Preferences nvs_wifi;
if( nvs_wifi.begin(WIFI_NVS_NAMESPACE,true) ) { // readonly mode
Serial.print(F("\n[wifiParams] opened NVS WiFi credentials namespace ..."));Serial.flush(); /*
// retrieve SSID/psk * Open NVS-WiFi namespace to retrieve SSID / PSK
nvs_wifi.getBytes(WIFI_NVS_SSID_KEY, mySSID, sizeof(mySSID)); */
nvs_wifi.getBytes(WIFI_NVS_PASS_KEY, myPSK, sizeof(myPSK)); if( not nvs_wifi.begin(WIFI_NVS_NAMESPACE,true) ) { // readonly mode
if( strlen(mySSID) and strlen(myPSK) ) { Serial.print(F("\n[NVS-WiFi] unable to open NVS namespace '"));Serial.print(WIFI_NVS_NAMESPACE);Serial.print(F("' ... reboot !"));Serial.flush();
// using retrived credentials to connect delay(2000);
Serial.print(F("\n[NVS-WiFi] retreieved WiFi credentials from NVS :)"));Serial.flush(); ESP.restart();
} delay(5000);
else { }
Serial.print(F("\n[NVS-WiFi] SSID and/or PSK missing ... cancel :("));Serial.flush();
mySSID[0] = '\0'; Serial.print(F("\n[NVS-WiFi] opened NVS WiFi credentials namespace ..."));Serial.flush();
myPSK[0] = '\0'; // retrieve SSID/psk
mySSID[0]='\0'; myPSK[0]='\0';
if( nvs_wifi.isKey(WIFI_NVS_SSID_KEY) ) {
nvs_wifi.getBytes(WIFI_NVS_SSID_KEY, mySSID, sizeof(mySSID));
}
if( nvs_wifi.isKey(WIFI_NVS_PASS_KEY) ) {
nvs_wifi.getBytes(WIFI_NVS_PASS_KEY, myPSK, sizeof(myPSK));
}
// close NVS namespace
nvs_wifi.end();
if( strlen(mySSID) and strlen(myPSK) ) {
// using retrieved credentials to connect
Serial.print(F("\n[NVS-WiFi] retrieved WiFi credentials from NVS :)"));Serial.flush();
snprintf(_tmp,sizeof(_tmp), "\n\tSSID : %s\n\tPASS : %s", mySSID, myPSK);
Serial.print(_tmp);Serial.flush();
delay(1000);
// do we need to change them ?
Serial.setTimeout(3000);
Serial.print(F("\n"));
Serial.print(F("\nDo we need to change them (Y/n) ? "));Serial.flush();
_answer='\0';
if( not Serial.readBytes(&_answer,1) or (_answer!='Y' and _answer!='y') ) {
Serial.print(F("\n\t... ok let's keep current WiFi credentials ... "));
delay(1000);
return;
} }
}
/*
* Entering SSID / pass to save to NVS
*/
Serial.print(F("\n[NVS-WiFi] now setting WiFi credentials (15s timeout) ... "));Serial.flush();
Serial.print(F("\n\tSSID --> "));Serial.flush();
Serial.setTimeout(15000);
_str = Serial.readString(); //read until timeout
_str.trim(); // remove any \r \n whitespace at the end of the String
if( not _str.length() ) {
Serial.print(F("\n\tno input detected ... reboot !"));Serial.flush();
delay(1000);
ESP.restart();
delay(5000);
}
strncpy(mySSID,_str.c_str(),sizeof(mySSID)); mySSID[sizeof(mySSID)-1]='\0';
Serial.print(F("\n\tPASS --> "));Serial.flush();
Serial.setTimeout(15000);
_str = Serial.readString(); //read until timeout
_str.trim(); // remove any \r \n whitespace at the end of the String
if( not _str.length() ) {
Serial.print(F("\n\tno input detected ... reboot !"));Serial.flush();
delay(1000);
ESP.restart();
delay(5000);
}
strncpy(myPSK,_str.c_str(),sizeof(myPSK)); myPSK[sizeof(myPSK)-1]='\0';
// save it to NVS ?
Serial.print(F("\n"));
Serial.print(F("\n[NVS-WiFi] entered crdentials :)"));Serial.flush();
snprintf(_tmp,sizeof(_tmp), "\n\tSSID : %s\n\tPASS : %s", mySSID, myPSK);
Serial.print(_tmp);Serial.flush();
delay(1000);
Serial.setTimeout(3000);
Serial.print(F("\nSave it to NVS (Y/n) ? "));Serial.flush();
_answer='\0';
if( not Serial.readBytes(&_answer,1) or (_answer!='Y' and _answer!='y') ) {
Serial.print(F("\n\t... NOT saving WiFi credentials ... reboot ... "));
delay(1000);
ESP.restart();
delay(5000);
}
// saving to namespace :)
if( not nvs_wifi.begin(WIFI_NVS_NAMESPACE,false) ) { // R/W mode
Serial.print(F("\n[NVS-WiFi] unable to open NVS namespace '"));Serial.print(WIFI_NVS_NAMESPACE);Serial.print(F("' ... reboot !"));Serial.flush();
delay(2000);
ESP.restart();
delay(5000);
}
Serial.print(F("\n[NVS-WiFi] save WiFi credentials to NVS namespace '"));Serial.print(WIFI_NVS_NAMESPACE);Serial.print(F("' ... "));Serial.flush();
if( nvs_wifi.putBytes(WIFI_NVS_SSID_KEY,mySSID, strlen(mySSID)+1) != strlen(mySSID)+1 ) {
Serial.print(F("\n[NVS-WiFi] ERROR while saving SSID to NVS ?!?!"));Serial.flush();
delay(1000); ESP.restart(); delay(5000);
}
if( nvs_wifi.putBytes(WIFI_NVS_PASS_KEY,myPSK, strlen(myPSK)+1) != strlen(myPSK)+1 ) {
Serial.print(F("\n[NVS-WiFi] ERROR while saving PSK to NVS ?!?!"));Serial.flush();
delay(1000); ESP.restart(); delay(5000);
}
// close NVS namespace
nvs_wifi.end();
// successfully saved WiFi credentials to NVS :)
Serial.print(F("\n\n[NVS-WiFi] successfully saved WiFi credentials to NVS :)"));
return;
#elif defined (ESP8266) #elif defined (ESP8266)
/* ESP8266 retrieve WiFi credentials from previous connexion */ /* ESP8266 retrieve WiFi credentials from previous connexion */
#error "NOT YET IMPLEMENTED #error "NOT YET IMPLEMENTED
#endif #endif
}
// start WiFi connexion either with DEFAULTS WiFi credentials or from retrieved ones // start WiFi connexion either with DEFAULTS WiFi credentials or from retrieved ones
//to be continued //to be continued
...@@ -110,7 +209,7 @@ void setup() { ...@@ -110,7 +209,7 @@ void setup() {
// --- LOOP -------------------------------------------------------------------- // --- LOOP --------------------------------------------------------------------
void loop() { void loop() {
// do we want to reboot ?
/* /*
* do something here ... * do something here ...
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment