I would like to know how to store data from a sensor for 24 hours in my esp32? the problem I have is that on my serial monitor the text is displayed by using spiffs but when opening the text document there is nothing. I also tried to save by using preferences but there not enugh space and i found it's better to use spiffs for long data.
the code :
//#include "SPIFFS.h"
#include <heltec.h>
#include <DHT12.h>
SSD1306Wire aff(0x3c, SDA_OLED, SCL_OLED, RST_OLED, GEOMETRY_64_32);
DHT12 dht12;
void setup() {
Serial.begin(115200);
dht12.begin();
aff.init();
aff.flipScreenVertically();
aff.setFont(ArialMT_Plain_10);
}
void loop() {
float temp = dht12.readTemperature();
float hum = dht12.readHumidity();
Serial.println(temp);
aff.clear();
aff.drawString(0, 0, "Temp:" + (String)temp + "�C");
aff.drawString(0, 10, "Hum:" + (String)hum + "%");
aff.display();
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
//--------- Write to file
File fileToWrite = SPIFFS.open("/test.txt", FILE_WRITE);
if (!fileToWrite) {
Serial.println("There was an error opening the file for writing");
return;
}
if (fileToWrite.print("ORIGINAL LINE")) {
Serial.println("File was written");;
} else {
Serial.println("File write failed");
}
fileToWrite.close();
//--------- Apend content to file
File fileToAppend = SPIFFS.open("/test.txt", FILE_APPEND);
if (!fileToAppend) {
Serial.println("There was an error opening the file for appending");
return;
}
if (fileToAppend.println(temp)) {
Serial.println("File content was appended");
} else {
Serial.println("File append failed");
}
fileToAppend.close();
//---------- Read file
File fileToRead = SPIFFS.open("/test.txt");
if (!fileToRead) {
Serial.println("Failed to open file for reading");
return;
}
Serial.println("File Content:");
while (fileToRead.available()) {
Serial.write(fileToRead.read());
}
fileToRead.close();
delay(3000);
}