Amir Asemanpayeh / Mbed 2 deprecated WeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
20:ec528f0e63c5
Parent:
19:61eaa5235ccf
Child:
21:d76880ca7499
--- a/main.cpp	Fri May 01 06:19:20 2015 +0000
+++ b/main.cpp	Sun May 03 14:35:37 2015 +0000
@@ -8,6 +8,7 @@
 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
 BusOut leds(LED4,LED3,LED2,LED1);
 Serial serial(USBTX,USBRX);
+LocalFileSystem local("local"); // define local file system
 InterruptIn button1(p16);
 InterruptIn button2(p17);
 InterruptIn button3 (p15);
@@ -43,7 +44,7 @@
 int powerSaverTime = 60;
 int dataLoggerTime = 60;
 
-int temperature;
+float temperature;
 float pressure;
 int averageTemperature ;
 int averagePressure ;
@@ -78,6 +79,25 @@
 char buffer5[14];
 
 
+
+//arrays used to store data to the local file
+char timeSent[100];
+char dateSent[100];
+char temperatureSent[100];
+char pressureSent[100];
+char temperatureRecieved[100];
+char pressureRecieved[100];
+char timeRecieved[100];
+char dateRecieved[100];
+
+
+//cahrs used to split arrays into indexes
+char * pch;
+char * pch1;
+char * pch2;
+char * pch3;
+
+
 int state=0; // used to navigate through the finite machines
 int stateplus1 ;
 int stateplus2 ;
@@ -225,6 +245,14 @@
 
 
 
+void saveToFile(char *data,char *data1,char *data2,char *data3)
+{
+    FILE* pFile = fopen("/local/textfile.csv","w"); // open file access
+    fprintf (pFile, "%s\n%s\n%s\n%s", data,data1,data2,data3);
+    fclose (pFile);
+}
+
+
 
 void updateTime()
 {
@@ -268,12 +296,12 @@
     if (unitFlag==1 || unitFlag==2 ) {
 
         temperature = measurement.temperature;
-        int length = sprintf(bufferT,"T = %0.1d C",temperature); // print formatted data to buffer
+        int length = sprintf(bufferT,"%0.1f",temperature); // print formatted data to buffer
         // it is important the format specifier ensures the length will fit in the buffer
     }
     if (unitFlag==3 || unitFlag==4 ) {
         temperature =( measurement.temperature*(9/5))+32;
-        int length = sprintf(bufferT,"T = %0.1d F",temperature); // print formatted data to buffer
+        int length = sprintf(bufferT,"%0.1f",temperature); // print formatted data to buffer
         // it is important the format specifier ensures the length will fit in the buffer
     }
 
@@ -282,12 +310,12 @@
 
     if (unitFlag==1 || unitFlag==3 ) {
         pressure = measurement.pressure;  // same idea with float
-        int  length = sprintf(bufferP,"P = %.2f mb",pressure); // print formatted data to buffer
+        int  length = sprintf(bufferP,"%.2f",pressure); // print formatted data to buffer
         // it is important the format specifier ensures the length will fit in the buffer
     }
     if (unitFlag==2 || unitFlag==4 ) {
         pressure = measurement.pressure*0.0009869;  // same idea with floats
-        int length = sprintf(bufferP,"P = %.2f atm",pressure); // print formatted data to buffer
+        int length = sprintf(bufferP,"%.2f",pressure); // print formatted data to buffer
         // it is important the format specifier ensures the length will fit in the buffer
     }
 
@@ -301,17 +329,18 @@
 }
 
 
-void writeToFile(char *data,char *data1)
+void writeToFile(char *data,char *data1,char *data2,char *data3)
 {
     FILE* pFile = fopen("/local/textfile.txt","w"); // open file access
 
-    fprintf (pFile, "%s%s" , data,data1);
+    fprintf (pFile, "%s\n%s\n%s\n%s\n" , data,data1,data2,data3);
     fclose (pFile);
 }
 
 
 
 
+
 void wakeUp ()  //Turns the LCD on after the mbed is interrupted
 {
 
@@ -409,6 +438,7 @@
 
 void loggerData() //  gets the temp and pressure data and stores to suitable arrays (the arrays are used to plot the desired graphs)
 {
+
     if(dataLoggerFlag ==1) {
         int k ;
         int sumTemperature = 0;
@@ -423,21 +453,72 @@
         dataLoggerFlag=0;
 //write the data to the arrays (arrays used to plot graphs)
         //read the data from sensor
+        //inserts the int values to char arrays to save them to the local file
+        //for (int l=0 ; l<i ; l++) {
 
-        //Store the values in the array
-        arrayT[i] = temperature;
-        arrayP[i] = pressure/25 ;
+        strcat(temperatureSent,bufferT);
+        strcat(temperatureSent,",");
+        strcat(pressureSent,bufferP);
+        strcat(pressureSent,",");
+
+        //}
+        //apends the new time and current stamp to the array with , between them
+        strcat(timeSent,bufferTime);
+        strcat(timeSent,",");
+        strcat (dateSent,bufferDate);
+        strcat (dateSent,",");
+
+        //send the arrays above to the local file
+        saveToFile(temperatureSent,pressureSent,timeSent,dateSent);
+        serial.printf ("sent temp %s\nsent pressure %s\nsent time %s\nsent date %s\n",temperatureSent,pressureSent,timeSent,dateSent);
+
+//reads the saved data back from the local file
+
+        FILE* pFilea = fopen("/local/textfile.csv","r"); // open file access
+        fscanf (pFilea, "%s\n %s\n %s\n %s\n", temperatureRecieved,pressureRecieved,timeRecieved,dateRecieved);
+        fclose (pFilea);
+
+        serial.printf ("rec temp %s\nrec pressure %s\nrec time %s\nrec date %s\n",temperatureRecieved,pressureRecieved,timeRecieved,dateRecieved);
+
 
+        //seperates the array into tokens(seperated after the delimiter , )
+        pch = strtok (timeRecieved,",");//pch is the arguement of the for str
+        while (pch != NULL) {
+            serial.printf (" splitted time = %s\n",pch);
+            pch = strtok (NULL, ",");
+        }
+        pch1 = strtok (dateRecieved,",");
+        while (pch1 != NULL) {
+            serial.printf (" splitted date= %s\n",pch1);
+            pch1 = strtok (NULL, ",");
+        }
+        pch2 = strtok (temperatureRecieved,",");
+        int p=0;
+        while (pch2 != NULL) {
+            serial.printf (" splitted temperature = %s\n",pch2);
+            arrayT[p]=atof(pch2);
+            pch2 = strtok (NULL, ",");
+            p++;
+        }
+        pch3 = strtok (pressureRecieved,",");
+        p=0;
+        while (pch3 != NULL) {
+            serial.printf (" splitted pressure= %s\n",pch3);
+            arrayP[p]=atof(pch3);
+            pch3 = strtok (NULL, ",");
+            p++;
+        }
 
 // calculate the average value of the arrays and save them to an integer
 
         for (k = 0 ; k<=i ; k++) { // loops through the arrays
-
+            printf("arrayT = %lf\n",arrayT[k]);
+            printf("arrayP = %lf\n",arrayP[k]);
             sumTemperature += arrayT[k]; // calculates the sum of the stored values
             averageTemperature = sumTemperature/(k+1) ; //calculates the average value
 
             sumPressure += arrayP[k]; // calculates the sum of the stored values
-            averagePressure =( 25*(sumPressure/(k+1))) ; //calculates the average value
+            averagePressure =(sumPressure/(k+1)) ; //calculates the average value
 
             if(arrayT[k]>maxTemperature) { // checks if any of the values in the array is bigger than the fist value
                 maxTemperature=arrayT[k];  // if any greater values, sets the max value to that
@@ -446,10 +527,10 @@
                 minTemperature=arrayT[k];
             }
             if(arrayP[k]>maxPressure) { // checks if any of the values in the array is bigger than the fist value
-                maxPressure=(25*arrayP[k]);  // if any greater values, sets the max value to that
+                maxPressure=(arrayP[k]);  // if any greater values, sets the max value to that
             }
             if(arrayP[k]<maxPressure) {
-                minPressure=(25*arrayP[k]);
+                minPressure=(arrayP[k]);
             }
 
         }
@@ -550,7 +631,7 @@
             int display2 = sprintf (buffer1, "");
             int display0 = sprintf (buffer2, "Threshold");
             int display1 = sprintf (buffer3, "Reached!");
-            int display4 = sprintf (buffer4, "%0.1d!",temperature);
+            int display4 = sprintf (buffer4, "%0.1f!",temperature);
             int display5 = sprintf (buffer5, "Stop");
             display();
             for (int i=0; i<=11; i++) {
@@ -573,7 +654,7 @@
             int display2 = sprintf (buffer1, "");
             int display0 = sprintf (buffer2, "Threshold");
             int display1 = sprintf (buffer3, "Reached!");
-            int display4 = sprintf (buffer4, "%0.1d!",temperature );
+            int display4 = sprintf (buffer4, "%0.1f!",temperature );
             int display5 = sprintf (buffer5, "Stop");
             display();
             for (int i=0; i<=11; i++) {
@@ -635,7 +716,7 @@
             }
         }
     }
-    exit:
+exit:
     redLED = 0 ;
 
 }
@@ -1313,7 +1394,7 @@
     powerSaverCheck();
 
     while(1) {
-        
+
         updateTime();
         loggerData();
         startMenu();