-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftatadevice_dht11.cpp
More file actions
219 lines (193 loc) · 4.65 KB
/
softatadevice_dht11.cpp
File metadata and controls
219 lines (193 loc) · 4.65 KB
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
#include "SoftataDevice_environsensors.h"
#include <arduino.h>
#include <dht.h>
#include "SoftataDeviceInfo.h"
#ifdef SoftataDevice_RPI_PICO_SHIELD
#define NUM_PINS 3
#define DEFAULT_PIN DHT11Pin_D
#define PINS 16,18,20
#define pins "16,18,20"
int Pins[] = {PINS};
#elif defined(RPI_PICO_DEFAULT)
#define DEFAULT_PIN DHT11Pin_D
#define PIN_START 0
#define PIN_END 26
#define pins "Any of GPIO 0 to 26"
#endif
SoftataDevice_DHT11::SoftataDevice_DHT11()
{
SoftataDevice_Sensor::num_properties = NUM_DHT11_PROPERTIES;
SoftataDevice_Sensor::sensorType = DHT11;
}
SoftataDevice_DHT11::SoftataDevice_DHT11(int pin)
{
Pin = pin;
SetupPin(Pin);
SoftataDevice_Sensor::num_properties = NUM_DHT11_PROPERTIES;
SoftataDevice_Sensor::sensorType = DHT11;
}
String SoftataDevice_DHT11::GetPins()
{
String _pins = String("DHT11 Sensor Pins:");
_pins.concat(pins);
_pins.concat(" Default:");
_pins.concat(DEFAULT_PIN);
Serial.println(_pins);
return String(_pins); //pins;
}
bool SoftataDevice_DHT11::SetupPin(int pin)
{
// Check that the nominated pin is valid
bool found = false;
#ifdef SoftataDevice_RPI_PICO_SHIELD
for (int i=0;i<NUM_PINS;i++)
{
if(pin == Pins[i])
{
found= true;
break;
}
}
#elif defined(RPI_PICO_DEFAULT)
if ((pin>=PIN_START) && (pin<=PIN_END))
found = true;
#endif
if(!found)
return false;
//Set the DHT11 pin
Pin = pin;
while(!Serial){delay(100);};
Serial.println("DHT Grove");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
//Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
return true;
}
bool SoftataDevice_DHT11::Setup(byte * settings, byte numSettings)
{
if(numSettings>0)
{
Pin = settings[0];
return SetupPin(Pin);
}
else
return false;
}
bool SoftataDevice_DHT11::Setup()
{
Pin = DEFAULT_PIN;
return SetupPin(Pin);
num_properties = 2;
}
Tristate SoftataDevice_DHT11::ReadAll(double * values)
{
dht DHT;
// READ DATA
Serial.print("DHT11:");
// Allow 3 retries
bool OK = false;
for (int i=0;i<4;i++)
{
int chk = DHT.read11(Pin);
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
OK = true;
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
if(OK)
break;
delay(100);
if(OK)
break;
}
if(!OK)
return (Tristate)false;
// DISPLAY DATA
values[0] = DHT.temperature;
values[1] = DHT.humidity;
return (Tristate)true;
}
String SoftataDevice_DHT11::GetTelemetry()
{
double values[2];
if(ReadAll(values))
{
String msg ="{\"temperature\":";
msg.concat(values[0]);
msg.concat(',');
msg.concat("\"humidity\":");
msg.concat(values[1]);
msg.concat('}');
return msg;
}
else
return "ERRORDBL";
}
CallbackInfo * SoftataDevice_DHT11::GetCallbackInfo()
{
info.period = 314159;
info.next = 137;
info.SensorIndex = 123456;
info.sendBT = false;
info.isSensor=false;
info.isRunning=false;
return &info;
}
double SoftataDevice_DHT11::Read(int property)
{
dht DHT;
// READ DATA
Serial.print("DHT11:");
// Allow 3 retries
bool OK = false;
for (int i=0;i<4;i++)
{
int chk = DHT.read11(Pin);
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK,");
OK = true;
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
if(OK)
break;
delay(100);
}
if(!OK)
return ERRORDBL;
// DISPLAY DATA
switch(property)
{
case 0:
return DHT.temperature;
break;
case 1:
return DHT.humidity;
break;
default:
return ERRORDBL;
break;
}
}