A library for the use of DHT11, a temperature and humidity sensor

Dependents:   DHT11_Hello_World IBMIoTClientEthernetExample_W5200 AutoPlants PB_Emma_Ethernet ... more

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
s_inoue_mbed 0:4d4c5ea17d86 6 #include "DHT11.h"
s_inoue_mbed 0:4d4c5ea17d86 7
s_inoue_mbed 0:4d4c5ea17d86 8 // Constructor
s_inoue_mbed 0:4d4c5ea17d86 9 DHT11::DHT11(PinName pin) : io(pin, PIN_INPUT, OpenDrain, 1), io_irq(pin)
s_inoue_mbed 0:4d4c5ea17d86 10 {
s_inoue_mbed 0:4d4c5ea17d86 11 io_irq.rise(this, &DHT11::pos_edge);
s_inoue_mbed 0:4d4c5ea17d86 12 io_irq.fall(this, &DHT11::neg_edge);
s_inoue_mbed 0:4d4c5ea17d86 13 io_irq.disable_irq();
s_inoue_mbed 0:4d4c5ea17d86 14 init();
s_inoue_mbed 0:4d4c5ea17d86 15 }
s_inoue_mbed 0:4d4c5ea17d86 16
s_inoue_mbed 0:4d4c5ea17d86 17 // Destructor
s_inoue_mbed 0:4d4c5ea17d86 18 DHT11::~DHT11(void) {}
s_inoue_mbed 0:4d4c5ea17d86 19
s_inoue_mbed 0:4d4c5ea17d86 20 // Reading the data bits from DHT11
s_inoue_mbed 0:4d4c5ea17d86 21 int DHT11::readData()
s_inoue_mbed 0:4d4c5ea17d86 22 {
s_inoue_mbed 0:4d4c5ea17d86 23 // Initialize
s_inoue_mbed 0:4d4c5ea17d86 24 eod = false;
s_inoue_mbed 0:4d4c5ea17d86 25 err = OK;
s_inoue_mbed 0:4d4c5ea17d86 26 data = 0;
s_inoue_mbed 0:4d4c5ea17d86 27 cnt = 0;
s_inoue_mbed 0:4d4c5ea17d86 28 wdt = 0;
s_inoue_mbed 0:4d4c5ea17d86 29
s_inoue_mbed 0:4d4c5ea17d86 30 // Checking the measurement frequency
s_inoue_mbed 0:4d4c5ea17d86 31 if (t.read_ms() < 2000 & first_time == false) {
s_inoue_mbed 0:4d4c5ea17d86 32 t.reset();
s_inoue_mbed 0:4d4c5ea17d86 33 return TOO_FAST_READ;
s_inoue_mbed 0:4d4c5ea17d86 34 }
s_inoue_mbed 0:4d4c5ea17d86 35
s_inoue_mbed 0:4d4c5ea17d86 36 // Checking the data bus
s_inoue_mbed 0:4d4c5ea17d86 37 if (io == 0) {
s_inoue_mbed 0:4d4c5ea17d86 38 io.input();
s_inoue_mbed 0:4d4c5ea17d86 39 return BUS_BUSY;
s_inoue_mbed 0:4d4c5ea17d86 40 }
s_inoue_mbed 0:4d4c5ea17d86 41
s_inoue_mbed 0:4d4c5ea17d86 42 // Sending start signal
s_inoue_mbed 0:4d4c5ea17d86 43 io.output();
s_inoue_mbed 0:4d4c5ea17d86 44 t.reset();
s_inoue_mbed 0:4d4c5ea17d86 45 t.start();
s_inoue_mbed 0:4d4c5ea17d86 46 do {
s_inoue_mbed 0:4d4c5ea17d86 47 io = 0;
s_inoue_mbed 0:4d4c5ea17d86 48 } while (t.read_ms() < 18);
s_inoue_mbed 0:4d4c5ea17d86 49 io.input();
s_inoue_mbed 0:4d4c5ea17d86 50 io = 1;
s_inoue_mbed 0:4d4c5ea17d86 51
s_inoue_mbed 0:4d4c5ea17d86 52 // Waiting for the start of the response signal
s_inoue_mbed 0:4d4c5ea17d86 53 t.reset();
s_inoue_mbed 0:4d4c5ea17d86 54 t.start();
s_inoue_mbed 0:4d4c5ea17d86 55 do {
s_inoue_mbed 0:4d4c5ea17d86 56 if (t.read_us() > 100) {
s_inoue_mbed 0:4d4c5ea17d86 57 io.input();
s_inoue_mbed 0:4d4c5ea17d86 58 io = 1;
s_inoue_mbed 0:4d4c5ea17d86 59 return NOT_PRESENT;
s_inoue_mbed 0:4d4c5ea17d86 60 }
s_inoue_mbed 0:4d4c5ea17d86 61 } while (io == 1);
s_inoue_mbed 0:4d4c5ea17d86 62
s_inoue_mbed 0:4d4c5ea17d86 63 // Wainting for the start of the ready signal
s_inoue_mbed 0:4d4c5ea17d86 64 t.reset();
s_inoue_mbed 0:4d4c5ea17d86 65 t.start();
s_inoue_mbed 0:4d4c5ea17d86 66 do {
s_inoue_mbed 0:4d4c5ea17d86 67 if (t.read_us() > 100) {
s_inoue_mbed 0:4d4c5ea17d86 68 io.input();
s_inoue_mbed 0:4d4c5ea17d86 69 io = 1;
s_inoue_mbed 0:4d4c5ea17d86 70 return NOT_READY;
s_inoue_mbed 0:4d4c5ea17d86 71 }
s_inoue_mbed 0:4d4c5ea17d86 72 } while (io == 0);
s_inoue_mbed 0:4d4c5ea17d86 73
s_inoue_mbed 0:4d4c5ea17d86 74 // Wainting for the end of the ready signal
s_inoue_mbed 0:4d4c5ea17d86 75 t.reset();
s_inoue_mbed 0:4d4c5ea17d86 76 t.start();
s_inoue_mbed 0:4d4c5ea17d86 77 do {} while (io == 1);
s_inoue_mbed 0:4d4c5ea17d86 78
s_inoue_mbed 0:4d4c5ea17d86 79 // Starting the pulse width sensing
s_inoue_mbed 0:4d4c5ea17d86 80
s_inoue_mbed 0:4d4c5ea17d86 81 io_irq.enable_irq();
s_inoue_mbed 0:4d4c5ea17d86 82
s_inoue_mbed 0:4d4c5ea17d86 83 do {
s_inoue_mbed 0:4d4c5ea17d86 84 wait_us(100);
s_inoue_mbed 0:4d4c5ea17d86 85 if (wdt > 50) {
s_inoue_mbed 0:4d4c5ea17d86 86 return WATCHDOG_ERR;
s_inoue_mbed 0:4d4c5ea17d86 87 }
s_inoue_mbed 0:4d4c5ea17d86 88 wdt++;
s_inoue_mbed 0:4d4c5ea17d86 89 } while (eod == false);
s_inoue_mbed 0:4d4c5ea17d86 90
s_inoue_mbed 0:4d4c5ea17d86 91 // Calculating the check sum
s_inoue_mbed 0:4d4c5ea17d86 92 chksum = ((data & 0xff00000000) >> 32)
s_inoue_mbed 0:4d4c5ea17d86 93 + ((data & 0x00ff000000) >> 24)
s_inoue_mbed 0:4d4c5ea17d86 94 + ((data & 0x0000ff0000) >> 16)
s_inoue_mbed 0:4d4c5ea17d86 95 + ((data & 0x000000ff00) >> 8);
s_inoue_mbed 0:4d4c5ea17d86 96 if (chksum != (data & 0x00000000ff)) {
s_inoue_mbed 0:4d4c5ea17d86 97 io.input();
s_inoue_mbed 0:4d4c5ea17d86 98 io = 1;
s_inoue_mbed 0:4d4c5ea17d86 99 return CHKSUM_ERR;
s_inoue_mbed 0:4d4c5ea17d86 100 } else {
s_inoue_mbed 0:4d4c5ea17d86 101 t.reset();
s_inoue_mbed 0:4d4c5ea17d86 102 first_time = false;
s_inoue_mbed 0:4d4c5ea17d86 103 return OK;
s_inoue_mbed 0:4d4c5ea17d86 104 }
s_inoue_mbed 0:4d4c5ea17d86 105 }
s_inoue_mbed 0:4d4c5ea17d86 106
s_inoue_mbed 0:4d4c5ea17d86 107 // Extracting humidity data from the received data
s_inoue_mbed 0:4d4c5ea17d86 108 int DHT11::readHumidity()
s_inoue_mbed 0:4d4c5ea17d86 109 {
s_inoue_mbed 0:4d4c5ea17d86 110 if (err == OK) {
s_inoue_mbed 0:4d4c5ea17d86 111 return (data & 0xff00000000) >> 32;
s_inoue_mbed 0:4d4c5ea17d86 112 } else {
s_inoue_mbed 0:4d4c5ea17d86 113 return 0xffffffff;
s_inoue_mbed 0:4d4c5ea17d86 114 }
s_inoue_mbed 0:4d4c5ea17d86 115 }
s_inoue_mbed 0:4d4c5ea17d86 116
s_inoue_mbed 0:4d4c5ea17d86 117 // Extracting temperature data from the received data
s_inoue_mbed 0:4d4c5ea17d86 118 int DHT11::readTemperature()
s_inoue_mbed 0:4d4c5ea17d86 119 {
s_inoue_mbed 0:4d4c5ea17d86 120 if (err == OK) {
s_inoue_mbed 0:4d4c5ea17d86 121 return (data & 0x0000ff0000) >> 16;
s_inoue_mbed 0:4d4c5ea17d86 122 } else {
s_inoue_mbed 0:4d4c5ea17d86 123 return 0xffffffff;
s_inoue_mbed 0:4d4c5ea17d86 124 }
s_inoue_mbed 0:4d4c5ea17d86 125 }
s_inoue_mbed 0:4d4c5ea17d86 126
s_inoue_mbed 0:4d4c5ea17d86 127 // Initialization of variables
s_inoue_mbed 0:4d4c5ea17d86 128 void DHT11::init(void)
s_inoue_mbed 0:4d4c5ea17d86 129 {
s_inoue_mbed 0:4d4c5ea17d86 130 t_pulse_us = 0;
s_inoue_mbed 0:4d4c5ea17d86 131 first_time = true;
s_inoue_mbed 0:4d4c5ea17d86 132 data = 0;
s_inoue_mbed 0:4d4c5ea17d86 133 chksum = 0;
s_inoue_mbed 0:4d4c5ea17d86 134 cnt = 0;
s_inoue_mbed 0:4d4c5ea17d86 135 wdt = 0;
s_inoue_mbed 0:4d4c5ea17d86 136 err = OK;
s_inoue_mbed 0:4d4c5ea17d86 137 eod = false;
s_inoue_mbed 0:4d4c5ea17d86 138 }
s_inoue_mbed 0:4d4c5ea17d86 139
s_inoue_mbed 0:4d4c5ea17d86 140 void DHT11::pos_edge(void)
s_inoue_mbed 0:4d4c5ea17d86 141 {
s_inoue_mbed 0:4d4c5ea17d86 142 // Disabling the interruptions
s_inoue_mbed 0:4d4c5ea17d86 143 io_irq.disable_irq();
s_inoue_mbed 0:4d4c5ea17d86 144
s_inoue_mbed 0:4d4c5ea17d86 145 // Initializing the Timer
s_inoue_mbed 0:4d4c5ea17d86 146 t.reset();
s_inoue_mbed 0:4d4c5ea17d86 147
s_inoue_mbed 0:4d4c5ea17d86 148 // Enabling the interruptions
s_inoue_mbed 0:4d4c5ea17d86 149 io_irq.enable_irq();
s_inoue_mbed 0:4d4c5ea17d86 150 }
s_inoue_mbed 0:4d4c5ea17d86 151
s_inoue_mbed 0:4d4c5ea17d86 152 void DHT11::neg_edge(void)
s_inoue_mbed 0:4d4c5ea17d86 153 {
s_inoue_mbed 0:4d4c5ea17d86 154 // Disabling the interruptions
s_inoue_mbed 0:4d4c5ea17d86 155 io_irq.disable_irq();
s_inoue_mbed 0:4d4c5ea17d86 156
s_inoue_mbed 0:4d4c5ea17d86 157 // Reading the length of
s_inoue_mbed 0:4d4c5ea17d86 158 t_pulse_us = t.read_us();
s_inoue_mbed 0:4d4c5ea17d86 159
s_inoue_mbed 0:4d4c5ea17d86 160 // Detecting 0 if the pulse width ranges from 26 us to 28 us
s_inoue_mbed 0:4d4c5ea17d86 161 if (20 <= t_pulse_us && t_pulse_us <= 30) {
s_inoue_mbed 0:4d4c5ea17d86 162 // Shifting the data buffer and not adding 1 (because this bit is zero)
s_inoue_mbed 0:4d4c5ea17d86 163 data = data << 1;
s_inoue_mbed 0:4d4c5ea17d86 164
s_inoue_mbed 0:4d4c5ea17d86 165 // Counting up the bits
s_inoue_mbed 0:4d4c5ea17d86 166 cnt++;
s_inoue_mbed 0:4d4c5ea17d86 167 }
s_inoue_mbed 0:4d4c5ea17d86 168
s_inoue_mbed 0:4d4c5ea17d86 169 // Detecting 1 if the pulse width ranges from 68 us to 72 us
s_inoue_mbed 0:4d4c5ea17d86 170 else if (60 <= t_pulse_us && t_pulse_us <= 80) {
s_inoue_mbed 0:4d4c5ea17d86 171 // Shifting the data buffer and adding 1 (because this bit is one)
s_inoue_mbed 0:4d4c5ea17d86 172 data = data << 1;
s_inoue_mbed 0:4d4c5ea17d86 173 data++;
s_inoue_mbed 0:4d4c5ea17d86 174
s_inoue_mbed 0:4d4c5ea17d86 175 // Counting up the bits
s_inoue_mbed 0:4d4c5ea17d86 176 cnt++;
s_inoue_mbed 0:4d4c5ea17d86 177 }
s_inoue_mbed 0:4d4c5ea17d86 178
s_inoue_mbed 0:4d4c5ea17d86 179 // Detecting the end of Data
s_inoue_mbed 0:4d4c5ea17d86 180 if (cnt < 40) {
s_inoue_mbed 0:4d4c5ea17d86 181 // Enabling the interruptions
s_inoue_mbed 0:4d4c5ea17d86 182 io_irq.enable_irq();
s_inoue_mbed 0:4d4c5ea17d86 183 } else {
s_inoue_mbed 0:4d4c5ea17d86 184 eod = true;
s_inoue_mbed 0:4d4c5ea17d86 185 }
s_inoue_mbed 0:4d4c5ea17d86 186 }