#include "FS.h" //setting up the various libraries need for operation #include "SD.h" #include #include #include int analogPin=36; //creation of variables for the rest of the program float rst = 0.0; float vout = 0.0; float avg = 0.0; String avgStr ; float total = 0.0; float Min = 90.0; String MinStr ; float Max = 0.0; String MaxStr ; float min10avg = 0.0; String min10avgStr ; float total10min = 0.0; float tempk= 0.0; float tempf= 0.0; int incre = 0; void min10average(float total10min){ //function that handles the 10 minute averages min10avg = total10min/600.0 ; min10avgStr = String(min10avg) + "F" + "," ; appendFile(SD, "/TempData.txt", min10avgStr.c_str() ); return; } void minimum(float tempf){ //function that checks to see if a measurement is less than if( tempf < Min ){ Min = tempf ; MinStr = "Min " + String(Min) + "F" + "," ; } return; } void maximum(float tempf){ //function that checks to see if a measurement is greater than the current max value if( tempf > Max){ Max = tempf ; MaxStr = "Max " + String(Max) + "F" + "," ; } return; } void continualavg(float tempf){ //function that handles the running/continual average total = total + tempf; avg = (total/incre); avgStr = "continual average " + String(avg) + "F" + "," ; return; } float converter(float vout){ //function that handles converting the value from reading the thermistor connected pin to a Fahrenheit value rst = 10000.0*vout/(3.3-vout); // this gives the actual resistance of the termistor based on supply voltage and value of resistor that is connected to the thermistor tempk = 1.0/(1.0/298.15+log(rst/10000.0)/4300.0) ; //converts thermistor resistance to temperature in kelvin tempf = (tempk - 273.15)*9.0/5.0+32.0; //conversion from kelvin to fahrenheit tempf = tempf - 4.0 ; return tempf; } void appendFile(fs::FS &fs, const char * path, const char * message){ //function for appending a string to a file Serial.printf("Appending to file: %s\n", path); File file = fs.open(path, FILE_APPEND); //status check for if the file opens successfully if(!file){ Serial.println("Failed to open file for appending"); return; } if(file.print(message)){ Serial.println("Message appended"); //details on status of appending } else { Serial.println("Append failed"); } file.println(); file.close(); } void readFile(fs::FS &fs, const char * path){ //function that opens the file to read its contents and then display them over serial 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) { //function that opens file and can write data to it, it also has the capability to create a file if one doesn't already exist 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 setup() { // put your setup code here, to run once: Serial.begin (115200) ; //sets up baud rate SD.begin(); //sets up SD card File file = SD.open("/TempData.txt"); // checks for file of indicated name, and calls writeFile function to create one if needed if(!file) { Serial.println("File doesn't exist"); Serial.println("Creating file..."); writeFile(SD, "/TempData.txt", "Begin"); } else { Serial.println("File already exists"); } file.close(); } void loop() { // put your main code here, to run repeatedly: while(millis()< 86600000){ //milis function set to a bit over 24hrs worth of milliseconds to trigger the program to do its final appending Serial.flush(); //clears serial buffer if(Serial.available()==0){ //checks to make sure nothing is in serial buffer int i= 0; for(i=0; i<600; i++){ //for loop for each 10 minute interval vout = analogRead(analogPin)*3.3/4095.0; incre = incre + 1 ; tempf = converter(vout); Serial.print(tempf); //prints fahrenheit value along with unit label Serial.println("F"); minimum(tempf); //function calls for the respective, min,max and rolling average functions maximum(tempf); continualavg(tempf); total10min = total10min + tempf; //running total for the 10 minute interval delay (1000); if(Serial.available()>0){ //checks to see if the is anything in the serial buffer and restarts loop if so return; } } min10average(total10min); total10min = 0.0; //resets 10 minute average total for the next cycle } else { appendFile(SD, "/TempData.txt", avgStr.c_str() ); //appends file with continual average value delay(1000); appendFile(SD, "/TempData.txt", MinStr.c_str() ); //appends file with min value delay(1000); appendFile(SD, "/TempData.txt", MaxStr.c_str() ); //appends file with max value delay(1000); readFile(SD, "/TempData.txt"); //prints out file contents to serial delay(10000000); //really long delay to basically freeze program } } appendFile(SD, "/TempData.txt", avgStr.c_str() ); //same appending block just for when the millis function registers that enough time has passed delay(1000); appendFile(SD, "/TempData.txt", MinStr.c_str() ); delay(1000); appendFile(SD, "/TempData.txt", MaxStr.c_str() ); delay(1000); readFile(SD, "/TempData.txt"); delay(10000000); }