Search code examples
c++arduinoesp8266eeprom

EEPROM ESP8266 - Inverted Question Mark


Im stuck here. I am trying to SAVE/LOAD board configuration to EEPROM in ESP8266 using my modified version of this example.

Here is my actual code:

char ssid[32] = "";
char password[32] = "";
char device_type[1] = "";
char device_name[32] = "";

/** Load WLAN credentials from EEPROM */
void loadBoardConfig()
{

  EEPROM.begin(512);

  int addr = 0;
  EEPROM.get(0, ssid);

  addr += sizeof(ssid);
  EEPROM.get(addr, password);

  addr += sizeof(password);
  EEPROM.get(addr, device_type);

  addr += sizeof(device_type);
  EEPROM.get(addr, device_name);

  addr += sizeof(device_name);

  char ok[2 + 1];
  EEPROM.get(addr, ok);

  EEPROM.end();

  if (String(ok) != String("OK"))
  {
    ssid[0] = 0;
    password[0] = 0;
    device_type[0] = 0;
    device_name[0] = 0;
  }
}

/** Store Board Config to EEPROM */
void saveBoardConfig()
{

  EEPROM.begin(512);

  int addr = 0;
  EEPROM.put(addr, ssid);

  addr += sizeof(ssid);
  EEPROM.put(addr, password);

  addr += sizeof(password);
  EEPROM.put(addr, device_type);

  addr += sizeof(device_type);
  EEPROM.put(addr, device_name);

  addr += sizeof(device_name);
  char ok[2 + 1] = "OK";
  EEPROM.put(addr, ok);

  EEPROM.commit();
  EEPROM.end();
}

I realized there it wasnt connecting properly to WiFi so i added this piece of code to test:

Serial.print("EEPROM CONFIG: [{ SSID: ");
Serial.printf("%c",ssid);
Serial.print(" },\n{ Password: ");
Serial.print(password);
Serial.print(" },\n{ Device Type: ");
Serial.print(device_type);
Serial.print(" },\n{ Device Name: ");
Serial.print(device_name);
Serial.println(" }]");
Serial.print("STATUS: ");
Serial.println(ok);

This is the result im getting in Serial Monitor:

EEPROM CONFIG: [{ SSID: ⸮ },
{ Password: ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ },
{ Device Type: ⸮ },
{ Device Name: ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ }]
STATUS: ⸮⸮⸮?x⸮ @⸮⸮⸮?

Any idea why is this happening?


Solution

  • I found out the problem. The code is OK, but ESP8266 does not have an actual EEPROM. Instead it is emulated an saved in the flash and I had my settings to erase all flash contents. I just switched to "Only Sketch" and it worked! Thanks!