Simple library for the DHT11 temperature and humidity sensor. Forked from an existing Mbed DHT11 project.
Simple DHT11 temperature and humidity library.
Example usage
#include "mbed.h" #include "DHT11.h" DHT11 dht(D8); // Change pin name here if required main() { printf("T:%d, H:%d\r\n", dht.readTemperature(), dht.readHumidity()); }
The sensor may be read as often as desired, but temperature and humidity values are cached and will only be updated if they are more than 2 seconds old. This is the underlying sensor update rate.
Please note that this project has been modified only enough to make it work for its intended purpose. Various parts of this project still need work, and the source code should not be seen as an example of best practice.
DHT11.h@9:056d1e9b428c, 2014-09-11 (annotated)
- Committer:
- s_inoue_mbed
- Date:
- Thu Sep 11 13:45:00 2014 +0000
- Revision:
- 9:056d1e9b428c
- Parent:
- 5:da586c935e88
- Child:
- 10:f0d789f49df7
Bug fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
s_inoue_mbed | 4:48798b126d93 | 1 | /* |
s_inoue_mbed | 4:48798b126d93 | 2 | * Library for the use of the DHT11, a temperature and humidity sensor |
s_inoue_mbed | 4:48798b126d93 | 3 | * Shigenori Inoue, September 10, 2014 |
s_inoue_mbed | 0:4d4c5ea17d86 | 4 | */ |
s_inoue_mbed | 0:4d4c5ea17d86 | 5 | #ifndef __DHT11__ |
s_inoue_mbed | 0:4d4c5ea17d86 | 6 | #define __DHT11__ |
s_inoue_mbed | 0:4d4c5ea17d86 | 7 | #include "mbed.h" |
s_inoue_mbed | 0:4d4c5ea17d86 | 8 | |
s_inoue_mbed | 0:4d4c5ea17d86 | 9 | /** Example: |
s_inoue_mbed | 0:4d4c5ea17d86 | 10 | * @code |
s_inoue_mbed | 0:4d4c5ea17d86 | 11 | * #include "mbed.h" |
s_inoue_mbed | 0:4d4c5ea17d86 | 12 | * #include "DHT11.h" |
s_inoue_mbed | 0:4d4c5ea17d86 | 13 | * |
s_inoue_mbed | 0:4d4c5ea17d86 | 14 | * DHT11 d; |
s_inoue_mbed | 0:4d4c5ea17d86 | 15 | * |
s_inoue_mbed | 0:4d4c5ea17d86 | 16 | * main() |
s_inoue_mbed | 0:4d4c5ea17d86 | 17 | * { |
s_inoue_mbed | 0:4d4c5ea17d86 | 18 | * int s; |
s_inoue_mbed | 0:4d4c5ea17d86 | 19 | * s = d.readData(); |
s_inoue_mbed | 2:1e997b7a1f9a | 20 | * if (s != DHT11::OK) { |
s_inoue_mbed | 0:4d4c5ea17d86 | 21 | * printf("Error!\r\n"); |
s_inoue_mbed | 0:4d4c5ea17d86 | 22 | * } |
s_inoue_mbed | 0:4d4c5ea17d86 | 23 | * else { |
s_inoue_mbed | 0:4d4c5ea17d86 | 24 | * printf("T:%d, H:%d\r\n", d.readTemperature(), d.readHumidity()); |
s_inoue_mbed | 0:4d4c5ea17d86 | 25 | * } |
s_inoue_mbed | 0:4d4c5ea17d86 | 26 | * } |
s_inoue_mbed | 0:4d4c5ea17d86 | 27 | * @endcode |
s_inoue_mbed | 0:4d4c5ea17d86 | 28 | */ |
s_inoue_mbed | 0:4d4c5ea17d86 | 29 | |
s_inoue_mbed | 0:4d4c5ea17d86 | 30 | class DHT11 |
s_inoue_mbed | 9:056d1e9b428c | 31 | { |
s_inoue_mbed | 0:4d4c5ea17d86 | 32 | public: |
s_inoue_mbed | 0:4d4c5ea17d86 | 33 | /** Create a DHT11 interface |
s_inoue_mbed | 0:4d4c5ea17d86 | 34 | * @param pin 1-wire-like serial I/O port of DHT11 |
s_inoue_mbed | 0:4d4c5ea17d86 | 35 | */ |
s_inoue_mbed | 0:4d4c5ea17d86 | 36 | DHT11(PinName pin); |
s_inoue_mbed | 0:4d4c5ea17d86 | 37 | ~DHT11(); |
s_inoue_mbed | 0:4d4c5ea17d86 | 38 | |
s_inoue_mbed | 0:4d4c5ea17d86 | 39 | /** Reading the data from the DHT11 |
s_inoue_mbed | 0:4d4c5ea17d86 | 40 | * @return Error code |
s_inoue_mbed | 0:4d4c5ea17d86 | 41 | * 0: OK. |
s_inoue_mbed | 0:4d4c5ea17d86 | 42 | * 1: Reading the data too often. |
s_inoue_mbed | 0:4d4c5ea17d86 | 43 | * 2: 1-wire bus is busy. |
s_inoue_mbed | 0:4d4c5ea17d86 | 44 | * 3: DHT11 does not respond. |
s_inoue_mbed | 0:4d4c5ea17d86 | 45 | * 4: DHT11 is not ready. |
s_inoue_mbed | 0:4d4c5ea17d86 | 46 | * 5: Checksum is incorrect. |
s_inoue_mbed | 5:da586c935e88 | 47 | * 6: Timeout. |
s_inoue_mbed | 0:4d4c5ea17d86 | 48 | */ |
s_inoue_mbed | 3:8cd064147bde | 49 | int readData(void); |
s_inoue_mbed | 3:8cd064147bde | 50 | |
s_inoue_mbed | 0:4d4c5ea17d86 | 51 | /** Reading the humidity from the data |
s_inoue_mbed | 9:056d1e9b428c | 52 | * @return Humidity in %, |
s_inoue_mbed | 9:056d1e9b428c | 53 | * regardless of the error from readData() |
s_inoue_mbed | 0:4d4c5ea17d86 | 54 | */ |
s_inoue_mbed | 0:4d4c5ea17d86 | 55 | int readHumidity(void); |
s_inoue_mbed | 3:8cd064147bde | 56 | |
s_inoue_mbed | 3:8cd064147bde | 57 | /** Reading the temperature from the data |
s_inoue_mbed | 9:056d1e9b428c | 58 | * @return Temperature in Celcius, |
s_inoue_mbed | 9:056d1e9b428c | 59 | * regardless of the error from readData() |
s_inoue_mbed | 0:4d4c5ea17d86 | 60 | */ |
s_inoue_mbed | 0:4d4c5ea17d86 | 61 | int readTemperature(void); |
s_inoue_mbed | 3:8cd064147bde | 62 | |
s_inoue_mbed | 0:4d4c5ea17d86 | 63 | enum ErrorDHT11 { |
s_inoue_mbed | 0:4d4c5ea17d86 | 64 | OK = 0, |
s_inoue_mbed | 9:056d1e9b428c | 65 | READ_TOO_OFTEN = 1, |
s_inoue_mbed | 0:4d4c5ea17d86 | 66 | BUS_BUSY = 2, |
s_inoue_mbed | 0:4d4c5ea17d86 | 67 | NOT_PRESENT = 3, |
s_inoue_mbed | 0:4d4c5ea17d86 | 68 | NOT_READY = 4, |
s_inoue_mbed | 0:4d4c5ea17d86 | 69 | CHKSUM_ERR = 5, |
s_inoue_mbed | 0:4d4c5ea17d86 | 70 | WATCHDOG_ERR = 6, |
s_inoue_mbed | 0:4d4c5ea17d86 | 71 | }; |
s_inoue_mbed | 0:4d4c5ea17d86 | 72 | |
s_inoue_mbed | 0:4d4c5ea17d86 | 73 | private: |
s_inoue_mbed | 0:4d4c5ea17d86 | 74 | DigitalInOut io; |
s_inoue_mbed | 0:4d4c5ea17d86 | 75 | InterruptIn io_irq; |
s_inoue_mbed | 0:4d4c5ea17d86 | 76 | Timer t; |
s_inoue_mbed | 0:4d4c5ea17d86 | 77 | uint32_t t_pulse_us; |
s_inoue_mbed | 9:056d1e9b428c | 78 | const static int t_tol_start; |
s_inoue_mbed | 9:056d1e9b428c | 79 | const static int t_tol_pulse; |
s_inoue_mbed | 0:4d4c5ea17d86 | 80 | bool first_time; |
s_inoue_mbed | 0:4d4c5ea17d86 | 81 | uint64_t data; |
s_inoue_mbed | 0:4d4c5ea17d86 | 82 | uint32_t chksum; |
s_inoue_mbed | 0:4d4c5ea17d86 | 83 | uint32_t cnt; |
s_inoue_mbed | 0:4d4c5ea17d86 | 84 | uint32_t wdt; |
s_inoue_mbed | 0:4d4c5ea17d86 | 85 | bool eod; |
s_inoue_mbed | 0:4d4c5ea17d86 | 86 | void init(void); |
s_inoue_mbed | 0:4d4c5ea17d86 | 87 | void pos_edge(void); |
s_inoue_mbed | 0:4d4c5ea17d86 | 88 | void neg_edge(void); |
s_inoue_mbed | 0:4d4c5ea17d86 | 89 | }; |
s_inoue_mbed | 0:4d4c5ea17d86 | 90 | |
s_inoue_mbed | 0:4d4c5ea17d86 | 91 | #endif |