我需要将一些数据保存到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);
在此处输入图像描述
正如您所看到的,EEPROM.read和EEPROM.get这两个函数在中都不起作用
另外,有人能帮我理解什么时候应该使用EEPROM.write和EEPROM.put吗?这两种方法似乎都适用于integer变量。然而,EEPROM.writes并不像它期望的那样使用字符串进行编译。
此外,在这种情况下,使用EEPROM.read是否正确,或者我应该使用EEPROM.get函数,因为我不确定的区别是什么
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
:::::::::::::::