// ESP32 with custom flash size
//
// Check out "flashSize" attr in diagram.json
#include "SPIFFS.h"
#include "time.h"
const char* passPath = "/pass.txt";
String pass;
int ln;
String weekdays;
String readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
return String();
}
String fileContent;
while(file.available()){
fileContent = file.readStringUntil('\0');
break;
}
return fileContent;
}
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, "a");
if(!file){
Serial.println("- failed to open file for writing");
return;
}
ln++;
char messageOut[20];
sprintf(messageOut, "%d%s", ln, message );
if(file.print(messageOut)){
Serial.println("- file written");
} else {
Serial.println("- frite failed");
}
}
String showTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
//return String();
}
char timeStringBuff[50]; //50 chars should be enough
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
Serial.println(timeStringBuff);
// showTime=strftime(&timeinfo1, "%A, %B %d %Y %H:%M:%S")
return (timeStringBuff);
}
void weekdaysDecoder(const char * message){
char formated[20];
for(int i=0;i<strlen(message);i++){
if(message[i]=='1'){
Serial.printf("%d,",i);
formated[i]=i;
}
}
Serial.println(formated);
}
void setup() {
Serial.begin(115200);
showTime();
ln=0;
printf("Flash size: %d bytes\n", spi_flash_get_chip_size());
initSPIFFS();
writeFile(SPIFFS, passPath, "Hallo WOrlds\n");
}
void initSPIFFS() {
if (!SPIFFS.begin(true)) {
Serial.println("An error has occurred while mounting SPIFFS");
} else{
Serial.println("SPIFFS mounted successfully");
}
}
void loop() {
delay(1000);
showTime();
pass = readFile(SPIFFS, passPath);
Serial.print(pass);
writeFile(SPIFFS, passPath, "Hallo WOrlds\n");
weekdaysDecoder("1010101");
}