-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sistema_de_Riego.ino
436 lines (391 loc) · 11.1 KB
/
Sistema_de_Riego.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
429
430
431
432
433
434
435
436
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include "RTClib.h"
#include <ArduinoJson.h>
#include "FS.h"
#include <LittleFS.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//RTC_DS3231 rtc;
int hora;
int minutos;
int dia;
const int bomba = 16; //UNO: 6 Wemos: 12 NodeMCU: 15
const int circuito1 = 0;
const int circuito2 = 14;
const int circuito3 = 12; //UNO: 9 Wemos: 2 NodeMCU: 12
const int goteo = 13; //UNO: 10 Wemos: 15 NodeMCU: 13
void HTTP_handleRoot();
//Tiempos de referencia para utilizar con la funcion millis() y poder contar tiempo de forma de no detener el procesador
//son los tiempos de duracion de riego para cada circuito
unsigned long ActualTime;
unsigned long c1Time=240000;
unsigned long c2Time=480000;
unsigned long c3Time=720000;
unsigned long gTime=1620000;
unsigned long endTime=1;
unsigned long ultima=0;
//variables que tendran el resultado de la suma de ActualTime + duracion X circuito
unsigned long c1TimeA;
unsigned long c2TimeA;
unsigned long c3TimeA;
unsigned long gTimeA;
unsigned long endTimeA;
bool contador = 1;
bool contador2 = false;
//string de llaves http
String JsonConfig;
String JsonConfig2;
String c1;
String c2;
String c3;
String g;
String rutine;
String rt;
String rt2;
ESP8266WebServer server(80);
int c1s = 1;
int c2s = 1;
int c3s = 1;
int gs = 1;
int rs = 1;
//UTC*60*60 -3*60*60
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "south-america.pool.ntp.org");
const char* ssid = "Flia Perez";
const char* password = "ponecualquiera";
//const char* ssid = "Notebooks ETEC";
//const char* password = "373k123*";
//ip fija para no tener que ir adivinando
IPAddress local_IP(192,168,54,200);
IPAddress gateway(192,168,54,1);
IPAddress subnet(255,255,255,0);
IPAddress primaryDNS(8,8,8,8);
IPAddress secundaryDNS(8,8,4,4);
//IPAddress local_IP(10,54,3,200);
//IPAddress gateway(192,168,40,8);
//IPAddress subnet(255,255,255,0);
//IPAddress primaryDNS(8,8,8,8);
//IPAddress secundaryDNS(8,8,4,4);
//horarios max 4
int h1[2] = {06, 00};
int h2[2] = {18, 00};
int h3[2] = {04, 00};
int h4[2] = {02, 00};
//dias:
bool days[7]= {true, true, true, true, true, true, true};
// if (days[day]==true){XD}
void ssd(){
if (LittleFS.exists("/config.json") == true){
File file = LittleFS.open("/config.json", "r");
DynamicJsonBuffer jsonBuffer(512);
JsonObject& json = jsonBuffer.parseObject(file);
if (!json.success()) {
Serial.println("parseObject() failed");
return;
}
Serial.print("la wea funciono:");
Serial.println(json["h1"][0].as<int>());
h1[0] = json["h1"][0];
h1[1] = json["h1"][1];
h2[0] = json["h2"][0];
h2[1] = json["h2"][1];
h3[0] = json["h3"][0];
h3[1] = json["h3"][1];
h4[0] = json["h4"][0];
h4[1] = json["h4"][1];
c1Time = json["duration"][0];
c2Time = json["duration"][1];
c3Time = json["duration"][2];
gTime = json["duration"][3];
for (int i = 0; i <= 6; i++){
days[i] = json["days"][i];
}
file.close();
}
}
void setup() {
Serial.begin(115200);
//rtc.begin();
if(!LittleFS.begin()){
Serial.println("An Error has occurred while mounting LittleFS");
return;
}
// Conectar a wifi
if(!WiFi.config(local_IP, gateway, subnet, primaryDNS, secundaryDNS)){
Serial.println("La configuracion de IP fija fallo");
}
WiFi.begin(ssid, password);
Serial.print("Conectando...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Conectado con éxito, mi IP es: ");
Serial.println(WiFi.localIP());
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
//arrancando el server
server.on ( "/", HTTP_handleRoot );
server.on("/json", []() {
if (LittleFS.exists("/config.json") == true){
File file = LittleFS.open("/config.json", "r");
server.send(200, "text/plain", file.readString() );
Serial.println("YES");
}});
server.on("/live", [](){
server.send(200, "text/plain", "{'time':'" + String(hora) + ":" + String(minutos) + "','C':[" + String(c1s) + "," + String(c2s) + "," + String(c3s) + "," + String(gs) + "," + String(rs) + "]}");
});
//si no funciona el server
server.onNotFound( HTTP_handleRoot );
server.begin();
pinMode(circuito1, OUTPUT);
pinMode(circuito2, OUTPUT);
pinMode(circuito3, OUTPUT);
pinMode(goteo, OUTPUT);
pinMode(bomba, OUTPUT);
//arrancar todo apagado:
digitalWrite(bomba,HIGH);
digitalWrite(circuito1,HIGH);
digitalWrite(circuito2,HIGH);
digitalWrite(circuito3,HIGH);
digitalWrite(goteo,HIGH);
timeClient.begin();
timeClient.setTimeOffset(-10800);
ssd();
}
// Circuitos
void circuito_1(){
digitalWrite(bomba, LOW);
digitalWrite(circuito1,LOW);
Serial.println("CIRCUITO 1 ENCENDIDO");
c1s=0;
}
void circuito_2(){
digitalWrite(bomba, LOW);
digitalWrite(circuito2,LOW);
Serial.println("CIRCUITO 2 ENCENDIDO");
c2s=0;
}
void circuito_3(){
digitalWrite(bomba, LOW);
digitalWrite(circuito3,LOW);
Serial.println("CIRCUITO 3 ENCENDIDO");
c3s=0;
}
void goteo_(){
digitalWrite(bomba, LOW);
digitalWrite(goteo,LOW);
Serial.println("GOTEO ENCENDIDO");
gs=0;
}
//OFF
void circuito_1OFF(){
if (gs == 1 && c2s == 1 && c3s == 1){
digitalWrite(bomba, HIGH);
}
digitalWrite(circuito1,HIGH);
Serial.println("CIRCUITO 1 APAGADO");
c1="NADA";
Serial.println(c1);
c1s=1;
}
void circuito_2OFF(){
if (gs == 1 && c1s == 1 && c3s == 1){
digitalWrite(bomba, HIGH);
}
digitalWrite(circuito2,HIGH);
Serial.println("CIRCUITO 2 APAGADO");
c2="NADA";
c2s=1;
}
void circuito_3OFF(){
if (gs == 1 && c1s == 1 && c2s == 1){
digitalWrite(bomba, HIGH);
}
digitalWrite(circuito3,HIGH);
Serial.println("CIRCUITO 3 APAGADO");
c3="NADA";
c3s=1;
}
void goteo_OFF(){
if (c1s == 1 && c2s == 1 && c3s == 1){
digitalWrite(bomba, HIGH);
}
digitalWrite(goteo,HIGH);
Serial.println("GOTEO APAGADO");
g="NADA";
gs=1;
}
void stop_all(){
digitalWrite(bomba, HIGH);
digitalWrite(circuito1, HIGH);
digitalWrite(circuito2, HIGH);
digitalWrite(circuito3, HIGH);
digitalWrite(goteo, HIGH);
rutine="NADA";
contador=1;
rs=1;
c1s=1;
c2s=1;
c3s=1;
gs=1;
}
void rutina(){
ActualTime = millis();
rs = 0;
if (contador==1){
c1TimeA = c1Time+ActualTime;
Serial.print("c1TimeA:");
Serial.println(c1TimeA);
c2TimeA = c2Time+ActualTime;
c3TimeA = c3Time+ActualTime;
gTimeA = gTime+ActualTime;
contador=0;
}
digitalWrite(bomba, LOW);
if(ActualTime<=c1TimeA){
Serial.println("Circuito 1 encendido");
digitalWrite(circuito1,LOW);
}
else if(ActualTime<=c2TimeA){
Serial.println("Circuito 1 apagado");
Serial.println("Circuito 2 encendido");
digitalWrite(circuito1, HIGH);
digitalWrite(circuito2, LOW);
}
else if(ActualTime<=c3TimeA){
Serial.println("Circuito 1 apagado");
Serial.println("Circuito 2 apagado");
Serial.println("Circuito 3 encendido");
digitalWrite(circuito2, HIGH);
digitalWrite(circuito3, LOW);
}
else if(ActualTime<=gTimeA){
Serial.println("Circuito 1 apagado");
Serial.println("Circuito 2 apagado");
Serial.println("Circuito 3 apagado");
Serial.println("GOTEO encendido");
digitalWrite(circuito3, HIGH);
digitalWrite(goteo, LOW);
}
else if (ActualTime>endTimeA){
digitalWrite(bomba, HIGH);
digitalWrite(goteo, HIGH);
Serial.println("SE TERMINO LA RUTINA");
contador=1;
contador2=false;
rs = 1;
}
}
void loop() {
ArduinoOTA.handle();
//inicia el server
server.handleClient();
//DateTime now = rtc.now();
//recoje la informacion enviada por la app
JsonConfig = server.arg("HT");
c1 = server.arg("c1");
c2 = server.arg("c2");
c3 = server.arg("c3");
g = server.arg("g");
rutine = server.arg("rutine");
rt = server.arg("r");
if (c1 == "ON")circuito_1();
else if (c1 == "OFF" && c1s==0)circuito_1OFF();
if (c2 == "ON") circuito_2();
else if (c2 == "OFF" && c2s==0) circuito_2OFF();
if (c3 == "ON") circuito_3();
else if (c3 == "OFF" && c3s==0) circuito_3OFF();
if (g == "ON") goteo_();
else if (g == "OFF" && gs==0) goteo_OFF();
if (rutine == "ON"){contador2 = true;}
else if (rutine == "OFF") {
stop_all();
contador2 = false;
}
/*
if (rt =! ""){
if (rt =! rt2){
rt = rt2;
digitalWrite(bomba, HIGH);
digitalWrite(circuito1, HIGH);
digitalWrite(circuito2, HIGH);
digitalWrite(circuito3, HIGH);
digitalWrite(goteo, HIGH);
contador2 = false;
}
}
*/
else if (JsonConfig != ""){
if (JsonConfig != JsonConfig2){
JsonConfig2 = JsonConfig;
Serial.println(JsonConfig);
LittleFS.remove("/config.json");
File file = LittleFS.open("/config.json", "w");
if (!file){
Serial.println("Error abriendo el archivo");
return;
}
file.print(JsonConfig);
delay(1);
file.close();
ESP.reset();
}
}
//reloj
if (millis()>=ultima){
ultima = millis() + 500;
timeClient.update();
dia = timeClient.getDay();
hora = timeClient.getHours();
minutos = timeClient.getMinutes();
//Serial.print(dia);
//Serial.print("\t");
//Serial.print(hora);
//Serial.print(":");
//Serial.println(minutos);
}
//while(hora==7 && minutos==0 || hora==19 && minutos==0){
if (days[dia] == true){
if (hora==h1[0] && minutos==h1[1] || hora==h2[0] && minutos==h2[1] || hora==h3[0] && minutos==h3[1] || hora==h4[0] && minutos==h4[1]){
contador2 = true;
}
}
if (contador2 == true){rutina();}
}
void HTTP_handleRoot(void) {
server.send ( 200, "text/html", "" );
delay(1);
}