Simple library for the DHT11 temperature and humidity sensor. Forked from an existing Mbed DHT11 project.

Dependents:   UoY-DHT11-test

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.

Committer:
s_inoue_mbed
Date:
Wed Sep 10 17:18:28 2014 +0000
Revision:
4:48798b126d93
Parent:
3:8cd064147bde
Child:
5:da586c935e88
Doxygen documentation has been corrected.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:4d4c5ea17d86 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 0:4d4c5ea17d86 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 0:4d4c5ea17d86 52 * @return Humidity in % if readData() returns no error. Otherwise, returns 0xffffffff.
s_inoue_mbed 0:4d4c5ea17d86 53 */
s_inoue_mbed 0:4d4c5ea17d86 54 int readHumidity(void);
s_inoue_mbed 3:8cd064147bde 55
s_inoue_mbed 3:8cd064147bde 56 /** Reading the temperature from the data
s_inoue_mbed 0:4d4c5ea17d86 57 * @return Temperature in Celcius if readData() returns no error. Otherwise, returns 0xffffffff.
s_inoue_mbed 0:4d4c5ea17d86 58 */
s_inoue_mbed 0:4d4c5ea17d86 59 int readTemperature(void);
s_inoue_mbed 3:8cd064147bde 60
s_inoue_mbed 0:4d4c5ea17d86 61 enum ErrorDHT11 {
s_inoue_mbed 0:4d4c5ea17d86 62 OK = 0,
s_inoue_mbed 0:4d4c5ea17d86 63 TOO_FAST_READ = 1,
s_inoue_mbed 0:4d4c5ea17d86 64 BUS_BUSY = 2,
s_inoue_mbed 0:4d4c5ea17d86 65 NOT_PRESENT = 3,
s_inoue_mbed 0:4d4c5ea17d86 66 NOT_READY = 4,
s_inoue_mbed 0:4d4c5ea17d86 67 CHKSUM_ERR = 5,
s_inoue_mbed 0:4d4c5ea17d86 68 WATCHDOG_ERR = 6,
s_inoue_mbed 0:4d4c5ea17d86 69 };
s_inoue_mbed 0:4d4c5ea17d86 70
s_inoue_mbed 0:4d4c5ea17d86 71 private:
s_inoue_mbed 0:4d4c5ea17d86 72 PinName _pin;
s_inoue_mbed 0:4d4c5ea17d86 73 DigitalInOut io;
s_inoue_mbed 0:4d4c5ea17d86 74 InterruptIn io_irq;
s_inoue_mbed 0:4d4c5ea17d86 75 Timer t;
s_inoue_mbed 0:4d4c5ea17d86 76 uint32_t t_pulse_us;
s_inoue_mbed 0:4d4c5ea17d86 77 bool first_time;
s_inoue_mbed 0:4d4c5ea17d86 78 uint64_t data;
s_inoue_mbed 0:4d4c5ea17d86 79 uint32_t chksum;
s_inoue_mbed 0:4d4c5ea17d86 80 uint32_t cnt;
s_inoue_mbed 0:4d4c5ea17d86 81 uint32_t wdt;
s_inoue_mbed 0:4d4c5ea17d86 82 bool eod;
s_inoue_mbed 0:4d4c5ea17d86 83 ErrorDHT11 err;
s_inoue_mbed 0:4d4c5ea17d86 84 void init(void);
s_inoue_mbed 0:4d4c5ea17d86 85 void pos_edge(void);
s_inoue_mbed 0:4d4c5ea17d86 86 void neg_edge(void);
s_inoue_mbed 0:4d4c5ea17d86 87 };
s_inoue_mbed 0:4d4c5ea17d86 88
s_inoue_mbed 0:4d4c5ea17d86 89 #endif