An example to use an AM2303 (a.k.a. DHT22) humidity sensor

Dependencies:   AM2303 TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (c) 2014 Shigenori Inoue, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "mbed.h"
00020 #include "TextLCD.h"
00021 #include "AM2303.h"
00022 
00023 /* LEDs for debugging */
00024 BusOut leds(LED4, LED3, LED2, LED1);
00025 
00026 /* LCD module */
00027 TextLCD lcd(p25, p24, p12, p13, p14, p23);
00028 
00029 /* Humidity sensor */
00030 AM2303 h(p18);
00031 
00032 /* The main function */
00033 int main()
00034 {
00035     /* Variables */
00036     int state;
00037 
00038     /* Show splash screen */
00039     lcd.cls();
00040     lcd.locate(0, 0);
00041     lcd.printf("AM2303 Humidity");
00042 
00043     /* Infinate loop */
00044     while(true) {
00045         /* Wait for measurement */
00046         wait(2.0);
00047 
00048         /* Read the measured results */
00049         state = h.readData();
00050 
00051         /* Show the data, otherwise error */
00052         lcd.locate(0, 1);
00053         if (state != AM2303::OK) {
00054             lcd.printf("<Error: %d>", state);
00055         } else {
00056             lcd.printf("T:%2.1fC, H:%2.1f%%", h.readTemperature(), h.readHumidity());
00057         }
00058 
00059         /* Blink LED for Debug */
00060         leds = leds + 1;
00061     }
00062 }