-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriaApplianceWithAirPurifier.ino
428 lines (384 loc) · 11.7 KB
/
TriaApplianceWithAirPurifier.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "DHT.h"
#include "MQ135.h"
#include "MATH.h"
#include <NeoSWSerial.h>
#include <string.h>
//arduino pin definition
//digital
#define DHTPIN 2
//analog
#define MQPIN 0
//sensor definitions
#define DHTTYPE DHT22
#define MDELAY 30000
#define TDELAY 500
//pins for relay activation
#define HEATERPIN 4
#define COOLERFAN 5
#define HUMIDIFIERPIN 7
#define AIRPURIFIERPIN 8
//sensor High/ Low limits
float TH = 80.0; // 80.0 <- 78.5 <- 80.0 <- 78.5
float TL = 75.0; //74.0 <- 76.0 <- 75.0 <- 73.0 <- adjestment for temp sensitivity esha/ anjali
float HH = 66.0; //66.0 <- 60.0 <- 60.0
float HL = 50.0; //45.0 <- 41.0 <- 45.0 <- 40.0 <- 57.0 <-30.0
float AH = 300.0; //300.0<-100<- 150.0 <- 10.0 <- 600.0 --change in library scale?
float AL = 0.0;
//status code and activation cd
String alertCd="";
int activationCd=-1;
String sensorCd = "";
int active = 1;
//settings send status
int sendSetting[3]={1,1,1};
//define bluetooth variables
const int rxPin = 10; //SoftwareSerial RX pin, connect to JY-MCY TX pin
const int txPin = 11; //SoftwareSerial TX pin, connect to JY-MCU RX pin
// level shifting to 3.3 volts may be needed
//initialize sensors
MQ135 gs = MQ135(MQPIN);
DHT dht(DHTPIN, DHTTYPE);
//define Software Serial with android bluetooth
NeoSWSerial triaSerial(rxPin, txPin); // RX, TX
char setbuff[50] = "";
//initialization and system setup
void setup() {
Serial.begin(9600);
delay(100);
triaSerial.begin(9600);
//initialize
Serial.println("Tria Environment Alert System Initialization");
//set pin mode
pinMode(DHTPIN, INPUT);
pinMode(MQPIN, INPUT);
//set output pins
pinMode(HEATERPIN, OUTPUT);
pinMode(COOLERFAN, OUTPUT);
pinMode(HUMIDIFIERPIN, OUTPUT);
pinMode(AIRPURIFIERPIN, OUTPUT);
//set output to off initially
digitalWrite(HEATERPIN, HIGH);
digitalWrite(COOLERFAN, HIGH);
digitalWrite(HUMIDIFIERPIN, HIGH);
digitalWrite(AIRPURIFIERPIN, HIGH);
//bigin data collection
dht.begin();
}
void loop() {
//wait few seconds between measurments
delay(MDELAY);
//Serial.println("inside 30 sec loop");
//read humidity
float humidity = dht.readHumidity();
//read temperature in celsius
float temperature = dht.readTemperature();
//read temperature in fahernheit
float ftemperature = dht.readTemperature(true);
//get calibration RZERO
float rzero = gs.getRZero();
//Serial.print("RZ: ");
//Serial.println(rzero);
//get ppm value
float gsppm = gs.getPPM();
//get temperature and humidity corrected ppm value
float gsppmc = gs.getCorrectedPPM(temperature, humidity);
//check if sensor data is valid for processing
if (isnan(humidity) || isnan(temperature) || isnan(ftemperature)) {
return;
}
//celsius heat index
float heatindex = dht.computeHeatIndex(temperature, humidity, false);
//faherenheit heat index
float fheatindex = dht.computeHeatIndex(ftemperature, humidity);
sensorCd = getSensorStatus(ftemperature, humidity, gsppmc);
//Serial.print(sensorCd);
String triaMsg = getTriaStatusMessage(sensorCd, ftemperature, humidity, gsppmc);
String statusMsg = sensorCd + ":" + alertCd + ":" + String(activationCd);
if(!statusMsg.equals("")){
Serial.println("Tria Status: " +statusMsg);
triaSerial.print(">" + statusMsg);
}
statusMsg="";
delay(TDELAY);
if (!triaMsg.equals("")) {
Serial.println("Tria Values: " + triaMsg);
triaSerial.print(triaMsg);
}
triaMsg = "";
delay(TDELAY);
if(sendSetting[0] == 1 || sendSetting[1] == 1 || sendSetting[2] == 1){
if(sendSetting[0] == 1){
Serial.println("Tria Settings: " + String(TH) + ':' + String(TL) );
triaSerial.print("R:T:" + String(TH) + ':' + String(TL));
sendSetting[0] =0;
} else if(sendSetting[1] == 1){
Serial.println("Tria Settings: " + String(HH) + ':' + String(HL) );
triaSerial.print("R:H:" + String(HH) + ':' + String(HL));
sendSetting[1] =0;
} else if (sendSetting[2] == 1){
Serial.println("Tria Settings: " + String(AH) + ':' + String(AL) );
triaSerial.print("R:A:" + String(AH) + ':' + String(AL));
sendSetting[2]=0;
}
}
byte n = triaSerial.available();
{
char* setparams[3];
char* ptr;
if (n != 0)
{
byte m = triaSerial.readBytesUntil('\n', setbuff, 50);
Serial.println("Set Property request received: " + String(setbuff));
byte index = 0;
ptr = strtok(setbuff, ":");
while(ptr != NULL){
setparams[index] = ptr;
index ++;
ptr = strtok(NULL, ":");
}
String s = processChange(setparams[0], setparams[1], atof(setparams[2]));
if (s = "S_OK"){
Serial.println("Successfully changed the property!");
Serial.print(setparams[1]);
Serial.print(" to ");
Serial.println(setparams[2]);
Serial.println("Current Property values - TH: " + String(TH) + " TL: "+ String(TL) + " HH: "+ String(HH) + " HL: "+ String(HL) + " AH: "+ String(AH));
}else{
Serial.println("Invalid change property request received!");
}
}
}
}
String processChange(String type, String deviceLevel, float value){
if (type == "S"){
if (deviceLevel == "TH"){
TH = value;
} else if (deviceLevel == "TL"){
TL = value;
} else if (deviceLevel == "HH"){
HH = value;
} else if (deviceLevel == "HL"){
HL = value;
} else if (deviceLevel == "AH"){
AH = value;
} else {
return "S_FAIL";
}
} else if( type == "C"){
if (deviceLevel == "GS"){
sendSetting[0]=1;sendSetting[1]=1;sendSetting[2]=1;
}else if(deviceLevel == "S"){
active = value;
}else {
return "S_FAIL";
}
}
return "S_OK";
}
//get unified status message for user and control devices
String getTriaStatusMessage(String sensorCd, float ftemperature, float humidity, float airppm) {
// String msg = "T:" + String(ftemperature) + ",H:" + String(humidity) + ",A:" + String(airppm) + ",C:" + sensorCd + ",M:";
String msg = String(ftemperature) + ":" + String(humidity) + ":" + String(airppm);// + ":";
//String alert;
//int activationCd = -1;
// 3st place is temperature, 2nd place is humidity & 1st place is air quality state
if (sensorCd.equals("NNN")) {
alertCd = "A"; //normal air quality, normal humidity, normal temperature
activationCd = 0;
activateDevice(0);
}
else if (sensorCd.equals("NNL")) {
alertCd = "B"; // normal air quality, normal humidity, low temperature
activationCd = 1;
activateDevice(1);
}
else if (sensorCd.equals("NLN")) {
alertCd = "C"; //normal air quality, low humidity, normal temperature
activationCd = 2;
activateDevice(2);
}
else if (sensorCd.equals("NLL")) {
alertCd = "D"; //normal air quality, low humidity, low temperature
activationCd = 3;
activateDevice(3);
}
else if (sensorCd.equals("NNH")) {
alertCd = "E"; //normal air quality, normal humidity, high temperature
activationCd = 4;
activateDevice(4);
}
else if (sensorCd.equals("NHN")) {
alertCd = "E"; //normal air quality, high humidity, normal temperature
activationCd = 4;
activateDevice(4);
}
else if (sensorCd.equals("HNN")) {
alertCd = "E"; //high/ bad air quality, normal humidity, normal temperature
activationCd = 5;
activateDevice(5); // 5 <- 4
}
else if (sensorCd.equals("HHN")) {
alertCd = "E"; // bad air quality, high humidity, notmal temperature
activationCd = 6;
activateDevice(6); // ideally de-humidifier
}
else if (sensorCd.equals("HNH")) {
alertCd = "E"; // bad air quality, normal humidity, high temperature
activationCd = 6;
activateDevice(6);
}
else if (sensorCd.equals("NHH")) {
alertCd = "E"; // normal/ low air quality, high temperature, high humidity
activationCd = 4;
activateDevice(4);
}
else if (sensorCd.equals("HHL")) {
alertCd = "F"; //bad air quality, high humidity, low temperature
activationCd = 6;
activateDevice(6);
}
else if (sensorCd.equals("HLH")) {
alertCd = "D"; // bad aq, low h, high t
activationCd = 7;
activateDevice(7);
}
else if (sensorCd.equals("HLL")) {
alertCd = "I"; //bad air quality, low h, low t
activationCd = 8;
activateDevice(8);
}
else if (sensorCd.equals("NLH")) {
alertCd = "J"; //low h, high t
activationCd = 9;
activateDevice(9);
}
else if (sensorCd.equals("NHL")) {
alertCd = "B"; //high h, low t
activationCd = 1;
activateDevice(1);
}
else if (sensorCd.equals("HLN")) {
alertCd = "K"; //bad aq, low h
activationCd = 10;
activateDevice(10);
}
else if (sensorCd.equals("HNL")) {
alertCd = "L"; //bad aq, low t
activationCd = 11;
activateDevice(11);
}
else if (sensorCd.equals("HHH")) {
alertCd = "G"; //bad air quality, high humidity, high temperature
activationCd = 6;
activateDevice(6);
}
return msg ;//+ String(activationCd);
}
// activate devices to crontrol temperature, humidity & air quality
void activateDevice(int activationCd) {
switch (activationCd) {
case 0:
digitalWrite(HEATERPIN, HIGH);
digitalWrite(HUMIDIFIERPIN, HIGH);
digitalWrite(AIRPURIFIERPIN, HIGH);
digitalWrite(COOLERFAN, HIGH);
break;
case 1:
digitalWrite(HEATERPIN, LOW);
digitalWrite(HUMIDIFIERPIN, HIGH);
digitalWrite(AIRPURIFIERPIN, HIGH);
digitalWrite(COOLERFAN, HIGH);
break;
case 2:
digitalWrite(HEATERPIN, HIGH);
digitalWrite(HUMIDIFIERPIN, LOW);
digitalWrite(AIRPURIFIERPIN, HIGH);
digitalWrite(COOLERFAN, HIGH);
break;
case 3:
digitalWrite(HEATERPIN, LOW);
digitalWrite(HUMIDIFIERPIN, LOW);
digitalWrite(AIRPURIFIERPIN, HIGH);
digitalWrite(COOLERFAN, HIGH);
break;
case 4:
digitalWrite(HEATERPIN, HIGH);
digitalWrite(HUMIDIFIERPIN, HIGH);
digitalWrite(AIRPURIFIERPIN, HIGH);
digitalWrite(COOLERFAN, LOW);
break;
case 5:
digitalWrite(HEATERPIN, HIGH);
digitalWrite(HUMIDIFIERPIN, HIGH);
digitalWrite(AIRPURIFIERPIN, LOW);
digitalWrite(COOLERFAN, HIGH);
break;
case 6:
digitalWrite(HEATERPIN, HIGH);
digitalWrite(HUMIDIFIERPIN, HIGH);
digitalWrite(AIRPURIFIERPIN, LOW);
digitalWrite(COOLERFAN, LOW);
break;
case 7:
digitalWrite(HEATERPIN, HIGH);
digitalWrite(HUMIDIFIERPIN, LOW);
digitalWrite(AIRPURIFIERPIN, LOW);
digitalWrite(COOLERFAN, LOW);
break;
case 8:
digitalWrite(HEATERPIN, LOW);
digitalWrite(HUMIDIFIERPIN, LOW);
digitalWrite(AIRPURIFIERPIN, LOW);
digitalWrite(COOLERFAN, HIGH);
break;
case 9:
digitalWrite(HEATERPIN, HIGH);
digitalWrite(HUMIDIFIERPIN, LOW);
digitalWrite(AIRPURIFIERPIN, HIGH);
digitalWrite(COOLERFAN, LOW);
break;
case 10:
digitalWrite(HEATERPIN, HIGH);
digitalWrite(HUMIDIFIERPIN, LOW);
digitalWrite(AIRPURIFIERPIN, LOW);
digitalWrite(COOLERFAN, HIGH);
break;
case 11:
digitalWrite(HEATERPIN, LOW);
digitalWrite(HUMIDIFIERPIN, HIGH);
digitalWrite(AIRPURIFIERPIN, LOW);
digitalWrite(COOLERFAN, HIGH);
break;
default:;
}
}
//function to determine sensor status
String getSensorStatus(float temperature, float humidity, float airppm) {
String st, sh, sa, ss;
//check temperature
if (temperature <= TH && temperature >= TL) {
st = "N";
} else if (temperature > TH) {
st = "H";
} else {
st = "L";
}
//check humidity
if (humidity <= HH && humidity >= HL) {
sh = "N";
} else if (humidity > HH) {
sh = "H";
} else {
sh = "L";
}
//check air quality
if (airppm <= AH) {
sa = "N";
} else if (airppm > AH) {
sa = "H";
} else {
sa = "N";
}
ss = sa + sh + st;
return ss;
}