-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_garden.ino
287 lines (244 loc) · 6.9 KB
/
arduino_garden.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
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include "RTClib.h"
#define PIN_1_MOTOR_A 4
#define PIN_2_MOTOR_A 5
#define PIN_1_MOTOR_B 6
#define PIN_2_MOTOR_B 7
#define TEMP_PIN 2
#define MOISTURE_PIN_1 A0
#define MOISTURE_PIN_2 A1
#define max_watering_times_per_pot 3
#define number_of_pots 2
#define check_time 18000000
//#define check_time 10000
#define DELAY_TIME 10000
#define DRY 550
#define WET 200
RTC_DS1307 rtc;
OneWire wires (TEMP_PIN);
DallasTemperature sensors (&wires);
SoftwareSerial bt_serial(10,11);
unsigned long current_check_time = 0;
struct motor {
int motor_pin_1;
int motor_pin_2;
int moisture_pin;
};
struct garden_sensors {
int current_moisture_value_sensor_1;
int current_moisture_value_sensor_2;
float current_temp;
int current_day;
int watering_times[number_of_pots];
motor motors[number_of_pots];
int previous_time;
int previous_moisture_value_sensor_1;
int previous_moisture_value_sensor_2;
int previous_day;
float previous_temp;
};
struct garden_sensors garden;
void setup(void)
{
Serial.begin(9600);
bt_serial.begin(38400);
init_rtc();
init_motors();
init_sensors();
init_garden();
}
void loop(void)
{
update_garden_values();
if(current_check_time<=0){
check_water();
current_check_time = check_time;
}
//print_current_values();
print_bt_serial_current_values();
delay(DELAY_TIME);
current_check_time -= DELAY_TIME;
}
void start_motor(int motor_pin_1, int motor_pin_2) {
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
}
void stop_motor(int motor_pin_1, int motor_pin_2) {
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, HIGH);
}
bool is_motor_running(int motor_pin_1, int motor_pin_2) {
int value1 = digitalRead(motor_pin_1);
int value2 = digitalRead(motor_pin_2);
if ((value1 == HIGH && value2 == LOW) || (value1 == LOW && value2 == HIGH)) {
return true;
}
return false;
}
void init_sensors() {
sensors.begin();
}
void init_motors() {
garden.motors[0].motor_pin_1 = PIN_1_MOTOR_A;
garden.motors[0].motor_pin_2 = PIN_2_MOTOR_A;
garden.motors[0].moisture_pin = MOISTURE_PIN_2;
garden.motors[1].motor_pin_1 = PIN_1_MOTOR_B;
garden.motors[1].motor_pin_2 = PIN_2_MOTOR_B;
garden.motors[1].moisture_pin = MOISTURE_PIN_1;
}
void init_garden() {
garden.current_moisture_value_sensor_1 = 0;
garden.current_moisture_value_sensor_2 = 0;
garden.current_temp = 0;
garden.current_day = get_day();
garden.previous_moisture_value_sensor_1 = 0;
garden.previous_moisture_value_sensor_2 = 0;
garden.previous_temp = 0;
garden.previous_day = get_day();
reset_watering_times();
}
float get_temperature() {
sensors.requestTemperatures();
return sensors.getTempCByIndex(0);
}
void init_rtc() {
while (!rtc.begin());
}
void adjust() {
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
DateTime get_date_time() {
return rtc.now();
}
int get_year() {
return rtc.now().year();
}
int get_month() {
return rtc.now().month();
}
int get_day() {
return rtc.now().day();
}
int get_hour() {
return rtc.now().hour();
}
int get_minute() {
return rtc.now().minute();
}
int get_second() {
return rtc.now().second();
}
int get_moisture_value(int sensor) {
return analogRead(sensor);
}
void check_water() {
for (int i = 0; i < number_of_pots; i++) {
if (garden.watering_times[i] < max_watering_times_per_pot) {
water(i);
}
}
}
void water(int motor_index) {
int current_month = get_month();
int current_hour = get_hour();
int water_hour_min = 0;
int water_hour_max = 99;
if (current_month >= 4 && current_month <= 10) {
water_hour_min = 6;
water_hour_max = 22;
} else {
water_hour_min = 8;
water_hour_max = 20;
}
if (current_hour >= water_hour_min && current_hour <= water_hour_max) {
if ((get_moisture_value(garden.motors[motor_index].moisture_pin)) > 300) { //soil saturation value depends on the plant
start_motor(garden.motors[motor_index].motor_pin_1, garden.motors[motor_index].motor_pin_2);
Serial.print("Starting motor ");
bt_serial.print("Starting motor ");
Serial.println(motor_index);
bt_serial.println(motor_index);
delay(4000);
stop_motor(garden.motors[motor_index].motor_pin_1, garden.motors[motor_index].motor_pin_2);
garden.watering_times[motor_index]++;
}
}
}
void update_garden_values() {
garden.previous_moisture_value_sensor_1 = garden.current_moisture_value_sensor_1;
garden.previous_moisture_value_sensor_2 = garden.current_moisture_value_sensor_2;
garden.previous_temp = garden.current_temp;
garden.previous_day = garden.current_day;
garden.current_temp = get_temperature();
garden.current_moisture_value_sensor_1 = get_moisture_value(MOISTURE_PIN_1);
garden.current_moisture_value_sensor_2 = get_moisture_value(MOISTURE_PIN_2);
garden.current_day = get_day();
if (garden.current_day != garden.previous_day) {
reset_watering_times();
}
}
void reset_watering_times() {
for (int i = 0; i < number_of_pots; i++) {
garden.watering_times[i] = 0;
}
}
void print_current_values() {
Serial.print("Moisture A0 Sensor: ");
Serial.println(garden.current_moisture_value_sensor_1);
Serial.print("Moisture A1 Sensor: ");
Serial.println(garden.current_moisture_value_sensor_2);
Serial.print("Temperature: ");
Serial.println(garden.current_temp);
for(int i=0;i<number_of_pots;i++){
Serial.print("Watering times for pot ");
Serial.print(i);
Serial.print(" ");
Serial.println(garden.watering_times[i]);
}
DateTime now = get_date_time();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
void print_bt_serial_current_values() {
bt_serial.println("-------------------------------------");
bt_serial.print("Moisture A0 Sensor: ");
bt_serial.println(garden.current_moisture_value_sensor_1);
bt_serial.print("Moisture A1 Sensor: ");
bt_serial.println(garden.current_moisture_value_sensor_2);
bt_serial.print("Temperature: ");
bt_serial.println(garden.current_temp);
for(int i=0;i<number_of_pots;i++){
bt_serial.print("Watering times for pot ");
bt_serial.print(i);
bt_serial.print(" ");
bt_serial.println(garden.watering_times[i]);
}
DateTime now = get_date_time();
bt_serial.print(now.year(), DEC);
bt_serial.print('/');
bt_serial.print(now.month(), DEC);
bt_serial.print('/');
bt_serial.print(now.day(), DEC);
bt_serial.print(" ");
bt_serial.print(now.hour(), DEC);
bt_serial.print(':');
bt_serial.print(now.minute(), DEC);
bt_serial.print(':');
bt_serial.print(now.second(), DEC);
bt_serial.println();
bt_serial.println("-------------------------------------");
}