Post topics, source code that relate to the Arduino Platform

User avatar
By fatalwir
#63025 Hello, I'm working on a project and I want to store WiFi config but I don't want to use existing projects (like WiFiManager). I'm trying few hours to store SSID and password with EEPROM library but with no success. :( Where is the problem? Here's my code:
Code: Select all#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>

#define CONFIG_ID 0x36
#define DEFAULT_MODE WIFI_STA
#define DEFAULT_SSID "myssid"
#define DEFAULT_PASS "mypass"

byte wifiMode;
String wifiSsid;
String wifiPass;

void setup() {
  Serial.begin(9600);

  EEPROM.begin(512);
  if(EEPROM.read(0)!=CONFIG_ID) {
    restartConfig(); 
  }

  loadConfig();

  if(wifiMode == WIFI_STA) {
    WiFi.disconnect();
    WiFi.begin(wifiSsid.c_str(), wifiPass.c_str());
    while (WiFi.status() != WL_CONNECTED) {
      delay(50);
    }
  }else if(wifiMode == WIFI_AP){
    IPAddress apIP(192, 168, 4, 1);
    WiFi.mode(WIFI_AP);
    WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
    if(wifiPass.length()==0){
      WiFi.softAP(wifiSsid.c_str());
    }else {
      WiFi.softAP(wifiSsid.c_str(), wifiPass.c_str());
    }
  }

  ...

}


void loop() {
  ...
}

void restartConfig() {
  saveConfig(DEFAULT_MODE, DEFAULT_SSID, DEFAULT_PASS);
}

void saveConfig(byte wMode, String ssid, String pass) {
  EEPROM.write(0, CONFIG_ID);
  EEPROM.write(1, wMode);
  for (int i = 0; i < ssid.length(); ++i){
    EEPROM.write(100+i, ssid[i]);
  }
  EEPROM.write(100+ssid.length(), 0);
  for (int i = 0; i < pass.length(); ++i){
    EEPROM.write(256+i, pass[i]);
  }
  EEPROM.write(256+pass.length(), 0);
  EEPROM.commit();
}

void loadConfig() {
  wifiMode = EEPROM.read(1);
 
  char c;
  int i = 0;
  while(true) {
    c = EEPROM.read(100+i);
    if(c==0) break;
    wifiSsid[i] = c;
    i++;
  }

  i = 0;
  while(true) {
    c = EEPROM.read(256+i);
    if(c==0) break;
    wifiPass[i] = c;
    i++;
  }
}


There are no errors when compiling and I'm starting to be desperate. Thanks for any advice.
User avatar
By Pablo2048
#71722 The EEPROM is 1 Flash sector actually, so life time (number of overwrite cycles) is the same as in SPIFFS. SPIFFS can never be faster than EEPROM access because EEPROM is simulated (mirrored) in RAM. SPIFFS can pretend better lifetime, but this is because of the wear leveling inside SPIFFS (so another slowdown comparing to EEPROM). EEPROM advantage over SPIFFS is that if you upload new SPIFFS content via IDE you get lost everything that you have saved in your sketch before - EEPROM content remains unchanged. Another advantage is security - if you store anything in SPIFFS there CAN be potential risk of getting passowrds for example via web interface. EEPROM is in this case more secure (but I still suggest to encipher passwords even in EEPROM...). I'm using EEPROM for configuration (SSID's, Passwords, device name, ...) and SPIFFS for the rest (web pages, logs, ...).