-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathGradyHillhouse_Garduino.ino
282 lines (237 loc) · 7.26 KB
/
GradyHillhouse_Garduino.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
/*
Automatic Garden Waterer and Datalogger
Grady Hillhouse (March 2015)
*/
#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include "SD.h"
#define DHTTYPE DHT22
#define ECHO_TO_SERIAL 1 //Sends datalogging to serial if 1, nothing if 0
#define LOG_INTERVAL 360000 //milliseconds between entries (6 minutes = 360000)
const int soilTempPin = A0;
const int soilMoisturePin = A1;
const int sunlightPin = A2;
const int dhtPin = 2;
const int chipSelect = 10;
const int LEDPinGreen = 6;
const int LEDPinRed = 7;
const int solenoidPin = 3;
const int wateringTime = 600000; //Set the watering time (10 min for a start)
const float wateringThreshold = 15; //Value below which the garden gets watered
DHT dht(dhtPin, DHTTYPE);
RTC_DS1307 rtc;
float soilTemp = 0; //Scaled value of soil temp (degrees F)
float soilMoistureRaw = 0; //Raw analog input of soil moisture sensor (volts)
float soilMoisture = 0; //Scaled value of volumetric water content in soil (percent)
float humidity = 0; //Relative humidity (%)
float airTemp = 0; //Air temp (degrees F)
float heatIndex = 0; //Heat index (degrees F)
float sunlight = 0; //Sunlight illumination in lux
bool watering = false;
bool wateredToday = false;
DateTime now;
File logfile;
/*
Soil Moisture Reference
Air = 0%
Really dry soil = 10%
Probably as low as you'd want = 20%
Well watered = 50%
Cup of water = 100%
*/
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
digitalWrite(LEDPinRed, HIGH);
while(1);
}
void setup() {
//Initialize serial connection
Serial.begin(9600); //Just for testing
Serial.println("Initializing SD card...");
pinMode(chipSelect, OUTPUT); //Pin for writing to SD card
pinMode(LEDPinGreen, OUTPUT); //LED green pint
pinMode(LEDPinRed, OUTPUT); //LED red pin
pinMode(solenoidPin, OUTPUT); //solenoid pin
digitalWrite(solenoidPin, LOW); //Make sure the valve is off
analogReference(EXTERNAL); //Sets the max voltage from analog inputs to whatever is connected to the Aref pin (should be 3.3v)
//Establish connection with DHT sensor
dht.begin();
//Establish connection with real time clock
Wire.begin();
if (!rtc.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
//Set the time and date on the real time clock if necessary
if (! rtc.isrunning()) {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
//Check if SD card is present and can be initialized
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
Serial.println("Card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
logfile.println("Unix Time (s),Date,Soil Temp (F),Air Temp (F),Soil Moisture Content (%),Relative Humidity (%),Heat Index (F),Sunlight Illumination (lux),Watering?"); //HEADER
#if ECHO_TO_SERIAL
Serial.println("Unix Time (s),Date,Soil Temp (F),Air Temp (F),Soil Moisture Content (%),Relative Humidity (%),Heat Index (F),Sunlight Illumination (lux),Watering?");
#endif ECHO_TO_SERIAL// attempt to write out the header to the file
now = rtc.now();
}
void loop() {
//delay software
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
//Three blinks means start of new cycle
digitalWrite(LEDPinGreen, HIGH);
delay(150);
digitalWrite(LEDPinGreen, LOW);
delay(150);
digitalWrite(LEDPinGreen, HIGH);
delay(150);
digitalWrite(LEDPinGreen, LOW);
delay(150);
digitalWrite(LEDPinGreen, HIGH);
delay(150);
digitalWrite(LEDPinGreen, LOW);
//Reset wateredToday variable if it's a new day
if (!(now.day()==rtc.now().day())) {
wateredToday = false;
}
now = rtc.now();
// log time
logfile.print(now.unixtime()); // seconds since 2000
logfile.print(",");
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print(",");
#if ECHO_TO_SERIAL
Serial.print(now.unixtime()); // seconds since 2000
Serial.print(",");
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.print(",");
#endif //ECHO_TO_SERIAL
//Collect Variables
soilTemp = (75.006 * analogRead(soilTempPin)*(3.3 / 1024)) - 42;
delay(20);
soilMoistureRaw = analogRead(soilMoisturePin)*(3.3/1024);
delay(20);
//Volumetric Water Content is a piecewise function of the voltage from the sensor
if (soilMoistureRaw < 1.1) {
soilMoisture = (10 * soilMoistureRaw) - 1;
}
else if (soilMoistureRaw < 1.3) {
soilMoisture = (25 * soilMoistureRaw) - 17.5;
}
else if (soilMoistureRaw < 1.82) {
soilMoisture = (48.08 * soilMoistureRaw) - 47.5;
}
else if (soilMoistureRaw < 2.2) {
soilMoisture = (26.32 * soilMoistureRaw) - 7.89;
}
else {
soilMoisture = (62.5 * soilMoistureRaw) - 87.5;
}
humidity = dht.readHumidity();
delay(20);
airTemp = dht.readTemperature(true);
delay(20);
heatIndex = dht.computeHeatIndex(airTemp,humidity);
//This is a rough conversion that I tried to calibrate using a flashlight of a "known" brightness
sunlight = pow(((((150 * 3.3)/(analogRead(sunlightPin)*(3.3/1024))) - 150) / 70000),-1.25);
delay(20);
//Log variables
logfile.print(soilTemp);
logfile.print(",");
logfile.print(airTemp);
logfile.print(",");
logfile.print(soilMoisture);
logfile.print(",");
logfile.print(humidity);
logfile.print(",");
logfile.print(heatIndex);
logfile.print(",");
logfile.print(sunlight);
logfile.print(",");
#if ECHO_TO_SERIAL
Serial.print(soilTemp);
Serial.print(",");
Serial.print(airTemp);
Serial.print(",");
Serial.print(soilMoisture);
Serial.print(",");
Serial.print(humidity);
Serial.print(",");
Serial.print(heatIndex);
Serial.print(",");
Serial.print(sunlight);
Serial.print(",");
#endif
if ((soilMoisture < wateringThreshold) && (now.hour() > 19) && (now.hour() < 22) && (wateredToday = false)) {
//water the garden
digitalWrite(solenoidPin, HIGH);
delay(wateringTime);
digitalWrite(solenoidPin, LOW);
//record that we're watering
logfile.print("TRUE");
#if ECHO_TO_SERIAL
Serial.print("TRUE");
#endif
wateredToday = true;
}
else {
logfile.print("FALSE");
#if ECHO_TO_SERIAL
Serial.print("FALSE");
#endif
}
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif
delay(50);
//Write to SD card
logfile.flush();
delay(5000);
}