ESP32 choice

You may wish to explore the preferences library in the standard Arduino package for the ESP32.

I use the preferences library to store the SSID, Password in the NVM quite painlessly and then can just use sketch to read them as needed. Since I run similar code on different boards at different locations, this saves hard coding a version for each board. I haven’t taken to storing the Blynk authorization numbers this way since that starts to lock the board to a single application. I appended the simple sketch to set these at the end of an application sketch but commented out. I can easily comment out the application and uncomment the appended preference sketch to set SSID and Password on a new ESP32 or reset on a previously configures board, then restore the desired sketch while re-commenting out the preference setting bit… Here is the code to set the preferences:

// copy below to end of new sketch to reset the preference keys for ssid and password
 ESP32 start counter example with Preferences library

 This simple example demonstrate using Preferences library to store how many times
 was ESP32 module started. Preferences library is wrapper around Non-volatile
 storage on ESP32 processor.

 created for arduino-esp32 09 Feb 2017
 by Martin Sloup (Arcao)
 Modified by James Monthony to store local SSID and Password in Preferences
 need to convert strings to char* for login using WiFi.h
*/
/*  

#include <Preferences.h>
#include <WiFi.h>

const char* ssid             = "YourSSID";            // SSID goes here
const char* password         = "YourPassword";       // your password goes here to be written
const char* notSet           = "Not Set!";           // function requires a default value this is it.

Preferences preferences;

void setup() {
  Serial.begin(115200);
  Serial.println();



  // Open Preferences with WiFiLink namespace. 
  // Each application module, library, etc.
  // has to use namespace name to prevent key name collisions. We will open storage in
  // RW-mode (second parameter) has to be false or read only.
  // Note: Namespace name is limited to 15 chars
 
  preferences.begin("WiFiLink", false);  false for read/write

  // you can uncomment as needed below to clear out old values. after running (once) the new value will
  // be stored.  then re-comment out the line and run again to be sure it is stored correctly.
  //  Remove all preferences under opened namespace by uncommenting below
  //  preferences.clear();

  //  Or remove the counter key only
 //   preferences.remove("counter");

 //  Or change the SSID
 //   preferences.remove("CurrentSSID");

 //  Or change the password
 //   preferences.remove("MyPassword");

 //  Get a counter value, if key is not exist return default value 0
 //  Note: Key name is limited to 15 chars too
  
  unsigned int            counter = preferences.getUInt("counter", 0);

   String                       CurrentSSID = preferences.getString("CurrentSSID", notSet);
   String                        MyPassword = preferences.getString("MyPassword",notSet);

 if (CurrentSSID == notSet) {
  preferences.putString( "CurrentSSID", ssid);
  CurrentSSID =  preferences.getString( "CurrentSSID", CurrentSSID);
 }

if (MyPassword == notSet) {
  preferences.putString( "MyPassword",password);
  MyPassword =  preferences.getString(  "MyPassword",MyPassword);
 }
   
  // Increase counter
  counter++;

  // Print counter to a Serial

  Serial.printf("Current counter value: %u\n", counter);
  Serial.printf("Current SSID is ");
  Serial.println (CurrentSSID);
  Serial.println("Current Password is ");
  Serial.println(MyPassword);
  Serial.println();
  

  // Store counter to the Preferences
  preferences.putUInt("counter", counter);

char cssid[CurrentSSID.length() + 1];;
char cpassword[MyPassword.length()+1];
CurrentSSID. toCharArray(cssid, CurrentSSID.length() + 1);
 MyPassword.toCharArray(cpassword, MyPassword.length()+1);   

  WiFi.begin(cssid, cpassword);

while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());



  // Close the Preferences
  preferences.end();

  // Wait 20 seconds
  Serial.println("Restarting in 20 seconds...");
  delay(20000);

  // Restart ESP
  ESP.restart();
}

void loop() {}  
  */

You can not replace a full file system, but for a few set paramaters, the preferences library is a great ESP32 feature.

Cheers

1 Like