Arduino ESP8266从EEPROM读取字符串

时间:2018-11-09 20:33:40

标签: string append esp8266 eeprom

到目前为止,我已经实现了从ESP8266 EEPROM读取字符串的操作。

但是,尝试将第二个字符串附加到刚刚读取的第一个字符串上是行不通的!

我首先在EEPROM的地址0处输入数字2。我从地址0读到6。

这是我的ESP8266.ino代码:

    String readM = "";
    String appendixStr = "|||";
    Serial.print("appendixStr = ");
    Serial.println(appendixStr);
    String dailyzStr = "";
    for (int a = 0; a < 7; ++a) {           // addr 0...6
        dailyzStr += char(EEPROM.read(a));
    }
    readM += dailyzStr + appendixStr;
    Serial.print("hmmm = ");
    Serial.println(readM);

这是日志显示的内容:

enter image description here

很明显,我希望得到hmmm = 2|||,但我只会得到hmmm = 2

为什么不能追加??

1 个答案:

答案 0 :(得分:1)

我建议使用此:

#include <EEPROM.h>
// Tell it where to store your config data in EEPROM
#define cfgStart 32
// To check if it is your config data
#define version "abc"

struct storeStruct_t{
  char myVersion[3];
  char name[32];
};

void saveConfig() {
  // Save configuration from RAM into EEPROM
  EEPROM.begin(4095);
  EEPROM.put( cfgStart, settings );
  delay(200);
  EEPROM.commit();                      // Only needed for ESP8266 to get data written
  EEPROM.end();                         // Free RAM copy of structure
}

void loadConfig() {
  // Loads configuration from EEPROM into RAM
  Serial.println("Loading config");
  storeStruct_t load;
  EEPROM.begin(4095);
  EEPROM.get( cfgStart, load);
  EEPROM.end();
  // Check if it is your real struct
  if (test.myVersion[0] != version[0] ||
      test.myVersion[1] != version[1] ||
      test.myVersion[2] != version[2]) {
    saveConfig();
    return;
  }
  settings = load;
}

// Empty settings struct which will be filled from loadConfig()
storeStruct_t settings = {
  "",
  ""
};

使用saveConfig()保存设置结构
如果要从EEPROM加载,请使用loadConfig()->它将存储在设置结构中