-
Notifications
You must be signed in to change notification settings - Fork 19
/
Sensor.h
415 lines (330 loc) · 10.7 KB
/
Sensor.h
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#ifndef Sensor_h
#define Sensor_h
#include <SoftwareSerial.h>
#include "DHT.h"
//Establish Serial Ports (RX, TX)
SoftwareSerial Serial4 (13, 12);
SoftwareSerial Serial5 (69, 68);
SoftwareSerial Serial6 (65, 64);
//Read the gas density command /Don't change the order
unsigned char hexdata[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
//DHT Temperature and Humidity Sensor
#define DHTPIN 53
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
class SENSOR {
private:
String commandString;
float waterTemp;
float conductivity;
float pH;
float oxygen;
float humidity;
float airTemp;
long carbon;
float par;
public:
//Constructor
SENSOR(){}
/************************************************************************************************
MEMBER FUNCTIONS
************************************************************************************************/
//GET A COMMAND FROM THE USER
String getCommand() {
boolean commandStringComplete = false;
Serial.print("Enter a command: ");
while (commandStringComplete == false) {
commandString = Serial.readStringUntil(13);
if (commandString.length() > 0) {
commandStringComplete = true;
}
}
Serial.println(commandString);
return commandString;
}
/*************************************************************************************************
SEND FUNCTIONS
**************************************************************************************************/
//SEND COMMAND TO WATER TEMPERATURE SENSOR (WT)
void WTsendCommand() {
Serial1.print(commandString);
Serial1.print('\r');
Serial.print("Response from WT: ");
String WTsendString = Serial1.readStringUntil(13);
Serial.println(WTsendString);
WTsendString = "";
WTsendString = Serial1.readStringUntil(13);
Serial.println(WTsendString);
Serial.println();
WTsendString = "";
WTsendString = Serial1.readStringUntil(13);
WTsendString = "";
commandString = "";
}
//SEND COMMAND TO CONDUCTIVITY SENSOR (EC)
void ECsendCommand(){
Serial2.print(commandString);
Serial2.print('\r');
Serial.print("Response from EC: ");
String ECsendString = Serial2.readStringUntil(13);
Serial.println(ECsendString);
ECsendString = "";
ECsendString = Serial2.readStringUntil(13);
Serial.println(ECsendString);
Serial.println();
ECsendString = "";
ECsendString = Serial2.readStringUntil(13);
ECsendString = "";
commandString = "";
}
//SEND COMMAND TO PH SENSOR (PH)
void PHsendCommand() {
Serial3.print(commandString);
Serial3.print('\r');
Serial.print("Response from PH: ");
String PHsendString = Serial3.readStringUntil(13);
Serial.println(PHsendString);
PHsendString = "";
PHsendString = Serial3.readStringUntil(13);
Serial.println(PHsendString);
Serial.println();
PHsendString = "";
PHsendString = Serial3.readStringUntil(13);
PHsendString = "";
commandString = "";
}
//SEND COMMAND TO DISSOLVED OXYGEN SENSOR (DO)
void DOsendCommand() {
Serial4.listen();
Serial4.print(commandString);
Serial4.print('\r');
Serial.print("Response from DO: ");
String DOsendString = Serial4.readStringUntil(13);
Serial.println(DOsendString);
DOsendString = "";
DOsendString = Serial4.readStringUntil(13);
Serial.println(DOsendString);
Serial.println();
DOsendString = "";
DOsendString = Serial4.readStringUntil(13);
DOsendString = "";
commandString = "";
}
/************************************************************************************************
READ FUNCTIONS
************************************************************************************************/
//READ DATA FROM WATER TEMPERATURE SENSOR (WT)
float readWT() {
Serial.print("Reading WT... ");
//Send command for single reading
Serial1.print('R');
Serial1.print('\r');
//Get reading
String WTreadString = Serial1.readStringUntil(13);
waterTemp = WTreadString.toFloat();
WTreadString = "";
//Get response. Prints *OK if read.
WTreadString = Serial1.readStringUntil(13);
if (WTreadString != "*OK"){ Serial.println("Failed to read water temperature!"); }
else{ Serial.println(WTreadString); }
WTreadString = "";
return waterTemp;
}
//READ DATA FROM CONDUCTIVITY SENSOR (EC)
float readEC(){
Serial.print("Reading EC... ");
//Send command for single reading
Serial2.print('R');
Serial2.print('\r');
//Get reading
String ECreadString = Serial2.readStringUntil(13);
conductivity = ECreadString.toFloat();
ECreadString = "";
//prints *OK if read.
ECreadString = Serial2.readStringUntil(13);
if (ECreadString != "*OK"){ Serial.println("Failed to read conductivity!"); }
else{ Serial.println(ECreadString); }
ECreadString = "";
return conductivity;
}
//READ DATA FROM PH SENSOR (PH)
float readPH() {
Serial.print("Reading PH... ");
//Send command for single reading
Serial3.print('R');
Serial3.print('\r');
//Get reading
String PHreadString = Serial3.readStringUntil(13);
pH = PHreadString.toFloat();
PHreadString = "";
//prints *OK if read
PHreadString = Serial3.readStringUntil(13);
if (PHreadString != "*OK"){ Serial.println("Failed to read pH level!"); }
else{ Serial.println(PHreadString); }
PHreadString = "";
return pH;
}
//READ DATA FROM DISSOLVED OXYGEN SENSOR (DO)
float readDO() {
//Activate software serial port 4
Serial4.listen();
Serial.print("Reading DO... ");
//Send command for single reading
Serial4.print('R');
Serial4.print('\r');
//Get reading
String DOreadString = Serial4.readStringUntil(13);
oxygen = DOreadString.toFloat();
DOreadString = "";
//prints *OK if read.
DOreadString = Serial4.readStringUntil(13);
if (DOreadString != "*OK"){ Serial.println("Failed to read dissolved oxygen!"); }
else{ Serial.println(DOreadString); }
DOreadString = "";
return oxygen;
}
//READ DATA FROM HUMIDITY SENSOR (HM)
float readHM() {
Serial.print("Reading HM... ");
humidity = dht.readHumidity();
if (isnan(humidity)) { Serial.println("Failed to read humidity!"); }
else{ Serial.println("*OK"); }
return humidity;
}
//READ DATA FROM AIR TEMPERATURE SENSOR (AT)
float readAT() {
Serial.print("Reading AT... ");
airTemp = dht.readTemperature(true);
if (isnan(airTemp)) { Serial.println("Failed to read air temperature!"); }
else{ Serial.println("*OK"); }
return airTemp;
}
//READ DATA FROM CARBON DIOXIDE SENSOR (CB)
long readCB() {
//Activate software serial port 5
Serial5.listen();
Serial.print("Reading CB... ");
Serial5.write(hexdata,9);
delay(500);
for(int i=0,j=0;i<9;i++){
if (Serial5.available()>0){
long hi, lo;
int ch = Serial5.read();
if(i==2){ hi=ch; } //High concentration
if(i==3){ lo=ch; } //Low concentration
if(i==8){
carbon = hi*256+lo; //CO2 concentration
}
}
}
if (isnan(carbon)) { Serial.println("Failed to read carbon dioxide!"); }
else { Serial.println("*OK"); }
return carbon;
}
//READ DATA FROM PAR SENSOR (PR)
float readPR(){
Serial.print("Reading PR... ");
float intercept = 0;
float slope = 430;
float count = analogRead(A0);
float voltage = count / 1023 * 5.0;
par = intercept + voltage * slope;
if (par==0) { Serial.println("Failed to read PAR level!"); }
else { Serial.println("*OK"); }
return par;
}
/********************************************************************************************
PRINT FUNCTIONS
********************************************************************************************/
//PRINT WT DATA TO SERIAL MONITOR
void printWT() {
Serial.print("Water Temperature: ");
Serial.print("\t");
Serial.print(waterTemp);
Serial.println(" *F");
}
//PRINT EC DATA TO SERIAL MONITOR
void printEC (){
Serial.print("Conductivity: ");
Serial.print("\t\t");
Serial.print(conductivity);
Serial.println(" microSiemens/cm");
}
//PRINT PH DATA TO SERIAL MONITOR
void printPH() {
Serial.print("pH: ");
Serial.print("\t\t\t");
Serial.println(pH);
}
//PRINT DO DATA TO SERIAL MONITOR
void printDO() {
Serial.print("Dissolved Oxygen: ");
Serial.print("\t");
Serial.print(oxygen);
Serial.println(" mg/L");
}
//PRINT HM DATA TO SERIAL MONITOR
void printHM() {
Serial.print("Humidity: ");
Serial.print("\t\t");
Serial.print(humidity);
Serial.println(" %");
}
//PRINT AT DATA TO SERIAL MONITOR
void printAT() {
Serial.print("Air Temperature: ");
Serial.print("\t");
Serial.print(airTemp);
Serial.println(" *F");
}
//PRINT CB DATA TO SERIAL MONITOR
void printCB() {
Serial.print("CO2 Concentration: ");
Serial.print("\t");
Serial.print(carbon);
Serial.println(" ppm");
}
//PRINT PR DATA TO SERIAL MONITOR
void printPR() {
Serial.print("PAR Level: ");
Serial.print("\t\t");
Serial.print(par);
Serial.println(" micromoles/m*s");
}
/*************************************************************************************************
ACCESSOR FUNCTIONS
*************************************************************************************************/
//GET WATER TEMPERATURE
float getWaterTemp(){
return waterTemp;
}
//GET CONDUCTIVITY
float getConductivity(){
return conductivity;
}
//GET PH
float getPH(){
return pH;
}
//GET DISSOLVED OXYGEN
float getOxygen(){
return oxygen;
}
//GET HUMIDITY
float getHumidity(){
return humidity;
}
//GET AIR TEMPERATURE
float getAirTemp(){
return airTemp;
}
//GET CARBON DIOXIDE
long getCarbon(){
return carbon;
}
//GET PAR LEVEL
float getPar(){
return par;
}
};
#endif