From 519e117be7b141d778275c8f275e46bf635d65af Mon Sep 17 00:00:00 2001 From: Francois <thiebolt@irit.fr> Date: Sun, 28 May 2023 00:25:36 +0200 Subject: [PATCH] update --- tests/nvs_namespace/nvs_namespace.ino | 129 +++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 15 deletions(-) diff --git a/tests/nvs_namespace/nvs_namespace.ino b/tests/nvs_namespace/nvs_namespace.ino index 03b6ed18..acc0cb28 100644 --- a/tests/nvs_namespace/nvs_namespace.ino +++ b/tests/nvs_namespace/nvs_namespace.ino @@ -5,6 +5,8 @@ * --- * TODO: * - 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 */ @@ -71,6 +73,9 @@ void endLoop( void ) { // --- MAIN -------------------------------------------------------------------- void setup() { + char _answer, _tmp[256]; + String _str; + delay(5000); Serial.begin(115200); // debug link Serial.println(F("\n\n\n[NVS] namespace demo ..."));Serial.flush(); @@ -79,27 +84,121 @@ void setup() { #ifdef ESP32 /* NVS @ ESP32 */ 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 - nvs_wifi.getBytes(WIFI_NVS_SSID_KEY, mySSID, sizeof(mySSID)); - nvs_wifi.getBytes(WIFI_NVS_PASS_KEY, myPSK, sizeof(myPSK)); - if( strlen(mySSID) and strlen(myPSK) ) { - // using retrived credentials to connect - Serial.print(F("\n[NVS-WiFi] retreieved WiFi credentials from NVS :)"));Serial.flush(); - } - else { - Serial.print(F("\n[NVS-WiFi] SSID and/or PSK missing ... cancel :("));Serial.flush(); - mySSID[0] = '\0'; - myPSK[0] = '\0'; + + /* + * Open NVS-WiFi namespace to retrieve SSID / PSK + */ + if( not nvs_wifi.begin(WIFI_NVS_NAMESPACE,true) ) { // readonly 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] opened NVS WiFi credentials namespace ..."));Serial.flush(); + // 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) /* ESP8266 retrieve WiFi credentials from previous connexion */ #error "NOT YET IMPLEMENTED #endif - } // start WiFi connexion either with DEFAULTS WiFi credentials or from retrieved ones //to be continued @@ -110,7 +209,7 @@ void setup() { // --- LOOP -------------------------------------------------------------------- void loop() { - + // do we want to reboot ? /* * do something here ... */ -- GitLab