|
| 1 | +/** |
| 2 | + * DHT11.h DHT11.cpp |
| 3 | + * Header file for the DHT11 library, providing functionalities to interface with |
| 4 | + * the DHT11 temperature & humidity sensor. |
| 5 | + * |
| 6 | + * Author: Dhruba Saha |
| 7 | + * Version: 2.1.0 |
| 8 | + * License: MIT |
| 9 | + * 241016 Anthony: |
| 10 | + * Add float temp return. |
| 11 | + */ |
| 12 | +#ifndef MINIR4_DHT11_H |
| 13 | +#define MINIR4_DHT11_H |
| 14 | + |
| 15 | +#include <Arduino.h> |
| 16 | + |
| 17 | +template <uint8_t PIN1, uint8_t PIN2> class MiniR4DHT11 { |
| 18 | +public: |
| 19 | + MiniR4DHT11() { |
| 20 | + _pin = PIN2; //目前都在DI_R位置,未來可以改成LEFT跟MATRIX現行感應器一樣 |
| 21 | + pinMode(_pin, OUTPUT); |
| 22 | + digitalWrite(_pin, HIGH); |
| 23 | + } |
| 24 | + |
| 25 | + static const int ERROR_CHECKSUM = 254; // Error code for checksum mismatch. |
| 26 | + static const int ERROR_TIMEOUT = 253; // Error code for timeout. |
| 27 | + // static const int ERROR_NOT_READY = 252; // Error code for not ready (delay not passed). |
| 28 | + static const int TIMEOUT_DURATION = 1000; // Timeout duration in milliseconds. |
| 29 | + |
| 30 | + /** |
| 31 | + * Sets the delay between consecutive sensor readings. |
| 32 | + * Default is 250ms if not set. |
| 33 | + */ |
| 34 | + void setDelay(unsigned long delay) { |
| 35 | + _delayMS = delay; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Reads and returns the temperature from the DHT11 sensor. |
| 40 | + * |
| 41 | + * @return Temperature in Celsius, or error codes if reading fails. |
| 42 | + */ |
| 43 | + float readTemperature() { |
| 44 | + byte data[5]; |
| 45 | + int error = readRawData(data); |
| 46 | + if (error != 0) { |
| 47 | + return error; |
| 48 | + } |
| 49 | + |
| 50 | + float t = (float)data[3] / 10; |
| 51 | + return data[2] + t; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Reads and returns the humidity from the DHT11 sensor. |
| 56 | + * |
| 57 | + * @return Humidity percentage, or error codes if reading fails. |
| 58 | + */ |
| 59 | + int readHumidity() { |
| 60 | + byte data[5]; |
| 61 | + int error = readRawData(data); |
| 62 | + if (error != 0) { |
| 63 | + return error; |
| 64 | + } |
| 65 | + |
| 66 | + return data[0]; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Reads temperature and humidity. |
| 71 | + * |
| 72 | + * @param temperature Reference to store temperature. |
| 73 | + * @param humidity Reference to store humidity. |
| 74 | + * @return 0 on success, otherwise error code. |
| 75 | + */ |
| 76 | + int readTemperatureHumidity(int &temperature, int &humidity) { |
| 77 | + byte data[5]; |
| 78 | + int error = readRawData(data); |
| 79 | + if (error != 0) { |
| 80 | + return error; |
| 81 | + } |
| 82 | + |
| 83 | + humidity = data[0]; |
| 84 | + float t = (float)data[3] / 10; |
| 85 | + temperature = data[2] + t; |
| 86 | + return 0; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Converts error codes into human-readable strings. |
| 91 | + * |
| 92 | + * @param errorCode Error code to convert. |
| 93 | + * @return String describing the error. |
| 94 | + */ |
| 95 | + String getErrorString(int errorCode) { |
| 96 | + switch (errorCode) { |
| 97 | + case ERROR_TIMEOUT: |
| 98 | + return "Error 253: Timeout reading from DHT11."; |
| 99 | + case ERROR_CHECKSUM: |
| 100 | + return "Error 254: Checksum mismatch reading from DHT11."; |
| 101 | + // case ERROR_NOT_READY: |
| 102 | + // return "Error 252: Not ready to read from DHT11. Delay not passed."; |
| 103 | + default: |
| 104 | + return "Error: Unknown."; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +private: |
| 109 | + int _pin; // Pin number for the DHT11 sensor. |
| 110 | + unsigned long _delayMS = 250; // Delay between readings. |
| 111 | + |
| 112 | + /** |
| 113 | + * Sends a start signal to the DHT11 sensor. |
| 114 | + */ |
| 115 | + void startSignal() { |
| 116 | + pinMode(_pin, OUTPUT); |
| 117 | + digitalWrite(_pin, LOW); |
| 118 | + delay(18); |
| 119 | + digitalWrite(_pin, HIGH); |
| 120 | + delayMicroseconds(40); |
| 121 | + pinMode(_pin, INPUT); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Reads raw data from the DHT11 sensor. |
| 126 | + * |
| 127 | + * @param data Array to store the raw data. |
| 128 | + * @return 0 on success, otherwise error code. |
| 129 | + */ |
| 130 | + int readRawData(byte data[5]) { |
| 131 | + delay(_delayMS); |
| 132 | + // if (millis() - _lastReadTime < _delayMS) { |
| 133 | + // return ERROR_NOT_READY; // if not reach delayMS, bypass. |
| 134 | + // } |
| 135 | + |
| 136 | + startSignal(); |
| 137 | + unsigned long timeout_start = millis(); |
| 138 | + |
| 139 | + while (digitalRead(_pin) == HIGH) { |
| 140 | + if (millis() - timeout_start > TIMEOUT_DURATION) { |
| 141 | + return ERROR_TIMEOUT; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + delayMicroseconds(80); |
| 146 | + if (digitalRead(_pin) == HIGH) { |
| 147 | + delayMicroseconds(80); |
| 148 | + for (int i = 0; i < 5; i++) { |
| 149 | + data[i] = readByte(); |
| 150 | + } |
| 151 | + |
| 152 | + if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) { |
| 153 | + // _lastReadTime = millis(); // update daley timer |
| 154 | + return 0; // Success |
| 155 | + } else { |
| 156 | + return ERROR_CHECKSUM; |
| 157 | + } |
| 158 | + } |
| 159 | + return ERROR_TIMEOUT; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Reads a byte of data from the sensor. |
| 164 | + * |
| 165 | + * @return The byte read from the sensor. |
| 166 | + */ |
| 167 | + byte readByte() { |
| 168 | + byte value = 0; |
| 169 | + for (int i = 0; i < 8; i++) { |
| 170 | + while (digitalRead(_pin) == LOW); |
| 171 | + delayMicroseconds(30); |
| 172 | + if (digitalRead(_pin) == HIGH) { |
| 173 | + value |= (1 << (7 - i)); |
| 174 | + } |
| 175 | + while (digitalRead(_pin) == HIGH); |
| 176 | + } |
| 177 | + return value; |
| 178 | + } |
| 179 | +}; |
| 180 | + |
| 181 | +#endif // MINIR4_DHT11_H |
0 commit comments