Cyber Physical Systems Lab 3
Dependencies: mbed C12832 LM75B
main.cpp@7:8b6bbd2d9889, 2020-12-05 (annotated)
- Committer:
- ciaranom
- Date:
- Sat Dec 05 15:46:50 2020 +0000
- Revision:
- 7:8b6bbd2d9889
- Parent:
- 6:e883d7b9c790
Data logger
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 0:ce7a8546502b | 1 | #include "mbed.h" |
chris | 2:9e757151de9b | 2 | #include "LM75B.h" |
chris | 5:608f2bf4d3f7 | 3 | #include "C12832.h" |
ciaranom | 7:8b6bbd2d9889 | 4 | #include <string> |
ciaranom | 7:8b6bbd2d9889 | 5 | #include <iostream> |
ciaranom | 7:8b6bbd2d9889 | 6 | #include <stdio.h> |
ciaranom | 7:8b6bbd2d9889 | 7 | #include <cstdlib> |
okano | 0:ce7a8546502b | 8 | |
chris | 5:608f2bf4d3f7 | 9 | C12832 lcd(p5, p7, p6, p8, p11); |
chris | 5:608f2bf4d3f7 | 10 | |
chris | 4:6df97cb10041 | 11 | LM75B sensor(p28,p27); |
chris | 4:6df97cb10041 | 12 | Serial pc(USBTX,USBRX); |
ciaranom | 7:8b6bbd2d9889 | 13 | float TempV; //making a float variable for the temperature value |
ciaranom | 7:8b6bbd2d9889 | 14 | float cycles = 300; |
ciaranom | 7:8b6bbd2d9889 | 15 | |
ciaranom | 7:8b6bbd2d9889 | 16 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
okano | 0:ce7a8546502b | 17 | |
ciaranom | 6:e883d7b9c790 | 18 | |
ciaranom | 6:e883d7b9c790 | 19 | |
ciaranom | 7:8b6bbd2d9889 | 20 | int main () //Main function *************************************************************************************** |
okano | 0:ce7a8546502b | 21 | { |
ciaranom | 7:8b6bbd2d9889 | 22 | FILE *fp = fopen("/local/temp3.csv", "a"); //Create the file |
ciaranom | 7:8b6bbd2d9889 | 23 | fclose (fp); //Close the file |
ciaranom | 7:8b6bbd2d9889 | 24 | |
ciaranom | 7:8b6bbd2d9889 | 25 | // Variables |
ciaranom | 6:e883d7b9c790 | 26 | int i =0; |
ciaranom | 7:8b6bbd2d9889 | 27 | // int j =0; |
ciaranom | 7:8b6bbd2d9889 | 28 | |
ciaranom | 7:8b6bbd2d9889 | 29 | |
ciaranom | 7:8b6bbd2d9889 | 30 | //while (j<288) //144 5 min cycles in 24 hours *** Main while loop for 3 functions, Writing, reading, displaying |
ciaranom | 7:8b6bbd2d9889 | 31 | |
ciaranom | 7:8b6bbd2d9889 | 32 | if (sensor.open()) //Try to open the LM75B |
ciaranom | 6:e883d7b9c790 | 33 | { |
ciaranom | 7:8b6bbd2d9889 | 34 | printf("Device detected!\n\r"); |
ciaranom | 7:8b6bbd2d9889 | 35 | } |
ciaranom | 7:8b6bbd2d9889 | 36 | else |
ciaranom | 6:e883d7b9c790 | 37 | { |
ciaranom | 6:e883d7b9c790 | 38 | error("Device not detected!\n"); |
ciaranom | 6:e883d7b9c790 | 39 | }//end if sensor open |
ciaranom | 6:e883d7b9c790 | 40 | |
ciaranom | 7:8b6bbd2d9889 | 41 | fp = fopen("/local/temp3.csv", "a"); //Open the file for writing to |
ciaranom | 6:e883d7b9c790 | 42 | |
ciaranom | 7:8b6bbd2d9889 | 43 | printf("Measuring temp... \n\r"); //Print confirmation of code running |
ciaranom | 7:8b6bbd2d9889 | 44 | while (i<cycles) |
ciaranom | 7:8b6bbd2d9889 | 45 | { |
ciaranom | 7:8b6bbd2d9889 | 46 | TempV = (float)sensor; //Temperature is the sensor value |
ciaranom | 7:8b6bbd2d9889 | 47 | |
ciaranom | 7:8b6bbd2d9889 | 48 | fprintf(fp, "%.2f\n", TempV); //print values to file |
ciaranom | 7:8b6bbd2d9889 | 49 | |
ciaranom | 7:8b6bbd2d9889 | 50 | i = i+1; // counter |
ciaranom | 7:8b6bbd2d9889 | 51 | wait(1); //Wait 1 seconds to 1*300s = 5 minutes |
ciaranom | 7:8b6bbd2d9889 | 52 | |
ciaranom | 7:8b6bbd2d9889 | 53 | } //end while loop for writing function |
ciaranom | 7:8b6bbd2d9889 | 54 | fclose (fp);//close the file |
ciaranom | 7:8b6bbd2d9889 | 55 | |
ciaranom | 7:8b6bbd2d9889 | 56 | char temps[5]; //Create a string that will contain temerature values from file |
chris | 4:6df97cb10041 | 57 | |
ciaranom | 7:8b6bbd2d9889 | 58 | |
ciaranom | 7:8b6bbd2d9889 | 59 | fp = fopen("/local/temp3.csv", "r"); //Open rfile for reading |
ciaranom | 7:8b6bbd2d9889 | 60 | |
ciaranom | 7:8b6bbd2d9889 | 61 | //min max total |
ciaranom | 7:8b6bbd2d9889 | 62 | double num = 0; |
ciaranom | 7:8b6bbd2d9889 | 63 | double total = 0; |
ciaranom | 7:8b6bbd2d9889 | 64 | double maxtemp = -99.99; |
ciaranom | 7:8b6bbd2d9889 | 65 | double mintemp = 99.99; |
ciaranom | 7:8b6bbd2d9889 | 66 | |
ciaranom | 7:8b6bbd2d9889 | 67 | while (fscanf(fp, "%s", temps)!= EOF) //scan to end of file |
ciaranom | 7:8b6bbd2d9889 | 68 | { |
ciaranom | 7:8b6bbd2d9889 | 69 | num = atof(temps); //string to number --> https://os.mbed.com/questions/7171/How-to-convert-String-to-Float-value/ |
ciaranom | 7:8b6bbd2d9889 | 70 | |
ciaranom | 7:8b6bbd2d9889 | 71 | if(num > maxtemp) //Calculating max number |
ciaranom | 7:8b6bbd2d9889 | 72 | { |
ciaranom | 7:8b6bbd2d9889 | 73 | maxtemp = num; |
ciaranom | 7:8b6bbd2d9889 | 74 | } |
ciaranom | 7:8b6bbd2d9889 | 75 | |
ciaranom | 7:8b6bbd2d9889 | 76 | if(num < mintemp) //Calculating min number |
ciaranom | 7:8b6bbd2d9889 | 77 | { |
ciaranom | 7:8b6bbd2d9889 | 78 | mintemp = num; |
ciaranom | 7:8b6bbd2d9889 | 79 | } |
ciaranom | 7:8b6bbd2d9889 | 80 | |
ciaranom | 7:8b6bbd2d9889 | 81 | total = total+ num; |
ciaranom | 7:8b6bbd2d9889 | 82 | |
ciaranom | 6:e883d7b9c790 | 83 | |
ciaranom | 7:8b6bbd2d9889 | 84 | } //while loop creating sting of values from file ends |
ciaranom | 7:8b6bbd2d9889 | 85 | double avg = total/(cycles); |
ciaranom | 7:8b6bbd2d9889 | 86 | |
ciaranom | 7:8b6bbd2d9889 | 87 | printf("Average: %.2f \n\r", avg); |
ciaranom | 7:8b6bbd2d9889 | 88 | printf("Max: %.2f \n\r", maxtemp); |
ciaranom | 7:8b6bbd2d9889 | 89 | printf("Min: %.2f \n\r", mintemp); |
ciaranom | 7:8b6bbd2d9889 | 90 | |
ciaranom | 7:8b6bbd2d9889 | 91 | fclose(fp); // close file |
ciaranom | 7:8b6bbd2d9889 | 92 | |
ciaranom | 7:8b6bbd2d9889 | 93 | //j=j+1 // Controls daily cycle |
okano | 0:ce7a8546502b | 94 | |
ciaranom | 7:8b6bbd2d9889 | 95 | } //end main |