Year Two Project ELEC 2645: Embedded Systems Project Portable Weather Station

Dependencies:   BMP180 ConfigFile N5110 PowerControl beep mbed

Committer:
OHstin
Date:
Mon May 11 15:25:52 2015 +0000
Revision:
0:da2b8c7a1ec1
Completed Weather Station

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OHstin 0:da2b8c7a1ec1 1 /**
OHstin 0:da2b8c7a1ec1 2 @file temperaturelog.h
OHstin 0:da2b8c7a1ec1 3 */
OHstin 0:da2b8c7a1ec1 4
OHstin 0:da2b8c7a1ec1 5 #ifndef TEMPERATURELOG_H
OHstin 0:da2b8c7a1ec1 6 #define TEMPERATURELOG_H
OHstin 0:da2b8c7a1ec1 7
OHstin 0:da2b8c7a1ec1 8 #include "Outputs.h"
OHstin 0:da2b8c7a1ec1 9 #include "ConfigFile.h"
OHstin 0:da2b8c7a1ec1 10 #include "Sensors.h"
OHstin 0:da2b8c7a1ec1 11
OHstin 0:da2b8c7a1ec1 12 /**
OHstin 0:da2b8c7a1ec1 13 @brief logs temperature values over a predetermined length of time\n
OHstin 0:da2b8c7a1ec1 14 @brief the logged data is then stored on the onboard flash memory\n
OHstin 0:da2b8c7a1ec1 15 @brief the LCD screen is turned off after 5 seconds to save energy
OHstin 0:da2b8c7a1ec1 16 @date April 2015
OHstin 0:da2b8c7a1ec1 17 */
OHstin 0:da2b8c7a1ec1 18
OHstin 0:da2b8c7a1ec1 19
OHstin 0:da2b8c7a1ec1 20
OHstin 0:da2b8c7a1ec1 21 class TemperatureLog
OHstin 0:da2b8c7a1ec1 22 {
OHstin 0:da2b8c7a1ec1 23 private:
OHstin 0:da2b8c7a1ec1 24 bool logFinished; // tracks if logging was successfuly finished
OHstin 0:da2b8c7a1ec1 25 int logCounter; //tracks how many times data has been logged
OHstin 0:da2b8c7a1ec1 26 bool changeScreen; // tracks if the user pressed a button that changes the screen
OHstin 0:da2b8c7a1ec1 27 int nextScreen; // tracks whether the user wants to go to
OHstin 0:da2b8c7a1ec1 28 //the previous screen or next screen
OHstin 0:da2b8c7a1ec1 29 Timeout stop; // timeout that will turn off the screen after a given time
OHstin 0:da2b8c7a1ec1 30 Ticker logTimer; // ticker that will log data at intervals
OHstin 0:da2b8c7a1ec1 31 Ticker batLedTimer; // ticker that will check battery status at intervals
OHstin 0:da2b8c7a1ec1 32 ConfigFile tcfgW; // temperature CFG Write
OHstin 0:da2b8c7a1ec1 33
OHstin 0:da2b8c7a1ec1 34
OHstin 0:da2b8c7a1ec1 35 void onBack(); // user pressed the back button
OHstin 0:da2b8c7a1ec1 36 void turnOffScreen(); // powers down the lcd screen
OHstin 0:da2b8c7a1ec1 37 void logData(); // logs data by storing it in ConfigFile object
OHstin 0:da2b8c7a1ec1 38 void saveData(); // saves data to the mbed flash drive
OHstin 0:da2b8c7a1ec1 39 void initCfg(int duration); // initialises the cfg object with vital data
OHstin 0:da2b8c7a1ec1 40 void checkBatVoltage(); // checks if batteryVoltage is low
OHstin 0:da2b8c7a1ec1 41
OHstin 0:da2b8c7a1ec1 42 public:
OHstin 0:da2b8c7a1ec1 43 /**
OHstin 0:da2b8c7a1ec1 44 Manages the execution of the Temperature Log Screen
OHstin 0:da2b8c7a1ec1 45
OHstin 0:da2b8c7a1ec1 46 @param - integer duration, the length of time to log temperature
OHstin 0:da2b8c7a1ec1 47 @returns
OHstin 0:da2b8c7a1ec1 48 -1 - navigate to previous screen
OHstin 0:da2b8c7a1ec1 49
OHstin 0:da2b8c7a1ec1 50 */
OHstin 0:da2b8c7a1ec1 51 int start(int duration);
OHstin 0:da2b8c7a1ec1 52
OHstin 0:da2b8c7a1ec1 53
OHstin 0:da2b8c7a1ec1 54 };
OHstin 0:da2b8c7a1ec1 55
OHstin 0:da2b8c7a1ec1 56 int TemperatureLog::start(int duration)
OHstin 0:da2b8c7a1ec1 57 {
OHstin 0:da2b8c7a1ec1 58 // initialisations
OHstin 0:da2b8c7a1ec1 59 logFinished = false;
OHstin 0:da2b8c7a1ec1 60 logCounter = 0;
OHstin 0:da2b8c7a1ec1 61 lcd.printString("Logging...",20,2);
OHstin 0:da2b8c7a1ec1 62
OHstin 0:da2b8c7a1ec1 63 backButton.mode(PullDown); // activate pullDown Resistor
OHstin 0:da2b8c7a1ec1 64 backButton.rise(this,&TemperatureLog::onBack); // call onBack when the back button is pressed
OHstin 0:da2b8c7a1ec1 65
OHstin 0:da2b8c7a1ec1 66 stop.attach(this,&TemperatureLog::turnOffScreen, 5); // turn off LCD screen after 5 seconds
OHstin 0:da2b8c7a1ec1 67 batLedTimer.attach(this, &TemperatureLog::checkBatVoltage, 10); // check battery voltage every 5 seconds
OHstin 0:da2b8c7a1ec1 68
OHstin 0:da2b8c7a1ec1 69 // check battery voltage
OHstin 0:da2b8c7a1ec1 70 checkBatVoltage();
OHstin 0:da2b8c7a1ec1 71 // turn on the log led
OHstin 0:da2b8c7a1ec1 72 turnOnLogLed();
OHstin 0:da2b8c7a1ec1 73 // initialise the cfg object
OHstin 0:da2b8c7a1ec1 74 initCfg(duration);
OHstin 0:da2b8c7a1ec1 75 // start the debounce timer
OHstin 0:da2b8c7a1ec1 76
OHstin 0:da2b8c7a1ec1 77 debounce.start();
OHstin 0:da2b8c7a1ec1 78
OHstin 0:da2b8c7a1ec1 79 // 84 data points should be recorded for every log
OHstin 0:da2b8c7a1ec1 80
OHstin 0:da2b8c7a1ec1 81 logData(); // log the data
OHstin 0:da2b8c7a1ec1 82
OHstin 0:da2b8c7a1ec1 83 if ( duration == 0) {
OHstin 0:da2b8c7a1ec1 84 // this intervals results makes 84 logs in 5 minutes
OHstin 0:da2b8c7a1ec1 85 logTimer.attach(this, &TemperatureLog::logData,3.614);
OHstin 0:da2b8c7a1ec1 86 } else if ( duration == 1) {
OHstin 0:da2b8c7a1ec1 87 // this intervals results makes 84 logs in 10 minutes
OHstin 0:da2b8c7a1ec1 88 logTimer.attach(this, &TemperatureLog::logData,7.229);
OHstin 0:da2b8c7a1ec1 89 } else if ( duration == 2) {
OHstin 0:da2b8c7a1ec1 90 // this intervals results makes 84 logs in 30 minutes
OHstin 0:da2b8c7a1ec1 91 logTimer.attach(this, &TemperatureLog::logData,21.687);
OHstin 0:da2b8c7a1ec1 92 } else {
OHstin 0:da2b8c7a1ec1 93 // this intervals results makes 84 logs in 1 hour
OHstin 0:da2b8c7a1ec1 94 logTimer.attach(this, &TemperatureLog::logData,42.373);
OHstin 0:da2b8c7a1ec1 95 }
OHstin 0:da2b8c7a1ec1 96
OHstin 0:da2b8c7a1ec1 97 while (!logFinished) {
OHstin 0:da2b8c7a1ec1 98
OHstin 0:da2b8c7a1ec1 99 // mbed goes to sleep to save power
OHstin 0:da2b8c7a1ec1 100 Sleep();
OHstin 0:da2b8c7a1ec1 101
OHstin 0:da2b8c7a1ec1 102 }
OHstin 0:da2b8c7a1ec1 103
OHstin 0:da2b8c7a1ec1 104 // remove interrupts
OHstin 0:da2b8c7a1ec1 105 logTimer.detach();
OHstin 0:da2b8c7a1ec1 106 backButton.rise(NULL);
OHstin 0:da2b8c7a1ec1 107
OHstin 0:da2b8c7a1ec1 108 turnOffLogLed();
OHstin 0:da2b8c7a1ec1 109
OHstin 0:da2b8c7a1ec1 110 return -1;
OHstin 0:da2b8c7a1ec1 111 }
OHstin 0:da2b8c7a1ec1 112
OHstin 0:da2b8c7a1ec1 113 void TemperatureLog::initCfg( int duration )
OHstin 0:da2b8c7a1ec1 114 {
OHstin 0:da2b8c7a1ec1 115
OHstin 0:da2b8c7a1ec1 116 // Create the Duration Key and Value Pair
OHstin 0:da2b8c7a1ec1 117 char *theDurationKey = "durationKey";
OHstin 0:da2b8c7a1ec1 118 char theDurationValue[32];
OHstin 0:da2b8c7a1ec1 119
OHstin 0:da2b8c7a1ec1 120 if ( duration == 0 ) {
OHstin 0:da2b8c7a1ec1 121
OHstin 0:da2b8c7a1ec1 122 char theDurationString[32] = "5 min";
OHstin 0:da2b8c7a1ec1 123 strcpy(theDurationValue,theDurationString);
OHstin 0:da2b8c7a1ec1 124
OHstin 0:da2b8c7a1ec1 125 } else if ( duration == 1) {
OHstin 0:da2b8c7a1ec1 126
OHstin 0:da2b8c7a1ec1 127 char theDurationString[32] = "10 min";
OHstin 0:da2b8c7a1ec1 128 strcpy(theDurationValue,theDurationString);
OHstin 0:da2b8c7a1ec1 129
OHstin 0:da2b8c7a1ec1 130 } else if ( duration == 2) {
OHstin 0:da2b8c7a1ec1 131
OHstin 0:da2b8c7a1ec1 132 char theDurationString[32] = "30 min";
OHstin 0:da2b8c7a1ec1 133 strcpy(theDurationValue,theDurationString);
OHstin 0:da2b8c7a1ec1 134
OHstin 0:da2b8c7a1ec1 135 } else {
OHstin 0:da2b8c7a1ec1 136
OHstin 0:da2b8c7a1ec1 137 char theDurationString[32] = "1 hour";
OHstin 0:da2b8c7a1ec1 138 strcpy(theDurationValue,theDurationString);
OHstin 0:da2b8c7a1ec1 139 }
OHstin 0:da2b8c7a1ec1 140
OHstin 0:da2b8c7a1ec1 141 // Create the Time and Value Pair
OHstin 0:da2b8c7a1ec1 142 char *theTimeKey = "timeKey";
OHstin 0:da2b8c7a1ec1 143 char theTimeValue[32];
OHstin 0:da2b8c7a1ec1 144
OHstin 0:da2b8c7a1ec1 145 // Create the Date Key and Value Pair
OHstin 0:da2b8c7a1ec1 146 char *theDateKey = "dateKey";
OHstin 0:da2b8c7a1ec1 147 char theDateValue[32];
OHstin 0:da2b8c7a1ec1 148
OHstin 0:da2b8c7a1ec1 149 ////GET THE CURRENT TIME///////////
OHstin 0:da2b8c7a1ec1 150 time_t seconds = time(NULL);
OHstin 0:da2b8c7a1ec1 151 strftime(theDateValue, 32, "%d/%m/%y", localtime(&seconds)); // format the date in day/month/year
OHstin 0:da2b8c7a1ec1 152 strftime(theTimeValue, 32, "%R", localtime(&seconds)); // format the time in HH:MM 24 Hr clock
OHstin 0:da2b8c7a1ec1 153 ////////////////////////////////////////////
OHstin 0:da2b8c7a1ec1 154
OHstin 0:da2b8c7a1ec1 155 // set the created Key and Value Pairs to the cfg object
OHstin 0:da2b8c7a1ec1 156 tcfgW.setValue(theDateKey,theDateValue);
OHstin 0:da2b8c7a1ec1 157 tcfgW.setValue(theTimeKey,theTimeValue);
OHstin 0:da2b8c7a1ec1 158 tcfgW.setValue(theDurationKey,theDurationValue);
OHstin 0:da2b8c7a1ec1 159
OHstin 0:da2b8c7a1ec1 160
OHstin 0:da2b8c7a1ec1 161 }
OHstin 0:da2b8c7a1ec1 162
OHstin 0:da2b8c7a1ec1 163 void TemperatureLog::logData()
OHstin 0:da2b8c7a1ec1 164 {
OHstin 0:da2b8c7a1ec1 165
OHstin 0:da2b8c7a1ec1 166 if ( logCounter < 84 ) { // // data is still logging
OHstin 0:da2b8c7a1ec1 167
OHstin 0:da2b8c7a1ec1 168 // get the current temperature
OHstin 0:da2b8c7a1ec1 169 float temperature = getTemperature();
OHstin 0:da2b8c7a1ec1 170
OHstin 0:da2b8c7a1ec1 171 // create a new Counter Key and Value Pair
OHstin 0:da2b8c7a1ec1 172 char *theCounterKey = "counterKey";
OHstin 0:da2b8c7a1ec1 173 char theCounterValue[32] = "";
OHstin 0:da2b8c7a1ec1 174
OHstin 0:da2b8c7a1ec1 175 // assign the logCounter as the CounterValue
OHstin 0:da2b8c7a1ec1 176 sprintf(theCounterValue,"%d",logCounter);
OHstin 0:da2b8c7a1ec1 177
OHstin 0:da2b8c7a1ec1 178 // Create a new Key and Value Pair
OHstin 0:da2b8c7a1ec1 179 char theKey[32] = "key";
OHstin 0:da2b8c7a1ec1 180 char theValue[32] = "";
OHstin 0:da2b8c7a1ec1 181
OHstin 0:da2b8c7a1ec1 182 // concatentate the Key Value and Counter Value strings
OHstin 0:da2b8c7a1ec1 183 strcat(theKey,theCounterValue);
OHstin 0:da2b8c7a1ec1 184
OHstin 0:da2b8c7a1ec1 185 sprintf(theValue,"%0.2f",temperature); // converting float to string
OHstin 0:da2b8c7a1ec1 186
OHstin 0:da2b8c7a1ec1 187 // set the created key and value pairs to the cfg object
OHstin 0:da2b8c7a1ec1 188 tcfgW.setValue(theCounterKey,theCounterValue);
OHstin 0:da2b8c7a1ec1 189 tcfgW.setValue(theKey,theValue);
OHstin 0:da2b8c7a1ec1 190
OHstin 0:da2b8c7a1ec1 191
OHstin 0:da2b8c7a1ec1 192 } else {
OHstin 0:da2b8c7a1ec1 193
OHstin 0:da2b8c7a1ec1 194 saveData(); // save the data
OHstin 0:da2b8c7a1ec1 195 logFinished = true; // logging has finished successfully
OHstin 0:da2b8c7a1ec1 196 }
OHstin 0:da2b8c7a1ec1 197 // increment the log counter
OHstin 0:da2b8c7a1ec1 198 logCounter++;
OHstin 0:da2b8c7a1ec1 199
OHstin 0:da2b8c7a1ec1 200 }
OHstin 0:da2b8c7a1ec1 201
OHstin 0:da2b8c7a1ec1 202 void TemperatureLog::saveData()
OHstin 0:da2b8c7a1ec1 203 {
OHstin 0:da2b8c7a1ec1 204 // save the data to onboard flash memory as a cfg file
OHstin 0:da2b8c7a1ec1 205 tcfgW.write("/local/temp.cfg");
OHstin 0:da2b8c7a1ec1 206
OHstin 0:da2b8c7a1ec1 207 }
OHstin 0:da2b8c7a1ec1 208
OHstin 0:da2b8c7a1ec1 209 void TemperatureLog::onBack()
OHstin 0:da2b8c7a1ec1 210 {
OHstin 0:da2b8c7a1ec1 211 if (debounceSuccess()) { // debouncing successful
OHstin 0:da2b8c7a1ec1 212 playSound();
OHstin 0:da2b8c7a1ec1 213 // if user presses the back button, save data and terminate logging
OHstin 0:da2b8c7a1ec1 214
OHstin 0:da2b8c7a1ec1 215 saveData(); // save the data
OHstin 0:da2b8c7a1ec1 216 logFinished = true; // logging has finished successfully
OHstin 0:da2b8c7a1ec1 217 debounce.reset(); // reset the debounce timer
OHstin 0:da2b8c7a1ec1 218 }
OHstin 0:da2b8c7a1ec1 219
OHstin 0:da2b8c7a1ec1 220
OHstin 0:da2b8c7a1ec1 221 }
OHstin 0:da2b8c7a1ec1 222
OHstin 0:da2b8c7a1ec1 223 void TemperatureLog::turnOffScreen()
OHstin 0:da2b8c7a1ec1 224 {
OHstin 0:da2b8c7a1ec1 225 // turn off the LCD screen to save power
OHstin 0:da2b8c7a1ec1 226 lcd.turnOff();
OHstin 0:da2b8c7a1ec1 227
OHstin 0:da2b8c7a1ec1 228 }
OHstin 0:da2b8c7a1ec1 229
OHstin 0:da2b8c7a1ec1 230 void TemperatureLog::checkBatVoltage()
OHstin 0:da2b8c7a1ec1 231 {
OHstin 0:da2b8c7a1ec1 232 // read the battery voltage
OHstin 0:da2b8c7a1ec1 233 float batteryVoltage = getVoltage();
OHstin 0:da2b8c7a1ec1 234
OHstin 0:da2b8c7a1ec1 235 if (batteryVoltage < 6.5) { // check if battery is low
OHstin 0:da2b8c7a1ec1 236
OHstin 0:da2b8c7a1ec1 237 if(!batStatLedState) { // if the battery low led is not already on
OHstin 0:da2b8c7a1ec1 238
OHstin 0:da2b8c7a1ec1 239 turnOnBatStatLed(); // turn on the battery low led
OHstin 0:da2b8c7a1ec1 240 }
OHstin 0:da2b8c7a1ec1 241
OHstin 0:da2b8c7a1ec1 242 } else {
OHstin 0:da2b8c7a1ec1 243
OHstin 0:da2b8c7a1ec1 244 turnOffBatStatLed(); // ensure that the battery low led is turned off
OHstin 0:da2b8c7a1ec1 245
OHstin 0:da2b8c7a1ec1 246 }
OHstin 0:da2b8c7a1ec1 247
OHstin 0:da2b8c7a1ec1 248
OHstin 0:da2b8c7a1ec1 249 }
OHstin 0:da2b8c7a1ec1 250
OHstin 0:da2b8c7a1ec1 251 #endif