I have a project that uses RAW binary data.
When I currently want to store the binary data, it doesn't seem to go right. The binary data is correct before writing to SPIFFS, after I want to read from SPIFFS and it gives way too many bytes or way too few. And all the values are completely random.
My question is: what is the correct way to write a byte-array to SPIFFS and reading SPIFFS to a byte-array.
The byte array can vary between 60 bytes and 80,000 bytes. The program runs on an ESP32 and has enough RAM to process this size.
Seems to suggest that the write parameter should be "wb"
Change the fopen mode to “wb”; this will open the file for writing/binary; It will also have the side effect of truncating an existing file, so you can “remove” the remove call. If you want to keep the remove, "wb" or “ab” will do the same with the exception that "ap" will automatically append to an existing file. This is true for the text version as well ("w" vs "a").
There seems to be some issues with the code, look at the comments:
void storeInSPIFFS(byte* payload) { //You should add a parameter with "size in bytes" for the payload
File storage = SPIFFS.open("/storage.bin", "w+");
if (storage)
{
Serial.println(F("Going to write payload to SPIFFS"));
size_t sizePayload = sizeof(payload)/sizeof(uint32_t)-1;
//sizePayload now equals the "number of uint32_t minus one" in payload, why?
Serial.println(sizePayload);
//Here you are printing bytes, not 32 bit integers
for (int i = 0; i < sizePayload; i++) { // for debug purposes check the content of the payload
Serial.print((byte)payload[i]); // DEBUG
Serial.print(":"); // DEBUG
} // DEBUG
Serial.println(""); // DEBUG
//Here you are writing a wrong amount of bytes to SPIFFS
storage.write(payload, sizePayload); //Also tried fixed value for length of buffer
//Here you are allocating and loading a buffer of wrong size
byte bufferInfo[sizePayload];
uint32_t nBytes = storage.readBytes((char*)bufferInfo, sizePayload); //
//Again, printing wrong data type or size
for (int i = 0; i < sizePayload; i++) { //Also tried fixed value
Serial.print((byte)bufferInfo[i]);
Serial.print(":");
}
Serial.print("NBytes:");
Serial.println(nBytes);
}
storage.close();
}