Writing/reading an EEPROM

Hi,

I’m trying to understand how to write to and read from an EEPROM with the following code but some I’m doing wrong because I don’t get any outputs.

I appreciate some help.

TIA

#include <EEPROM.h>

String qsid = "My_SSID";
String qpass = "My_Password";
String qtz = "1";
String qip = "192,168,0,47";
String qrouter = "192,168,0,1";
String qsubnet = "255,255,255,0";
String qmypass = "admin";
int epcount = 0;

String epsid = "";
String eppass = "";
String eptz = "";
String epip = "";
String epsubnet = "";
String eprouter = "";
String epmypass = "";

void setup() {
  Serial.begin(115200);
  delay(1000);

  String allintoEP;
  allintoEP += qsid;
  allintoEP += qpass;
  allintoEP += qtz;
  allintoEP += qip;
  allintoEP += qsubnet;
  allintoEP += qrouter;
  allintoEP += qmypass;

  char epssid[allintoEP.length()];
  allintoEP.toCharArray(epssid, allintoEP.length());

  while (epcount < allintoEP.length()) {
    if (allintoEP[epcount] == 0) {
      allintoEP[epcount] = 42; // puts a "*" between variables
    }
    EEPROM.write(epcount, allintoEP[epcount]);
    epcount++;
  }
  EEPROM.commit();
  delay(100);

}

void loop() {

  epsid = "";
  eppass = "";
  eptz = "";
  epip = "";
  epsubnet = "";
  eprouter = "";
  epmypass = "";

  int len = 50;
  int addr = 0;
  int count = 0;
  byte value;

  while (count < len) {
    value = EEPROM.read(addr);
    if (value != 42) epsid += char(value);
    count++;
    addr++;
    if (value == 42) break;
  }
  while (count < len) {
    value = EEPROM.read(addr);
    if (value != 42) eppass += char(value);
    count++;
    addr++;
    if (value == 42) break;
  }
  while (count < len) {
    value = EEPROM.read(addr);
    if (value != 42) eptz += char(value);
    count++;
    addr++;
    if (value == 42) break;
  }
  while (count < len) {
    value = EEPROM.read(addr);
    if (value != 42) epip += char(value);
    count++;
    addr++;
    if (value == 42) break;
  }
  while (count < len) {
    value = EEPROM.read(addr);
    if (value != 42) epsubnet += char(value);
    count++;
    addr++;
    if (value == 42) break;
  }
  while (count < len) {
    value = EEPROM.read(addr);
    if (value != 42) eprouter += char(value);
    count++;
    addr++;
    if (value == 42) break;
  }
  while (count < len) {
    value = EEPROM.read(addr);
    if (value != 42) epmypass += char(value);
    count++;
    addr++;
    if (value == 42) break;
  }

//    epip = EEPROM.read(50);
//    epsubnet = EEPROM.read(53);
//    eprouter = EEPROM.read(56);
//    epmypass = EEPROM.read(59);


    Serial.println();
    Serial.println("Read from EEProm");
    Serial.print("espid= "); Serial.println(epsid);
    Serial.print("eppass= "); Serial.println(eppass);
    Serial.print("eptz= "); Serial.println(eptz);
    Serial.print("epIP= "); Serial.println(epip);
    Serial.print("epSubnet= "); Serial.println(epsubnet);
    Serial.print("epRouter= "); Serial.println(eprouter);
    Serial.print("epmyPass= "); Serial.println(epmypass);
    Serial.println();
  
  delay(1000);
}
char epssid[allintoEP.length()];
  allintoEP.toCharArray(epssid, allintoEP.length());

Why all the String stuff just to turn it into a char array later? Why not just build the string in the char array to begin with?

There won't be any characters that equal 0 in the completed string except at the very end. When you concatenate them it takes off the terminating null. Then later when you're looking for 42 you're not going to find it because they weren't put in.

You've also got some issues with the while loops in loop. The first one ends when count == len, and all the rest use the same condition, so none of the others will ever run. They won't break on 42 because there aren't any 42's in the string.

Ok, here’s a different approach but still cannot get to read from the eeprom…

/* Using ESP8266-12 */

#include <EEPROM.h>

struct MyObject {
  char  ssid[16];
  char  pass[16];
  char ip[16];
  int hr;
};

int eeAddress = 0; //EEPROM address to start reading from

void write_eeprom();
void read_eeprom();

void setup() {

  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  write_eeprom();
  delay(5000);
  read_eeprom();

}

void loop() {}

void write_eeprom() {

  //Data to store.
  MyObject customVar = {
    "mySSID",
    "myPass",
    "192,168,0,57",
    4
  };

  EEPROM.put(eeAddress, customVar);
  Serial.print("Written custom data type!");
}

void read_eeprom() {

  Serial.println("Read from EEPROM: ");

  MyObject customVar; //Variable to store custom object read from EEPROM.
  EEPROM.get(eeAddress, customVar);

  Serial.println("Read custom object from EEPROM: ");
  Serial.println(customVar.ssid);
  Serial.println(customVar.pass);
  Serial.println(customVar.ip);
  Serial.println(customVar.hr);
}

UPDATE: Solved by adding EEPROM.begin(4096); in setup.

I ran it on an Uno and here’s the output as it appears in serial monitor:

Written custom data type!object from EEPROM:
mySSID
myPass
192,168,0,57
4