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 15:14:31 2014 +0000
Revision:
0:4d4c5ea17d86
Child:
1:95b80cc3f676
First version of the library for the use of DHT11, temperature and humidity sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
s_inoue_mbed 0:4d4c5ea17d86 1 /** @file
s_inoue_mbed 0:4d4c5ea17d86 2 Library for the use of the DHT11, a temperature and humidity sensor
s_inoue_mbed 0:4d4c5ea17d86 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 0:4d4c5ea17d86 20 * if (s != 0) {
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 int readData(void);
s_inoue_mbed 0:4d4c5ea17d86 41
s_inoue_mbed 0:4d4c5ea17d86 42 /** Reading the data from the DHT11
s_inoue_mbed 0:4d4c5ea17d86 43 * @return Error code
s_inoue_mbed 0:4d4c5ea17d86 44 * 0: OK.
s_inoue_mbed 0:4d4c5ea17d86 45 * 1: Reading the data too often.
s_inoue_mbed 0:4d4c5ea17d86 46 * 2: 1-wire bus is busy.
s_inoue_mbed 0:4d4c5ea17d86 47 * 3: DHT11 does not respond.
s_inoue_mbed 0:4d4c5ea17d86 48 * 4: DHT11 is not ready.
s_inoue_mbed 0:4d4c5ea17d86 49 * 5: Checksum is incorrect.
s_inoue_mbed 0:4d4c5ea17d86 50 * 6: Timeout
s_inoue_mbed 0:4d4c5ea17d86 51 */
s_inoue_mbed 0:4d4c5ea17d86 52
s_inoue_mbed 0:4d4c5ea17d86 53 /** Reading the humidity from the data
s_inoue_mbed 0:4d4c5ea17d86 54 * @return Humidity in % if readData() returns no error. Otherwise, returns 0xffffffff.
s_inoue_mbed 0:4d4c5ea17d86 55 */
s_inoue_mbed 0:4d4c5ea17d86 56 int readHumidity(void);
s_inoue_mbed 0:4d4c5ea17d86 57
s_inoue_mbed 0:4d4c5ea17d86 58 /** Reading the humidity from the data
s_inoue_mbed 0:4d4c5ea17d86 59 * @return Temperature in Celcius if readData() returns no error. Otherwise, returns 0xffffffff.
s_inoue_mbed 0:4d4c5ea17d86 60 */
s_inoue_mbed 0:4d4c5ea17d86 61 int readTemperature(void);
s_inoue_mbed 0:4d4c5ea17d86 62
s_inoue_mbed 0:4d4c5ea17d86 63 enum ErrorDHT11 {
s_inoue_mbed 0:4d4c5ea17d86 64 OK = 0,
s_inoue_mbed 0:4d4c5ea17d86 65 TOO_FAST_READ = 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 PinName _pin;
s_inoue_mbed 0:4d4c5ea17d86 75 DigitalInOut io;
s_inoue_mbed 0:4d4c5ea17d86 76 InterruptIn io_irq;
s_inoue_mbed 0:4d4c5ea17d86 77 Timer t;
s_inoue_mbed 0:4d4c5ea17d86 78 uint32_t t_pulse_us;
s_inoue_mbed 0:4d4c5ea17d86 79 bool first_time;
s_inoue_mbed 0:4d4c5ea17d86 80 uint64_t data;
s_inoue_mbed 0:4d4c5ea17d86 81 uint32_t chksum;
s_inoue_mbed 0:4d4c5ea17d86 82 uint32_t cnt;
s_inoue_mbed 0:4d4c5ea17d86 83 uint32_t wdt;
s_inoue_mbed 0:4d4c5ea17d86 84 bool eod;
s_inoue_mbed 0:4d4c5ea17d86 85 ErrorDHT11 err;
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