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:48:35 2014 +0000
Revision:
7:50f5c8efd967
Parent:
6:257e2ab66d0f
Child:
8:160047ca45bf
readData() has been simplified.

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