-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffeebot.ino
433 lines (364 loc) · 12.4 KB
/
coffeebot.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
#include <Switcher.h> //Switcher_lib library to operate relays with timers and periods.
#include <Adafruit_SSD1306.h> //OLED SCREEN library
#include <avr/wdt.h> // We have a watchdog just in case (wdt.h)
#include <EEPROM.h>
#define TENSEC (1000UL * 10)
#define ONEMIN (1000UL * 60 * 1)
#define THIRTYMIN (1000UL * 60 * 30)
Adafruit_SSD1306 display(4); //OLED DECLARATION
// ANALOG INPUTS //
int moisture_sensor = A2;
// DIGITAL INPUTS //
int lights_p = 11; // Lights relay
int pump_p = 10; // Water pump relay
int moisture_p = 8; // Moisture sensor relay
// SWITCH DECLARATIONS //
Switcher lights(lights_p,HIGH);
Switcher pump(pump_p,HIGH);
Switcher soil(moisture_p,HIGH);
// SENSORS VARIABLES //
int moisture; //We store here the moisture values
// GLOBAL VARIABLES //
int hoursLight = 14; //Number of daily hours per Day
int hoursDark = 10; //Number of nightly hours per Day
int min_moisture=42; //Desired min moisture target
unsigned long checktime30 = THIRTYMIN;
unsigned long checktime10s = TENSEC;
unsigned long checktime1m = ONEMIN;
int last_lights=0;
int last_pump=0;
int act;
bool regar;
int day=2;
int d=1;
int attempt=0;
int eeAddress=0;
void setup() {
Serial.begin(9600);
Serial.println(F("Arduino Setting up"));
wdt_disable(); // We disable the watchdog during setup
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
intro();
pinMode(lights_p,OUTPUT);
pinMode(pump_p,OUTPUT);
pinMode(moisture_p,OUTPUT);
digitalWrite(lights_p,HIGH);
digitalWrite(pump_p,HIGH);
digitalWrite(moisture_p,HIGH);
if ( EEPROM.read(eeAddress) == 255 ) { //In case EEPROM is never been initialized
EEPROM.put(eeAddress,day);
Serial.println("First case EEPROM");
Serial.println(EEPROM.get(eeAddress,day));
}
else if ( EEPROM.get(eeAddress,d) < day ) { //In case EEPROM has some int data at frist position inferior than day = 1
EEPROM.put(eeAddress,day);
Serial.println("Second case EEPROM");
Serial.print(day);
Serial.print( EEPROM.get(eeAddress,d));
}
else if ( sizeof(EEPROM.get(eeAddress,d)) != sizeof(day) ) { //In case EEPROM has any random non-int data at frist position
EEPROM.put(eeAddress,day);
Serial.println("Third case EEPROM");
Serial.print(day);
Serial.print( EEPROM.get(eeAddress,d));
}
else { //Retrive data in case of Powercut
day = EEPROM.get(eeAddress,d);
Serial.println("Fourth case EEPROM");
Serial.print(day);
Serial.print( EEPROM.get(eeAddress,d));
}
delay(600);
moisture = SoilSensor(); //Get first Read from Soil Moisture Sensor
test_lights();
test_pump();
lights.Start();
delay(1000);
wdt_enable(WDTO_8S); // Watchdog setup set to 8 seconds
Serial.println(F("End of Setup"));
}
void loop() {
wdt_reset(); //Watchdog reset on every loop
pump.Timer(1,0); //We check if water pump 1 was ON, if so, we turn it of after 1Second.
// STUFF WE CHECK EVERY 10S //
if((long)(millis() - checktime10s) >= 0) {
if (regar == 1) {
moisture = SoilSensor();
regar = 0;
watering();
}
switch (act%3) {
case 1:
firstscreen();
break;
case 2:
secondscreen();
break;
default:
intro();
act = 0; //We turn act to 0 when act%0 = 0 to avoid overflow.
break;
}
act = act+1;
checktime10s = millis() + TENSEC;
}
// STUFF WE CHECK EVERY 1min //
if((long)(millis() - checktime1m) >= 0) {
if (moisture <= min_moisture) {
pump.Start();
regar = 1;
attempt = attempt + 1;
if (attempt > 7) {
alert();
}
else {
watering();
}
}
else {
attempt = 0;
}
last_lights=(millis()-lights._previousMillis)/3600000;
last_pump=(millis()-pump._previousMillis)/60000;
if (lights.Period(hoursDark,hoursLight,2) == 1) { //We check if it's time to switch light state
day = day + 1;
EEPROM.put(eeAddress,day);
}
checktime1m = millis() + ONEMIN;
}
// STUFF WE CHECK EVERY 30min //
if((long)(millis() - checktime30) >= 0) {
moisture = SoilSensor();
checktime30 = millis() + THIRTYMIN;
}
}
// SOIL SENSOR READ //
int SoilSensor(){
int a=0;
soil.Start();
delay(200); //We should be avoiding delays, but here we are. We need to wait to get the analogread lecture. TO-DO: Find something better darling.
a= analogRead(moisture_sensor); //We read the analog value of the mositure sensor
a = constrain(a,400,1023); //We constrain the values to avoid faults
a = map(a,1023,400,0,99); //We map it from 0% to 99% due to display limitations: 100% value does not fit on the little display we use
// 401-440 very wet, water inmersed.
// 480-540 humid
// 1023 dry
soil.Stop();
return (a);
}
// SCREENS DISPLAY FUNCTIONS //
void firstscreen() {
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Dia:");
display.setCursor(24,0);
display.println(day);
display.setCursor(84,0);
display.println("Luz:");
display.setCursor(110,0);
if (digitalRead(lights_p)) {
display.println("OFF");
}
else {
display.println("ON");
}
display.setCursor(0,8);
display.drawLine(0,8,display.width(),8, WHITE);
display.setTextSize(2); // Draw 2X-scale text
display.setCursor(4,14);
display.println(moisture);
display.setCursor(30,14);
display.println("%");
display.setTextSize(1); // Draw 2X-scale text
display.drawLine(46,8,46,46, WHITE);
display.setCursor(51,13);
display.println("Ultimo riego");
display.setCursor(62,23);
display.println("hace");
display.setCursor(90,23);
if (last_pump<59) {
display.println(last_pump);
display.setCursor(102,23);
display.println("min");
}
else {
display.println((last_pump/60));
display.setCursor(102,23);
display.println("h");
}
display.display();
}
void secondscreen() {
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Dia:");
display.setCursor(24,0);
display.println(EEPROM.get(eeAddress,day));
display.setCursor(84,0);
display.println("Luz:");
display.setCursor(110,0);
if (digitalRead(lights_p)) {
display.println("OFF");
}
else { display.println("ON");}
display.setCursor(0,8);
display.drawLine(0,8,display.width(),8, WHITE);
display.setTextSize(2); // Draw 2X-scale text
display.setCursor(4,14);
display.println(moisture);
display.setCursor(30,14);
display.println("%");
display.setTextSize(1); // Draw 2X-scale text
display.drawLine(46,8,46,46, WHITE);
if (digitalRead(lights_p)) {
display.setCursor(54,13);
display.println("Amanecer en");
display.setCursor(78,23);
display.println(hoursDark-last_lights);
display.setCursor(90,23);
display.println("h");
}
else {
display.setCursor(54,13);
display.println("Anochecer en");
display.setCursor(78,23);
display.println(hoursLight-last_lights);
display.setCursor(90,23);
display.println("h");
}
display.display();
}
void intro() {
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" The plantbot");
display.fillCircle(54, 15, 3, WHITE);
display.fillCircle(72, 15, 3, WHITE);
display.drawPixel(46, 23, WHITE);
display.drawPixel(45, 23, WHITE);
display.drawPixel(44, 23, WHITE);
display.drawPixel(44, 22, WHITE);
display.drawPixel(43, 22, WHITE);
display.drawPixel(43, 21, WHITE);
display.drawPixel(42, 21, WHITE);
display.drawPixel(82, 23, WHITE);
display.drawPixel(83, 23, WHITE);
display.drawPixel(83, 22, WHITE);
display.drawPixel(84, 22, WHITE);
display.drawPixel(84, 21, WHITE);
display.drawPixel(85, 21, WHITE);
display.drawLine(45,24,82,24, WHITE);
display.display();
}
void alert() {
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setCursor(35,8);
display.println("ERROR");
display.display();
}
void watering() {
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" The plantbot");
display.setCursor(0,8);
display.drawLine(0,8,display.width(),8, WHITE);
display.setTextSize(2); // Draw 2X-scale text
display.setCursor(4,14);
display.println(moisture);
display.setCursor(30,14);
display.println("%");
display.setTextSize(1); // Draw 2X-scale text
display.drawLine(46,8,46,46, WHITE);
display.setCursor(51,13);
display.println(" Serie riego");
display.setCursor(70,23);
display.println("num:");
display.setCursor(102,23);
display.println(attempt);
display.display();
}
///////////////////////////////////////////////////////////////////////////////////////////////TESTING LIGHTS/////////////////////////////////////////////////////////////////////////////////////////////////////
void test_lights() {
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" Lights test ");
display.drawLine(0,8,display.width(),8, WHITE);
display.setCursor(0,18);
display.println(" ---START---");
display.display();
lights.Start();
delay(600);
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" Lights test ");
display.drawLine(0,8,display.width(),8, WHITE);
display.setCursor(0,18);
display.println(" ---STOP---");
display.display();
lights.Stop();
delay(600);
}
///////////////////////////////////////////////////////////////////////////////////////////////TESTING WATER PUMP/////////////////////////////////////////////////////////////////////////////////////////////////////
void test_pump() {
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" Watering test ");
display.drawLine(0,8,display.width(),8, WHITE);
display.setCursor(0,18);
display.println(" ---START---");
display.display();
pump.Start();
delay(400);
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" Watering test ");
display.drawLine(0,8,display.width(),8, WHITE);
display.setCursor(0,18);
display.println(" ---STOP---");
display.display();
pump.Stop();
delay(800);
}
///////////////////////////////////////////////////////////////////////////////////////////////TESTING SOILSENSOR/////////////////////////////////////////////////////////////////////////////////////////////////////
void test_soil() {
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" The plantbot test");
display.drawLine(0,8,display.width(),8, WHITE);
display.setCursor(0,18);
display.println(" ---START---");
display.display();
//soil.start();
delay(600);
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" The plantbot test");
display.drawLine(0,8,display.width(),8, WHITE);
display.setCursor(0,18);
display.println(" ---STOP---");
display.display();
//soil.stop();
delay(600);
}