-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreen_wall_irrigation.ino
124 lines (99 loc) · 2.64 KB
/
green_wall_irrigation.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
#include "DHT.h"
#define SEPARATOR "|"
#define ERROR_CODE_OK "0"
#define ERROR_CODE_DHT_SENSOR "1"
// define pin and sensor type for DHT
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// define pin for float switch
byte floatSwitchPin = 6;
byte led = 13; // used for testing, this will be water pump when it's connected
int receivedDataFromSerial = 0;
float receivedDataFromSerial2 = 0;
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(led, OUTPUT); // used for testing, this will be water pump when it's connected
}
void loop() {
while (Serial.available() > 0) {
receivedDataFromSerial = Serial.parseInt();
switch (receivedDataFromSerial) {
case 1:
readTemperatureAndHumidity();
break;
case 2:
readFloatSwitchValue();
break;
case 3:
// read pump state
readWaterPumpValue();
break;
case 4:
// open pump
while(receivedDataFromSerial == 4 && receivedDataFromSerial2 == 0) {
while(Serial.available() > 0){
receivedDataFromSerial2 = Serial.parseFloat();
}
}
if(receivedDataFromSerial2 != 1) {
digitalWrite(led, HIGH);
delay(receivedDataFromSerial2);
digitalWrite(led, LOW);
receivedDataFromSerial = 0;
receivedDataFromSerial2 = 0;
}
break;
case 5:
// close pump
digitalWrite(led, LOW);
break;
default:
// if nothing else matches, do the default
// default is optional
Serial.println("Error!");
}
Serial.println();
delay(1000);
}
// send data
delay(2000);
readTemperatureAndHumidity();
readFloatSwitchValue();
readWaterPumpValue();
Serial.println();
}
String readTemperatureAndHumidity() {
// read humidity
float humidity = dht.readHumidity();
// read temperature as Celsius
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
return ERROR_CODE_DHT_SENSOR;
}
Serial.print(temperature);
Serial.print(SEPARATOR);
Serial.print(humidity);
Serial.print(SEPARATOR);
return ERROR_CODE_OK;
}
void readFloatSwitchValue() {
if(digitalRead(floatSwitchPin) == HIGH) {
Serial.print("0");
Serial.print(SEPARATOR);
} else {
Serial.print("1");
Serial.print(SEPARATOR);
}
}
void readWaterPumpValue() {
if(digitalRead(led) == HIGH) {
Serial.print("1");
Serial.print(SEPARATOR);
} else {
Serial.print("0");
Serial.print(SEPARATOR);
}
}