-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
276 lines (203 loc) · 6.7 KB
/
main.cpp
File metadata and controls
276 lines (203 loc) · 6.7 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
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiMulti.h>
#include <WiFiClientSecure.h>
#include <SHT1x.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 0
#define dataPin 16
#define clockPin 17
SHT1x sht1x(dataPin, clockPin);
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
extern "C" {
#include "esp_system.h"
#include <driver/adc.h>
uint8_t temprature_sens_read(); //include idf
}
String host = "labsistemas.javerianacali.edu.co:8002";
String apiKeySoil = "G7RYW1XF9ED3ECAH";
String apiKeyAir = "4ELO3TBBFNITUNUL";
String apiKeyInter = "F3G1EZRNC2HN9F8O";
const char* ssid = "ciat-visitors";
const char* password = "javeriana1";
const int sleepTimeS = 30;
const int inpVH400 = 35;
const int inpPar =33;
const int inpWind = 32;
const int inpMeth = 34;
//const int in
/* LED pin */
byte ledPin = 5;
float vmc;
float temp1;
float par;
float temp_c;
//float temp_f;
float humidity;
String windDir = "";
#define WIND_DIR_PIN 36
//methane variables
float R0 = 11.820; //Sensor Resistance in fresh air from
float m = -0.318; //Slope
float b = 1.133; //Y-Intercept
void windDirCalc(int vin)
{
if (vin < 150) windDir="202.5";
else if (vin < 300) windDir = "180";
else if (vin < 400) windDir = "247.5";
else if (vin < 600) windDir = "225";
else if (vin < 900) windDir = "292.5";
else if (vin < 1100) windDir = "270";
else if (vin < 1500) windDir = "112.5";
else if (vin < 1700) windDir = "135";
else if (vin < 2250) windDir = "337.5";
else if (vin < 2350) windDir = "315";
else if (vin < 2700) windDir = "67.5";
else if (vin < 3000) windDir = "90";
else if (vin < 3200) windDir = "22.5";
else if (vin < 3400) windDir = "45";
else if (vin < 4000) windDir = "0";
else windDir = "0";
}
float readVH400(int inpVH400) {
// Read value and convert to voltage
int lecturaVH400 = analogRead(inpVH400);
float sensorVoltage = lecturaVH400*(3.0 / 4096.0);
float VWC;
// Calculate VWC
if(sensorVoltage <= 1.1) {
VWC = 10*sensorVoltage;
} else if(sensorVoltage > 1.1 && sensorVoltage <= 1.3) {
VWC = 25*sensorVoltage-17.5;
} else if(sensorVoltage > 1.3 && sensorVoltage <= 1.82) {
VWC = 48.08*sensorVoltage-47.5;
} else if(sensorVoltage > 1.82) {
VWC = 26.32*sensorVoltage-7.89;
}
return(VWC);
}
void setup() {
Serial.begin(115200);
sensors.begin();
pinMode(inpVH400,INPUT);
pinMode(inpMeth,INPUT);
pinMode(inpPar,INPUT);
pinMode(inpWind,INPUT);
pinMode(ledPin, OUTPUT);
sensors.begin();
Serial.begin(115200);
delay(2000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
digitalWrite(ledPin, HIGH); //status WiFi
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
digitalWrite(ledPin, HIGH);
delay(10);
digitalWrite(ledPin, LOW);
HTTPClient http;
float vmc = readVH400(inpVH400);
uint8_t temp = temprature_sens_read();
float temp1 = (temp-32)/1.8;
//Methane linear
float lectura = analogRead(inpMeth);
float voltMeth = lectura * (3.0 / 4096.0);
float ratio = (((5.0 * 10.0) / voltMeth) - 10.0)/R0;
double ppm_log = (log10(ratio) - b) / m; //Get ppm value in linear scale
double ppm = pow(10, ppm_log)/ 10000;
// PAR sensor
float parLectura = analogRead(inpPar);
float voltPar = parLectura * (3.0 /4096.0);
float par = voltPar * 5000; //(4000/0.8)
//Wind sensor reading
float windLectura = analogRead(inpWind);
float voltWind = windLectura *(3.0/4096);
float windSpeed = 32.4 * (voltWind-0.4)/1.6;
//windDirCalc
windDirCalc(analogRead(WIND_DIR_PIN));
//humidity and Temperature
float temp_c;
//float temp_f;
float humidity;
// Read values from the sensor
temp_c = sht1x.readTemperatureC();
//temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();
//DS18B20
sensors.requestTemperatures();
float tempSoil = sensors.getTempCByIndex(0);
Serial.print("Temperature soil is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
Serial.print("Methane ppm:");
Serial.println(ppm);
Serial.print("VMC :");
Serial.println(vmc);
Serial.print("temp internal :");
Serial.println(temp1);
Serial.print("wind :");
Serial.println(windSpeed);
Serial.print("Temperature: ");
Serial.print(temp_c, DEC);
Serial.print("C / ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
sensors.requestTemperatures();
String updateURL = "http://" + host + "/update?api_key=" + apiKeySoil +"&field1="+String(vmc)+"&field2="+String(sensors.getTempCByIndex(0));
String updateURL2 = "http://" + host + "/update?api_key=" + apiKeyInter +"&field1="+String(temp1);//segundo canal
String updateURL3 = "http://" + host + "/update?api_key=" + apiKeyAir +"&field1="+String(ppm)+"&field7="+String(par)+"&field4="+String(windSpeed);
updateURL3 +="&field3="+String(humidity)+"&field2="+String(temp_c)+"&field5="+String(windDir)+"&field6="+String("0");
http.begin(updateURL);
//http.GET()
volatile int httpCode = http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end();
http.begin(updateURL2); //Specify the URL
http.GET();
//Make the request
//Make the request
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end(); //Free the resources
http.begin(updateURL3); //Specify the URL
http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end();
}else{
Serial.println("Error in WiFi connection");
}
delay(10000); //Send a request every 10 seconds
Serial.println("deepsleep empieza");
ESP.deepSleep(sleepTimeS * 1000000);
Serial.println("Deep end");
}