Hi All,
So, I am using esp32 for my project. I am receiving some data over BLE and I'm trying to store that data in EEPROM of esp32 via the Preferences library. I'm using esp32 BLE UART code to receive data. The data is receiving fine. The problem starts when I'm trying to save the data.
So below is the code -
void ProcessData(char c[], int l){
char cc[l];
String s = "";
for(int i = 0; i < l; i++){
cc[i] = c[i];
s = s + cc[i];
}
Serial.print("BLE Read Data is: ");
Serial.println(s);
Serial.print("BLE Read Data l :");
Serial.println(s.length());
if (s == "OTA") {
Serial.println("OTA enabled for 60 sec");
StoreData("ble", "false");
Serial.println(ReadData("ble"));
delay(500);
ESP.restart();
}
}
#include <Preferences.h>
Preferences preferences;
void StoreData(const char* key, const char* val){
preferences.begin("store",false);
preferences.putString(key, val);
preferences.end();
}
String ReadData(const char* val){
preferences.begin("store",false);
String ret = preferences.getString(val);
preferences.end();
return ret;
}
Now, I have a separate file for each code, BLE.h is where the BLE UART code resides and so is the ProcessData() function and OTA.h is where StoreData() and ReadData() are present, with Preferences library defined and Preferences initiated.
The problem here is - I am receiving data over BLE fine, but the code isn't storing data to the Preferences.
As seen from the code above, I am storing StoreData("ble", "false"); and when I read it back, it still prints "true"!
Since the data is sent from BLE, I can send it multiple times and after few tries it does writes successfully!
Why do you think it's happening like this?
Help!
PS - I thought it might be because I need to add some delay between read and write to the EEPROM, so I did and it still didn't work.