-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathESW_Project.ino
More file actions
356 lines (323 loc) · 10.6 KB
/
ESW_Project.ino
File metadata and controls
356 lines (323 loc) · 10.6 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
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
#include <DHT.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <string.h>
#include <PubSubClient.h>
#include <ThingSpeak.h>
#include <Arduino.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <time.h>
#include "SparkFun_SGP30_Arduino_Library.h"
#define CSE_IP "192.168.245.105"
#define CSE_PORT 5089
#define OM2M_ORGIN "admin:admin"
#define OM2M_MN "/~/in-cse/in-name/"
#define OM2M_AE "Smart_Farming"
#define OM2M_DATA_CONT "Sensors/Data"
int some_var = 0;
const char *ssid = "GALAXY KING";
const char *password = "DILEEPPRASANTHi";
const char *ntpServer = "pool.ntp.org";
const char *server = "mqtt3.thingspeak.com";
const int port = 1883;
const char *mqttUserName = "Jzw9Hg8SASECLzQ3GSUmPAc";
const char ClientID[] = "Jzw9Hg8SASECLzQ3GSUmPAc";
const char *mqttPass = "NjfZwpnqeOsa+aBvhe7dBZyq";
long int channelID = 2281910;
const char *apiKey = "GML8ND13LVFXZ7E2"; // write apikey
HTTPClient http;
WiFiClient client;
PubSubClient mqttClient(server, port, client);
SGP30 VOCsensor;
#define DHTPIN 32
#define DHTTYPE DHT11
int _moisture, sensor_analog, LDRValue, CO2Val, VOCVal;
const int sensor_pin = 34;
const int LDRPin = 33;
DHT dht(DHTPIN, DHTTYPE);
void setup(void)
{
dht.begin();
pinMode(LDRPin, INPUT);
Serial.begin(115200);
Wire.begin();
if (VOCsensor.begin() == false)
{
Serial.println("No SGP30 Detected. Check connections.");
while (1);
}
VOCsensor.initAirQuality();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.println("Connecting to WiFi...");
delay(1000);
}
Serial.println("WiFi Connected!");
ThingSpeak.begin(client);
mqttClient.setServer(server, port);
configTime(0, 0, ntpServer);
}
String field_values[6];
void loop(void)
{
some_var = some_var % 6;
while (mqttClient.connected() == NULL)
{
Serial.println("COnnecting to mqtt...");
mqttConnect();
delay(1000);
}
StaticJsonDocument<200> jsonDoc;
jsonDoc["secret"] = "secretanicheppaga";
jsonDoc["plant_name"] = "Mango";
// jsonDoc["msg"] = "this is a sample msg";
// jsonDoc["notif_type"] = "danger";
String jsonStr;
// serializeJson(jsonDoc, jsonStr);
mqttClient.loop();
VOCsensor.measureAirQuality();
CO2Val = VOCsensor.CO2;
VOCVal = VOCsensor.TVOC;
sensor_analog = analogRead(sensor_pin);
//(more than 3000 is random value)(range: 10 -1000)
// for(int i = 0;i < 20;i++){
// Serial.println("Values of CO2 and VOC are being calculated");
// }
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
_moisture = (100 - ((sensor_analog / 4095.00) * 100));
Serial.print("Moisture Percentage = ");
if (_moisture == 100)
{
_moisture = 0;
}
Serial.print(_moisture);
Serial.println("%");
Serial.print("Light Intensity: ");
LDRValue = analogRead(LDRPin);
int LDRPerc = (4096 - LDRValue)*100/4096;
Serial.print(LDRPerc);
Serial.println("%");
if (isnan(humidity) || isnan(temperature))
{
if (isnan(humidity))
{
humidity = 0;
}
if (isnan(temperature))
{
temperature = 0;
}
Serial.println("Failed to read from DHT sensor cause getting 0");
}
else
{
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C\n\n");
}
Serial.print("CO2: ");
Serial.print(CO2Val);
Serial.print(" ppm\tTVOC: ");
Serial.print(VOCVal);
Serial.println(" ppb");
if (some_var == 0)
{
if (temperature > 35)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! High Temperature.";
jsonDoc["notif_type"] = "warning";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
else if (temperature < 25)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! Low Temperature.";
jsonDoc["notif_type"] = "warning";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
}
delay(100);
// humidity
if (some_var == 1)
{
if (humidity < 40)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! Low Humidity.";
jsonDoc["notif_type"] = "warning";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
else if (humidity > 80)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! High Humidity.";
jsonDoc["notif_type"] = "warning";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
}
delay(100);
// ldr
if (some_var == 2)
{
if (LDRPerc < 20)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! Low Intensity.";
jsonDoc["notif_type"] = "warning";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
}
delay(100);
// soil moisture
if (some_var == 3)
{
if (_moisture > 80)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! High Soil Moisture.";
jsonDoc["notif_type"] = "warning";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
else if (_moisture < 20)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! Low Soil Moisture.";
jsonDoc["notif_type"] = "danger";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
}
delay(100);
// co2
if (some_var == 4)
{
if (CO2Val > 800)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! High CO2.Probability of pathogen attack!!";
jsonDoc["notif_type"] = "warning";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
else if (CO2Val < 400)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! Low CO2.";
jsonDoc["notif_type"] = "warning";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
}
delay(100);
// voc
if (some_var == 5)
{
if (VOCVal > 100)
{
http.begin(client, "http://greenplant.pythonanywhere.com/receive");
http.addHeader("Content-Type", "application/json");
jsonDoc["msg"] = "Caution! High VOC.Probability of pathogen attack!!";
jsonDoc["notif_type"] = "danger";
serializeJson(jsonDoc, jsonStr);
Serial.println(http.POST(jsonStr));
http.end();
}
}
delay(100);
some_var = some_var + 1;
// payload for the string
// VOC = field 4 ; CO2 = field 5;
field_values[0] = "field1=" + String(temperature) + "&";
field_values[1] = "field2=" + String(_moisture) + "&";
field_values[2] = "field3=" + String(LDRPerc) + "&";
field_values[3] = "field4=" + String(VOCVal) + "&";
field_values[4] = "field5=" + String(CO2Val) + "&";
field_values[5] = "field6=" + String(humidity) + "&";
String answer = "";
for (int i = 0; i < 6; i++)
{
answer += field_values[i];
}
int field_to_publish[1] = {6};
mqttPublish(channelID, apiKey, answer, field_to_publish);
String data;
String server = "http://" + String() + CSE_IP + ":" + String() + CSE_PORT + String() + OM2M_MN;
http.begin(server + String() + OM2M_AE + "/" + OM2M_DATA_CONT + "/");
http.addHeader("X-M2M-Origin", OM2M_ORGIN);
http.addHeader("Content-Type", "application/json;ty=4");
http.addHeader("Content-Length", "100");
data = "[" + String(temperature) + ", " + String(_moisture) + ", " + String(LDRPerc) + ", " + String(VOCVal) + ", " + String(CO2Val) + ", " + String(humidity) + "]";
String req_data = String() + "{\"m2m:cin\": {"
+
"\"con\": \"" + data + "\","
+
"\"lbl\": \"" + "V1.0.0" + "\","
//+ "\"rn\": \"" + "cin_"+String(i++) + "\","
+
"\"cnf\": \"text\""
+
"}}";
int code = http.POST(req_data);
http.end();
Serial.println(code);
delay(500);
}
void mqttConnect()
{
while (!mqttClient.connected())
{
Serial.print("Attempting connection to MQTT...");
Serial.print("\n");
if (mqttClient.connect(ClientID, mqttUserName, mqttPass))
{
Serial.println("connected to MQTT broker");
Serial.print("\n");
}
else
{
Serial.print("failed due to error ");
Serial.print(mqttClient.state());
Serial.print("\n");
Serial.println("making a retry...");
Serial.print("\n");
delay(3000);
}
}
}
void mqttPublish(long pubChannelID, String pubWriteAPIKey, String data, int fieldArray[])
{
String topicstring = "channels/" + String(pubChannelID) + "/publish"; //+String(pubWriteAPIKey);
mqttClient.publish(topicstring.c_str(), data.c_str());
delay(1000);
}