-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
632 lines (525 loc) · 12.7 KB
/
main.cpp
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <Arduino.h>
/*
Project: automatic irrigation system for arduino with website
Author: kl0ibi
date: 2022-05-27
MCU: Arduino Mega 2560
external MCU: esp32
*/
//constants
//Valve
#define VALVE_LEFT_TOP 0
#define VALVE_LEFT_BOTTOM 1
#define VALVE_RIGHT_TOP 2
#define VALVE_RIGHT_BOTTOM 3
//LDR
#define LDR 4
//humidity sensor outdoor
#define HUMIDITY_SENSOR_OUTDOOR 5
//temperature sensor
#define TEMPERATURE_SENSOR 16
//humidity sensor earth
#define HUM_LEFT_TOP 6
#define HUM_LEFT_BOTTOM 7
#define HUM_RIGHT_TOP 8
#define HUM_RIGHT_BOTTOM 9
//rain sensor
#define RAIN_SENSOR 10
//voltage sensor
#define VOLTAGE_SENSOR 11
//hall effect sensor
#define HALL_LEFT_TOP 12
#define HALL_LEFT_BOTTOM 13
#define HALL_RIGHT_TOP 14
#define HALL_RIGHT_BOTTOM 15
#define HUMIDITY_THRESHOLD_LOW 40
#define HUMIDITY_THRESHOLD_HIGH 60
#define TIME_AFTER_WATERING 300
#define WATERING_TIME 30
volatile uint32_t millis_counter = 0;
volatile uint32_t seconds_counter = 0;
volatile double usedWater = 34;
float humidity=80;
float temperature=23.5;
float voltage=12.4;
int hum_right_top=343;
int hum_right_bottom=342;
int hum_left_top=34;
int hum_left_bottom=34;
int rain=0;
int light=0;
#define WINDOW_SIZE 5
int value_hum_left_top[WINDOW_SIZE] = {};
int value_hum_left_bottom[WINDOW_SIZE] = {};
int value_hum_right_top[WINDOW_SIZE] = {};
int value_hum_right_bottom[WINDOW_SIZE] = {};
int filtered_hum_left_top[WINDOW_SIZE] = {};
int filtered_hum_left_bottom[WINDOW_SIZE] = {};
int filtered_hum_right_top[WINDOW_SIZE] = {};
int filtered_hum_right_bottom[WINDOW_SIZE] = {};
bool water_left_top = false;
bool water_left_bottom = false;
bool water_right_top = false;
bool water_right_bottom = false;
bool valve_left_top_locked = false;
bool valve_left_bottom_locked = false;
bool valve_right_top_locked = false;
bool valve_right_bottom_locked = false;
bool timer_isRunning = false;
char newDay_keyword[] = "newDay";
char recievedChar;
char * strtokIndx;
char buf[20];
enum States{
ST_BOOTUP,
ST_SENSOR,
ST_WATERING,
ST_RAIN,
ST_CLOSED,
ST_ERROR
};
enum Events{
EV_HUM,
EV_HALL1,
EV_HALL2,
EV_HALL3,
EV_HALL4,
EV_RAIN,
EV_TIMEOUT,
EV_TIMEOUT0,
EV_TIMEOUT3,
EV_TIMEOUT4,
EV_TIMEOUT5,
EV_NONE
};
volatile enum States curState = ST_SENSOR;
volatile enum Events curEvent = EV_NONE;
int readline(int readch, char *buffer, int len) {
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
default:
if (pos < len - 1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
case '\r': // Ignore CR
break;
case '\n': // Return on new-line
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
}
}
return 0;
}
void checkSerialInput() {
strcpy(buf, "");
while (Serial.available()) {
readline(Serial.read(), buf, 80);
}
if ((buf[0] != NULL)) {
strtokIndx = strtok(buf, ",");
if (strcmp(strtokIndx, newDay_keyword) == 0) {
Serial.println("reset water counter");
usedWater= 0;
}
else {
Serial.println("Unknown Command: " + String(strtokIndx));
}
}
}
void sendDatatoEsp32()
{
static float data[8] = {0, 0, 0, 0 ,0 ,0 ,0, 0};
//static int temp_data[2]= {0,0};
if(data[0] != humidity || data[1] != temperature || data[2] != voltage || data[3] != hum_right_top || data[4] != hum_right_bottom || data[5] != hum_left_top || data[6] != hum_left_bottom || data[7] != usedWater)
{
data[0] = humidity;
data[1] = temperature;
data[2] = voltage;
data[3] = hum_right_top;
data[4] = hum_right_bottom;
data[5] = hum_left_top;
data[6] = hum_left_bottom;
data[7] = usedWater;
Serial.println(String(data[0]) + "," + String(data[1]) + "," + String(data[2]) + "," + String(data[3]) + "," + String(data[4]) + "," + String(data[5]) + "," + String(data[6]) + "," + String(data[7]));
}
}
void init_pins()
{
/*
* 4 Valve Pins
* atleast 4 humidity sensors for watering
* 1 Pin for rainsensor
* 1 Pin for light sensor
* 1 Pin for voltage sensor
* 4 Pins for hall effect sensors
*/
pinMode(VALVE_LEFT_BOTTOM, OUTPUT);
pinMode(VALVE_LEFT_TOP, OUTPUT);
pinMode(VALVE_RIGHT_BOTTOM, OUTPUT);
pinMode(VALVE_RIGHT_TOP, OUTPUT);
pinMode(LDR, INPUT);
pinMode(HUMIDITY_SENSOR_OUTDOOR, INPUT);
pinMode(HUM_LEFT_TOP, INPUT);
pinMode(HALL_RIGHT_TOP, INPUT);
pinMode(HALL_RIGHT_BOTTOM, INPUT);
}
void init_Timer1()
{
//just for debugging not calculated time
//1ms Timer
TCCR1A=0x00;
TCCR1B|=(1<<WGM12);
TIMSK1|=(1<<OCIE1A);
OCR1A=200;
}
void Timer3_init()
{
//just for debugging not calculated time
//1ms Timer
TCCR3A=0x00;
TCCR3B|=(1<<WGM32);
TIMSK3|=(1<<OCIE3A);
OCR3A=200;
}
void Timer4_init()
{
//just for debugging not calculated time
//1ms Timer
TCCR4A=0x00;
TCCR4B|=(1<<WGM42);
TIMSK4|=(1<<OCIE4A);
OCR4A=200;
}
void Timer5_init()
{
//just for debugging not calculated time
//1ms Timer
TCCR5A=0x00;
TCCR5B|=(1<<WGM52);
TIMSK5|=(1<<OCIE5A);
OCR5A=200;
}
void Timer0_init()
{
//just for debugging not calculated time
//1ms Timer
//TCCR0A=0x00;
//TCCR0B|=(1<<WGM02);
// TIMSK0|=(1<<OCIE0A);
//OCR0A=200;
}
void timer_start()
{
//start timer
TCCR1B|=(1<<CS11)|(1<<CS10);
}
void timer_stop()
{
//stop timer
TCCR1B&=~((1<<CS11)|(1<<CS10));
}
void timer0_start()
{
//start timer
TCCR0B|=(1<<CS02)|(1<<CS00);
}
void timer0_stop()
{
//stop timer
TCCR0B&=~((1<<CS02)|(1<<CS00));
}
void timer3_start()
{
//start timer
TCCR3B|=(1<<CS32)|(1<<CS30);
}
void timer3_stop()
{
//stop timer
TCCR3B&=~((1<<CS32)|(1<<CS30));
}
void timer4_start()
{
//start timer
TCCR4B|=(1<<CS42)|(1<<CS40);
}
void timer4_stop()
{
//stop timer
TCCR4B&=~((1<<CS42)|(1<<CS40));
}
void timer5_start()
{
//start timer
TCCR5B|=(1<<CS52)|(1<<CS50);
}
void timer5_stop()
{
//stop timer
TCCR5B&=~((1<<CS52)|(1<<CS50));
}
double calculatedWater()
{
//0.6309 l/min
return (((double)millis_counter/1000)*0.6309);
}
void readSensor()
{
//read sensors
humidity = analogRead(HUMIDITY_SENSOR_OUTDOOR);
temperature = analogRead(TEMPERATURE_SENSOR);
voltage = analogRead(VOLTAGE_SENSOR);
hum_right_top = analogRead(HUM_RIGHT_TOP);
hum_right_bottom = analogRead(HUM_RIGHT_BOTTOM);
hum_left_bottom = analogRead(HUM_LEFT_BOTTOM);
//read rain sensor
rain = digitalRead(RAIN_SENSOR);
//read light sensor
light = analogRead(LDR);
}
int compare_int(const void *a, const void *b)
{
int *x = (int *)a;
int *y = (int *)b;
return *x - *y;
}
int median_filter_2(int feld[WINDOW_SIZE])
{
int temp[WINDOW_SIZE];
int median;
memcpy(temp, feld, WINDOW_SIZE*sizeof(int));
//search median (sort temp-array and take the middle)
qsort(temp, WINDOW_SIZE, sizeof(int), compare_int);
median = temp[WINDOW_SIZE / 2];
return median;
}
int average_filter(int feld[], int anz)
{
int average;
int sum = 0;
for (int i = 0; i < anz; i++)
sum += feld[i];
average = sum / anz;
return average;
}
int main()
{
Serial.begin(115200);
init_pins();
init_Timer1();
sei();
//debug
DDRA=0xFF;
PORTA=0x00;
while(1)
{
switch(curState)
{
case ST_BOOTUP:
//wait for serial connection with esp32
if(Serial.available()>0)
{
Serial.println("Serial connection established");
curState=ST_SENSOR;
}
break;
case ST_SENSOR:
//read sensors
for (int j=0; j< WINDOW_SIZE; j++)
{
for(int i=0; i < WINDOW_SIZE; i++)
{
value_hum_left_bottom[i] = analogRead(HUM_LEFT_BOTTOM);
value_hum_left_top[i] = analogRead(HUM_LEFT_TOP);
value_hum_right_bottom[i] = analogRead(HUM_RIGHT_BOTTOM);
value_hum_right_top[i] = analogRead(HUM_RIGHT_TOP);
}
filtered_hum_left_bottom[j] = median_filter_2(value_hum_left_bottom);
filtered_hum_left_top[j] = median_filter_2(value_hum_left_top);
filtered_hum_right_bottom[j] = median_filter_2(value_hum_right_bottom);
filtered_hum_right_top[j] = median_filter_2(value_hum_right_top);
}
//calculate average of filtered values
hum_left_bottom = average_filter(filtered_hum_left_bottom, WINDOW_SIZE);
hum_left_top = average_filter(filtered_hum_left_top, WINDOW_SIZE);
hum_right_bottom = average_filter(filtered_hum_right_bottom, WINDOW_SIZE);
hum_right_top = average_filter(filtered_hum_right_top, WINDOW_SIZE);
if(hum_left_bottom > HUMIDITY_THRESHOLD_LOW && hum_left_bottom < HUMIDITY_THRESHOLD_HIGH && !valve_left_bottom_locked)
{
water_left_bottom=true;
curEvent=EV_HUM;
}
if(hum_left_top > HUMIDITY_THRESHOLD_LOW && hum_left_top < HUMIDITY_THRESHOLD_HIGH && !valve_left_top_locked)
{
water_left_top=true;
curEvent=EV_HUM;
}
if(hum_right_bottom > HUMIDITY_THRESHOLD_LOW && hum_right_bottom < HUMIDITY_THRESHOLD_HIGH && !valve_right_bottom_locked)
{
water_right_bottom=true;
curEvent=EV_HUM;
}
if(hum_right_top > HUMIDITY_THRESHOLD_LOW && hum_right_top < HUMIDITY_THRESHOLD_HIGH && !valve_right_top_locked)
{
water_right_top=true;
curEvent=EV_HUM;
}
if(curEvent==EV_HUM)
{
curState=ST_WATERING;
curEvent=EV_NONE;
}
break;
case ST_WATERING:
//valve open
if(water_left_bottom)
{
valve_left_bottom_locked=true;
water_left_bottom=false;
digitalWrite(VALVE_LEFT_BOTTOM, HIGH);
}
if(water_left_top)
{
valve_left_top_locked=true;
water_left_top=false;
digitalWrite(VALVE_LEFT_TOP, HIGH);
}
if(water_right_bottom)
{
valve_right_bottom_locked=true;
water_right_bottom=false;
digitalWrite(VALVE_RIGHT_BOTTOM, HIGH);
}
if(water_right_top)
{
valve_right_top_locked=true;
water_right_top=false;
digitalWrite(VALVE_RIGHT_TOP, HIGH);
}
//wait for watering
if(timer_isRunning==false)
{
timer_isRunning=true;
timer_start();
}
if(curEvent==EV_TIMEOUT)
{
timer_stop();
//valve close
digitalWrite(VALVE_LEFT_BOTTOM, LOW);
digitalWrite(VALVE_LEFT_TOP, LOW);
digitalWrite(VALVE_RIGHT_BOTTOM, LOW);
digitalWrite(VALVE_RIGHT_TOP, LOW);
curState=ST_SENSOR;
curEvent=EV_NONE;
if(valve_left_bottom_locked)
{
timer0_start();
}
if(valve_left_top_locked)
{
timer5_start();
}
if(valve_right_bottom_locked)
{
timer3_start();
}
if(valve_right_top_locked)
{
timer4_start();
}
}
if(curEvent==EV_TIMEOUT0)
{
timer0_stop();
valve_left_bottom_locked=false;
}
if(curEvent==EV_TIMEOUT3)
{
timer3_stop();
valve_right_bottom_locked=false;
}
if(curEvent==EV_TIMEOUT4)
{
timer4_stop();
valve_right_top_locked=false;
}
if(curEvent==EV_TIMEOUT5)
{
timer5_stop();
valve_left_top_locked=false;
}
break;
case ST_RAIN:
Serial.println("Rain detected");
break;
case ST_CLOSED:
Serial.println("Lid closed");
break;
case ST_ERROR:
Serial.println("Error");
break;
}
sendDatatoEsp32();
checkSerialInput();
}
}
ISR(TIMER1_COMPA_vect)
{
millis_counter++;
if(millis_counter>=WATERING_TIME)
{
millis_counter=0;
seconds_counter++;
}
usedWater=calculatedWater();
}
ISR(TIMER1_OVF_vect)
{
millis_counter++;
if(millis_counter>=TIME_AFTER_WATERING)
{
millis_counter=0;
seconds_counter++;
curEvent=EV_TIMEOUT3;
}
}
ISR(TIMER3_COMPA_vect)
{
millis_counter++;
if(millis_counter>=TIME_AFTER_WATERING)
{
millis_counter=0;
seconds_counter++;
curEvent=EV_TIMEOUT3;
}
}
ISR(TIMER4_COMPA_vect)
{
millis_counter++;
if(millis_counter>=TIME_AFTER_WATERING)
{
millis_counter=0;
seconds_counter++;
curEvent=EV_TIMEOUT4;
}
}
ISR(TIMER5_COMPA_vect)
{
millis_counter++;
if(millis_counter>=TIME_AFTER_WATERING)
{
millis_counter=0;
seconds_counter++;
curEvent=EV_TIMEOUT5;
}
}