-
Notifications
You must be signed in to change notification settings - Fork 0
/
olduino.ino
296 lines (263 loc) · 6.7 KB
/
olduino.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
#include <SoftwareSerial.h>
#include "src/functions.h"
#include "src/DhtSensor.h"
#include "src/DS18B20.h"
#include "src/EcSensor.h"
#include "src/DFRobot_PH.h"
#include "src/DFRobot_CO2.h"
#include "src/Pump.h"
#include "src/Relay.h"
// running multiple pumps via a motor requires control pins
#define motor1EnA 2
#define motor1EnB 7
#define motor2EnA 8
#define motor2EnB 13
// Set hardware pins for connected devices
DhtSensor dhtSensor(32);
DS18B20 tempWaterSensor(41);
DFRobot_PH phSensor(A0);
DFRobot_CO2 CO2Sensor(A1);
// EC sensor uses software serial
SoftwareSerial ecSerial(15, 16);
EcSensor ecSensor(ecSerial);
Pump pumpPhIncr(3, 4);
Pump pumpPhDecr(5, 6);
Pump pumpEcIncr(9, 10);
Pump pumpO2Incr(11, 12);
Relay relay0(47);
Relay relay1(51);
Relay relay2(46);
Relay relay3(50);
static char serialReadBuffer[80];
int serialReadPos = 0;
static unsigned long currentTime = 0;
/*
Send commands to connected devices
deviceType: "sensor"
deviceNr:
0 = pH sensor
1 = EC sensor
commands depend on the sensor and are listed in the sensor manuals
deviceType: "pump"
deviceNr:
0 = pH+ pump
1 = pH- pump
2 = EC+ pump
3 = O2+ pump
command:
1 = turns the pump on
1,2000 = turns the pump on for 2 seconds
0 = turns pump off
"pump", "relay"
*/
void sendCommand(char *deviceType, int deviceNr, char *command)
{
if (command == NULL)
return;
if (strcmp(deviceType, "sensor") == 0)
{
switch (deviceNr)
{
case 0: // pH sensor
phSensor.write(command);
break;
case 1: // EC sensor
ecSensor.write(command);
break;
default:
Serial.print("Sensor ");
Serial.print(deviceNr);
Serial.println(" is not defined.");
break;
}
}
else if (strcmp(deviceType, "pump") == 0)
{
// split pump command in 2 comma separated components
char *com = strtok(command, ","); // read command
char *arg = strtok(NULL, " "); // read argument
int comNum = atoi(com);
int millisec = atoi(arg);
Pump *pump;
if (deviceNr == 0)
pump = &pumpPhIncr;
else if (deviceNr == 1)
pump = &pumpPhDecr;
else if (deviceNr == 2)
pump = &pumpEcIncr;
else if (deviceNr == 3)
pump = &pumpO2Incr;
else
{
Serial.print("Pump ");
Serial.print(deviceNr);
Serial.println(" is not defined.");
return;
}
if (comNum == 0)
pump->stop();
else if (comNum == 1) // turn pump on
{
if (millisec)
pump->start(millisec);
else
pump->start();
}
}
else if (strcmp(deviceType, "relay") == 0)
{
char *com = strtok(command, ","); // read command
int comNum = atoi(com);
Relay *relay;
if (deviceNr == 0)
relay = &relay0;
else if (deviceNr == 1)
relay = &relay1;
else if (deviceNr == 2)
relay = &relay2;
else if (deviceNr == 3)
relay = &relay3;
else
{
Serial.print("Relay ");
Serial.print(deviceNr);
Serial.println(" is not defined.");
return;
}
if (comNum == 0)
relay->stop();
else if (comNum == 1)
relay->start();
}
}
// Get instructions from server
void checkSerialInput()
{
// Check if the server sent new instructions
if (readline(Serial.read(), serialReadBuffer, 80, serialReadPos) > 0)
{
// Parse the string in the buffer to the command parts (receiver-nr command)
char *deviceType = strtok(serialReadBuffer, "-");
char *deviceNr = strtok(NULL, " ");
char *command = strtok(NULL, " ");
sendCommand(deviceType, atoi(deviceNr), command);
Serial.print(deviceType);
Serial.print("-");
Serial.print(deviceNr);
Serial.print(" ");
Serial.println(command);
}
}
// Get sensor feedback
void checkDevices()
{
currentTime = millis();
ecSensor.read();
phSensor.check(currentTime);
CO2Sensor.check(currentTime);
tempWaterSensor.check(currentTime);
dhtSensor.check(currentTime);
pumpPhIncr.check(currentTime);
pumpPhDecr.check(currentTime);
pumpEcIncr.check(currentTime);
pumpO2Incr.check(currentTime);
}
// Send measurements to server in JSON format
void printSensorValues()
{
// If no sensor has new measurements, don't send any message
if (ecSensor.hasNewMeasurements() ||
phSensor.hasNewMeasurements() ||
CO2Sensor.hasNewMeasurements() ||
dhtSensor.hasNewMeasurements() ||
tempWaterSensor.hasNewMeasurements())
{
// Convert float values to string
char floatHelp[10];
Serial.print('{');
if (tempWaterSensor.hasNewMeasurements())
{
float temp = tempWaterSensor.getTemperature();
if (!isnan(temp))
{
Serial.print("\"WaterTemp\": ");
dtostrf(temp, 8, 3, floatHelp);
Serial.print(floatHelp);
Serial.print(",");
}
}
if (dhtSensor.hasNewMeasurements())
{
Serial.print("\"Humidity\":");
dtostrf(dhtSensor.getHumidty(), 8, 3, floatHelp);
Serial.print(floatHelp);
Serial.print(",");
Serial.print("\"Temp\":");
dtostrf(dhtSensor.getTemperature(), 8, 3, floatHelp);
Serial.print(floatHelp);
Serial.print(",");
}
if (phSensor.hasNewMeasurements())
{
Serial.print("\"PH\":");
dtostrf(phSensor.getPh(), 8, 3, floatHelp);
Serial.print(floatHelp);
Serial.print(",");
}
if (CO2Sensor.hasNewMeasurements())
{
Serial.print("\"CO2\":");
dtostrf(CO2Sensor.getCO2(), 8, 3, floatHelp);
Serial.print(floatHelp);
Serial.print(",");
}
if (ecSensor.hasNewMeasurements())
{
Serial.print("\"EC\":");
dtostrf(ecSensor.getEc(), 8, 3, floatHelp);
Serial.print(floatHelp);
Serial.print(",");
Serial.print("\"TDS\":");
dtostrf(ecSensor.getTds(), 8, 3, floatHelp);
Serial.print(floatHelp);
Serial.print(",");
Serial.print("\"SAL\":");
dtostrf(ecSensor.getSal(), 8, 3, floatHelp);
Serial.print(floatHelp);
Serial.print(",");
Serial.print("\"GRAV\":");
dtostrf(ecSensor.getGrav(), 8, 3, floatHelp);
Serial.print(floatHelp);
Serial.print(",");
}
// Do not end JSON with a comma
Serial.print("\"Debug\":0");
Serial.println('}');
// Wait until the string was sent
Serial.flush();
}
}
void setup()
{
Serial.begin(9600);
ecSerial.begin(9600);
// Other serials are initiated in their class construction
// Initialize motor control pins
pinMode(motor1EnA, OUTPUT);
pinMode(motor1EnB, OUTPUT);
pinMode(motor2EnA, OUTPUT);
pinMode(motor2EnB, OUTPUT);
analogWrite(motor1EnA, 200);
analogWrite(motor1EnB, 200);
analogWrite(motor2EnA, 200);
analogWrite(motor2EnB, 200);
// Wait for serial port to connect. Needed for native USB port only
while (!Serial)
continue;
}
void loop()
{
checkSerialInput();
checkDevices();
printSensorValues();
}