-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyahp.ino
369 lines (303 loc) · 10.8 KB
/
yahp.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
/*
Yet Another Hydroponics Project (YAHP)
Author: Riccardo Finotello
A first project using a ESP32 Dev Module. A simple hydroponics capable to decide
whether to irrigate our poor plants.
Arduino IoT cloud reference sketch: https://create.arduino.cc/cloud/things/01230fa2-09ad-4d33-81da-7b189e58c540
The following variables are automatically generated and updated when changes are made to the Thing
CloudSwitch fanButton;
CloudSwitch fanSwitch;
CloudSwitch lightButton;
CloudSwitch lighting;
CloudSwitch lightSwitch;
CloudSwitch nebulizerButton;
CloudSwitch nebulizerSwitch;
CloudSwitch nebulizing;
CloudSwitch ventilation;
CloudSwitch waterButton;
CloudSwitch watering;
CloudSwitch waterSwitch;
CloudTemperatureSensor temperature;
CloudLuminousIntensity luminosity;
CloudPercentage day_intensity;
CloudRelativeHumidity humidity;
CloudRelativeHumidity moist_0;
CloudRelativeHumidity moist_1;
CloudRelativeHumidity moist_2;
CloudRelativeHumidity moist_3;
CloudRelativeHumidity moisture;
CloudSchedule onScheduler;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "yahpProperties.h"
#include "thingProperties.h"
// Variables
int SUNRISE_HOUR = 0; // hour of sunrise
int SUNRISE_MINUTE = 0; // minute sunrise
int SUNSET_HOUR = 0; // hour of sunset
int SUNSET_MINUTE = 0; // minute of sunset
DHT dht(DHTPIN, DHTTYPE); // humidity sensor
RTC_DS3231 rtc; // real time clock
DateTime now; // RTC now
int* yearPeriod; // sunrise and set time (array - shape[4])
int day_minutes = 0; // minutes of light in the day
int wait = 0; // wait interval
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
// Initialize YAHP
Serial.print("\n");
Serial.print("*************************\n");
Serial.print(" Initializing YAHP \n");
Serial.print(" \n");
Serial.print(" auth: thesfinox \n");
Serial.print(" ver: 3.0.0 \n");
Serial.print("*************************\n\n");
// Init the RTC
if (! rtc.begin()) {
Serial.println("Error: couldn't find RTC!");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("Error: RTC lost power, setting time...");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
// Set the builtin led to output
pinMode(NEBULIZERPIN, OUTPUT);
pinMode(WATERPIN, OUTPUT);
pinMode(LIGHTPIN_0, OUTPUT);
pinMode(LIGHTPIN_1, OUTPUT);
pinMode(FANPIN, OUTPUT);
pinMode(MOIST_0, INPUT);
pinMode(MOIST_1, INPUT);
pinMode(MOIST_2, INPUT);
pinMode(MOIST_3, INPUT);
// Initialize the DHT interface
dht.begin();
// Determine night and day boundaries
// See: https://www.worlddata.info/europe/france/sunset.php
now = rtc.now();
yearPeriod = onYearPeriod(now.month());
SUNRISE_HOUR = yearPeriod[0];
SUNRISE_MINUTE = yearPeriod[1];
SUNSET_HOUR = yearPeriod[2];
SUNSET_MINUTE = yearPeriod[3];
day_minutes = (SUNSET_HOUR*60 + SUNSET_MINUTE) - (SUNRISE_HOUR*60 + SUNRISE_MINUTE);
Serial.print("Date: ");
Serial.print(now.day());
Serial.print("/");
Serial.print(now.month());
Serial.print("/");
Serial.print(now.year());
Serial.print(" ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.print(now.second());
Serial.print("\n");
Serial.print("Sunrise: ");
Serial.print(SUNRISE_HOUR);
Serial.print(":");
Serial.print(SUNRISE_MINUTE);
Serial.print(" - Sunset: ");
Serial.print(SUNSET_HOUR);
Serial.print(":");
Serial.print(SUNSET_MINUTE);
Serial.print("\n");
// Init the irrigation system
watering = false;
waterSwitch = true;
waterButton = false;
// Init the lights
lighting = false;
lightSwitch = true;
lightButton = false;
// Init the fans
ventilation = false;
fanSwitch = true;
fanButton = false;
// Initi the nebulizers
nebulizing = false;
nebulizerSwitch = true;
nebulizerButton = false;
// Run tests
Serial.println("Launching tests...");
testOutputPin(LIGHTPIN_0);
testOutputPin(LIGHTPIN_1);
testOutputPin(NEBULIZERPIN);
testOutputPin(FANPIN);
testOutputPin(WATERPIN);
Serial.println("Tests ended...");
delay(3000);
// Start collecting data
Serial.println("HEADER");
Serial.println("time,day_intensity,humidity,temperature,luminosity,lighting,moist_0,moist_1,moist_2,moist_3,watering,nebulizing,ventilation");
Serial.println("HEADER");
}
void loop() {
ArduinoCloud.update();
if (millis() - wait > WAIT_THRESH)
{
wait = millis();
// Read RTC
now = rtc.now();
yearPeriod = onYearPeriod(now.month());
SUNRISE_HOUR = yearPeriod[0];
SUNRISE_MINUTE = yearPeriod[1];
SUNSET_HOUR = yearPeriod[2];
SUNSET_MINUTE = yearPeriod[3];
day_minutes = (SUNSET_HOUR*60 + SUNSET_MINUTE) - (SUNRISE_HOUR*60 + SUNRISE_MINUTE);
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(",");
day_intensity = onDayPeriod(now, day_minutes, DAY_DESCENT, SUNRISE_HOUR, SUNRISE_MINUTE, SUNSET_HOUR, SUNSET_MINUTE);
// day_intensity = 100.0; // DEBUG
Serial.print(day_intensity);
Serial.print(",");
// Read sensors
humidity = dht.readHumidity();
Serial.print(humidity);
Serial.print(",");
temperature = dht.readTemperature();
Serial.print(temperature);
Serial.print(",");
luminosity = map(4095 - analogRead(PHOTOPIN), 0, 4095, 0, 100);
Serial.print(luminosity);
Serial.print(",");
Serial.print(lighting);
Serial.print(",");
moist_0 = map(4095 - analogRead(MOIST_0), 0, 4095, 0, 100);
moist_0 = calibrate(moist_0, 17, 58);
moist_1 = map(4095 - analogRead(MOIST_1), 0, 4095, 0, 100);
moist_1 = calibrate(moist_1, 17, 58);
moist_2 = map(4095 - analogRead(MOIST_2), 0, 4095, 0, 100);
moist_2 = calibrate(moist_2, 17, 58);
moist_3 = map(4095 - analogRead(MOIST_3), 0, 4095, 0, 100);
moist_3 = calibrate(moist_3, 37, 85);
moisture = (moist_0 + moist_1 + moist_2 + moist_3) / 4.0;
Serial.print(moist_0);
Serial.print(",");
Serial.print(moist_1);
Serial.print(",");
Serial.print(moist_2);
Serial.print(",");
Serial.print(moist_3);
Serial.print(",");
Serial.print(watering);
Serial.print(",");
Serial.print(nebulizing);
Serial.print(",");
Serial.print(ventilation);
Serial.print("\n");
}
// Actions
if (onScheduler.isActive())
{
watering = switchConditionLogic(waterSwitch, day_intensity, moisture, MOIST_THRESH_DRY, MOIST_THRESH_WET, watering, waterButton); // decide what to do
nebulizing = switchConditionLogic(nebulizerSwitch, day_intensity, humidity, HUMIDITY_THRESH_DRY, HUMIDITY_THRESH_WET, nebulizing, nebulizerButton); // decide what to do
lighting = switchConditionLogic(lightSwitch, day_intensity, luminosity, INTENSITY_THRESHOLD_LOW, INTENSITY_THRESHOLD_HIGH, lighting, lightButton); // decide what to do
ventilation = switchConditionLogic(fanSwitch, 1.0, 100.0 - humidity, 100 - FAN_THRESH_WET, 100 - FAN_THRESH_DRY, ventilation, fanButton); // decide what to do
if (nebulizing)
{
ventilation = false;
}
} else
{
watering = false;
nebulizing = false;
lighting = false;
ventilation = false;
}
activateDigitalPin(watering, WATERPIN); // finally, activate, if needed
activateDigitalPin(nebulizing, NEBULIZERPIN); // finally, activate, if needed
activateDigitalPin(lighting, LIGHTPIN_0, LIGHTPIN_1); // finally, activate, if needed
activateDigitalPin(ventilation, FANPIN); // finally, activate, if needed
}
/*
Since WaterSwitch is READ_WRITE variable, onWaterSwitchChange() is
executed every time a new value is received from IoT Cloud.
*/
void onWaterSwitchChange() {}
/*
Since LightSwitch is READ_WRITE variable, onLightSwitchChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLightSwitchChange() {}
/*
Since LightButton is READ_WRITE variable, onLightButtonChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLightButtonChange() {}
/*
Since WaterButton is READ_WRITE variable, onWaterButtonChange() is
executed every time a new value is received from IoT Cloud.
*/
void onWaterButtonChange() {}
/*
Since FanSwitch is READ_WRITE variable, onFanSwitchChange() is
executed every time a new value is received from IoT Cloud.
*/
void onFanSwitchChange() {}
/*
Since FanButton is READ_WRITE variable, onFanButtonChange() is
executed every time a new value is received from IoT Cloud.
*/
void onFanButtonChange() {}
/*
Since Ventilation is READ_WRITE variable, onVentilationChange() is
executed every time a new value is received from IoT Cloud.
*/
void onVentilationChange() {}
/*
Since Nebulizing is READ_WRITE variable, onNebulizingChange() is
executed every time a new value is received from IoT Cloud.
*/
void onNebulizingChange() {}
/*
Since NebulizerSwitch is READ_WRITE variable, onNebulizerSwitchChange() is
executed every time a new value is received from IoT Cloud.
*/
void onNebulizerSwitchChange() {}
/*
Since NebulizerButton is READ_WRITE variable, onNebulizerButtonChange() is
executed every time a new value is received from IoT Cloud.
*/
void onNebulizerButtonChange() {}
/*
Since OnScheduler is READ_WRITE variable, onOnSchedulerChange() is
executed every time a new value is received from IoT Cloud.
*/
void onOnSchedulerChange() {
// Add your code here to act upon OnScheduler change
}