Skip to content

Saved credentials access

Saved credentials in the flash

AutoConnect stores the established WiFi connection in the flash memory of the ESP8266/ESP32 module and equips the class to access the credentials from the sketch. You can read, write or erase the credentials using this class individually. It's AutoConnectCredential class which provides the access method to the saved credentials in the flash.1

Credentials storage location

The location where AutoConnect saves credentials depends on the module type and the AutoConnect library version, also arduino-esp32 core version. In either case, the location is flash memory, but EEPROM and Preferences (in the nvs2) are used depending on the library versions.

AutoConnect Arduino core
for ESP8266
Arduino core for ESP32
1.0.2 earlier 1.0.3 later
v0.9.12 earlier EEPROM EEPROM (partition) Not supported
v1.0.0 later Preferences (nvs)

(Can be used EEPROM with turning off AUTOCONNECT_USE_PREFERENCES macro)

Preferences (nvs)

However, sketches do not need to know where to store credentials using the commonly accessible AutoConnectCredential API.

If you are using an Arduino core for ESP32 1.0.2 earlier and need to use credentials in EEPROM for backward compatibility, turns off the AUTOCONNECT_USE_PREFERENCES3 macro definition in AutoConnectCredentials.h file. AutoConnect behaves assuming that credentials are stored in EEPROM if AUTOCONNECT_USE_PREFERENCES is not defined.

AutoConnectCredential

Include header

#include <AutoConnectCredential.h>

Constructors

AutoConnectCredential();

AutoConnectCredential default constructor. The default offset value is 0. In ESP8266 or ESP32 with arduino core 1.0.2 earlier, if the offset value is 0, the credential area starts from the top of the EEPROM. If you use this area in a user sketch, AutoConnect may overwrite that data.

AutoConnectCredential(uint16_t offset);
Parameter
offsetSpecies offset from the top of the EEPROM for the credential area together. The offset value is from 0 to the flash sector size. This parameter is ignored for AutoConnect v1.0.0 or later with arduino-esp32 core 1.0.3 or later.

Public member functions

entries

uint8_t entries(void)

Returns number of entries as contained credentials.

Return value
Number of entries as contained credentials.

load

int8_t load(const char* ssid, struct station_config* config)

Load a credential entry and store to config.

Parameters
ssidSSID to be loaded.
configstation_config
Return value
Save the specified SSID's credential entry to station_config pointed to by the parameter as config. -1 is returned if the SSID is not saved.

load

bool load(int8_t entry, struct station_config* config)

Load a credential entry and store to config.

Parameters
entrySpecifies the index number based 0 to be loaded.
configstation_config
Return value
Save the specified credential entry to station_config pointed to by the parameter as config. -1 is returned if specified number is not saved.

save

bool save(const struct station_config* config)

Save a credential entry.

Parameter
configstation_config to be saved.
Return value
trueSuccessfully saved.
falseFailed to save.

del

bool del(const char* ssid)

Delete a credential the specified SSID.

Parameter
ssidSSID to be deleted.
Return value
trueSuccessfully deleted.
falseFailed to delete.

Clear saved credentials

There is no particular API for batch clearing of all credential data stored by AutoConnect. It is necessary to prepare a sketch function that combines several AutoConnectCredential APIs to erase all saved credentials. The following function is an implementation example, and you can use it to achieve batch clearing.

void deleteAllCredentials(void) {
  AutoConnectCredential credential;
  struct station_config config;
  uint8_t ent = credential.entries();

  while (ent--) {
    credential.load(0, &config);
    credential.del((const char*)&config.ssid[0]);
  }
}

The data structures

station_config

A structure is included in the ESP8266 SDK. You can use it in the sketch like as follows:

extern "C" {
#include <user_interface.h>
}
struct station_config {
  uint8 ssid[32];
  uint8 password[64];
  uint8 bssid_set;
  uint8 bssid[6];
};

The credential entry

A data structure of the credential saving area in EEPROM as the below. 4

Byte offset Length Value
0 8 AC_CREDT
8 1 Number of contained entries (uint8_t)
9 2 Container size, excluding size of AC_CREDT and size of the number of entries(width for uint16_t type).
11 variable SSID terminated by 0x00. Max length is 32 bytes.
variable variable Password plain text terminated by 0x00. Max length is 64 bytes.
variable 6 BSSID
variable Contained the next entries. (Continuation SSID+Password+BSSID)
variable 1 0x00. End of container.

  1. An example using AutoConnectCredential is provided as an example of a library sketch to delete saved credentials. 

  2. The namespace for Preferences used by AutoConnect is AC_CREDT

  3. Available only for AutoConnect v1.0.0 and later. 

  4. There may be 0xff as an invalid data in the credential saving area. The 0xff area would be reused.