Project D weather station
Dependencies: BMP180 N5110 mbed
main.cpp@0:35d7d920fbc9, 2015-05-07 (annotated)
- Committer:
- qk2277
- Date:
- Thu May 07 16:14:42 2015 +0000
- Revision:
- 0:35d7d920fbc9
the first version of my project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
qk2277 | 0:35d7d920fbc9 | 1 | /** |
qk2277 | 0:35d7d920fbc9 | 2 | @Project A Weather Station |
qk2277 | 0:35d7d920fbc9 | 3 | @Measure the current environment temperature and pressure |
qk2277 | 0:35d7d920fbc9 | 4 | @file main.cpp |
qk2277 | 0:35d7d920fbc9 | 5 | @brief header file containing function prototypes, defines and global variables |
qk2277 | 0:35d7d920fbc9 | 6 | @auther Kun Qian |
qk2277 | 0:35d7d920fbc9 | 7 | @data 07/05/2015 |
qk2277 | 0:35d7d920fbc9 | 8 | */ |
qk2277 | 0:35d7d920fbc9 | 9 | |
qk2277 | 0:35d7d920fbc9 | 10 | |
qk2277 | 0:35d7d920fbc9 | 11 | #include "mbed.h" |
qk2277 | 0:35d7d920fbc9 | 12 | #include "BMP180.h" |
qk2277 | 0:35d7d920fbc9 | 13 | #include "N5110.h" |
qk2277 | 0:35d7d920fbc9 | 14 | |
qk2277 | 0:35d7d920fbc9 | 15 | /** |
qk2277 | 0:35d7d920fbc9 | 16 | @namespace lcd |
qk2277 | 0:35d7d920fbc9 | 17 | @namespace BMP180 |
qk2277 | 0:35d7d920fbc9 | 18 | @namespace mbed |
qk2277 | 0:35d7d920fbc9 | 19 | */ |
qk2277 | 0:35d7d920fbc9 | 20 | |
qk2277 | 0:35d7d920fbc9 | 21 | |
qk2277 | 0:35d7d920fbc9 | 22 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26);//The ports being connected of the mbed |
qk2277 | 0:35d7d920fbc9 | 23 | BusOut leds(LED4,LED3,LED2,LED1);//The ports being connected of the LCD |
qk2277 | 0:35d7d920fbc9 | 24 | BMP180 bmp180(p28,p27);//The ports being connected to the sensor |
qk2277 | 0:35d7d920fbc9 | 25 | Serial serial(USBTX,USBRX);//Timer set-up tool |
qk2277 | 0:35d7d920fbc9 | 26 | |
qk2277 | 0:35d7d920fbc9 | 27 | /** |
qk2277 | 0:35d7d920fbc9 | 28 | Define the variable will be used in the programming |
qk2277 | 0:35d7d920fbc9 | 29 | */ |
qk2277 | 0:35d7d920fbc9 | 30 | |
qk2277 | 0:35d7d920fbc9 | 31 | void serialISR();//ISR that is called when serial data is received |
qk2277 | 0:35d7d920fbc9 | 32 | void setTime();// function to set the UNIX time |
qk2277 | 0:35d7d920fbc9 | 33 | int setTimerFlag = 0;// flag for ISR |
qk2277 | 0:35d7d920fbc9 | 34 | char rxString[16];//Create a 16 chars row to display the data |
qk2277 | 0:35d7d920fbc9 | 35 | |
qk2277 | 0:35d7d920fbc9 | 36 | /** |
qk2277 | 0:35d7d920fbc9 | 37 | Set up a project title before the data shows on the LCD |
qk2277 | 0:35d7d920fbc9 | 38 | */ |
qk2277 | 0:35d7d920fbc9 | 39 | int main() |
qk2277 | 0:35d7d920fbc9 | 40 | { |
qk2277 | 0:35d7d920fbc9 | 41 | lcd.init(); |
qk2277 | 0:35d7d920fbc9 | 42 | bmp180.init();//Display the word before the sensor data |
qk2277 | 0:35d7d920fbc9 | 43 | lcd.printString("Weather",0,0);//At the location (0,0),display word "Weather" |
qk2277 | 0:35d7d920fbc9 | 44 | lcd.printString("Station",1,3);//At the location (1,3),display word "Station" |
qk2277 | 0:35d7d920fbc9 | 45 | wait(2.0);//The word above stay for 2s |
qk2277 | 0:35d7d920fbc9 | 46 | lcd.clear();//Clean the display for the continued work |
qk2277 | 0:35d7d920fbc9 | 47 | |
qk2277 | 0:35d7d920fbc9 | 48 | /** |
qk2277 | 0:35d7d920fbc9 | 49 | Start the measuremrnt progressing |
qk2277 | 0:35d7d920fbc9 | 50 | */ |
qk2277 | 0:35d7d920fbc9 | 51 | Measurement measurement; |
qk2277 | 0:35d7d920fbc9 | 52 | |
qk2277 | 0:35d7d920fbc9 | 53 | serial.attach(&serialISR);//attach serial ISR |
qk2277 | 0:35d7d920fbc9 | 54 | char t[30];//Create a 30 chars row to display the time |
qk2277 | 0:35d7d920fbc9 | 55 | |
qk2277 | 0:35d7d920fbc9 | 56 | /** |
qk2277 | 0:35d7d920fbc9 | 57 | Create the row for the timer and ensure the location of it |
qk2277 | 0:35d7d920fbc9 | 58 | */ |
qk2277 | 0:35d7d920fbc9 | 59 | while(1) { |
qk2277 | 0:35d7d920fbc9 | 60 | |
qk2277 | 0:35d7d920fbc9 | 61 | time_t seconds = time(NULL);//get current time |
qk2277 | 0:35d7d920fbc9 | 62 | // format time into a string (time and date) |
qk2277 | 0:35d7d920fbc9 | 63 | strftime(t, 30 , "%X %D",localtime(&seconds));// |
qk2277 | 0:35d7d920fbc9 | 64 | // print over serial |
qk2277 | 0:35d7d920fbc9 | 65 | serial.printf("Time = %s\n" ,t);//Display the timer |
qk2277 | 0:35d7d920fbc9 | 66 | lcd.printString(t,0,5);//The location of the timer |
qk2277 | 0:35d7d920fbc9 | 67 | |
qk2277 | 0:35d7d920fbc9 | 68 | if(setTimerFlag) {// if updated time has been sent |
qk2277 | 0:35d7d920fbc9 | 69 | setTimerFlag = 0;//clear flag |
qk2277 | 0:35d7d920fbc9 | 70 | setTime();// update time |
qk2277 | 0:35d7d920fbc9 | 71 | |
qk2277 | 0:35d7d920fbc9 | 72 | } |
qk2277 | 0:35d7d920fbc9 | 73 | |
qk2277 | 0:35d7d920fbc9 | 74 | /** |
qk2277 | 0:35d7d920fbc9 | 75 | Display the data measure by the sensor and show on the LCD |
qk2277 | 0:35d7d920fbc9 | 76 | Enbsure the form and location of it |
qk2277 | 0:35d7d920fbc9 | 77 | */ |
qk2277 | 0:35d7d920fbc9 | 78 | measurement = bmp180.readValues();// |
qk2277 | 0:35d7d920fbc9 | 79 | char T[14];//Create a 14 chars row to display the temperature |
qk2277 | 0:35d7d920fbc9 | 80 | int length =sprintf(T,"T = %.2f C",measurement.temperature);//Set up "T = sensor data" as the thing will be shown |
qk2277 | 0:35d7d920fbc9 | 81 | if (length <= 14)//Judge the length of chars |
qk2277 | 0:35d7d920fbc9 | 82 | lcd.printString(T,0,1);//The location of the T will be shown |
qk2277 | 0:35d7d920fbc9 | 83 | char P[14];//Create a 14 chars row to display the pressure |
qk2277 | 0:35d7d920fbc9 | 84 | length = sprintf(P,"P = %.2f mb",measurement.pressure);//Set up "P = sensor data" as the thing will be shown |
qk2277 | 0:35d7d920fbc9 | 85 | lcd.printString(P,0,3); //The location of the P will be shown |
qk2277 | 0:35d7d920fbc9 | 86 | wait(1);//Repeat the circulate each 1s |
qk2277 | 0:35d7d920fbc9 | 87 | lcd.clear(); //Clear the data for next processing |
qk2277 | 0:35d7d920fbc9 | 88 | } |
qk2277 | 0:35d7d920fbc9 | 89 | } |
qk2277 | 0:35d7d920fbc9 | 90 | |
qk2277 | 0:35d7d920fbc9 | 91 | /** |
qk2277 | 0:35d7d920fbc9 | 92 | Create a string to the interge and set the current time |
qk2277 | 0:35d7d920fbc9 | 93 | */ |
qk2277 | 0:35d7d920fbc9 | 94 | void setTime()//// print time for debugging |
qk2277 | 0:35d7d920fbc9 | 95 | { |
qk2277 | 0:35d7d920fbc9 | 96 | |
qk2277 | 0:35d7d920fbc9 | 97 | serial.printf("set_time - %s",rxString); |
qk2277 | 0:35d7d920fbc9 | 98 | //// atoi() converts a string to an integer |
qk2277 | 0:35d7d920fbc9 | 99 | int time = atoi(rxString); |
qk2277 | 0:35d7d920fbc9 | 100 | //update the time |
qk2277 | 0:35d7d920fbc9 | 101 | set_time(time); |
qk2277 | 0:35d7d920fbc9 | 102 | } |
qk2277 | 0:35d7d920fbc9 | 103 | /** |
qk2277 | 0:35d7d920fbc9 | 104 | When the serial interrupt occurs, read rs string either |
qk2277 | 0:35d7d920fbc9 | 105 | */ |
qk2277 | 0:35d7d920fbc9 | 106 | void serialISR()// when a serial interrupt occurs, read rx string into buffer |
qk2277 | 0:35d7d920fbc9 | 107 | { |
qk2277 | 0:35d7d920fbc9 | 108 | |
qk2277 | 0:35d7d920fbc9 | 109 | serial.gets(rxString,16); |
qk2277 | 0:35d7d920fbc9 | 110 | //// set flag |
qk2277 | 0:35d7d920fbc9 | 111 | setTimerFlag = 1; |
qk2277 | 0:35d7d920fbc9 | 112 | } |