An example to use an AM2303 (a.k.a. DHT22) humidity sensor
Dependencies: AM2303 TextLCD mbed
main.cpp@0:a1cd181526fc, 2014-10-13 (annotated)
- Committer:
- s_inoue_mbed
- Date:
- Mon Oct 13 14:24:24 2014 +0000
- Revision:
- 0:a1cd181526fc
An example program to use an AM2303 humidity sensor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
s_inoue_mbed | 0:a1cd181526fc | 1 | /* Copyright (c) 2014 Shigenori Inoue, MIT License |
s_inoue_mbed | 0:a1cd181526fc | 2 | * |
s_inoue_mbed | 0:a1cd181526fc | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
s_inoue_mbed | 0:a1cd181526fc | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
s_inoue_mbed | 0:a1cd181526fc | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
s_inoue_mbed | 0:a1cd181526fc | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
s_inoue_mbed | 0:a1cd181526fc | 7 | * furnished to do so, subject to the following conditions: |
s_inoue_mbed | 0:a1cd181526fc | 8 | * |
s_inoue_mbed | 0:a1cd181526fc | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
s_inoue_mbed | 0:a1cd181526fc | 10 | * substantial portions of the Software. |
s_inoue_mbed | 0:a1cd181526fc | 11 | * |
s_inoue_mbed | 0:a1cd181526fc | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
s_inoue_mbed | 0:a1cd181526fc | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
s_inoue_mbed | 0:a1cd181526fc | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
s_inoue_mbed | 0:a1cd181526fc | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
s_inoue_mbed | 0:a1cd181526fc | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
s_inoue_mbed | 0:a1cd181526fc | 17 | */ |
s_inoue_mbed | 0:a1cd181526fc | 18 | |
s_inoue_mbed | 0:a1cd181526fc | 19 | #include "mbed.h" |
s_inoue_mbed | 0:a1cd181526fc | 20 | #include "TextLCD.h" |
s_inoue_mbed | 0:a1cd181526fc | 21 | #include "AM2303.h" |
s_inoue_mbed | 0:a1cd181526fc | 22 | |
s_inoue_mbed | 0:a1cd181526fc | 23 | /* LEDs for debugging */ |
s_inoue_mbed | 0:a1cd181526fc | 24 | BusOut leds(LED4, LED3, LED2, LED1); |
s_inoue_mbed | 0:a1cd181526fc | 25 | |
s_inoue_mbed | 0:a1cd181526fc | 26 | /* LCD module */ |
s_inoue_mbed | 0:a1cd181526fc | 27 | TextLCD lcd(p25, p24, p12, p13, p14, p23); |
s_inoue_mbed | 0:a1cd181526fc | 28 | |
s_inoue_mbed | 0:a1cd181526fc | 29 | /* Humidity sensor */ |
s_inoue_mbed | 0:a1cd181526fc | 30 | AM2303 h(p18); |
s_inoue_mbed | 0:a1cd181526fc | 31 | |
s_inoue_mbed | 0:a1cd181526fc | 32 | /* The main function */ |
s_inoue_mbed | 0:a1cd181526fc | 33 | int main() |
s_inoue_mbed | 0:a1cd181526fc | 34 | { |
s_inoue_mbed | 0:a1cd181526fc | 35 | /* Variables */ |
s_inoue_mbed | 0:a1cd181526fc | 36 | int state; |
s_inoue_mbed | 0:a1cd181526fc | 37 | |
s_inoue_mbed | 0:a1cd181526fc | 38 | /* Show splash screen */ |
s_inoue_mbed | 0:a1cd181526fc | 39 | lcd.cls(); |
s_inoue_mbed | 0:a1cd181526fc | 40 | lcd.locate(0, 0); |
s_inoue_mbed | 0:a1cd181526fc | 41 | lcd.printf("AM2303 Humidity"); |
s_inoue_mbed | 0:a1cd181526fc | 42 | |
s_inoue_mbed | 0:a1cd181526fc | 43 | /* Infinate loop */ |
s_inoue_mbed | 0:a1cd181526fc | 44 | while(true) { |
s_inoue_mbed | 0:a1cd181526fc | 45 | /* Wait for measurement */ |
s_inoue_mbed | 0:a1cd181526fc | 46 | wait(2.0); |
s_inoue_mbed | 0:a1cd181526fc | 47 | |
s_inoue_mbed | 0:a1cd181526fc | 48 | /* Read the measured results */ |
s_inoue_mbed | 0:a1cd181526fc | 49 | state = h.readData(); |
s_inoue_mbed | 0:a1cd181526fc | 50 | |
s_inoue_mbed | 0:a1cd181526fc | 51 | /* Show the data, otherwise error */ |
s_inoue_mbed | 0:a1cd181526fc | 52 | lcd.locate(0, 1); |
s_inoue_mbed | 0:a1cd181526fc | 53 | if (state != AM2303::OK) { |
s_inoue_mbed | 0:a1cd181526fc | 54 | lcd.printf("<Error: %d>", state); |
s_inoue_mbed | 0:a1cd181526fc | 55 | } else { |
s_inoue_mbed | 0:a1cd181526fc | 56 | lcd.printf("T:%2.1fC, H:%2.1f%%", h.readTemperature(), h.readHumidity()); |
s_inoue_mbed | 0:a1cd181526fc | 57 | } |
s_inoue_mbed | 0:a1cd181526fc | 58 | |
s_inoue_mbed | 0:a1cd181526fc | 59 | /* Blink LED for Debug */ |
s_inoue_mbed | 0:a1cd181526fc | 60 | leds = leds + 1; |
s_inoue_mbed | 0:a1cd181526fc | 61 | } |
s_inoue_mbed | 0:a1cd181526fc | 62 | } |