Hi all,
Reative programming beginner tinkering with a "SparkFun Thing Plus" ESP32 board in the Arduino IDE. I've been using tutorials online to create a basic sketch which fires up a web server on the board and allows me to control the onboard LED on pin 13 from a web browser. So far so good.
I then decided to use the preferences library to store the Wi-Fi credentials (and potentially later allow them to be overwritten from the browser). I've managed to implement this in a very basic first try kind of way, but I'm finding that despite storing both the SSID and Wi-Fi password with unique keys, the code was replacing the SSID value with the password value at some point, and I can't work out why or where.
I then changed the SSID and password values to allow me to copy and paste the output here, and now no longer populates the SSID variable at all, but also is not trying to use the password value.
I have erased the NVS and tried again but can now not get the SSID variable to hold a value from preferences. Any advice would be greatly appreciated.
My understanding of the behaviour I'm expecting for the code I'm using to define the variables is:
- Create const char * variable called SSID
- Create the preferences string with a key of SSID and if no value stored (currently clearing the stored values on every boot) define the value of WIFINAME then use .c_str() to allow me to store it in my const char * variable
- So in theory const char * SSID now holds the value of WIFINAME
- Output this value to the serial
- Repeat steps 1-4 for password
const char * ssid = preferences.getString ("ssid", "WIFINAME").c_str();
Serial.println(ssid);
const char * password = preferences.getString ("password", "PASSWORD123").c_str();
Serial.println(password);
I'm using:
Arduino IDE 1.8.13 (Standalone, not from Windows Store)
ESP32 board manager by Espressif Systems 1.0.6
Windows 10 64bit
SparkFun Thing Plus - ESP32 WROOM
Full code:
// Load Wi-Fi library
#include <WiFi.h>
// Load Preferences library
#include <Preferences.h>
// Define name for Preferences
Preferences preferences;
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output13State = "off";
// Assign output variables to GPIO pins
const int output13 = 13;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
// Open Preferences with my-app namespace. Each application module, library, etc
// has to use a namespace name to prevent key name collisions. We will open storage in
// RW-mode (second parameter has to be false).
// Note: Namespace name is limited to 15 chars.
preferences.begin("vanLife", false);
preferences.clear();
// Replace with your network credentials
const char * ssid = preferences.getString ("ssid", "WIFINAME").c_str();
Serial.println(ssid);
const char * password = preferences.getString ("password", "PASSWORD123").c_str();
Serial.println(password);
// Initialize the output variables as outputs
pinMode(output13, OUTPUT);
// Set outputs to LOW
digitalWrite(output13, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.print("Using password ");
Serial.println(password);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
// Close the Preferences
preferences.end();
}
Output was doing this:
WIFI NAME
PASSWORD123
Connecting to PASSWORD123
Using password PASSWORD123
...........................
Now it does this:
PASSWORD123
Connecting to
Using password PASSWORD123
...........................