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 17:43:14 2014 +0000
Revision:
6:257e2ab66d0f
Parent:
4:48798b126d93
Child:
7:50f5c8efd967
Reorganized initialization function

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