Problem with EEPROM

when i try to get the ssid/pw from the eeprom the wifi will not connect. if i remove the variable from the eeprom and call define it normally at the top of the code it works. why is the eeprom method not working? i can print the variables from the eeprom with serial.print and can confirm the output form the serial console looks correct. what am i missing here. im using String because the ssid and password are coming from an http server.

this dont work,

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ArduinoOTA.h>
#include <FS.h>   //Include File System Headers

ESP8266WebServer OTA_httpServer(81);
ESP8266HTTPUpdateServer OTA_httpUpdater;
ESP8266WebServer server(80);

IPAddress local_IP(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress Client1(192, 168, 4, 2);
IPAddress Client2(192, 168, 4, 3);
IPAddress Client3(192, 168, 4, 4);


IPAddress sta_local_IP(10, 0, 0, 232);
IPAddress sta_gateway(10, 0, 0, 1);
IPAddress sta_subnet(255, 0, 0, 0);

const char* ssid = "LCS2";
const char* password = "a1b2c3d5";


unsigned long now=0;

struct {
String sta_ssid="SSID";
String sta_password="PASSWORD";
} _EEPROM;
int addr = 0;

#include "htmlFunctions.h"
void _writeEEPROM () {
  EEPROM.put(addr, _EEPROM);
  EEPROM.commit();    //Store data to EEPROM
  Serial.println("EEPROM WRITE");
}

void setup() {
  // #include "OTA.h" // server.on("/status", []() {
  Serial.begin(115200);
  EEPROM.begin(512);  //Initialize EEPROM
  EEPROM.get(addr, _EEPROM);
  SPIFFS.begin();
  WiFi.persistent(0);
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(_EEPROM.sta_ssid,_EEPROM.sta_password);
  WiFi.config(sta_local_IP, sta_subnet, sta_gateway);
  WiFi.softAP(ssid, password, 9, false, 15);
  WiFi.softAPConfig(local_IP, local_IP, subnet);


  server.on("/", handleRoot);
  server.on("/wifisettings", handleSubmitWIFI);
  server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI
  server.begin();
}

void loop() {
  if(millis()-now>=1000){
    Serial.println(sta_ssid);
    now=millis();
  }
  // Serial.println(WiFi.localIP());
  OTA_httpServer.handleClient(); //ota server "htt p://x.x.x.x :81/update"
  ArduinoOTA.handle();
  server.handleClient();//Checks for web server activity

}

this does work,

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ArduinoOTA.h>
#include <FS.h>   //Include File System Headers

ESP8266WebServer OTA_httpServer(81);
ESP8266HTTPUpdateServer OTA_httpUpdater;
ESP8266WebServer server(80);

IPAddress local_IP(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress Client1(192, 168, 4, 2);
IPAddress Client2(192, 168, 4, 3);
IPAddress Client3(192, 168, 4, 4);


IPAddress sta_local_IP(10, 0, 0, 232);
IPAddress sta_gateway(10, 0, 0, 1);
IPAddress sta_subnet(255, 0, 0, 0);

const char* ssid = "LCS2";
const char* password = "a1b2c3d5";
String sta_ssid="SSID";
String sta_password="PASSWORD";

unsigned long now=0;

struct {

} _EEPROM;
int addr = 0;

#include "htmlFunctions.h"
void _writeEEPROM () {
  EEPROM.put(addr, _EEPROM);
  EEPROM.commit();    //Store data to EEPROM
  Serial.println("EEPROM WRITE");
}

void setup() {
  // #include "OTA.h" // server.on("/status", []() {
  Serial.begin(115200);
  EEPROM.begin(512);  //Initialize EEPROM
  EEPROM.get(addr, _EEPROM);
  SPIFFS.begin();
  WiFi.persistent(0);
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(sta_ssid,sta_password);
  WiFi.config(sta_local_IP, sta_subnet, sta_gateway);
  WiFi.softAP(ssid, password, 9, false, 15);
  WiFi.softAPConfig(local_IP, local_IP, subnet);


  server.on("/", handleRoot);
  server.on("/wifisettings", handleSubmitWIFI);
  server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI
  server.begin();
}

void loop() {
  if(millis()-now>=1000){
    Serial.println(sta_ssid);
    now=millis();
  }
  // Serial.println(WiFi.localIP());
  OTA_httpServer.handleClient(); //ota server "htt p://x.x.x.x :81/update"
  ArduinoOTA.handle();
  server.handleClient();//Checks for web server activity

}

OKay so i found out that the password variable is not being update. can someone help me find the problem here,

this is what that variable print out at the serial console after trying to update them,

21:52:03.157 → ssid
21:52:03.157 → ⸮⸮d`⸮bq7 <–password

I found that if i update the eeprom strings then they are correct. but after i write the eeprom and restart the esp the strings are lost?

You can not store a String object in eeprom. The String object is a pointer to an underlying character array. You need to store the character array itself in the eeprom.

Here i have made the code into one sketch to make things a little more simple.

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ArduinoOTA.h>
#include <FS.h>   //Include File System Headers

ESP8266WebServer OTA_httpServer(81);
ESP8266HTTPUpdateServer OTA_httpUpdater;
ESP8266WebServer server(80);

IPAddress local_IP(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress Client1(192, 168, 4, 2);
IPAddress Client2(192, 168, 4, 3);
IPAddress Client3(192, 168, 4, 4);


IPAddress sta_local_IP(10, 0, 0, 232);
IPAddress sta_gateway(10, 0, 0, 1);
IPAddress sta_subnet(255, 0, 0, 0);

const char* ssid = "LCS2";
const char* password = "a1b2c3d5";


unsigned long now = 0;
unsigned long  addr = 10;

struct {
  String sta_ssid = "Wireless";
  String sta_password = "password";
} _EEPROM;
bool loadFromSpiffs(String path) {
  String dataType = "text/plain";
  if (path.endsWith("/")) path += "index.htm";

  if (path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
  else if (path.endsWith(".html")) dataType = "text/html";
  else if (path.endsWith(".htm")) dataType = "text/html";
  else if (path.endsWith(".css")) dataType = "text/css";
  else if (path.endsWith(".js")) dataType = "application/javascript";
  else if (path.endsWith(".png")) dataType = "image/png";
  else if (path.endsWith(".gif")) dataType = "image/gif";
  else if (path.endsWith(".jpg")) dataType = "image/jpeg";
  else if (path.endsWith(".ico")) dataType = "image/x-icon";
  else if (path.endsWith(".xml")) dataType = "text/xml";
  else if (path.endsWith(".pdf")) dataType = "application/pdf";
  else if (path.endsWith(".zip")) dataType = "application/zip";
  File dataFile = SPIFFS.open(path.c_str(), "r");
  if (server.hasArg("download")) dataType = "application/octet-stream";
  if (server.streamFile(dataFile, dataType) != dataFile.size()) {
  }

  dataFile.close();
  return true;
}
void handleWebRequests() {
  if (loadFromSpiffs(server.uri())) return;
  String message = "File Not Detected\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  Serial.println(message);
}


void handleSubmitWIFI() { //dispaly values and write to memmory
  String response = "<p>The ssid is ";
  response += server.arg("ssid");
  _EEPROM.sta_ssid = server.arg("ssid");
  response += "
";
  response += "And the password is ";
  response += server.arg("Password");
  _EEPROM.sta_password = server.arg("Password");
  response += "
";
  response += "And the IP Address is ";
  response += server.arg("IP");
  response += "</P>
";
  response += "<H2><a href=\"/\">go home</a></H2>
";
  Serial.println(response);
  _writeEEPROM ();
  server.send(200, "text/html", response);
  delay(1000);
  //ESP.restart();
}
void handleRoot() {
  Serial.println("root");
  if (server.hasArg("ssid") && server.hasArg("Password")) { //If all form fields contain data call handelSubmit()
    Serial.println("submit");
    handleSubmitWIFI();
    server.sendHeader("Location", "/index.html", true);  //Redirect to our html web page
    server.send(302, "text/plane", "");
  }

}
void _writeEEPROM () {
  EEPROM.put(addr, _EEPROM);
  EEPROM.commit();    //Store data to EEPROM
  Serial.println("EEPROM WRITE");
}
//#include "htmlFunctions.h"

void setup() {
  // #include "OTA.h" // server.on("/status", []() {
  SPIFFS.begin();
  Serial.begin(115200);

  EEPROM.begin(256);  //Initialize EEPROM
  EEPROM.get(addr, _EEPROM);

  WiFi.persistent(0);
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(_EEPROM.sta_ssid, _EEPROM.sta_password);
  WiFi.config(sta_local_IP, sta_subnet, sta_gateway);
  WiFi.softAP(ssid, password, 9, false, 15);
  WiFi.softAPConfig(local_IP, local_IP, subnet);


  server.on("/", handleRoot);
  server.on("/wifisettings", handleSubmitWIFI);
  server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI
  server.begin();
}

void loop() {
  if (millis() - now >= 1000) {
    Serial.println(_EEPROM.sta_ssid);
    Serial.println(_EEPROM.sta_password);
    now = millis();
  }
  // Serial.println(WiFi.localIP());
  OTA_httpServer.handleClient(); //ota server "htt p://x.x.x.x :81/update"
  ArduinoOTA.handle();
  server.handleClient();//Checks for web server activity

}

i have not used strings that much and i don’t know how to store the char array of a string into the EEPROM. Could you please show me what i need to do. i would be very thankful, thankyou

Oky i think i got it to work,

struct {
char sta_ssid[15];
char sta_password[15];
} _EEPROM;

server.arg(“ssid”).toCharArray(_EEPROM.sta_ssid, 15); server.arg(“Password”).toCharArray(_EEPROM.sta_password, 15) ;

hopefully its okay to leave the 15 length there

Oky i think i got it to work,

struct {
char sta_ssid[15];
char sta_password[15];
} _EEPROM;

server.arg(“ssid”).toCharArray(_EEPROM.sta_ssid, 15); server.arg(“Password”).toCharArray(_EEPROM.sta_password, 15) ;

hopefully its okay to leave the 15 length there

I agree that .toCharArray() is the correct function to use.

I don’t think there is anything wrong with using the max length, but alternatively you could test that (server.arg(“ssid”).length() +1) and(server.arg(“Password”).length()+1) are <=15 and then use

server.arg("ssid").toCharArray(_EEPROM.sta_ssid, server.arg("ssid").length() +1);
server.arg("Password").toCharArray(_EEPROM.sta_password,server.arg("Password").length()+1) ;