-
Notifications
You must be signed in to change notification settings - Fork 12
/
_P005_DHT.ino
226 lines (201 loc) · 6.84 KB
/
_P005_DHT.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//#######################################################################################################
//######################## Plugin 006: Temperature and Humidity sensor DHT 11/22 ########################
//#######################################################################################################
MyMessage *msgHum005;
MyMessage *msgTemp005;
#define PLUGIN_005
#define PLUGIN_ID_005 5
#define PLUGIN_NAME_005 "Temperature & Humidity - DHT"
#define PLUGIN_VALUENAME1_005 "Temperature"
#define PLUGIN_VALUENAME2_005 "Humidity"
uint8_t Plugin_005_DHT_Pin;
boolean Plugin_005(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_005;
Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
Device[deviceCount].VType = SENSOR_TYPE_TEMP_HUM;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 2;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_005);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_005));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_005));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
String options[2];
options[0] = F("DHT 11");
options[1] = F("DHT 22");
int optionValues[2];
optionValues[0] = 11;
optionValues[1] = 22;
string += F("<TR><TD>DHT Type:<TD><select name='plugin_005_dhttype'>");
for (byte x = 0; x < 2; x++)
{
string += F("<option value='");
string += optionValues[x];
string += "'";
if (choice == optionValues[x])
string += F(" selected");
string += ">";
string += options[x];
string += F("</option>");
}
string += F("</select>");
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg("plugin_005_dhttype");
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
success = true;
break;
}
case PLUGIN_INIT:
{
if (!msgHum005)
msgHum005 = new MyMessage(event->BaseVarIndex, V_HUM);
if (!msgTemp005)
msgTemp005 = new MyMessage(event->BaseVarIndex + 1, V_TEMP);
present(event->BaseVarIndex, S_HUM);
present(event->BaseVarIndex + 1, S_TEMP);
Serial.print("Present DHT: ");
Serial.println(event->BaseVarIndex);
success = true;
break;
}
case PLUGIN_READ:
{
byte dht_dat[5];
byte dht_in;
byte i;
byte Retry = 0;
boolean error = false;
byte Par3 = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
Plugin_005_DHT_Pin = Settings.TaskDevicePin1[event->TaskIndex];
pinMode(Plugin_005_DHT_Pin, OUTPUT);
// DHT start condition, pull-down i/o pin for 18ms
digitalWrite(Plugin_005_DHT_Pin, LOW); // Pull low
delay(18);
digitalWrite(Plugin_005_DHT_Pin, HIGH); // Pull high
delayMicroseconds(40);
pinMode(Plugin_005_DHT_Pin, INPUT); // change pin to input
//delayMicroseconds(40);
dht_in = digitalRead(Plugin_005_DHT_Pin);
if (!dht_in)
{
delayMicroseconds(80);
dht_in = digitalRead(Plugin_005_DHT_Pin);
if (dht_in)
{
delayMicroseconds(40); // now ready for data reception
for (i = 0; i < 5; i++)
{
byte data = Plugin_005_read_dht_dat();
if (data != -1)
dht_dat[i] = data;
else
{
addLog(LOG_LEVEL_ERROR, (char*)"DHT : protocol timeout!");
error = true;
}
}
if (!error)
{
// Checksum calculation is a Rollover Checksum by design!
byte dht_check_sum = dht_dat[0] + dht_dat[1] + dht_dat[2] + dht_dat[3]; // check check_sum
if (dht_dat[4] == dht_check_sum)
{
if (Par3 == 11)
{
UserVar[event->BaseVarIndex] = float(dht_dat[2]); // Temperature
UserVar[event->BaseVarIndex + 1] = float(dht_dat[0]); // Humidity
}
if (Par3 == 22)
{
if (dht_dat[2] & 0x80) // negative temperature
UserVar[event->BaseVarIndex] = -0.1 * word(dht_dat[2] & 0x7F, dht_dat[3]);
else
UserVar[event->BaseVarIndex] = 0.1 * word(dht_dat[2], dht_dat[3]);
UserVar[event->BaseVarIndex + 1] = word(dht_dat[0], dht_dat[1]) * 0.1; // Humidity
}
String log = F("DHT : Temperature: ");
log += UserVar[event->BaseVarIndex];
addLog(LOG_LEVEL_INFO, log);
log = F("DHT : Humidity: ");
log += UserVar[event->BaseVarIndex + 1];
addLog(LOG_LEVEL_INFO, log);
send(msgTemp005->set(UserVar[event->BaseVarIndex], 1));
send(msgHum005->set(UserVar[event->BaseVarIndex + 1], 1));
success = true;
} // checksum
} // error
} // dht
} // !dht
if(!success)
{
UserVar[event->BaseVarIndex] = NAN;
UserVar[event->BaseVarIndex + 1] = NAN;
}
break;
}
}
return success;
}
/*********************************************************************************************\
* DHT sub to get an 8 bit value from the receiving bitstream
\*********************************************************************************************/
int Plugin_005_read_dht_dat(void)
{
byte i = 0;
byte result = 0;
byte counter = 0;
noInterrupts();
for (i = 0; i < 8; i++)
{
while ((!digitalRead(Plugin_005_DHT_Pin)) && (counter < 100))
{
delayMicroseconds(1);
counter++;
}
if (counter >= 100)
{
interrupts();
return -1;
}
delayMicroseconds(30);
if (digitalRead(Plugin_005_DHT_Pin))
result |= (1 << (7 - i));
counter = 0;
while ((digitalRead(Plugin_005_DHT_Pin)) && (counter < 100))
{
delayMicroseconds(1);
counter++;
}
if (counter >= 100)
{
interrupts();
return -1;
}
}
interrupts();
return result;
}