Unable to save/retrieve custom variables in WiFiManager
Piornet opened this issue · 0 comments
Piornet commented
Basic Infos
Hardware
WiFimanager Branch/Release: Master (2.0.17)
Hardware: ESP32-S3-USB-OTG (YD-ESP32-S3)
Description
I'm having difficulty with custom fields in my sketch not saving. I've added Longitude, Latitude, and an API key - however it only displays the defaults and not the entries saved via the AP.
Settings in IDE
Additional libraries:
Sketch
#BEGIN
#include <FS.h> // This needs to be first, or it all crashes and burns...
#include <WiFiManager.h> // Wifi and location settings for the device https://github.com/tzapu/WiFiManager
#include <String.h> // This is for the connection string to the OpenWeather API
#include <ArduinoJson.h> // This parses the JSON return from the OpenWeather API https://github.com/bblanchon/ArduinoJson
#ifdef ESP32
#include <SPIFFS.h>
#endif
// Global Variables
WiFiClient client;
char servername[]="api.openweathermap.org";
String result;
// WifiManager webpage settings default variables
char latitude[12] = "38.8048";
char longitude[12] = "-77.0469";
String apiKey = "xxxxx";
// Flag for saving data
bool shouldSaveConfig = false;
// Callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Hi there! Starting up...");
// clean FS, for testing
// SPIFFS.format();
//read configuration from FS json
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
#if defined(ARDUINOJSON_VERSION_MAJOR) && ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
auto deserializeError = deserializeJson(json, buf.get());
serializeJson(json, Serial);
if ( ! deserializeError ) {
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
#endif
Serial.println("\nparsed json");
strcpy(latitude, json["latitude"]);
strcpy(longitude, json["longitude"]);
Serial.println("Read variables");
} else {
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
Serial.println("failed to mount FS");
}
//end read
// The extra parameters to be configured (can be either global or just in the setup)
// After connecting, parameter.getValue() will get you the configured value
// id/name placeholder/prompt default length
WiFiManagerParameter custom_latitude_token("latitude", "Latitude", latitude, 10);
WiFiManagerParameter custom_longitude_token("longitude", "Longitude", longitude, 10);
WiFiManagerParameter custom_key("apikey", "API Key", apiKey.c_str(), 32);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//add all your parameters here
wifiManager.addParameter(&custom_latitude_token);
wifiManager.addParameter(&custom_longitude_token);
wifiManager.addParameter(&custom_key);
//reset settings - for testing
//wifiManager.resetSettings();
//set minimu quality of signal so it ignores AP's under that quality
//defaults to 8
//wifiManager.setMinimumSignalQuality();
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
//wifiManager.setTimeout(120);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("WeatherDisplay", "")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
//read updated parameters
strcpy(latitude, custom_latitude_token.getValue());
strcpy(longitude, custom_longitude_token.getValue());
apiKey = custom_key.getValue();
//strcpy(apiKey, custom_key.getValue());
Serial.println("The values in the file are: ");
Serial.println("\tLatitude : " + String(custom_latitude_token.getValue()));
Serial.println("\tLongitude : " + String(custom_longitude_token.getValue()));
Serial.println("\API Key : " + String(custom_key.getValue()));
Serial.println("The new variables are: ");
Serial.println("\tLatitude : " + String(latitude));
Serial.println("\tLongitude : " + String(longitude));
Serial.println("\API Key : " + String(apiKey));
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
#if defined(ARDUINOJSON_VERSION_MAJOR) && ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
#endif
json["latitude"] = latitude;
json["longitude"] = longitude;
json["apiKey"] = apiKey;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
#if defined(ARDUINOJSON_VERSION_MAJOR) && ARDUINOJSON_VERSION_MAJOR >= 6
serializeJson(json, Serial);
serializeJson(json, configFile);
#else
json.printTo(Serial);
json.printTo(configFile);
#endif
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
}
void loop()
{
}
#END
Debug Messages
connected...yeey :)
The values in the file are:
Latitude : 38.8048
Longitude : -77.0469
API Key : xxxxx
The new variables are:
Latitude : 38.8048
Longitude : -77.0469
API Key : xxxxx
local ip
192.168.1.109