#include "FS.h" #include "SD.h" #include "SPI.h" void readFile(fs::FS &fs, const char * path){ //imported from arduino SD example: reads the contents of selected File Serial.printf("Reading file: %s\n", path); File file = fs.open(path); if(!file){ Serial.println("Failed to open file for reading"); return; } Serial.print("Read from file: "); while(file.available()){ Serial.write(file.read()); } file.close(); } void writeFile(fs::FS &fs, const char * path, const char * message){ //imported from arduino SD example: overwrites/creates file and prints data to it Serial.printf("Writing file: %s\n", path); File file = fs.open(path, FILE_WRITE); if(!file){ Serial.println("Failed to open file for writing"); return; } if(file.print(message)){ Serial.println("File written"); } else { Serial.println("Write failed"); } file.close(); } void appendFile(fs::FS &fs, const char * path, const char* message){ //imported from arduino SD example: appends data to the end of selected File Serial.printf("Appending to file: %s\n", path); File file = fs.open(path, FILE_APPEND); if(!file){ Serial.println("Failed to open file for appending"); return; } if(file.print(message)){ Serial.println("Message appended"); } else { Serial.println("Append failed"); } file.close(); } //Code above is SD card functions and configuration, code below is for the reading of temperature data. unsigned long testTimer; // Timer to read the pH probe every second unsigned long writeTimer; // Timer to write data to SD card every 10 minutes int timeStamp; // time in minutes since device powered on, used as the header for each piece of data appended to pH.txt float pH_Value; // pH value calculated from voltage read by sensor String dataPoint; // string combination of tenMinuteAverage and timeStamp that is printed to the pH.txt float Voltage; // actual value read from pH probe, used to calculate pH, it is its own value to aid calibration of probe and troubleshooting float oneSecondData_arr[600]; // stores the pH data taken at one second intervals, length 600 because there are 600 seconds in 10 min int oneSecondArrayIndex=0; // used to index the oneSecondData array float tenMinuteData_arr[7056]; // stores the ten minute averages locally on the ESP32 for ease of calculation, length 7056 because thats the number of 10minute intervals before millis() rolls over int tenMinuteArrayIndex=0; // used to index the tenMinuteData array float tenMinuteAverage=0; // every 10 minutes average of the last 600 seconds of data float averageArray(String chosenArray){ //averages the selected array, returns average float average=0; if(chosenArray.equals("oneSecondData")){ for(int i=0; i<600; i++){ average=average+oneSecondData_arr[i]; } average=average/600; } if(chosenArray.equals("overall")){ for(int i=0; ihigh){ high=tenMinuteData_arr[i]; } if(tenMinuteData_arr[i](testTimer+1000)){ Voltage = analogRead(35) * (3.3 / 4096); pH_Value=-5.70*Voltage+21.34; oneSecondData_arr[oneSecondArrayIndex]=pH_Value; oneSecondArrayIndex++; Serial.println(pH_Value); testTimer=millis(); } if(millis()>(writeTimer+600000)){ Serial.println("Appending the following 10 Minute pH average to pH.txt"); tenMinuteAverage=averageArray("oneSecondData"); Serial.println(tenMinuteAverage); timeStamp=millis()/60000; dataPoint=String(timeStamp)+"_"+String(tenMinuteAverage)+"\r\n"; appendFile(SD, "/pH.txt", dataPoint.c_str()); tenMinuteData_arr[tenMinuteArrayIndex]=tenMinuteAverage; tenMinuteArrayIndex++; tenMinuteAverage=0; oneSecondArrayIndex=0; writeTimer=millis(); } if(Serial.available()>0){ commands(Serial.readString()); } }