Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By dontek
#25734 Board: Adafruit HUZZAH ESP8266
Arduino IDE: 1.6.5
Board Manager URL: http://arduino.esp8266.com/stable/packa ... index.json
(all downloaded within last couple days)

Issue: I can write to EEPROM and read back successfully, until a power cycle, then EEPROM is lost.

What am I doing wrong?

Code: Select all#include <EEPROM.h>

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

  int eeAddress = 0;

  struct wifiConfig
  {
    char* ssid;
    char* wifiKey;
    char* host;
  };

  wifiConfig configuration_;

  EEPROM.begin(512);
  delay(100);
  EEPROM.get(eeAddress, configuration_);
  yield();
  EEPROM.end();

  delay(3000);

  Serial.print("Read: ");
  Serial.println(configuration_.ssid);
  Serial.println(configuration_.wifiKey);
  Serial.println(configuration_.host);

  // Nothing Ever Output
 
  delay(3000);

  char ssid[] = "BLAH";
  char wifiKey[] = "yada";
  char host[] = "dontek.net";

  wifiConfig configuration = { ssid, wifiKey, host };

  EEPROM.begin(512);
  delay(100);
  EEPROM.put(eeAddress, configuration);
  yield();
  EEPROM.commit();  // Tried, since .end() was not work, this doesn't help.
  EEPROM.end();

  delay(3000);

  wifiConfig configuration__;

  EEPROM.begin(512);
  delay(100);
  EEPROM.get(eeAddress, configuration__);
  yield();
  EEPROM.end();

  delay(3000);

  Serial.print("Read: ");
  Serial.println(configuration__.ssid);
  Serial.println(configuration__.wifiKey);
  Serial.println(configuration__.host);
  Serial.println("");

   // Values Output as Expected
 
  Serial.println("Rebooting in 5 seconds...");
  Serial.println("");
  Serial.println("");

  delay(5000);

  ESP.deepSleep(5000000, WAKE_RF_DEFAULT);
}

void loop() {}
User avatar
By martinayotte
#25739 You are trying to save a wifiConfig structure quite blindly.
You defined this structures which contains pointer, not strings, so it is the pointers/addresses that are saved, not the actual strings.
When you reload the structure, those pointers are pointing nowhere, the strings are gone after reset ... :ugeek:

You should try to stuff your structure with strings with fixed length, a bit like :

Code: Select all  struct wifiConfig
  {
    char ssid[32];
    char wifiKey[32];
    char host[32];
  };
User avatar
By dontek
#25803 Wow, it seems so obvious now that you pointed it out. Doesn't do me much good to store pointers that are blown away when the power cycles. :oops:

For practice, can you suggest a way to accomplish the task without using fixed-length character arrays? (I understand that for this particular task of storing to EEPROM memory, fixed-length has its advantages in the scope of "reserving" the space in EEPROM for a given element and avoiding possible "junk" data if the whole space is not cleaned between writes and reads.)

I assume I could use String types, but that would introduce a fair amount of extra overhead, would it not? Other solutions?

Code Updates:

Code: Select allconst int eepromAddress = 0;

struct wifiConfig
{
  char ssid[32];
  char wifiKey[64];
  char host[64];
} configuration;

strcpy(configuration.ssid, "BLAH");
strcpy(configuration.wifiKey, "yada");
strcpy(configuration.host, "dontek.net");

EEPROM.begin(512);
delay(10);
EEPROM.put(eepromAddress, configuration);
yield();
EEPROM.end();