Amir Asemanpayeh / Mbed 2 deprecated WeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Barometer.h Source File

Barometer.h

Go to the documentation of this file.
00001 /**
00002 
00003 @file Barometer.h
00004 @brief Header file containing member functions and variables
00005 @author Amir M. Asemanpayeh
00006 @date May 2015
00007 
00008 @brief Header file - Contains Global Variables, GPIO and Pins and Function prototypes
00009 
00010 */
00011 
00012 
00013 #include "mbed.h"
00014 #include "N5110.h"
00015 #include "BMP180.h"
00016 #include "PowerControl/PowerControl.h"
00017 #include "PowerControl/EthernetPowerControl.h"
00018 
00019 /**
00020 @namespace bmp180
00021 @brief A I2C instance to interact with the BMP sensor \n SDA, SCL
00022 */
00023 BMP180 bmp180(p28,p27);
00024 
00025 
00026 /**
00027 @namespace lcd
00028 @brief A serial data protocol to operate with a master/slave relationship to interact with the lcd \n VCC,SCE,RST,D/C,MOSI,SCLK,LED
00029 */
00030 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
00031 
00032 
00033 /**
00034 @namespace leds
00035 @brief GPIO bus output for status LEDs
00036 */
00037 BusOut leds(LED4,LED3,LED2,LED1);
00038 
00039 
00040 /**
00041 @namespace serial
00042 @brief A USB Virtual Serial Port to print out messages to a host PC terminal
00043 */
00044 Serial serial(USBTX,USBRX);
00045 
00046 
00047 /**
00048 @namespace local
00049 @brief A local file system declared to store data files
00050 */
00051 LocalFileSystem local("local");
00052 
00053 
00054 /**
00055 @namespace button1
00056 @brief An Interrupt on digital pushbutton1 input p16
00057 */
00058 InterruptIn button1(p16);
00059 
00060 
00061 /**
00062 @namespace button2
00063 @brief An Interrupt on digital pushbutton2 input p17
00064 */
00065 InterruptIn button2(p17);
00066 
00067 
00068 /**
00069 @namespace button3
00070 @brief An Interrupt on digital pushbutton3 input p15
00071 */
00072 InterruptIn button3 (p15);
00073 
00074 
00075 /**
00076 @namespace button4
00077 @brief An Interrupt on digital pushbutton4 input p18
00078 */
00079 InterruptIn button4 (p18);
00080 
00081 
00082 
00083 
00084 /**
00085 @namespace redLED
00086 @brief A PwmOut interface is used to control the frequency and mark-space ratio of a led (The red LED)
00087 */
00088 PwmOut redLED(p24);//red
00089 
00090 
00091 /**
00092 @namespace yellowLED
00093 @brief A PwmOut interface is used to control the frequency and mark-space ratio of a led (The yellow LED)
00094 */
00095 PwmOut yellowLED(p23);//yellow
00096 
00097 
00098 /**
00099 @namespace greenLED
00100 @brief A PwmOut interface is used to control the frequency and mark-space ratio of a led (The green LED)
00101 */
00102 PwmOut greenLED(p22);
00103 
00104 
00105 /**
00106 @namespace buzzer
00107 @brief A PwmOut interface is used to control the frequency and mark-space ratio of a buzzer
00108 pulse train
00109 */
00110 PwmOut buzzer(p21);
00111 
00112 
00113 /**
00114 @namespace timer
00115 @brief Create ticker object for temperature and pressure readings
00116 */
00117 Ticker timer;
00118 
00119 
00120 /**
00121 @namespace dataLoggerTimer
00122 @brief a ticker object for the data logger functions
00123 */
00124 Ticker dataLoggerTimer ;
00125 
00126 
00127 /**
00128 @namespace powerSaverTimeout
00129 @brief create a timeout object for the poweerSaver function
00130 */
00131 Timeout powerSaverTimeout;
00132 
00133 /**
00134 @namespace debounce
00135 @brief create a debounce timer object to implement a debounce counter to avoid multiple interrupts.
00136 */
00137 Timer debounce;
00138 
00139 
00140 int timerFlag  = 1; /*!< Flag for tempesrature pressure reading ISR */
00141 int tickerFlag  = 0 ;  /*!<Flag to show the minutes passed */
00142 int button1Flag  = 0; /*!< Flag set in button1Pressed ISR */
00143 int button2Flag  = 0 ; /*!<Flag set in button2Pressed ISR  */
00144 int button3Flag  = 0 ; /*!< Flag set in button3Pressed ISR  */
00145 int button4Flag  = 0 ; /*!< Flag set in button4Pressed ISR */
00146 int unitFlag  = 1 ; /*!< Flag set in the unitSettings function, indicates the units picked by user */
00147 int powerSaverFlag  = 0 ; /*!<Flag set in the powerSettings function when the powerSaver is switched on */
00148 int lcdPowerFlag  = 1 ; /*!< Flag set in the savePower function to indicate the status of the LCD (ON or OFF) */
00149 int dataLoggerFlag  = 0 ; /*!< Flag set by user in the dataLoggerSetting function to turn on the logger */
00150 int runLoggerFlag  = 0 ;  /*!< Fag set when the logger is switched on, allows user to navigate to this option in the main menu */
00151 int alarmClockFlag  = 0 ; /*!< Flag set in the AlarmClock function, indicates if the alarm clock has been turned on */
00152 int thresholdAlarmFlag  = 0 ; /*!<Flag set in the thresholdAlarmmenu function,indicates if the thresholds are set */
00153 
00154 int UNIXdate =0;   /*!<An integer to store the value of UNIX time calculated by mktime function */
00155 int currentTime  ;/*!<An integer to store the UNIX value of the current time */
00156 int powerSaverTime  = 60;    /*!<An integer to set the initial value of the power saver ticker */
00157 int dataLoggerTime  = 60;    /*!<An integer used to set the initial value of the data logger ticker */
00158 
00159 float temperature ;  /*!<A float value to store the value of the temperature recieved from the sensor */
00160 float pressure ; /*!<A float value to store the value of the pressure recieved from the sensor */
00161 float averageTemperature  ;  /*!<A float value to store the average value of temperature readingsn taken over a period of time */
00162 float averagePressure  ; /*!<A float value to store the average value of pressure readings over a period of time */
00163 float minTemperature  ;  /*!<A float value to save the minimum value of temperature readings */
00164 float maxTemperature  ;  /*!<A float value to store the maximum value of temperature readings */
00165 float minPressure ;  /*!<A float value to store the minimum value of pressure readings */
00166 float maxPressure ;  /*!<A float value to store the maximum value of pressure readings */
00167 
00168 
00169 
00170 
00171 float arrayT [83] ; /*!<A A float array to store the values of temperature to calculate the average,minimum and maximum values*/
00172 float arrayP [83] ; /*!<A A float array to store the values of pressure to calculate the average,minimum and maximum values*/
00173 float arrayTG [83] ; /*!<A A float array to store the values of temperature to plot the related graph */
00174 float arrayPG [83] ;/*!<A A float array to store the values of pressure to plot the related graph */
00175 
00176 
00177 char bufferTime [14]; /*!<A A char array (buffer) to store time*/
00178 char bufferDate [14]; /*!<A A char array (buffer) to store date*/
00179 char bufferT [14];/*!<A A char array (buffer) to store the value of temperature in char*/
00180 char bufferP [14];  /*!<A A char array (buffer) to store the value of pressure in char */
00181 // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
00182 // so can display a string of a maximum 14 characters in length
00183 // or create formatted strings - ensure they aren't more than 14 characters long
00184 
00185 
00186 
00187 char buffer0 [14];   /*!<A A char array (buffer) used to display data on the LCD at (0,0)*/
00188 char buffer1 [14];   /*!<A A char array (buffer) used to display data on the LCD at (0,1)*/
00189 char buffer2 [14];   /*!<A A char array (buffer) used to display data on the LCD at (0,2)*/
00190 char buffer3 [14];   /*!<A A char array (buffer) used to display data on the LCD at (0,3)*/
00191 char buffer4 [14];   /*!<A A char array (buffer) used to display data on the LCD at (0,4)*/
00192 char buffer5 [14];   /*!<A A char array (buffer) used to display data on the LCD at (0,5)*/
00193 
00194 
00195 
00196 //arrays used to store data to the local file
00197 char timeSent [1000]; /*!<A A char array used to save time staps to the local file */
00198 char dateSent [1000]; /*!<A A char array used to save date stamps the local file */
00199 char temperatureSent [1000]; /*!<A A char array used to save the temperature readings to the local file */
00200 char pressureSent [1000]; /*!<A A char array used to save the pressure readings to the local file */
00201 char temperatureRecieved [1000]; /*!<A A char array used to retrive the saved temperature readings from the local file */
00202 char pressureRecieved [1000]; /*!<A A char array used to retrive the saved pressure readings from the local file  */
00203 char timeRecieved [1000]; /*!<A A char array used to retrive the saved time stamps from the local file  */
00204 char dateRecieved [1000]; /*!<A A char array used to retrive the saved date stamps from the local file */
00205 
00206 
00207 
00208 char * pch ; /*!<A A char array used to split the char arrays of temperature values retrived from the local file to the individual temperature strings */
00209 char * pch1 ; /*!<A A char array used to split the char arrays of pressure values retrived from the local file to the individual pressure strings*/
00210 char * pch2 ; /*!<A  A char array used to split the char arrays of times retrived from the local file to the individual time strings*/
00211 char * pch3 ; /*!<A  A char array used to split the char arrays of dates retrived from the local file to the individual date strings*/
00212 
00213 
00214 int state =0; /*!<A An integer used to navigate through the finite machines (indexes)*/
00215 
00216 float frequency []= {659,554,659,554,659,554,659,554,659,554,659,554}; /*!<A An array of floats with different frequencies for the buzzer*/
00217 
00218 
00219 
00220 /**
00221 A structure, contains the output, titles and nextstate data
00222 @param output - integer value holds the output value of the members
00223 @param title - char array contains titles of the members
00224 @param nextState - int array contains the values for the next and previous states
00225 */
00226 struct  State {
00227     int output;
00228     char title[14];
00229     int nextState[2];
00230 };
00231 
00232 
00233 /**
00234 Initialises a typedef struct, contains the output, titles and nextstate data for the start menu
00235 */
00236 typedef struct State STyp1; // for the start menu
00237 STyp1 fsmA[4] = {
00238     {0,"Live Data",{1,3}},
00239     {1,"Saved Data",{2,0}},
00240     {2,"Alarms",{3,1}},
00241     {3,"Settings",{0,2}},
00242 };
00243 
00244 
00245 /**
00246 Initialises a typedef struct, contains the output, titles and nextstate data for the settings menu
00247 */
00248 typedef struct State STyp2; // for the settings menu
00249 STyp2 fsmB[4] = {
00250     {0,"Date/Time",{1,3}},
00251     {1,"Units",{2,0}},
00252     {2,"Power Saver",{3,1}},
00253     {3,"Data Logger",{0,2}},
00254 };
00255 
00256 /**
00257 Initialises a typedef struct, contains the output, titles and nextstate data for the time/date settings also the alarmclock function
00258 */
00259 typedef struct State STyp3; // for the time/date settings
00260 STyp3 fsmC[5] = {
00261     {12,"Hour:",{1,4}},
00262     {0,"Min:",{2,0}},
00263     {21,"Day:",{3,1}},
00264     {4,"Month:",{4,2}},
00265     {2015,"Year:",{0,3}},
00266 };
00267 
00268 
00269 /**
00270 Initialises a typedef struct, contains the output, titles and nextstate data for the unitsettings function
00271 */
00272 typedef struct State STyp4; // for the units settings
00273 STyp3 fsmD[4] = {
00274     {1,"C/mb",{1,3}},
00275     {2,"C/atm",{2,0}},
00276     {3,"F/mb",{3,1}},
00277     {4,"F/atm",{0,2}},
00278 };
00279 
00280 /**
00281 Initialises a typedef struct, contains the output, titles and nextstate data for the powerSaverSetting
00282 */
00283 typedef struct State STyp5; // for the Power saver settings
00284 STyp5 fsmE[2] = {
00285     {0,"Off",{1,1}},
00286     {60,"On",{0,0}},
00287 };
00288 
00289 /**
00290 Initialises a typedef struct, contains the output, titles and nextstate data for the alarms menu
00291 */
00292 typedef struct State Stype6; // for the alarms menu
00293 Stype6 fsmF[2] = {
00294     {0,"Thresholds",{1,1}},
00295     {1,"Alarm Clock", {0,0}},
00296 };
00297 
00298 /**
00299 Initialises a typedef struct, contains the output, titles and nextstate data for the threshold and alarmclock functions
00300 */
00301 typedef struct State Stype7; // yes or no
00302 Stype7 fsmG[2] = {
00303     {1,"Yes",{1,1}},
00304     {0,"No", {0,0}},
00305 };
00306 
00307 /**
00308 Initialises a typedef struct, contains the output, titles and nextstate data for the threshold menu
00309 */
00310 typedef struct State Stype8; // for the thresholds
00311 Stype8 fsmH[4] = {
00312     {20,"Min T:",{1,3}},
00313     {30,"Max T:",{2,0}},
00314     {1000,"Min P:",{3,1}},
00315     {1100,"Max P:",{0,2}},
00316 };
00317 
00318 
00319 /**
00320 ISR attached to the fisrt button interrupt
00321 @param button1Flag - integer to indicate if the interrupt has occured
00322 */
00323 void button1Pressed();
00324 
00325 /**
00326 ISR attached to the second button interrupt
00327 @param button2Flag - integer to indicate if the interrupt has occured
00328 */
00329 void button2Pressed();
00330 
00331 /**
00332 ISR attached to the third button interrupt
00333 @param button3Flag - integer to indicate if the interrupt has occured
00334 */
00335 void button3Pressed();
00336 /**
00337 ISR attached to the fourth button interrupt
00338 @param button4Flag - integer to indicate if the interrupt has occured
00339 */
00340 void button4Pressed();
00341 
00342 
00343 
00344 
00345 /**
00346 ISR attached to the ticker timer
00347 @param timerFlag - integer to indicate the timer is expired
00348 */
00349 void timerExpired();
00350 
00351 /**
00352 ISR attached to the ticker dataLoggerTimer
00353 @param dataLoggerFlag - integer to indicate the data logger timer is expired
00354 */
00355 void dataLoggerTimerExpired ();
00356 
00357 
00358 /**
00359 Opens the local folder, creates a csv file and stores the arrays.
00360 @param data - char array inserted in the local file
00361 @param data1 - char array inserted in the local file
00362 @param data2 - char array inserted in the local file
00363 @param data3 - char array inserted in the local file
00364 */
00365 void saveToFile(char *data,char *data1,char *data2,char *data3);
00366 
00367 
00368 
00369 
00370 /**
00371 Gets the local time from the RTC and stores them into two arrays
00372 @param currentTime - integer to store the current data epoch unix time stamp
00373 @param bufferTime - char array to store the time stamp
00374 @param bufferTime - char array to store the date stamp
00375 */
00376 void updateTime();
00377 
00378 
00379 
00380 /**
00381 Displays the char arrays on the LCD
00382 @param buffer 0 - char array printed on (0,0)
00383 @param buffer 1 - char array printed on (0,1)
00384 @param buffer 2 - char array printed on (0,2)
00385 @param buffer 3 - char array printed on (0,3)
00386 @param buffer 4 - char array printed on (0,4)
00387 @param buffer 5 - char array printed on (0,5)
00388 */
00389 void display();
00390 
00391 /**
00392 Reads the data from the sensor and stores them into arrays
00393 @param bufferT - char array to store temperature
00394 @param bufferP - char array to store pressure
00395 @param temperature - float to store the value of the temperature
00396 @param pressure - float to store the value of the pressure
00397 */
00398 void readData();
00399 
00400 
00401 /**
00402 ISR attached to power saver timeout to set the flag to 1
00403 @param powerSaverFlag - integer to indicate if the state of the power saver
00404 */
00405 void powerSaverExpired();
00406 
00407 
00408 /**
00409 Checks if the power saver option is set by the user, if yes puts the device in sleep mode and turns off the lcd.
00410 Also if the buttons are pressed it wakes up the device and attached the timeout
00411 */
00412 void powerSaverCheck();
00413 
00414 
00415 
00416 /**
00417 Indicates the live data on the screen and updates the data once a second
00418 @param timerFlag - integer set when the timer ends
00419 */
00420 void liveData();
00421 
00422 /**
00423 Recieves the data, puts them in arrays and saves them to the file. Saved arrays are restored and used to calculate the minimun maximum average values
00424 @param sumTemperature - integer to store the total value of temperature values
00425 @param sumPressure - integer to store the total value of pressure values
00426 
00427 @param temperatureSent - char with the temperature values saved to the file
00428 @param pressureSent- char with the pressure values saved to the file
00429 @param timeSent- char with the time stamps saved to the file
00430 @param dateSent- char with the data stamps saved to the file
00431 
00432 @param temperatureRecieved - char with the temperature values restored from the file
00433 @param pressureRecieved- char with the pressure values restored from the file
00434 @param timeRecieved- char with the time stamps restored from the file
00435 @param dateRecieved- char with the data stamps restored from the file
00436 
00437 @param minTemperature - int to store the minimum value of temperature
00438 @param minPressure - int to store the minimum value of pressure
00439 @param maxTemperature - int to store the maximum value of temperature
00440 @param maxPressure - int to store the maximum value of pressure
00441 @param avgTemperature - int to store the average value of temperature
00442 @param avgPressure - int to store the average value of pressure
00443 */
00444 void loggerData();
00445 
00446 
00447 /**
00448 Uses the data from the loggerData function to indicate the calculated values and plot the graphs
00449 */
00450 void dataLogger();
00451 
00452 
00453 
00454 /**
00455 Checks the current values of temperature and pressure and compares to the threshold values, triggers the alarm if match
00456 */
00457 void thresholdCheck();
00458 
00459 
00460 /**
00461 Alloes the user to set the values for the thresholds and also turn the alarm on or off
00462 */
00463 void threshold();
00464 
00465 /**
00466 Compares the set time in the alarm clock section with the current time, triggers the alarm when they match
00467 */
00468 void alarmClockCheck();
00469 
00470 
00471 
00472 /**
00473 Allows the user to set the min , hour, day, month and year values to set the alarm clock
00474 */
00475 void alarmClock ();
00476 
00477 /**
00478 Alarm menu, includes two submenus : alarmclock and thresholds
00479 */
00480 void alarmsMenu ();
00481 
00482 /**
00483 *Fourth option in the settings menu./n
00484 *Allows the user to turn on the data logger and set the value of time out /n
00485 *Attaches a timeout to set the dataloggerFlag /n
00486 @param dataLoggerFlag - integer used as a flag to indicate if the datalogger option is switched on or off
00487 @param state - integer used to navigate through fsm states
00488 */
00489 void dataLoggerSetting();
00490 
00491 /**
00492 *Third option in the settings menu./n
00493 *Allows the user to turn on the power saving option and set the value of time out /n
00494 *Attaches a timeout to set the powerSaverFlag /n
00495 @param powerSaverFlag - integer used as a flag to indicate if the power saver option is switched on or off
00496 @param state - integer used to navigate through fsm states
00497 */
00498 void powerSaverSetting();
00499 
00500 /**
00501 *Calculates the UNIX time value./n
00502 *Sets the integer values entered by user to the tm structure mebers /n
00503 *Converts the tm structure to time_t /n
00504 @param timeinfo - tm struct sent to mktime function
00505 @param rawtime - fundamental arithmetic type to represent the current time informations
00506 */
00507 void calculateUNIXTime();
00508 
00509 /**
00510 *The second option in the settings menu./n
00511 *Allows the user to change the units /n
00512 @param unitFlag - integer used as a flag to indicate the units set
00513 @param state - integer used to navigate through fsm states
00514 */
00515 void unitsSetting();
00516 
00517 
00518 /**
00519 *The first option in the settings menu./n
00520 *Allows the user to enter time and date /n
00521 *Sends the recieved int arrays to the calculateUNIXtime function to calculate the unix time /n
00522 *sets the current time to the calculated unix time.
00523 @param state - integer used to navigate through fsm states
00524 */
00525 void timeDateSetting();
00526 
00527 
00528 /**
00529 *The setting menu displayed when the fourth state of fsmA is picked from the main menu /n
00530 *Depends on the state of the finite state machine used for this function, put the related title in the screen buffers /n
00531 *Also copy the bufferTime (buffer used to store time) on the first screen buffer (at the top) /n
00532 *Displays the screen buffers on the LCD/n
00533 *switch statement checks the current state of the fsm (fsmB),
00534 *The related function to each fsm state is executed as when a rising edge from the 4th button interrupts (button 4 is pressed)
00535 
00536 */
00537 void settingsMenu();
00538 
00539 /**
00540 *The main menu displayed at the start /n
00541 *Depends on the state of the finite state machine (fsmA) used for this function, put the related title in the screen buffers /n
00542 *Also copy the bufferTime (buffer used to store time) on the first screen buffer (at the top) /n
00543 *Displays the screen buffers on the LCD/n
00544 *switch statement checks the current state of the fsm (fsmA),
00545 *The related function to each fsm state is executed as when a rising edge from the 4th button interrupts (button 4 is pressed)
00546 
00547 */
00548 void startMenu();
00549 
00550 
00551 
00552 
00553