MatrixMiniR4 1.1.5
Matrix Mini R4 Arduino Library API Documentation
Loading...
Searching...
No Matches
MiniR4_DHT11.h
Go to the documentation of this file.
1
15#ifndef MINIR4_DHT11_H
16#define MINIR4_DHT11_H
17
18#include <Arduino.h>
19
30template <uint8_t PIN1, uint8_t PIN2>
32{
33public:
35 {
36 _pin = PIN2; //PIN設定在DI_R位置,通過MiniR4Digital_h模板Pin1_2順序來初始化Grove或MATRIX物件
37 pinMode(_pin, OUTPUT);
38 digitalWrite(_pin, HIGH);
39 }
40
41 static const int ERROR_CHECKSUM = 254; // Error code for checksum mismatch.
42 static const int ERROR_TIMEOUT = 253; // Error code for timeout.
43 // static const int ERROR_NOT_READY = 252; // Error code for not ready (delay not passed).
44 static const int TIMEOUT_DURATION = 1000; // Timeout duration in milliseconds.
45
53 void setDelay(unsigned long delay)
54 {
55 _delayMS = delay;
56 }
57
67 {
68 byte data[5];
69 int error = readRawData(data);
70 if (error != 0)
71 {
72 return error;
73 }
74
75 float t = (float)data[3] / 10;
76 return data[2] + t;
77 }
78
88 {
89 byte data[5];
90 int error = readRawData(data);
91 if (error != 0)
92 {
93 return error;
94 }
95
96 return data[0];
97 }
98
106 int readTemperatureHumidity(float &temperature, int &humidity)
107 {
108 byte data[5];
109 int error = readRawData(data);
110 if (error != 0)
111 {
112 return error;
113 }
114
115 humidity = data[0];
116 float t = (float)data[3] / 10;
117 temperature = data[2] + t;
118 return 0;
119 }
120
127 String getErrorString(int errorCode)
128 {
129 switch (errorCode)
130 {
131 case ERROR_TIMEOUT:
132 return "Error 253: Timeout reading from DHT11.";
133 case ERROR_CHECKSUM:
134 return "Error 254: Checksum mismatch reading from DHT11.";
135 // case ERROR_NOT_READY:
136 // return "Error 252: Not ready to read from DHT11. Delay not passed.";
137 default:
138 return "Error: Unknown.";
139 }
140 }
141
142private:
143 int _pin; // Pin number for the DHT11 sensor.
144 unsigned long _delayMS = 250; // Delay between readings.
145
149 void startSignal()
150 {
151 pinMode(_pin, OUTPUT);
152 digitalWrite(_pin, LOW);
153 delay(18);
154 digitalWrite(_pin, HIGH);
155 delayMicroseconds(40);
156 pinMode(_pin, INPUT);
157 }
158
165 int readRawData(byte data[5])
166 {
167 delay(_delayMS);
168 // if (millis() - _lastReadTime < _delayMS) {
169 // return ERROR_NOT_READY; // if not reach delayMS, bypass.
170 // }
171
172 startSignal();
173 unsigned long timeout_start = millis();
174
175 while (digitalRead(_pin) == HIGH)
176 {
177 if (millis() - timeout_start > TIMEOUT_DURATION)
178 {
179 return ERROR_TIMEOUT;
180 }
181 }
182
183 delayMicroseconds(80);
184 if (digitalRead(_pin) == HIGH)
185 {
186 delayMicroseconds(80);
187 for (int i = 0; i < 5; i++)
188 {
189 data[i] = readByte();
190 }
191
192 if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))
193 {
194 // _lastReadTime = millis(); // update daley timer
195 return 0; // Success
196 }
197 else
198 {
199 return ERROR_CHECKSUM;
200 }
201 }
202 return ERROR_TIMEOUT;
203 }
204
210 byte readByte()
211 {
212 byte value = 0;
213 for (int i = 0; i < 8; i++)
214 {
215 while (digitalRead(_pin) == LOW)
216 ;
217 delayMicroseconds(30);
218 if (digitalRead(_pin) == HIGH)
219 {
220 value |= (1 << (7 - i));
221 }
222 while (digitalRead(_pin) == HIGH)
223 ;
224 }
225 return value;
226 }
227};
228
229#endif // MINIR4_DHT11_H
A template class to interface with the DHT11 temperature & humidity sensor.
static const int ERROR_TIMEOUT
static const int ERROR_CHECKSUM
int readHumidity()
Reads and returns the humidity from the DHT11 sensor.
static const int TIMEOUT_DURATION
float readTemperature()
Reads and returns the temperature from the DHT11 sensor.
String getErrorString(int errorCode)
int readTemperatureHumidity(float &temperature, int &humidity)
void setDelay(unsigned long delay)
Sets the delay between consecutive sensor readings.