Sorry guys I seem to be stuck again.
I have managed to write data from my data structure to the SD card successfully.
But I cannot get the ESP32 to read that data from the SD card and put it back into the structure.
I’ve looked at numerous articles and tried all sorts of different ways but to no avail.
In theory it looks so easy,
There are two parts to the code SD1 writes data to the SD card
SDW1 is my hopeless attempt to read the data back into the structure.
sorry to disturb your weekend.
#include <M5Stack.h>
#include <TaskScheduler.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
const int chipSelect = 4;
File dataFile;
typedef struct Board1 {
byte sensornumber; // Sensor number provided by e.g. Sensor=3
String Time; //Time
float Temperature; // DHT Temperature
float Humidity; // DHT Humidity
float BMP_Temperature; // BMP Temperature
float BMP_Pressure; // Bmp Pressure
int Light; // Light
} Board1;
struct Board1 Board_1;
//void SD1();
void SDW1();
//###############################################################################
void setup() {
Serial.begin(115200);
M5.begin();
M5.Lcd.fillScreen(DARKGREEN);
M5.Lcd.setCursor(10, 10);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(3);
M5.Lcd.println("Starting!");
M5.update();
// **dummy data**
Board_1.BMP_Pressure=1010;
Board_1.BMP_Temperature=26.6;
Board_1.Humidity=77;
Board_1.Light=555;
Board_1.sensornumber=44;
Board_1.Temperature=25.5;
Board_1.Time="10:20:30";
//SD1();
SDW1();
}
//###############################################################################
void loop() {
}
//###############################################################################
void SD1(){
auto filename="/Data1.csv";
auto myFile = SD.open(filename, FILE_APPEND);
if(myFile){
myFile.print(Board_1.sensornumber);
myFile.print(",");
myFile.print(Board_1.Time);
myFile.print(",");
myFile.print(Board_1.Temperature);
myFile.print(",");
myFile.print(Board_1.Humidity);
myFile.print(",");
myFile.print(Board_1.BMP_Temperature);
myFile.print(",");
myFile.print(Board_1.BMP_Pressure);
myFile.print(",");
myFile.println(Board_1.Light);
myFile.close();
Serial.println("Done");
}
else{
Serial.println("Error opening Data1.csv");
}
}
//#########################################################################
void SDW1(){
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
delay(2000);
Serial.println("card initialized.");
dataFile = SD.open("/Data1.csv", FILE_READ);
delay(2000);
while (dataFile.available()){
Serial.write(dataFile.read());
}
dataFile.close();
}