A library for the use of AM2303 (a.k.a. DHT22), a temperature and humidity sensor.

Dependents:   AM2303_Hello_World

Fork of DHT11 by Shigenori Inoue

Committer:
s_inoue_mbed
Date:
Mon Oct 13 14:18:58 2014 +0000
Revision:
12:1ad0612823e9
Parent:
DHT11.h@10:f0d789f49df7
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
s_inoue_mbed 10:f0d789f49df7 1 /* Copyright (c) 2014 Shigenori Inoue, MIT License
s_inoue_mbed 10:f0d789f49df7 2 *
s_inoue_mbed 12:1ad0612823e9 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
s_inoue_mbed 12:1ad0612823e9 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
s_inoue_mbed 12:1ad0612823e9 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
s_inoue_mbed 12:1ad0612823e9 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
s_inoue_mbed 10:f0d789f49df7 7 * furnished to do so, subject to the following conditions:
s_inoue_mbed 10:f0d789f49df7 8 *
s_inoue_mbed 12:1ad0612823e9 9 * The above copyright notice and this permission notice shall be included in all copies or
s_inoue_mbed 10:f0d789f49df7 10 * substantial portions of the Software.
s_inoue_mbed 10:f0d789f49df7 11 *
s_inoue_mbed 12:1ad0612823e9 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
s_inoue_mbed 12:1ad0612823e9 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
s_inoue_mbed 12:1ad0612823e9 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
s_inoue_mbed 12:1ad0612823e9 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
s_inoue_mbed 10:f0d789f49df7 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
s_inoue_mbed 0:4d4c5ea17d86 17 */
s_inoue_mbed 12:1ad0612823e9 18
s_inoue_mbed 12:1ad0612823e9 19 #ifndef __AM2303__
s_inoue_mbed 12:1ad0612823e9 20 #define __AM2303__
s_inoue_mbed 0:4d4c5ea17d86 21 #include "mbed.h"
s_inoue_mbed 0:4d4c5ea17d86 22
s_inoue_mbed 0:4d4c5ea17d86 23 /** Example:
s_inoue_mbed 0:4d4c5ea17d86 24 * @code
s_inoue_mbed 0:4d4c5ea17d86 25 * #include "mbed.h"
s_inoue_mbed 12:1ad0612823e9 26 * #include "AM2303.h"
s_inoue_mbed 0:4d4c5ea17d86 27 *
s_inoue_mbed 12:1ad0612823e9 28 * AM2303 d;
s_inoue_mbed 0:4d4c5ea17d86 29 *
s_inoue_mbed 0:4d4c5ea17d86 30 * main()
s_inoue_mbed 0:4d4c5ea17d86 31 * {
s_inoue_mbed 0:4d4c5ea17d86 32 * int s;
s_inoue_mbed 12:1ad0612823e9 33 * s = h.readData();
s_inoue_mbed 12:1ad0612823e9 34 * if (s != AM2303::OK) {
s_inoue_mbed 0:4d4c5ea17d86 35 * printf("Error!\r\n");
s_inoue_mbed 0:4d4c5ea17d86 36 * }
s_inoue_mbed 0:4d4c5ea17d86 37 * else {
s_inoue_mbed 12:1ad0612823e9 38 * printf("T:%f, H:%f\r\n", h.readTemperature(), h.readHumidity());
s_inoue_mbed 0:4d4c5ea17d86 39 * }
s_inoue_mbed 0:4d4c5ea17d86 40 * }
s_inoue_mbed 0:4d4c5ea17d86 41 * @endcode
s_inoue_mbed 0:4d4c5ea17d86 42 */
s_inoue_mbed 0:4d4c5ea17d86 43
s_inoue_mbed 12:1ad0612823e9 44 class AM2303
s_inoue_mbed 12:1ad0612823e9 45 {
s_inoue_mbed 0:4d4c5ea17d86 46 public:
s_inoue_mbed 12:1ad0612823e9 47 /** Create a AM2303 interface
s_inoue_mbed 12:1ad0612823e9 48 * @param pin 1-wire-like serial I/O port of AM2303
s_inoue_mbed 0:4d4c5ea17d86 49 */
s_inoue_mbed 12:1ad0612823e9 50 AM2303(PinName pin);
s_inoue_mbed 12:1ad0612823e9 51 ~AM2303();
s_inoue_mbed 0:4d4c5ea17d86 52
s_inoue_mbed 12:1ad0612823e9 53 /** Reading the data from the AM2303
s_inoue_mbed 0:4d4c5ea17d86 54 * @return Error code
s_inoue_mbed 0:4d4c5ea17d86 55 * 0: OK.
s_inoue_mbed 0:4d4c5ea17d86 56 * 1: Reading the data too often.
s_inoue_mbed 0:4d4c5ea17d86 57 * 2: 1-wire bus is busy.
s_inoue_mbed 12:1ad0612823e9 58 * 3: AM2303 does not respond.
s_inoue_mbed 12:1ad0612823e9 59 * 4: AM2303 is not ready.
s_inoue_mbed 0:4d4c5ea17d86 60 * 5: Checksum is incorrect.
s_inoue_mbed 5:da586c935e88 61 * 6: Timeout.
s_inoue_mbed 0:4d4c5ea17d86 62 */
s_inoue_mbed 3:8cd064147bde 63 int readData(void);
s_inoue_mbed 3:8cd064147bde 64
s_inoue_mbed 0:4d4c5ea17d86 65 /** Reading the humidity from the data
s_inoue_mbed 9:056d1e9b428c 66 * @return Humidity in %,
s_inoue_mbed 9:056d1e9b428c 67 * regardless of the error from readData()
s_inoue_mbed 0:4d4c5ea17d86 68 */
s_inoue_mbed 12:1ad0612823e9 69 float readHumidity(void);
s_inoue_mbed 3:8cd064147bde 70
s_inoue_mbed 3:8cd064147bde 71 /** Reading the temperature from the data
s_inoue_mbed 9:056d1e9b428c 72 * @return Temperature in Celcius,
s_inoue_mbed 9:056d1e9b428c 73 * regardless of the error from readData()
s_inoue_mbed 0:4d4c5ea17d86 74 */
s_inoue_mbed 12:1ad0612823e9 75 float readTemperature(void);
s_inoue_mbed 3:8cd064147bde 76
s_inoue_mbed 12:1ad0612823e9 77 enum ErrorAM2303 {
s_inoue_mbed 0:4d4c5ea17d86 78 OK = 0,
s_inoue_mbed 9:056d1e9b428c 79 READ_TOO_OFTEN = 1,
s_inoue_mbed 0:4d4c5ea17d86 80 BUS_BUSY = 2,
s_inoue_mbed 0:4d4c5ea17d86 81 NOT_PRESENT = 3,
s_inoue_mbed 0:4d4c5ea17d86 82 NOT_READY = 4,
s_inoue_mbed 0:4d4c5ea17d86 83 CHKSUM_ERR = 5,
s_inoue_mbed 0:4d4c5ea17d86 84 WATCHDOG_ERR = 6,
s_inoue_mbed 0:4d4c5ea17d86 85 };
s_inoue_mbed 12:1ad0612823e9 86 uint64_t data;
s_inoue_mbed 12:1ad0612823e9 87
s_inoue_mbed 0:4d4c5ea17d86 88 private:
s_inoue_mbed 0:4d4c5ea17d86 89 DigitalInOut io;
s_inoue_mbed 0:4d4c5ea17d86 90 InterruptIn io_irq;
s_inoue_mbed 0:4d4c5ea17d86 91 Timer t;
s_inoue_mbed 0:4d4c5ea17d86 92 uint32_t t_pulse_us;
s_inoue_mbed 12:1ad0612823e9 93 const static int t_tol_start;
s_inoue_mbed 9:056d1e9b428c 94 const static int t_tol_pulse;
s_inoue_mbed 0:4d4c5ea17d86 95 bool first_time;
s_inoue_mbed 12:1ad0612823e9 96 /* uint64_t data; */
s_inoue_mbed 0:4d4c5ea17d86 97 uint32_t chksum;
s_inoue_mbed 0:4d4c5ea17d86 98 uint32_t cnt;
s_inoue_mbed 0:4d4c5ea17d86 99 uint32_t wdt;
s_inoue_mbed 0:4d4c5ea17d86 100 bool eod;
s_inoue_mbed 0:4d4c5ea17d86 101 void init(void);
s_inoue_mbed 0:4d4c5ea17d86 102 void pos_edge(void);
s_inoue_mbed 0:4d4c5ea17d86 103 void neg_edge(void);
s_inoue_mbed 0:4d4c5ea17d86 104 };
s_inoue_mbed 0:4d4c5ea17d86 105
s_inoue_mbed 0:4d4c5ea17d86 106 #endif