Cyber Physical Systems Lab 3

Dependencies:   mbed C12832 LM75B

Files at this revision

API Documentation at this revision

Comitter:
ciaranom
Date:
Sat Dec 05 15:46:50 2020 +0000
Parent:
6:e883d7b9c790
Commit message:
Data logger

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r e883d7b9c790 -r 8b6bbd2d9889 main.cpp
--- a/main.cpp	Tue Nov 10 12:11:20 2020 +0000
+++ b/main.cpp	Sat Dec 05 15:46:50 2020 +0000
@@ -1,68 +1,95 @@
 #include "mbed.h"
 #include "LM75B.h"
 #include "C12832.h"
+#include <string>
+#include <iostream>
+#include <stdio.h>
+#include <cstdlib>
 
 C12832 lcd(p5, p7, p6, p8, p11);
 
 LM75B sensor(p28,p27);
 Serial pc(USBTX,USBRX);
+float TempV; //making a float variable for the temperature value 
+float cycles = 300;
+
+LocalFileSystem local("local"); // Create the local filesystem under the name "local"
 
 
 
-int main ()
+int main () //Main function ***************************************************************************************
 {
-    //variables 
-    LocalFileSystem local("local");               // Create the local filesystem under the name "local"
-    FILE *fp = fopen("/local/temp2.csv", "w");  // Open "out.txt" on the local file system for writing
+    FILE *fp = fopen("/local/temp3.csv", "a");  //Create the file
+    fclose (fp);                                //Close the file
+
+// Variables 
     int i =0;
-    //float temps [5];
-    //float sensorval;
-    //Try to open the LM75B
-    if (sensor.open()) 
+//    int j =0;
+
+    
+//while (j<288) //144 5 min cycles in 24 hours *** Main while loop for 3 functions, Writing, reading, displaying 
+    
+    if (sensor.open()) //Try to open the LM75B
     {
-        printf("Device detected!\n");
-        while (i < 5) 
-        {
-            lcd.cls();
-            lcd.locate(0,3);
-            lcd.printf("Temp = %.3f", (float)sensor);
-            wait(0.5);
-            fprintf(fp, "%.3f,", (float)sensor);
-            //cur_temp = (float)sensor;
-            //sensorval = (float)sensor;
-            //printf("\n\r %.3f\n\r",sensorval);
-            //temps[i] = sensorval;
-            //pc.   printf("%.3f ",temps[i]);
-            i = i+1;
-            wait(0.5);
-        }//end while loop
-    } 
-    else
+        printf("Device detected!\n\r");        
+    }    
+    else 
     {
         error("Device not detected!\n");
     }//end if sensor open
-    
-    //for(int j = 0; j<i; j++) //causing infinite loop 
-   
         
-    //close files        
-    fclose(fp);
+    fp = fopen("/local/temp3.csv", "a"); //Open the file for writing to    
     
-       FILE * pFile;
-   char mystring [5];
+    printf("Measuring temp... \n\r"); //Print confirmation of code running      
+    while (i<cycles)
+    {
+        TempV = (float)sensor; //Temperature is the sensor value 
+
+        fprintf(fp, "%.2f\n", TempV); //print values to file 
+
+        i = i+1; // counter
+        wait(1); //Wait 1 seconds to 1*300s = 5 minutes         
+            
+    } //end while loop for writing function
+    fclose (fp);//close the file 
+        
+    char temps[5]; //Create a string that will contain temerature values from file
 
-   pFile = fopen ("/local/temp2.csv" , "r");
-   if (pFile == NULL) perror ("Error opening file");
-   else {
-     if ( fgets (mystring , 5 , pFile) != NULL )
-       pc.printf(mystring);
-     fclose (pFile);
-     //pc.printf("%.3f ",temps[i]);
-   }
-   return 0;
+        
+    fp = fopen("/local/temp3.csv", "r"); //Open rfile for reading 
+            
+    //min max total
+    double num = 0;
+    double total = 0;
+    double maxtemp = -99.99;
+    double mintemp = 99.99;
+            
+    while (fscanf(fp, "%s", temps)!= EOF) //scan to end of file
+    {          
+        num = atof(temps); //string to number --> https://os.mbed.com/questions/7171/How-to-convert-String-to-Float-value/
+        
+        if(num > maxtemp) //Calculating max number
+        {
+            maxtemp = num;
+        }
+        
+        if(num < mintemp) //Calculating min number
+        {
+            mintemp = num; 
+        }
+            
+        total = total+ num;
+
    
-    //printf("%.3f",temps[i]);
-    }
-//end main
+    } //while loop creating sting of values from file ends
+        double avg = total/(cycles);
+        
+        printf("Average: %.2f \n\r", avg);
+        printf("Max: %.2f \n\r", maxtemp);
+        printf("Min: %.2f \n\r", mintemp);
+        
+        fclose(fp); // close file
+    
+    //j=j+1 // Controls daily cycle      
 
-
+}   //end main
\ No newline at end of file