Implementaion of a weather station using the BMP180 sensor, nokia N5110 lcd screen and the mbed NXP CPC1768 microcontroller
Dependencies: BMP180 N5110 PowerControl mbed
Fork of DocTest by
main.h@2:afdc5d6ca47f, 2015-05-11 (annotated)
- Committer:
- FFLopes
- Date:
- Mon May 11 21:28:14 2015 +0000
- Revision:
- 2:afdc5d6ca47f
- Parent:
- 1:6fc14cd8ccf1
- Child:
- 3:f7036017b319
Finished version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:b85460bc73b9 | 1 | /** |
eencae | 0:b85460bc73b9 | 2 | @file main.h |
eencae | 1:6fc14cd8ccf1 | 3 | @brief Header file containing functions prototypes, defines and global variables. |
FFLopes | 2:afdc5d6ca47f | 4 | @brief This is the main function to control the embedded system and the BMP180 sensor and the N5110 LCD screen. |
FFLopes | 2:afdc5d6ca47f | 5 | @brief Revision 1.5. |
FFLopes | 2:afdc5d6ca47f | 6 | @author Felipe F. Lopes |
FFLopes | 2:afdc5d6ca47f | 7 | @date May 2015 |
eencae | 0:b85460bc73b9 | 8 | */ |
eencae | 0:b85460bc73b9 | 9 | |
eencae | 0:b85460bc73b9 | 10 | #ifndef MAIN_H |
eencae | 0:b85460bc73b9 | 11 | #define MAIN_H |
eencae | 0:b85460bc73b9 | 12 | |
eencae | 0:b85460bc73b9 | 13 | |
eencae | 0:b85460bc73b9 | 14 | #include "mbed.h" |
FFLopes | 2:afdc5d6ca47f | 15 | #include "N5110.h" |
FFLopes | 2:afdc5d6ca47f | 16 | #include "BMP180.h" |
FFLopes | 2:afdc5d6ca47f | 17 | #include "PowerControl/PowerControl.h" |
FFLopes | 2:afdc5d6ca47f | 18 | #include "PowerControl/EthernetPowerControl.h" |
eencae | 0:b85460bc73b9 | 19 | |
FFLopes | 2:afdc5d6ca47f | 20 | //Here the initial values for the data are stored, maybe it can be possible to use them though the entire program |
FFLopes | 2:afdc5d6ca47f | 21 | //The temperatures are stored in Celsius |
FFLopes | 2:afdc5d6ca47f | 22 | #define maxThresTemp 50 |
FFLopes | 2:afdc5d6ca47f | 23 | #define minThresTemp 0 |
FFLopes | 2:afdc5d6ca47f | 24 | |
FFLopes | 2:afdc5d6ca47f | 25 | //Constants that will be used to define states |
FFLopes | 2:afdc5d6ca47f | 26 | #define changeMaxTemp 10 |
FFLopes | 2:afdc5d6ca47f | 27 | #define changeMinTemp 3 |
FFLopes | 2:afdc5d6ca47f | 28 | #define dontChangeTemp 20 |
FFLopes | 2:afdc5d6ca47f | 29 | #define mediumGood 5 |
FFLopes | 2:afdc5d6ca47f | 30 | #define highGood 6 |
FFLopes | 2:afdc5d6ca47f | 31 | #define mediumBad 7 |
FFLopes | 2:afdc5d6ca47f | 32 | #define highBad 8 |
FFLopes | 2:afdc5d6ca47f | 33 | #define notDefined 9 |
FFLopes | 2:afdc5d6ca47f | 34 | |
FFLopes | 2:afdc5d6ca47f | 35 | //Pins for the Leds |
FFLopes | 2:afdc5d6ca47f | 36 | #define maxTempAlertPin p23 |
FFLopes | 2:afdc5d6ca47f | 37 | #define minTempAlertPin p22 |
FFLopes | 2:afdc5d6ca47f | 38 | |
FFLopes | 2:afdc5d6ca47f | 39 | //Pin values for the SDA and SCL lines of the BMP180 sensor. The data and clock lines respectivly |
FFLopes | 2:afdc5d6ca47f | 40 | #define sdaPin p9 |
FFLopes | 2:afdc5d6ca47f | 41 | #define sclPin p10 |
FFLopes | 2:afdc5d6ca47f | 42 | |
FFLopes | 2:afdc5d6ca47f | 43 | //Pins to connect the LCD Screen |
FFLopes | 2:afdc5d6ca47f | 44 | #define pwrPin p5 |
FFLopes | 2:afdc5d6ca47f | 45 | #define scePin p6 |
FFLopes | 2:afdc5d6ca47f | 46 | #define rstPin p7 |
FFLopes | 2:afdc5d6ca47f | 47 | #define dcPin p8 |
FFLopes | 2:afdc5d6ca47f | 48 | #define mosiPin p11 |
FFLopes | 2:afdc5d6ca47f | 49 | #define sclkPin p13 |
FFLopes | 2:afdc5d6ca47f | 50 | #define ledPin p24 |
FFLopes | 2:afdc5d6ca47f | 51 | |
FFLopes | 2:afdc5d6ca47f | 52 | //Pin to turn the LCD on/off |
FFLopes | 2:afdc5d6ca47f | 53 | #define LCDStatePin p18 |
FFLopes | 2:afdc5d6ca47f | 54 | |
FFLopes | 2:afdc5d6ca47f | 55 | //pin to turn loggin on/off |
FFLopes | 2:afdc5d6ca47f | 56 | #define dataLoggingPin p29 |
FFLopes | 2:afdc5d6ca47f | 57 | |
FFLopes | 2:afdc5d6ca47f | 58 | //pin to read from the potentiometer |
FFLopes | 2:afdc5d6ca47f | 59 | #define potentiometerPin p20 |
FFLopes | 2:afdc5d6ca47f | 60 | |
FFLopes | 2:afdc5d6ca47f | 61 | //pin for button to change Threshold |
FFLopes | 2:afdc5d6ca47f | 62 | #define trhesPinButton p25 |
FFLopes | 2:afdc5d6ca47f | 63 | |
FFLopes | 2:afdc5d6ca47f | 64 | //pin to buzzer |
FFLopes | 2:afdc5d6ca47f | 65 | #define buzzerPin p21 |
FFLopes | 2:afdc5d6ca47f | 66 | |
FFLopes | 2:afdc5d6ca47f | 67 | //pin for button for change of units shown |
FFLopes | 2:afdc5d6ca47f | 68 | #define changeUnitsPin p26 |
FFLopes | 2:afdc5d6ca47f | 69 | |
FFLopes | 2:afdc5d6ca47f | 70 | /** |
FFLopes | 2:afdc5d6ca47f | 71 | @brief Variable to store the value of the forecasted weather |
FFLopes | 2:afdc5d6ca47f | 72 | */ |
FFLopes | 2:afdc5d6ca47f | 73 | int chanceOfWeather = notDefined; |
FFLopes | 2:afdc5d6ca47f | 74 | |
FFLopes | 2:afdc5d6ca47f | 75 | /** |
FFLopes | 2:afdc5d6ca47f | 76 | @brief Counter for the amount of times that the measurement was get on the minute |
FFLopes | 2:afdc5d6ca47f | 77 | */ |
FFLopes | 2:afdc5d6ca47f | 78 | int minuteCounter = 0; |
FFLopes | 2:afdc5d6ca47f | 79 | /** |
FFLopes | 2:afdc5d6ca47f | 80 | @brief Counter for the amount of times that the measurement was taken on the hour |
FFLopes | 2:afdc5d6ca47f | 81 | */ |
FFLopes | 2:afdc5d6ca47f | 82 | int hourCounter = 0; |
FFLopes | 2:afdc5d6ca47f | 83 | /** |
FFLopes | 2:afdc5d6ca47f | 84 | @brief Counter for the hours |
eencae | 1:6fc14cd8ccf1 | 85 | */ |
FFLopes | 2:afdc5d6ca47f | 86 | int hoursCounter = 0; |
FFLopes | 2:afdc5d6ca47f | 87 | |
FFLopes | 2:afdc5d6ca47f | 88 | /** |
FFLopes | 2:afdc5d6ca47f | 89 | @brief values for initial the threshold temperatures |
FFLopes | 2:afdc5d6ca47f | 90 | */ |
FFLopes | 2:afdc5d6ca47f | 91 | float maxTemp = 30; |
FFLopes | 2:afdc5d6ca47f | 92 | float minTemp = 10; |
FFLopes | 2:afdc5d6ca47f | 93 | |
FFLopes | 2:afdc5d6ca47f | 94 | /** |
FFLopes | 2:afdc5d6ca47f | 95 | @brief This variable will track with the embed should show the temperature in celcius or farenheight and change the pressure as well |
FFLopes | 2:afdc5d6ca47f | 96 | */ |
FFLopes | 2:afdc5d6ca47f | 97 | bool changeUnits=false; |
FFLopes | 2:afdc5d6ca47f | 98 | |
FFLopes | 2:afdc5d6ca47f | 99 | /** |
FFLopes | 2:afdc5d6ca47f | 100 | @brief Variable to see if the lcd is on or not |
FFLopes | 2:afdc5d6ca47f | 101 | */ |
FFLopes | 2:afdc5d6ca47f | 102 | bool isOn= false; |
FFLopes | 2:afdc5d6ca47f | 103 | |
FFLopes | 2:afdc5d6ca47f | 104 | /** |
FFLopes | 2:afdc5d6ca47f | 105 | @brief Variable to track if the temperature threshold is going to be changed |
FFLopes | 2:afdc5d6ca47f | 106 | */ |
FFLopes | 2:afdc5d6ca47f | 107 | |
FFLopes | 2:afdc5d6ca47f | 108 | int option = dontChangeTemp; |
FFLopes | 2:afdc5d6ca47f | 109 | |
FFLopes | 2:afdc5d6ca47f | 110 | /** |
FFLopes | 2:afdc5d6ca47f | 111 | @brief Struct variable that will store the value of the temperature and pressure, initiali the temperature is in celcis and the pressure in mbar |
FFLopes | 2:afdc5d6ca47f | 112 | */ |
FFLopes | 2:afdc5d6ca47f | 113 | |
FFLopes | 2:afdc5d6ca47f | 114 | Measurement measurement; |
eencae | 0:b85460bc73b9 | 115 | |
FFLopes | 2:afdc5d6ca47f | 116 | /** |
FFLopes | 2:afdc5d6ca47f | 117 | @brief This vector we will store the measurements of pressure over a minute |
FFLopes | 2:afdc5d6ca47f | 118 | */ |
FFLopes | 2:afdc5d6ca47f | 119 | Measurement measurementMinute[60]; |
FFLopes | 2:afdc5d6ca47f | 120 | |
FFLopes | 2:afdc5d6ca47f | 121 | /** |
FFLopes | 2:afdc5d6ca47f | 122 | @brief This vector we will store the measurements of pressure over a hour |
FFLopes | 2:afdc5d6ca47f | 123 | */ |
FFLopes | 2:afdc5d6ca47f | 124 | Measurement measurementHour[60]; |
FFLopes | 2:afdc5d6ca47f | 125 | |
FFLopes | 2:afdc5d6ca47f | 126 | /** |
FFLopes | 2:afdc5d6ca47f | 127 | @brief This vector we will store the measurements of pressure over four hours |
FFLopes | 2:afdc5d6ca47f | 128 | */ |
FFLopes | 2:afdc5d6ca47f | 129 | Measurement measurementHours[4]; |
FFLopes | 2:afdc5d6ca47f | 130 | |
FFLopes | 2:afdc5d6ca47f | 131 | /** |
FFLopes | 2:afdc5d6ca47f | 132 | @brief Pins to conect the leds for the visual warmings |
FFLopes | 2:afdc5d6ca47f | 133 | */ |
FFLopes | 2:afdc5d6ca47f | 134 | DigitalOut maxTempAlert (maxTempAlertPin); |
FFLopes | 2:afdc5d6ca47f | 135 | DigitalOut minTempAlert (minTempAlertPin); |
FFLopes | 2:afdc5d6ca47f | 136 | |
FFLopes | 2:afdc5d6ca47f | 137 | /** |
FFLopes | 2:afdc5d6ca47f | 138 | @brief Buzzer to be fired when the temperature is not in the range defined by the thresholds |
FFLopes | 2:afdc5d6ca47f | 139 | */ |
FFLopes | 2:afdc5d6ca47f | 140 | PwmOut buzzer(buzzerPin); |
FFLopes | 2:afdc5d6ca47f | 141 | |
FFLopes | 2:afdc5d6ca47f | 142 | /** |
FFLopes | 2:afdc5d6ca47f | 143 | @brief Creating of the LCD Object |
FFLopes | 2:afdc5d6ca47f | 144 | */ |
FFLopes | 2:afdc5d6ca47f | 145 | N5110 lcd(pwrPin, scePin, rstPin, dcPin, mosiPin, sclkPin, ledPin); |
FFLopes | 2:afdc5d6ca47f | 146 | |
FFLopes | 2:afdc5d6ca47f | 147 | /** |
FFLopes | 2:afdc5d6ca47f | 148 | @brief main part of the project, the sensor |
FFLopes | 2:afdc5d6ca47f | 149 | */ |
FFLopes | 2:afdc5d6ca47f | 150 | BMP180 sensor (sdaPin,sclPin); |
eencae | 0:b85460bc73b9 | 151 | |
eencae | 1:6fc14cd8ccf1 | 152 | /** |
FFLopes | 2:afdc5d6ca47f | 153 | @brief Button to change units |
FFLopes | 2:afdc5d6ca47f | 154 | */ |
FFLopes | 2:afdc5d6ca47f | 155 | InterruptIn unitsButton(changeUnitsPin); |
FFLopes | 2:afdc5d6ca47f | 156 | |
FFLopes | 2:afdc5d6ca47f | 157 | /** |
FFLopes | 2:afdc5d6ca47f | 158 | @brief Button for Trheshold change |
FFLopes | 2:afdc5d6ca47f | 159 | */ |
FFLopes | 2:afdc5d6ca47f | 160 | DigitalIn thresholdButton (trhesPinButton); |
FFLopes | 2:afdc5d6ca47f | 161 | |
FFLopes | 2:afdc5d6ca47f | 162 | /** |
FFLopes | 2:afdc5d6ca47f | 163 | @brief Button to turn LCD on/off |
FFLopes | 2:afdc5d6ca47f | 164 | */ |
FFLopes | 2:afdc5d6ca47f | 165 | DigitalIn LCDState(LCDStatePin); |
FFLopes | 2:afdc5d6ca47f | 166 | |
FFLopes | 2:afdc5d6ca47f | 167 | /** |
FFLopes | 2:afdc5d6ca47f | 168 | @brief Button to turn logging on/off |
FFLopes | 2:afdc5d6ca47f | 169 | */ |
FFLopes | 2:afdc5d6ca47f | 170 | DigitalIn logging(dataLoggingPin); |
FFLopes | 2:afdc5d6ca47f | 171 | |
FFLopes | 2:afdc5d6ca47f | 172 | /** |
FFLopes | 2:afdc5d6ca47f | 173 | @brief Potentiometer read |
FFLopes | 2:afdc5d6ca47f | 174 | */ |
FFLopes | 2:afdc5d6ca47f | 175 | AnalogIn pot (potentiometerPin); |
FFLopes | 2:afdc5d6ca47f | 176 | |
FFLopes | 2:afdc5d6ca47f | 177 | /** |
FFLopes | 2:afdc5d6ca47f | 178 | @brief Local file system |
FFLopes | 2:afdc5d6ca47f | 179 | */ |
FFLopes | 2:afdc5d6ca47f | 180 | LocalFileSystem local("file"); |
FFLopes | 2:afdc5d6ca47f | 181 | |
FFLopes | 2:afdc5d6ca47f | 182 | /** |
FFLopes | 2:afdc5d6ca47f | 183 | @brief serial interface |
FFLopes | 2:afdc5d6ca47f | 184 | */ |
FFLopes | 2:afdc5d6ca47f | 185 | Serial serial(USBTX,USBRX); |
FFLopes | 2:afdc5d6ca47f | 186 | |
FFLopes | 2:afdc5d6ca47f | 187 | /** |
FFLopes | 2:afdc5d6ca47f | 188 | @brief Ticker to get new temperature and pressure readings |
FFLopes | 2:afdc5d6ca47f | 189 | */ |
FFLopes | 2:afdc5d6ca47f | 190 | Ticker readUpdates; |
FFLopes | 2:afdc5d6ca47f | 191 | |
FFLopes | 2:afdc5d6ca47f | 192 | /** |
FFLopes | 2:afdc5d6ca47f | 193 | @brief Ticker to update the IO |
FFLopes | 2:afdc5d6ca47f | 194 | */ |
FFLopes | 2:afdc5d6ca47f | 195 | Ticker iO; |
FFLopes | 2:afdc5d6ca47f | 196 | |
FFLopes | 2:afdc5d6ca47f | 197 | /** |
FFLopes | 2:afdc5d6ca47f | 198 | @brief Interupt to enable the change on the values of the threshold |
FFLopes | 2:afdc5d6ca47f | 199 | */ |
FFLopes | 2:afdc5d6ca47f | 200 | InterruptIn enableChange(trhesPinButton); |
FFLopes | 2:afdc5d6ca47f | 201 | |
FFLopes | 2:afdc5d6ca47f | 202 | /** |
FFLopes | 2:afdc5d6ca47f | 203 | @brief String to be shown on the LCD |
FFLopes | 2:afdc5d6ca47f | 204 | */ |
FFLopes | 2:afdc5d6ca47f | 205 | char buffer[14]; |
FFLopes | 2:afdc5d6ca47f | 206 | |
FFLopes | 2:afdc5d6ca47f | 207 | /** |
FFLopes | 2:afdc5d6ca47f | 208 | @brief contains the time string |
FFLopes | 2:afdc5d6ca47f | 209 | */ |
FFLopes | 2:afdc5d6ca47f | 210 | char rxString[14]; |
FFLopes | 2:afdc5d6ca47f | 211 | char date[14]; |
FFLopes | 2:afdc5d6ca47f | 212 | |
FFLopes | 2:afdc5d6ca47f | 213 | /** |
FFLopes | 2:afdc5d6ca47f | 214 | @brief function to update and check the IO |
FFLopes | 2:afdc5d6ca47f | 215 | */ |
FFLopes | 2:afdc5d6ca47f | 216 | void ioUpdate(); |
FFLopes | 2:afdc5d6ca47f | 217 | /** |
FFLopes | 2:afdc5d6ca47f | 218 | @brief function to change the date received to a format easily for a human to read |
eencae | 1:6fc14cd8ccf1 | 219 | */ |
FFLopes | 2:afdc5d6ca47f | 220 | void formatData(); |
FFLopes | 2:afdc5d6ca47f | 221 | |
FFLopes | 2:afdc5d6ca47f | 222 | /** |
FFLopes | 2:afdc5d6ca47f | 223 | @brief Function to be called to received the UNIX time when sent over the USB |
FFLopes | 2:afdc5d6ca47f | 224 | */ |
FFLopes | 2:afdc5d6ca47f | 225 | void getTimeUpdate(); |
FFLopes | 2:afdc5d6ca47f | 226 | |
FFLopes | 2:afdc5d6ca47f | 227 | /** |
FFLopes | 2:afdc5d6ca47f | 228 | @brief This function gets the UNIX time received over the USB and store as an integer on the RAM memory |
FFLopes | 2:afdc5d6ca47f | 229 | */ |
FFLopes | 2:afdc5d6ca47f | 230 | void setTime(); |
FFLopes | 2:afdc5d6ca47f | 231 | |
FFLopes | 2:afdc5d6ca47f | 232 | /** |
FFLopes | 2:afdc5d6ca47f | 233 | @brief Function to save power on the mbed by disabling the ethernet port |
FFLopes | 2:afdc5d6ca47f | 234 | */ |
FFLopes | 2:afdc5d6ca47f | 235 | void powerSaving(); |
FFLopes | 2:afdc5d6ca47f | 236 | |
FFLopes | 2:afdc5d6ca47f | 237 | /** |
FFLopes | 2:afdc5d6ca47f | 238 | @brief this function reads the temperature and pressure value detected on the BMP180 sensor and store in the RAM memory |
FFLopes | 2:afdc5d6ca47f | 239 | */ |
FFLopes | 2:afdc5d6ca47f | 240 | void getMeasurements(); |
FFLopes | 2:afdc5d6ca47f | 241 | |
FFLopes | 2:afdc5d6ca47f | 242 | /** |
FFLopes | 2:afdc5d6ca47f | 243 | @brief This function gets the temperature in celcius, pressure in mbar and the time formated and store in a .csv file |
FFLopes | 2:afdc5d6ca47f | 244 | */ |
FFLopes | 2:afdc5d6ca47f | 245 | void writeOnFile(); |
FFLopes | 2:afdc5d6ca47f | 246 | |
FFLopes | 2:afdc5d6ca47f | 247 | /** |
FFLopes | 2:afdc5d6ca47f | 248 | @brief Function to check if the temperature is within the threshold defined, if not it call another function to alert the user. |
FFLopes | 2:afdc5d6ca47f | 249 | */ |
FFLopes | 2:afdc5d6ca47f | 250 | void checkValues(); |
FFLopes | 2:afdc5d6ca47f | 251 | |
FFLopes | 2:afdc5d6ca47f | 252 | /** |
FFLopes | 2:afdc5d6ca47f | 253 | @brief Function to be called when the temperature is over the maximun defined threshold |
FFLopes | 2:afdc5d6ca47f | 254 | */ |
FFLopes | 2:afdc5d6ca47f | 255 | void highTempAction(); |
FFLopes | 2:afdc5d6ca47f | 256 | |
FFLopes | 2:afdc5d6ca47f | 257 | /** |
FFLopes | 2:afdc5d6ca47f | 258 | @brief Function to be called in case the temperature is lower than the minimum defined threshold |
FFLopes | 2:afdc5d6ca47f | 259 | */ |
FFLopes | 2:afdc5d6ca47f | 260 | void lowTempAction(); |
FFLopes | 2:afdc5d6ca47f | 261 | |
FFLopes | 2:afdc5d6ca47f | 262 | /** |
FFLopes | 2:afdc5d6ca47f | 263 | @brief If the temperature is within the define threshold this function is called |
FFLopes | 2:afdc5d6ca47f | 264 | */ |
FFLopes | 2:afdc5d6ca47f | 265 | void normalTempAction(); |
FFLopes | 2:afdc5d6ca47f | 266 | |
FFLopes | 2:afdc5d6ca47f | 267 | /** |
FFLopes | 2:afdc5d6ca47f | 268 | @brief After the user decided to change the temperature threshold this function will see which of the threshold need to be changed and read the potentiomenter input |
FFLopes | 2:afdc5d6ca47f | 269 | @param option - option to which threshold is going to be changed |
FFLopes | 2:afdc5d6ca47f | 270 | */ |
FFLopes | 2:afdc5d6ca47f | 271 | void changeThreshold(int op); |
FFLopes | 2:afdc5d6ca47f | 272 | |
FFLopes | 2:afdc5d6ca47f | 273 | /** |
FFLopes | 2:afdc5d6ca47f | 274 | @brief Change the units shown in the LCD, from celcius to farenheight and from mbar to atm |
FFLopes | 2:afdc5d6ca47f | 275 | */ |
FFLopes | 2:afdc5d6ca47f | 276 | void unitsChanger(); |
FFLopes | 2:afdc5d6ca47f | 277 | |
FFLopes | 2:afdc5d6ca47f | 278 | /** |
FFLopes | 2:afdc5d6ca47f | 279 | @brief function to start the change of the threshold |
FFLopes | 2:afdc5d6ca47f | 280 | */ |
FFLopes | 2:afdc5d6ca47f | 281 | void enableThresChange(); |
FFLopes | 2:afdc5d6ca47f | 282 | |
FFLopes | 2:afdc5d6ca47f | 283 | /** |
FFLopes | 2:afdc5d6ca47f | 284 | @brief this function will store the measurements taken every second up to 4 hours, take the average of the readings and call the function to make the weather predictions |
FFLopes | 2:afdc5d6ca47f | 285 | */ |
FFLopes | 2:afdc5d6ca47f | 286 | void storeMeasurement(); |
FFLopes | 2:afdc5d6ca47f | 287 | |
FFLopes | 2:afdc5d6ca47f | 288 | /** |
FFLopes | 2:afdc5d6ca47f | 289 | @brief based on the values stored and their average this function will make a basic weather prediction |
FFLopes | 2:afdc5d6ca47f | 290 | */ |
FFLopes | 2:afdc5d6ca47f | 291 | void makePrediction(); |
eencae | 0:b85460bc73b9 | 292 | |
eencae | 0:b85460bc73b9 | 293 | #endif |