Problem using Preferences with AutoConnect #375
-
Can you be more specific on how to use AutoConnect while also using Preferences. I don't understand how to use data1 data2 or data3 as listed in your example to be able to use Preferences and AutoConnect together. Is there a way to simply have AutoConnect use a file in the fat or SPIFFS area to store the wifi credentials so that this is not an issue? Or is there a way to get the size of of the data that AutoConnect is going to need and adjust boundaryOffset so that AutoConnect uses the last portion of EEPROM? Is there a way to use "Config.autoSave = AC_SAVECREDENTIAL_NEVER" but take the current credentials and store them in EEPROM via Preferences? So that it doesn't store every credential ever used just the latest one? Is there a way to modify the NVS area to be larger so that both can use it, is it like a normal filesystem, and if AutoConnect data is already there, will Preferences just use the space afterwards? When I found AutoConnect it was exactly what I was looking for, it worked great, I only hope I can find a way to use both. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I tried making the nvs partition larger but it screws up the hardcoded 0x10000 app start location, so in the end I just sacrificed the original location and moved nvs to before the spiffs partition and then gave it 512KB of size, this seems to give AutoConnect enough room. I also avoided using Preferences until after I set up AutoConnect. I have no idea if the order mattered or if the size helped or if it was all just a coincidence but it's working again so I'm happy. Here is the partition scheme I used in the end: nvs2, data, nvs, 0x9000, 0x5000, |
Beta Was this translation helpful? Give feedback.
-
My answers are also individual because your question is fragmented.
Which one of my examples?
There is no way. AutoConnect does not support it. User sketches can destroy it when you save the credentials to SPIFFS. AutoConnect cannot protect it. The only reasonable way is to partition the AutoConnect credentials by the Preference key.
If you only want to enable the last credential, then with
You have already found the answer. Adjust the partition table to fit the storage area required for your sketch. |
Beta Was this translation helpful? Give feedback.
-
Here is snippet: #include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
#include <Preferences.h>
#include <AutoConnect.h>
extern "C" {
#include <esp_wifi.h>
}
const char* PREFS_NAMESPACE = "credential";
const char* PREFS_KEY = "credential";
Preferences prefs;
wifi_config_t w_config;
AutoConnect portal;
AutoConnectConfig config;
void onConnect(IPAddress& ip) {
esp_err_t err;
Serial.println("WiFi connected");
if ((err = esp_wifi_get_config(WIFI_IF_STA, &w_config)) == ESP_OK) {
Serial.printf("%.*s: ", sizeof(wifi_config_t::sta.ssid), w_config.sta.ssid);
if (prefs.putBytes(PREFS_KEY, &w_config, sizeof(w_config)) == sizeof(wifi_config_t))
Serial.println("credential saved");
else
Serial.println("failed to save");
}
else
Serial.printf("esp_wifi_get_config err(%d): %s\n", err, esp_err_to_name(err));
}
void setup() {
delay(1000);
Serial.begin(115200);
WiFi.enableSTA(true);
prefs.begin(PREFS_NAMESPACE);
if (prefs.getBytes(PREFS_KEY, &w_config, sizeof(wifi_config_t)) == sizeof(wifi_config_t)) {
esp_wifi_set_config(WIFI_IF_STA, &w_config);
Serial.printf("SSID %.*s restored\n", sizeof(wifi_config_t::sta.ssid), w_config.sta.ssid);
}
else
Serial.println("No available SSID saved");
config.autoSave = AC_SAVECREDENTIAL_NEVER;
portal.config(config);
portal.onConnect(onConnect);
portal.begin();
}
void loop() {
portal.handleClient();
} |
Beta Was this translation helpful? Give feedback.
@MacGyverr
Here is snippet: