|
| 1 | +/** |
| 2 | + * @file DHT.h |
| 3 | + * @version 1.0 |
| 4 | + * |
| 5 | + * @section License |
| 6 | + * Copyright (C) 2017, Mikael Patel |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef DHT_H |
| 20 | +#define DHT_H |
| 21 | + |
| 22 | +#include "GPIO.h" |
| 23 | + |
| 24 | +/** |
| 25 | + * DHT Humidity & Temperature Sensor device driver template class. |
| 26 | + * @param[in] DATA_PIN board pin for data signal. |
| 27 | + * @param[in] DEVICE type number (11, 21 or 22). |
| 28 | + * @section Circuit |
| 29 | + * @code |
| 30 | + * DHTXX |
| 31 | + * +------------+ |
| 32 | + * (VCC)---------------1-|VCC ====== | |
| 33 | + * (DATA_PIN)----------2-|DATA ====== | |
| 34 | + * 3-| ====== | |
| 35 | + * (GND)---------------4-|GND ====== | |
| 36 | + * +------------+ |
| 37 | + * @endcode |
| 38 | + * Connect DHT to data pin, VCC and ground. A pullup resistor from |
| 39 | + * the pin to VCC should be used. Most DHT modules have a built-in |
| 40 | + * pullup resistor. |
| 41 | + */ |
| 42 | +template<BOARD::pin_t DATA_PIN, uint8_t DEVICE> |
| 43 | +class DHT { |
| 44 | +public: |
| 45 | + /** |
| 46 | + * Initiate device communication pin. |
| 47 | + */ |
| 48 | + DHT() : |
| 49 | + m_humidity(0), |
| 50 | + m_temperature(0) |
| 51 | + { |
| 52 | + m_data.input(); |
| 53 | + m_data.low(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Return latest humidity reading. |
| 58 | + * @return humidity. |
| 59 | + */ |
| 60 | + float humidity() |
| 61 | + { |
| 62 | + return (m_humidity); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Return latest temperature reading. |
| 67 | + * @return temperature. |
| 68 | + */ |
| 69 | + float temperature() |
| 70 | + { |
| 71 | + return (m_temperature); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Read humidity and temperature from device. Returns number of changed |
| 76 | + * values (0-2) otherwise negative error code (-1: check sum error, |
| 77 | + * -2: response pulse error) |
| 78 | + */ |
| 79 | + int read(float& humidity, float& temperature) |
| 80 | + { |
| 81 | + // Issue start signal and wait for device to respond |
| 82 | + m_data.output(); |
| 83 | + delay(START_SIGNAL); |
| 84 | + m_data.input(); |
| 85 | + uint8_t retry = 16; |
| 86 | + do { |
| 87 | + delayMicroseconds(PULLUP); |
| 88 | + } while (m_data != 0 && --retry); |
| 89 | + if (retry == 0 || m_data.pulse() < THRESHOLD) return (-2); |
| 90 | + |
| 91 | + // Read data from the device. Each bit is pulse width coded; |
| 92 | + // low for 50 us, and high 26-28 us for zero(0), and 70 us for one(1). |
| 93 | + uint8_t chsum = 0; |
| 94 | + uint8_t d[5]; |
| 95 | + for (int i = 0; i < 5; i++) { |
| 96 | + uint8_t v = 0; |
| 97 | + for (int j = 0; j < 8; j++) |
| 98 | + v = (v << 1) | (m_data.pulse() > THRESHOLD); |
| 99 | + d[i] = v; |
| 100 | + if (i < 4) chsum += v; |
| 101 | + } |
| 102 | + |
| 103 | + // Validate check sum |
| 104 | + if (chsum != d[4]) return (-1); |
| 105 | + |
| 106 | + // Check type of device and convert data |
| 107 | + if (DEVICE == 11) { |
| 108 | + humidity = d[0]; |
| 109 | + temperature = d[2]; |
| 110 | + } |
| 111 | + else { |
| 112 | + humidity = ((d[0] << 8) | d[1]) / 10.0; |
| 113 | + temperature = (((d[2] & 0x7f) << 8) | d[3]) / 10.0; |
| 114 | + if (d[2] & 0x80) temperature *= -1; |
| 115 | + } |
| 116 | + |
| 117 | + // Check for value changes |
| 118 | + int res = (humidity != m_humidity) + (temperature != m_temperature); |
| 119 | + if (res > 0) { |
| 120 | + m_humidity = humidity; |
| 121 | + m_temperature = temperature; |
| 122 | + } |
| 123 | + |
| 124 | + // Return number of changed values: 0, 1 och 2 |
| 125 | + return (res); |
| 126 | + } |
| 127 | + |
| 128 | +protected: |
| 129 | + /** Start signal pulse width in milliseconds. */ |
| 130 | + static const uint16_t START_SIGNAL = 18; |
| 131 | + |
| 132 | + /** Pullup delay in microseconds. */ |
| 133 | + static const uint16_t PULLUP = 4; |
| 134 | + |
| 135 | + /** Pulse threshold in microseconds. */ |
| 136 | + static const int THRESHOLD = 60; |
| 137 | + |
| 138 | + /** Board pin for data communication. */ |
| 139 | + GPIO<DATA_PIN> m_data; |
| 140 | + |
| 141 | + /** Latest humidity reading. */ |
| 142 | + float m_humidity; |
| 143 | + |
| 144 | + /** Latest temperature reading. */ |
| 145 | + float m_temperature; |
| 146 | +}; |
| 147 | + |
| 148 | +/** |
| 149 | + * DHT11 Humidity & Temperature Sensor device driver template class. |
| 150 | + * @param[in] DATA_PIN board pin for data signal. |
| 151 | + */ |
| 152 | +template<BOARD::pin_t DATA_PIN> class DHT11 : public DHT<DATA_PIN, 11> {}; |
| 153 | + |
| 154 | +/** |
| 155 | + * DHT21 Humidity & Temperature Sensor device driver template class. |
| 156 | + * @param[in] DATA_PIN board pin for data signal. |
| 157 | + */ |
| 158 | +template<BOARD::pin_t DATA_PIN> class DHT21 : public DHT<DATA_PIN, 21> {}; |
| 159 | + |
| 160 | +/** |
| 161 | + * DHT22 Humidity & Temperature Sensor device driver template class. |
| 162 | + * @param[in] DATA_PIN board pin for data signal. |
| 163 | + */ |
| 164 | +template<BOARD::pin_t DATA_PIN> class DHT22 : public DHT<DATA_PIN, 22> {}; |
| 165 | +#endif |
0 commit comments