-
Notifications
You must be signed in to change notification settings - Fork 0
/
Greenhouse.cpp
307 lines (292 loc) · 7.51 KB
/
Greenhouse.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2018 Marco De Nicolo
*/
#include "Greenhouse.hpp"
Greenhouse::Greenhouse(int32_t dht_pin, int32_t waterlevel_pin, int32_t moisture_pin, int32_t photoresistor_pin, int32_t fan_pin, int32_t valve_pin, int32_t light_pin)
{
this->dht_pin = dht_pin;
this->waterlevel_pin = waterlevel_pin;
this->moisture_pin = moisture_pin;
this->photoresistor_pin = photoresistor_pin;
this->fan_pin = fan_pin;
this->valve_pin = valve_pin;
this->light_pin = light_pin;
dht = new DHT_Unified(dht_pin, DHT11); //pin,type
pinMode(waterlevel_pin, OUTPUT);
pinMode(moisture_pin, OUTPUT);
pinMode(photoresistor_pin, INPUT);
pinMode(fan_pin, OUTPUT);
digitalWrite(fan_pin, HIGH);
pinMode(valve_pin, OUTPUT);
digitalWrite(valve_pin, HIGH);
pinMode(light_pin, OUTPUT);
pinMode(A0,INPUT);
dht->begin();
irrigation_time = false;
light_state = false;
fan_state = false;
valve_state = false;
day_light = false;
auto_light = true;
min_env_humidity = 20; // %
max_env_humidity = 85;
min_ground_humidity = 1; // [0-4]
max_ground_humidity = 3;
min_temperature = 10.0; // C
max_temperature = 47.0;
//water_alarm_level = 1; // [0-4]
env_humidity = 50; // %
ground_humidity = 2; // [0-4]
water_level = 2; // [0-4]
temperature = 23.0; // C
epoch = 0;
}
float Greenhouse::getTemperature()
{
return temperature;
}
float Greenhouse::getMaxTemperature()
{
return max_temperature;
}
float Greenhouse::getMinTemperature()
{
return min_temperature;
}
uint8_t Greenhouse::getEnvHumidity()
{
return env_humidity;
}
uint8_t Greenhouse::getMaxEnvHumidity()
{
return max_env_humidity;
}
uint8_t Greenhouse::getMinEnvHumidity()
{
return min_env_humidity;
}
uint8_t Greenhouse::getGroundHumidity()
{
return ground_humidity;
}
uint8_t Greenhouse::getMaxGroundHumidity()
{
return max_ground_humidity;
}
uint8_t Greenhouse::getMinGroundHumidity()
{
return min_ground_humidity;
}
uint8_t Greenhouse::getWaterLevel()
{
return water_level;
}
//uint8_t Greenhouse::getWaterAlarmLevel();
bool Greenhouse::getLightSensor()
{
return day_light;
}
bool Greenhouse::getLightState()
{
return light_state;
}
bool Greenhouse::getFanState()
{
return fan_state;
}
bool Greenhouse::getValveState()
{
return valve_state;
}
vector<TimeTable> Greenhouse::getWeekTimeTable()
{
return week_tt;
}
bool Greenhouse::getIrrigationState()
{
return irrigation_time;
}
bool Greenhouse::getAutoLight()
{
return auto_light;
}
void Greenhouse::setMaxTemperature(float temperature)
{
max_temperature = temperature;
}
void Greenhouse::setMinTemperature(float temperature)
{
min_temperature = temperature;
}
void Greenhouse::setMaxEnvHumidity(uint8_t humidity)
{
max_env_humidity = humidity;
}
void Greenhouse::setMinEnvHumidity(uint8_t humidity)
{
min_env_humidity = humidity;
}
void Greenhouse::setMaxGroundHumidity(uint8_t humidity)
{
max_ground_humidity = humidity;
}
void Greenhouse::setMinGroundHumidity(uint8_t humidity)
{
min_ground_humidity = humidity;
}
void Greenhouse::setIrrigationState(bool state)
{
irrigation_time = state;
}
void Greenhouse::setAutoLight(bool state)
{
auto_light = state;
}
void Greenhouse::turnLight(bool state)
{
/*
* turn light .........................
*/
if(state)
digitalWrite(light_pin, HIGH);
else
digitalWrite(light_pin, LOW);
light_state = state;
}
void Greenhouse::startFan(uint64_t seconds)
{
fan_t = millis();
fan_sec = seconds*1000;
fan_state = true;
digitalWrite(fan_pin,LOW); //lowtrigger
}
void Greenhouse::stopFan()
{
fan_state = false;
digitalWrite(fan_pin,HIGH);
}
void Greenhouse::startIrrigation(uint64_t seconds)
{
valve_t = millis();
valve_sec = seconds*1000;
valve_state = true;
digitalWrite(valve_pin,LOW);
}
void Greenhouse::stopIrrigation()
{
valve_state = false;
digitalWrite(valve_pin,HIGH);
}
void Greenhouse::addIrrigation(TimeTable tt)
{
if(week_tt.size() < MAX_TT_SIZE && tt.day>=0 && tt.day<=6 && tt.h>=0 && tt.h<=23 && tt.m>=0 && tt.m<=59)
{
tt.done = false;
week_tt.push_back(tt);
}
}
void Greenhouse::removeIrrigation(uint32_t index)
{
if(index >= 0 && index < week_tt.size())
week_tt.erase(week_tt.begin()+index);
}
void Greenhouse::setTime(uint64_t timer)
{
epoch = timer;
}
void Greenhouse::updateData()
{
/*
* read sensors data
*/
day_light = !digitalRead(photoresistor_pin);
sensors_event_t event;
dht->temperature().getEvent(&event);
if (isnan(event.temperature))
Serial.println("Error reading temperature!");
else
temperature = event.temperature;
dht->humidity().getEvent(&event);
if (isnan(event.relative_humidity))
Serial.println("Error reading humidity!");
else
env_humidity = event.relative_humidity;
digitalWrite(moisture_pin, HIGH);
delay(1000);
int reada = analogRead(A0);
if(reada<12)
ground_humidity = 0; //very low
else if(reada<46)
ground_humidity = 1; //low
else if(reada<90)
ground_humidity = 2; //medium
else
ground_humidity = 3; //high
digitalWrite(moisture_pin, LOW);
delay(200);
digitalWrite(waterlevel_pin, HIGH);
delay(1000);
reada = analogRead(A0);
if(reada<12)
water_level = 0; //empty
else if(reada<40)
water_level = 1; //low
else if(reada<50)
water_level = 2; //medium
else
water_level = 3; //high
digitalWrite(waterlevel_pin, LOW);
//Fan timer control and set fan_state=false
if(fan_state)
{
uint64_t now = millis();
if((now-fan_t)>=fan_sec)
stopFan();
}
//Valve timer control and set valve_state=false
if(valve_state)
{
uint64_t now = millis();
if((now-valve_t)>=valve_sec)
stopIrrigation();
}
if(temperature >= max_temperature || env_humidity >= max_env_humidity)
startFan(20);
if(temperature <= min_temperature || env_humidity <= min_env_humidity)
stopFan();
if(ground_humidity >= max_ground_humidity)
stopIrrigation();
if(ground_humidity <= min_ground_humidity)
startIrrigation(20);
if(auto_light)
{
if(!day_light && !light_state)
turnLight(true);
else if(day_light && light_state)
turnLight(false);
}
if(irrigation_time && !valve_state)
{
time_t t = epoch+millis()/1000;
struct tm * timeinfo = localtime(&t);
for(int i=0; i<week_tt.size(); i++)
{
if((timeinfo->tm_hour+UTC)%24 == 0 && timeinfo->tm_min == 0)
week_tt[i].done = false;
if(!week_tt[i].done && week_tt[i].day == timeinfo->tm_wday && week_tt[i].h == (timeinfo->tm_hour+UTC)%24 && week_tt[i].m == timeinfo->tm_min)
{
startIrrigation(30);
week_tt[i].done = true;
break;
}
}
}
/*Serial.println("Moisture Sensor: " + String(ground_humidity));
Serial.println("Water Level Sensor: " + String(water_level));
Serial.println("Photoresistor Sensor: " + String(day_light));
Serial.println("Temperature Sensor: " + String(temperature));
Serial.println("Humidity Sensor: " + String(env_humidity));*/
}