ESP32将字符串写入EEPROM

问题描述

我需要保存一些数据到EEPROM,因为即使在设备关闭的情况下,我也想保留该值。当设备再次通电时,我需要记住最后一个值是什么。

我将整数值保存到EEPROM没问题,如下所示:

void parse_item_inside(char* payload){



    cJSON* jsonObj = cJSON_Parse((char*)(payload));

  // Get pointer to specific field in JSON object

    cJSON* serial = cJSON_GetObjectItem(jsonObj,"Serial");

    cJSON* quantity = cJSON_GetObjectItem(jsonObj,"Quantity");

     //Get integer value - other fields allow access to strings,floats etc.



    strcpy(item_inside.serial,serial->valuestring);

    item_inside.quantity = quantity->valueint;



    EEPROM.put(2,item_inside.quantity);

    Serial.print("Checking serial before puting to eeprom=");

    Serial.println(item_inside.serial);

    EEPROM.put(4,item_inside.serial);

    //EEPROM.write(4,item_inside.serial);



    EEPROM.commit();



    Serial.print("ITEM_INSIDE_QUANTITY=");

    Serial.println(EEPROM.read(2));







    Serial.print("ITEM_INSIDE_SERIAL READ=");

    Serial.println(EEPROM.read(4));

    Serial.print("ITEM_INSIDE_SERIAL get=");

    Serial.println(EEPROM.get(4,local_serial));

    OLED_display(item_inside.quantity,number_to_pick);



// Delete JSON object (don't forget this)

  cJSON_Delete(jsonObj);



    }



但是,当我尝试放置字符串变量(item_inside.serial)

EEPROM.put(4,item_inside.serial);

enter image description here

如您所见,这两个功能EEPROM.read和EEPROM.get都不起作用

此外,有人可以帮助我了解何时应使用EEPROM.write和EEPROM.put。两者似乎都可以很好地用于整数变量,但是,EEPROM.write不能像预期的那样使用字符串进行编译。

此外,在这种情况下使用的是EEPROM.read还是正确的,或者我应该使用EEPROM.get函数代替,因为我不确定有什么区别

解决方法

Juraj-你是对的。我的地址有些问题。我无法在注释中正确发布代码,因此无法做出新的答案。 item_inside序列初始化如下:

char item_inside.serial[10];
    int address=0;
    EEPROM.writeByte(address,item_inside.quantity);       
    address += sizeof(byte);
    
    EEPROM.writeString(address,item_inside.serial);
    address += 10;

    EEPROM.commit();

现在它可以正确地写入EEPROM

,

使用结构来保存数据记录:

struct SERIAL_DATA
{
    uint_8 numBytes;
    char   data[10];
};

#define BEGIN_OF_PAD 0

保存到 EEPROM:

int addr = BEGIN_OF_PAD;
DATA_RECORD serialData;                            // Instanciate a record

serialData.numBytes = item_inside.quantity;        // Add field to record

String tmpStr = String(item_inside.serial);        // Could use strcpy() too. 
                                                   // I prefer to use String in case I
                                                   // need to validate the data. 
tmpStr.toCharArray(serialData.data,tmpStr.length()); // Add field to the record 

EEPROM.put(addr,serialData);                      // Put record into EEPROM buffer
EEPROM.commit();                                   // Commit

addr += sizeof(serialData);                        // Advance address for next record

从 EEPROM 中检索:

#import <iostream>
using namespace std;

addr = BEGIN_OF_PAD;
DATA_RECORD retrievedData;                         // Instanciate a record

EEPROM.get(addr,retrievedData);                   // Retrieve a record from the EEPROM

cout<<"Data size: "<<retrievedData.numBytes<<endl; // Process the record as needed
cout<<"Data: "     <<retrievedData.data    <<endl;

addr += sizeof(serialData);                        // Advance address for next record

:::::::::::::::

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...