-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-watering.ino
218 lines (177 loc) · 5.43 KB
/
arduino-watering.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
//https://github.com/contrem/arduino-timer
#include <arduino-timer.h>
const int BUFFER_SIZE = 1;
char buf[BUFFER_SIZE];
uint8_t MOISTURE_SENSOR_PINS[6];
String MOISTURE_SENSOR_IDS[6];
int MOISTURE_SENSOR_DRY[6];
int MOISTURE_SENSOR_WET[6];
uint8_t WATER_OUTLET_PINS[4];
int WATER_OUTLET_STATES[4];
String WATER_OUTLET_IDS[4];
Timer<>::Task WATER_OUTLET_OFF_TIMERS[4];
String command;
auto timer = timer_create_default();
void setWaterOutletState(int outletIndex, bool state = false) {
if (state) {
WATER_OUTLET_STATES[outletIndex] = LOW;
if (WATER_OUTLET_OFF_TIMERS[outletIndex] != NULL) {
timer.cancel(WATER_OUTLET_OFF_TIMERS[outletIndex]);
}
WATER_OUTLET_OFF_TIMERS[outletIndex] = timer.in(10000, setWaterOutletState, outletIndex);
} else {
WATER_OUTLET_STATES[outletIndex] = HIGH;
if (WATER_OUTLET_OFF_TIMERS[outletIndex] != NULL) {
timer.cancel(WATER_OUTLET_OFF_TIMERS[outletIndex]);
}
}
}
void setup() {
analogReference(EXTERNAL);
// Water outlets pins
WATER_OUTLET_PINS[0] = 5;
WATER_OUTLET_PINS[1] = 6;
WATER_OUTLET_PINS[2] = 7;
WATER_OUTLET_PINS[3] = 8;
// Water outlet IDs
WATER_OUTLET_IDS[0] = "1";
WATER_OUTLET_IDS[1] = "2";
WATER_OUTLET_IDS[2] = "3";
WATER_OUTLET_IDS[3] = "4";
// Moisture sensor pins
MOISTURE_SENSOR_PINS[0] = A0;
MOISTURE_SENSOR_PINS[1] = A1;
MOISTURE_SENSOR_PINS[2] = A2;
MOISTURE_SENSOR_PINS[3] = A3;
MOISTURE_SENSOR_PINS[4] = A4;
MOISTURE_SENSOR_PINS[5] = A5;
// Moisture sensor IDs
MOISTURE_SENSOR_IDS[0] = "1";
MOISTURE_SENSOR_IDS[1] = "2";
MOISTURE_SENSOR_IDS[2] = "3";
MOISTURE_SENSOR_IDS[3] = "4";
MOISTURE_SENSOR_IDS[4] = "5";
MOISTURE_SENSOR_IDS[5] = "6";
// Moisture sensor dry thresholds
MOISTURE_SENSOR_DRY[0] = 500;
MOISTURE_SENSOR_DRY[1] = 500;
MOISTURE_SENSOR_DRY[2] = 500;
MOISTURE_SENSOR_DRY[3] = 500;
MOISTURE_SENSOR_DRY[4] = 500;
MOISTURE_SENSOR_DRY[5] = 500;
// Moisture sensor wet thresholds
MOISTURE_SENSOR_WET[0] = 240;
MOISTURE_SENSOR_WET[1] = 240;
MOISTURE_SENSOR_WET[2] = 240;
MOISTURE_SENSOR_WET[3] = 240;
MOISTURE_SENSOR_WET[4] = 240;
MOISTURE_SENSOR_WET[5] = 240;
// Set water outlet pins up
for (int i = 0; i < sizeof(WATER_OUTLET_PINS); i = i + 1) {
pinMode(WATER_OUTLET_PINS[i], OUTPUT);
digitalWrite(WATER_OUTLET_PINS[i], HIGH);
setWaterOutletState(i, false);
}
// Comms
Serial.begin(9600);
timer.every(5000, sendHeartbeat);
}
void sendSensorReading(int sensorIndex) {
int readingRaw = analogRead(
MOISTURE_SENSOR_PINS[sensorIndex]
);
Serial.print("MS:" + MOISTURE_SENSOR_IDS[sensorIndex] + ":");
Serial.print(readingRaw);
Serial.print(":");
Serial.println(translateToMoistureReadingPercentage(sensorIndex, readingRaw));
}
void sendWaterOutletState(int outletIndex) {
String realState;
String setState;
// Below look inversed because LOW is actually 'water on' due to sinking current turning the relay on
if (digitalRead(WATER_OUTLET_PINS[outletIndex])) {
realState = "0";
} else {
realState = "1";
}
if (WATER_OUTLET_STATES[outletIndex]) {
setState = "0";
} else {
setState = "1";
}
Serial.println("WO:" + WATER_OUTLET_IDS[outletIndex] + ":" + realState + ":" + setState);
}
int translateToMoistureReadingPercentage(int sensorIndex, int readingRaw) {
int percentage = map(
readingRaw,
MOISTURE_SENSOR_DRY[sensorIndex],
MOISTURE_SENSOR_WET[sensorIndex],
0,
100
);
if (percentage > 100) {
percentage = 100;
}
if (percentage < 0) {
percentage = 0;
}
return percentage;
}
bool sendHeartbeat() {
Serial.println("HEARTBEAT");
return true;
}
void loop() {
timer.tick();
while (Serial.available()) {
int rlen = Serial.readBytes(buf, BUFFER_SIZE);
for(int i = 0; i < rlen; i++) {
command = buf[i];
if (command.equals("a")) { // Water off
for (int i = 0; i < sizeof(WATER_OUTLET_PINS); i++) {
setWaterOutletState(i, false);
}
} else if (command.equals("b")) { // Water on
for (int i = 0; i < sizeof(WATER_OUTLET_PINS); i++) {
setWaterOutletState(i, true);
}
} else if (command.equals("c")) {
setWaterOutletState(0, true);
} else if (command.equals("d")) {
setWaterOutletState(0, false);
} else if (command.equals("e")) {
WATER_OUTLET_STATES[1] = LOW;
setWaterOutletState(1, true);
} else if (command.equals("f")) {
setWaterOutletState(1, false);
} else if (command.equals("g")) {
setWaterOutletState(2, true);
} else if (command.equals("h")) {
setWaterOutletState(2, false);
} else if (command.equals("i")) {
setWaterOutletState(3, true);
} else if (command.equals("j")) {
setWaterOutletState(3, false);
} else if (command.equals("k")) { // Send sensor readings
Serial.println("READINGS_START");
for (int i = 0; i < sizeof(MOISTURE_SENSOR_PINS); i++) {
sendSensorReading(i);
}
Serial.println("READINGS_END");
} else if (command.equals("l")) { // Send water outlet states
Serial.println("OUTLETS_START");
for (int i = 0; i < sizeof(WATER_OUTLET_PINS); i++) {
sendWaterOutletState(i);
}
Serial.println("OUTLETS_END");
}
}
}
for (int i = 0; i < sizeof(WATER_OUTLET_PINS); i++) {
digitalWrite(
WATER_OUTLET_PINS[i],
WATER_OUTLET_STATES[i]
);
}
delay(50);
}