Simple Weather Station. Data Logger and gives primitive weather predictions.

Dependencies:   BMP180 N5110 PowerControl beep mbed

Files at this revision

API Documentation at this revision

Comitter:
el13nsp
Date:
Sat May 09 17:08:06 2015 +0000
Parent:
5:cae171557246
Child:
7:b92507a1a1e5
Commit message:
Updated comments;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat May 09 15:10:14 2015 +0000
+++ b/main.cpp	Sat May 09 17:08:06 2015 +0000
@@ -8,6 +8,7 @@
 #include "main.h"
 
 // Interrupt Service Routine
+
 void timerExpired()
 {
     timerFlag = 1;
@@ -27,12 +28,12 @@
 {
     writingTimerFlag = 1;
 }
+
 void graphTimerExpired()
 {
     graphTimerFlag = 1;
 }
 
-//Interrupt service routine
 void buttonAPressed()
 {
     leds = 15;
@@ -57,7 +58,7 @@
 void buttonDPressed() //Toggle unit in ISR
 {
     leds = 15;
-    unit = !unit;
+    unit = !unit; // unti switches between celsius and fahrenheit and mb and hpa
 }
 
 int main()
@@ -80,7 +81,8 @@
 
     PHY_PowerDown();
     //int result = semihost_powerdown();
-
+    
+    //Sets button to pull up mode
     buttonB.mode(PullUp);
     buttonC.mode(PullUp);
     buttonD.mode(PullUp);
@@ -111,7 +113,7 @@
 
     Measurement measurement;  // measurement structure declared in BMP180 class
 
-    float pressureChange = 0;
+    float pressureChange = 0; // Initializes the pressure change variable to 0
 
     while(1) {
 
@@ -121,7 +123,8 @@
 
         char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
         // so can display a string of a maximum 14 characters in length
-
+        
+        //If-else statement to switch the LCD ON or OFF based on potentiometer value
         if(potValue<0.1) {
 
             lcd.turnOff();
@@ -134,14 +137,16 @@
             lcd.init();
 
         }
-
+        
+        //If state timer flag is set
         if(stateTimerFlag) {
 
-            stateTimerFlag = 0;
+            stateTimerFlag = 0; //reset flag
 
             // read values (T in Celsius and P in mb) and print over serial port
             measurement = bmp180.readValues();
-
+            
+            //Sets pressure reading in an array to compare over the hour
             if(j==12) {
                 for (int n = 0; n < 11; n++) {
                     presscompare[n] = presscompare[n+1];
@@ -151,14 +156,16 @@
 
             presscompare[j] = measurement.pressure;
             j++;
-
+            
+            //If-else statement to check if prediction is possible or not
             if( presscompare[11] == 0) {
                 prediction = 0;
             } else
                 prediction = 1;
 
-            pressureChange = presscompare[11] - presscompare[0];
-
+            pressureChange = presscompare[11] - presscompare[0]; //Gets pressure change over the hour
+            
+            //If-else if statements to set the state based on pressure change
             if((pressureChange < 0.1) &&  (pressureChange > -0.1))
                 state = 0;
             else if((pressureChange > 0.1) &&  (pressureChange <= 0.5))
@@ -177,17 +184,19 @@
                 state = 7;
             else if(pressureChange < -2.0)
                 state = 8;
-
+            
+            //Turns alarm ON if weather prediction is stormy
             if((state == 8) && (prediction == 1))
                 alarm = 1;
             else
                 alarm = 0;
 
         }
-
+        
+        //If timer flag is set
         if(timerFlag) {
 
-            timerFlag = 0;
+            timerFlag = 0; //reset flag
 
             // read values (T in Celsius and P in mb) and print over serial port
             measurement = bmp180.readValues();
@@ -196,29 +205,29 @@
 
             leds = 0;
 
-            if(unit == celcius) {
+            if(unit == celcius) {  //If unit is celcius
 
                 int length = sprintf(buffer,"T = %.2f C",measurement.temperature); // print formatted data to buffer
                 // it is important the format specifier ensures the length will fit in the buffer
                 if (length <= 14)  // if string will fit on display
                     lcd.printString(buffer,0,1);           // display on screen
 
-                length = sprintf(buffer,"P = %.2f mb",measurement.pressure);
+                length = sprintf(buffer,"P = %.2f mb",measurement.pressure); // print formatted data to buffer
                 if (length <= 14)
-                    lcd.printString(buffer,0,2);
+                    lcd.printString(buffer,0,2);           // display on screen
 
             }
 
-            else if(unit == fahrenheit) {
+            else if(unit == fahrenheit) {  //If unit is fahrenheit
 
                 int length = sprintf(buffer,"T = %.2f F",(((measurement.temperature*9)/5)+32)); // print formatted data to buffer
                 // it is important the format specifier ensures the length will fit in the buffer
                 if (length <= 14)  // if string will fit on display
                     lcd.printString(buffer,0,1);           // display on screen
 
-                length = sprintf(buffer,"P = %.1f hpa",measurement.pressure);
+                length = sprintf(buffer,"P = %.1f hpa",measurement.pressure); // print formatted data to buffer
                 if (length <= 14)
-                    lcd.printString(buffer,0,2);
+                    lcd.printString(buffer,0,2);           // display on screen
 
             }
 
@@ -228,10 +237,6 @@
             strftime(timebuffer, 14 , "%T", localtime(&seconds));
             strftime(datebuffer, 14 , "%D", localtime(&seconds));
 
-            //int test = 3 -20;
-            //sprintf(buffer,"%d",test); // print formatted data to buffer
-            //lcd.printString(buffer,0,3);
-
             // Display on LCD
             lcd.printString(timebuffer,20,4);
             lcd.printString(datebuffer,20,5);
@@ -242,25 +247,28 @@
             // format time into a string (time and date)
             strftime(buffer, 30 , "%X %D", localtime(&seconds));
             strftime(timebuffer, 30 , "%X %D", localtime(&seconds));
+            
             // print over serial
             serial.printf("Time = %s\n",buffer);
 
         }
-
+        
+        //If alarm timer flag is set
         if(alarmTimerFlag) {
-            alarmTimerFlag = 0;
+            alarmTimerFlag = 0; //reset flag
 
-            if(alarm==1) {
-                buzzer.beep(1000,0.5);
-                redled = 1;
+            if(alarm==1) {  //If alarm is set
+                buzzer.beep(1000,0.5); // Buzzer beeps
+                redled = 1; // Red led is turned ON
             } else
-                redled =0;
+                redled =0; //Red led is turned OFF
 
         }
+        
+        //If writing timer flag is set
+        if(writingTimerFlag) {  
 
-        if(writingTimerFlag) {  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-            writingTimerFlag = 0;
+            writingTimerFlag = 0; //reset flag
 
             // read values (T in Celsius and P in mb) and print over serial port
             measurement = bmp180.readValues();
@@ -270,11 +278,13 @@
             }
 
         }
-
+        
+        //If graph timer flag is set
         if(graphTimerFlag) {
 
-            graphTimerFlag = 0;
-
+            graphTimerFlag = 0; // reset flag
+            
+            //Sets temperature and pressure reading into arrays to display as graphs
             if(i==84) {
                 for (int n = 0; n < 83; n++) {
                     temparray[n] = temparray[n+1];
@@ -292,7 +302,7 @@
         if (buttonAFlag) { // if flag A is set
             lcd.clear();
             lcd.printString("T",0,0);
-            lcd.plotArray(temparray);
+            lcd.plotArray(temparray); // Display graph of temperature
             lcd.refresh();
             wait(5.0);
             buttonAFlag = 0; //reset flag
@@ -301,7 +311,7 @@
         if (buttonBFlag) { // if flag B is set
             lcd.clear();
             lcd.printString("P",0,0);
-            lcd.plotArray(pressarray);
+            lcd.plotArray(pressarray); // Display graph if pressure
             lcd.refresh();
             wait(5.0);
             buttonBFlag = 0; //reset flag
@@ -310,8 +320,8 @@
         if(buttonCFlag) { // if flag C is set
 
             lcd.clear();
-            //prediction = 1;
-            if(prediction == 0) {
+
+            if(prediction == 0) { // If prediction is not possible
 
                 lcd.printString("PREDICTION",10,1);
                 lcd.printString("UNAVAILABLE",8,2);
@@ -319,14 +329,14 @@
                 lcd.printString("DATA",28,5);
 
             } else {
-                setState();
+                setState(); //Calls function to give prediciton
             }
 
             lcd.refresh();
 
             wait(5.0);
 
-            buttonCFlag = 0;
+            buttonCFlag = 0; // reset flag
         }
 
         if (setTimeFlag) { // if updated time has been sent
@@ -336,11 +346,11 @@
     }
 }
 
-
+//Function to display weather prediction
 void setState()
 {
 
-    switch(state) {
+    switch(state) { // Displays prediction based on state
 
         case 0:
             cloudySun();
@@ -392,6 +402,7 @@
 
 }
 
+//Function for setting RTC
 void setTime()
 {
 // print time for debugging
@@ -410,6 +421,19 @@
     setTimeFlag = 1;
 }
 
+
+void writeDataToFile(float temp,float press,char* time) // Function for logging data into a csv file
+{
+    greenled = 1; // turn on LEDs for feedback
+    FILE *fp = fopen("/local/Datalog.csv", "a"); // open 'log.txt' for appending
+    // if the file doesn't exist it is created, if it exists, data is appended to the end
+    fprintf(fp,"T = %f C, P = %f mb, Time = %s\n",temp, press, time); // print string to file
+    fclose(fp); // close file
+    greenled = 0; // turn off LEDs to signify file access has finished
+}
+
+//Functions to set pixels for displaying various weather predictions
+
 void sunny()
 {
 
@@ -522,12 +546,4 @@
 
 }
 
-void writeDataToFile(float temp,float press,char* time)
-{
-    greenled = 1; // turn on LEDs for feedback
-    FILE *fp = fopen("/local/Datalog.csv", "a"); // open 'log.txt' for appending
-    // if the file doesn't exist it is created, if it exists, data is appended to the end
-    fprintf(fp,"T = %f C, P = %f mb, Time = %s\n",temp, press, time); // print string to file
-    fclose(fp); // close file
-    greenled = 0; // turn off LEDs to signify file access has finished
-}
+