-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherStation.ino
349 lines (309 loc) · 11.6 KB
/
weatherStation.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
#include "DHT.h"
#define DHTPIN 7 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // Type of DHT sensor (DHT22)
#define BLUE 10
#define GREEN 11
#define RED 12
#define BUZZER 6
const int AO_Pin = 7; // Connect the AO of MQ-4 sensor with analog channel 0 pin (A0) of Arduino
const int DO_Pin = 8; // Connect the DO of MQ-4 sensor with digital pin 8 (D8) of Arduino
int threshold; // Create a variable to store the digital output of the MQ-4 sensor
int methaneGas; // Create a variable to store the analog output of the MQ-4 sensor
const int sensorLight = A0;
#define DEBUG true
DHT dht(DHTPIN, DHTTYPE);
float temperatureThreshold = 19.0; // Initial temperature threshold
float temperatureThresholdHigh = 22.0; // Initial higher temperature threshold
void setup() {
Serial1.begin(115200);
Serial1.setTimeout(500);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
sendData("AT+RST\r\n", 2000, false); // reset module
sendData("AT+CWJAP=\"WeatherStation\",\"nuPl0uaAzi\"\r\n", 2000, false);
sendData("AT+CWMODE=2\r\n", 1000, false);
sendData("AT+CIFSR\r\n", 1000, DEBUG);
sendData("AT+CWSAP?\r\n", 2000, DEBUG);
sendData("AT+CIPMUX=1\r\n", 1000, false);
sendData("AT+CIPSERVER=1,80\r\n", 1000, false);
pinMode(BLUE, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(DO_Pin, INPUT);
pinMode(BUZZER, OUTPUT);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
digitalWrite(BUZZER, LOW);
Serial.begin(9600);
dht.begin();
}
void loop() {
float temperature, humidity, methaneGas, light = 0.0;
temperature = readTemperature();
humidity = readHumidity();
methaneGas = readMethaneGas();
light = readLight();
if (Serial1.available()) {
if (Serial1.find("+IPD,")) {
delay(500);
int connectionId = Serial1.read() - 48;
String request = Serial1.readStringUntil('\r');
if (request.indexOf("/l0") != -1) {
Serial.println("Turning LED ON");
digitalWrite(LED_BUILTIN, HIGH);
} else if (request.indexOf("/l1") != -1) {
Serial.println("Turning LED OFF");
digitalWrite(LED_BUILTIN, LOW);
} else if (request.indexOf("/setThresholdLow") != -1) {
String thresholdValueStr = getValue(request, "value=");
if (thresholdValueStr != "") {
temperatureThreshold = thresholdValueStr.toFloat();
if (temperatureThreshold > temperatureThresholdHigh) {
// Swap the values
float temp = temperatureThreshold;
temperatureThreshold = temperatureThresholdHigh;
temperatureThresholdHigh = temp;
}
Serial.print("Temperature threshold updated to: ");
Serial.println(temperatureThreshold);
}
} else if (request.indexOf("/setThresholdHigh") != -1) {
String thresholdValueHighStr = getValue(request, "value=");
if (thresholdValueHighStr != "") {
temperatureThresholdHigh = thresholdValueHighStr.toFloat();
if (temperatureThreshold > temperatureThresholdHigh) {
// Swap the values
float temp = temperatureThreshold;
temperatureThreshold = temperatureThresholdHigh;
temperatureThresholdHigh = temp;
}
Serial.print("Higher Temperature threshold updated to: ");
Serial.println(temperatureThresholdHigh);
}
}
String webpage = "<h1>Weather Station App</h1>";
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
if (temperature > 0) {
webpage += "<h2>Temperature:</h2>";
webpage += temperature;
webpage += " C";
}
if (humidity > 0) {
webpage += "<h2>Humidity:</h2>";
webpage += humidity;
webpage += "%";
}
if (methaneGas > 0) {
webpage += "<h2>Methane gas:</h2>";
webpage += methaneGas;
webpage += " ppm";
}
if (light > 0) {
webpage += "<h2>Luminosity:</h2>";
webpage += light;
webpage += " lx";
}
webpage += "<h2>Do you want to light on a led?</h2>";
webpage += "<a href=\"/l0\"><button>ON</button></a>";
webpage += "<a href=\"/l1\"><button>OFF</button></a>";
webpage += "<h2>Current Lower Temperature Threshold:</h2>";
webpage += temperatureThreshold;
webpage += " C";
webpage += "<h2>Current Higher Temperature Threshold:</h2>";
webpage += temperatureThresholdHigh;
webpage += " C";
webpage += "<h2>Set Lower Temperature Threshold:</h2>";
webpage += "<form action=\"/setThresholdLow\" method=\"GET\">";
webpage += "<label for=\"threshold\">Threshold Value:</label>";
webpage += "<input type=\"text\" id=\"threshold\" name=\"value\">";
webpage += "<input type=\"submit\" value=\"Set\">";
webpage += "</form>";
webpage += "<h2>Set Higher Temperature Threshold:</h2>";
webpage += "<form action=\"/setThresholdHigh\" method=\"GET\">";
webpage += "<label for=\"thresholdHigh\">Higher Threshold Value:</label>";
webpage += "<input type=\"text\" id=\"thresholdHigh\" name=\"value\">";
webpage += "<input type=\"submit\" value=\"Set\">";
webpage += "</form>";
cipSend += webpage.length();
cipSend += "\r\n";
sendData(cipSend, 100, DEBUG);
sendData(webpage, 150, DEBUG);
String closeCommand = "AT+CIPCLOSE=";
closeCommand += connectionId;
closeCommand += "\r\n";
sendData(closeCommand, 300, DEBUG);
}
}
}
String sendData(String command, const int timeout, boolean debug) {
String response = "";
Serial1.print(command);
long int time = millis();
while ((time + timeout) > millis()) {
while (Serial1.available()) {
char c = Serial1.read();
response += c;
}
}
if (response.indexOf("/l0") != -1) {
Serial.println("Turning LED ON");
digitalWrite(LED_BUILTIN, HIGH);
}
if (response.indexOf("/l1") != -1) {
Serial.println("Turning LED OFF");
digitalWrite(LED_BUILTIN, LOW);
}
if (debug) {
Serial.print(response);
}
return response;
}
float readTemperature() {
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
Serial.print("Temperature: ");
Serial.println(temperature);
if (temperature < temperatureThreshold) {
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
} else if (temperature >= temperatureThreshold && temperature < temperatureThresholdHigh) {
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
} else {
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
}
}
return temperature;
}
float readHumidity() {
//return millis();
//delay(2000); // Wait for 2 seconds between readings
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
return humidity;
}
float readMethaneGas() {
methaneGas = analogRead(AO_Pin); // Read the analog output measurement sample from the MQ4 sensor's AO pin
threshold = digitalRead(DO_Pin); // Read the digital output of MQ-4 sensor's DO pin
Serial.print("Threshold: "); // Print out the text "Threshold: "
Serial.print(threshold); // Print the threshold reached - with will either print be LOW or HIGH (above or underneath)
Serial.print(", "); // print a comma and space
Serial.print("Methane Concentration: "); // Print out the text "Methane Concentration: "
Serial.println(methaneGas); // Print out the methane value - the analog output - beteewn 0 and 1023
if (threshold == HIGH) {
//if (AO_Out >= 200){
//digitalWrite(YELLOW, HIGH); // If the threshold is reached, then the LED lights up to indicate a higher concentration
digitalWrite(BUZZER, HIGH);
} else {
//digitalWrite(YELLOW, LOW); // If the threshold is not reached (stays at 0), the LED stays off
digitalWrite(BUZZER, LOW);
}
//delay(1000); // Set a 10 second delay
return methaneGas;
}
float readLight() {
int sensorValue = analogRead(sensorLight);
// Map the sensor value to a range (optional)
int mappedValue = map(sensorValue, 0, 1023, 0, 100); // Adjust the output range as needed
// Print the values to the Serial monitor
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
Serial.print(" Mapped Value: ");
Serial.println(mappedValue);
return mappedValue;
}
String getValue(String data, String key) {
String startKey = key;
String endKey = "&";
int startIndex = data.indexOf(startKey);
if (startIndex == -1) {
return "";
}
int endIndex = data.indexOf(endKey, startIndex);
if (endIndex == -1) {
endIndex = data.length();
}
return data.substring(startIndex + startKey.length(), endIndex);
}
// void checkClientCommands() {
// float temperature, humidity, methaneGas, light = 0.0;
// temperature = readTemperature();
// humidity = readHumidity();
// methaneGas = readMethaneGas();
// light = readLight();
// if (Serial1.available()) {
// if (Serial1.find("+IPD,")) {
// delay(500);
// int connectionId = Serial1.read() - 48;
// String request = Serial1.readStringUntil('\r');
// if (request.indexOf("/l0") != -1) {
// Serial.println("Turning LED ON");
// digitalWrite(LED_BUILTIN, HIGH);
// } else if (request.indexOf("/l1") != -1) {
// Serial.println("Turning LED OFF");
// digitalWrite(LED_BUILTIN, LOW);
// } else if (request.indexOf("/setThresholdLow") != -1) {
// String thresholdValueStr = getValue(request, "value=");
// if (thresholdValueStr != "") {
// temperatureThreshold = thresholdValueStr.toFloat();
// Serial.print("Temperature threshold updated to: ");
// Serial.println(temperatureThreshold);
// }
// }
// String webpage = "<h1>Weather Station App</h1>";
// String cipSend = "AT+CIPSEND=";
// cipSend += connectionId;
// cipSend += ",";
// if (temperature > 0) {
// webpage += "<h2>Temperature:</h2>";
// webpage += temperature;
// webpage += " C";
// }
// if (humidity > 0) {
// webpage += "<h2>Humidity:</h2>";
// webpage += humidity;
// webpage += "%";
// }
// if (methaneGas > 0) {
// webpage += "<h2>Methane gas:</h2>";
// webpage += methaneGas;
// webpage += " ppm";
// }
// if (light > 0) {
// webpage += "<h2>Luminosity:</h2>";
// webpage += light;
// webpage += " lx";
// }
// webpage += "<h2>Do you want to light on a led?</h2>";
// webpage += "<a href=\"/l0\"><button>ON</button></a>";
// webpage += "<a href=\"/l1\"><button>OFF</button></a>";
// webpage += "<h2>Set Temperature Threshold:</h2>";
// webpage += "<form action=\"/setThresholdLow\" method=\"GET\">";
// webpage += "<label for=\"threshold\">Threshold Value:</label>";
// webpage += "<input type=\"text\" id=\"threshold\" name=\"value\">";
// webpage += "<input type=\"submit\" value=\"Set\">";
// webpage += "</form>";
// cipSend += webpage.length();
// cipSend += "\r\n";
// sendData(cipSend, 100, DEBUG);
// sendData(webpage, 150, DEBUG);
// String closeCommand = "AT+CIPCLOSE=";
// closeCommand += connectionId;
// closeCommand += "\r\n";
// sendData(closeCommand, 300, DEBUG);
// }
// }
// }