forked from thaussma/OpenGarden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenGarden.cpp
596 lines (506 loc) · 13.6 KB
/
OpenGarden.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* OpenGarden sensor platform for Arduino from Cooking-hacks.
*
* Copyright (C) Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Version: 2.3
* Design: David Gascón
* Implementation: Victor Boria, Luis Martin & Jorge Casanova
*/
#include <Console.h>
#include "OpenGarden.h"
#include "Wire.h"
#include "DHT.h"
#include "OneWire.h"
#include "RF12.h"
#include "Ports.h"
#include "RTClib.h"
#include "DallasTemperature.h"
#include "RemoteReceiver.h"
#include "RemoteSwitch.h"
#include "filter.h"
RTC_DS1307 RTC;
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
OneWire oneWire(soilTemperaturePin); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
OpenGardenClass::OpenGardenClass()
{
}
//Sensor initializing functions:
void OpenGardenClass::initSensors(void){
pinMode(sensorPowerPin, OUTPUT);
pinMode(ecPowerPin, OUTPUT);
digitalWrite(sensorPowerPin, LOW);
digitalWrite(ecPowerPin, LOW);
sensors.begin();
dht.begin();
}
void OpenGardenClass::sensorPowerON(void){
digitalWrite(ecPowerPin, LOW);
delay(100);
digitalWrite(sensorPowerPin, HIGH);
}
void OpenGardenClass::sensorPowerOFF(void){
digitalWrite(ecPowerPin, LOW);
digitalWrite(sensorPowerPin, LOW);
}
//Transceiver functions:
void OpenGardenClass::initRF(void){
rf12_initialize(myNodeID,RF12_433MHZ,100); // (GatewayNodeID,freq,network)
rf12_recvDone();
}
void OpenGardenClass::receiveFromNode(void){
if (rf12_recvDone() && rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0) {
nodeID = rf12_hdr & 0x1F; // get node ID
airPacket = *(Payload*) rf12_data;
if (RF12_WANTS_ACK) { // Send ACK if requested
rf12_sendStart(RF12_ACK_REPLY, 0, 0);
}
}
//Receive data and store the last value in buffer
if (nodeID == 1)
nodesBuffer[0] = airPacket; //Node:1->Buffer:0
else if (nodeID == 2)
nodesBuffer[1] = airPacket; //Node:2->Buffer:1
else if (nodeID == 3)
nodesBuffer[2] = airPacket; //Node:3->Buffer:2
else if (nodeID == 4)
nodesBuffer[3] = airPacket; //Node:4->Buffer:3
else if (nodeID == 5)
nodesBuffer[4] = airPacket; //Node:5->Buffer:4
else if (nodeID == 6)
nodesBuffer[5] = airPacket; //Node:6->Buffer:5
else if (nodeID == 7)
nodesBuffer[6] = airPacket; //Node:7->Buffer:6
}
Payload OpenGardenClass::getNodeData(uint8_t node){
if (node > 10)
return nodesBuffer[0];
else
return nodesBuffer[node];
}
void OpenGardenClass::printNode(Payload nodePacket){
if (nodePacket.temperature == 0 && nodePacket.humidity == 0 && nodePacket.moisture == 0 && nodePacket.light == 0 && nodePacket.supplyV == 0){
Console.println("No node connected");
}
else{
Console.print("Temperature:");
Console.print(nodePacket.temperature);
Console.println("*C");
Console.print("Humidity:");
Console.print(nodePacket.humidity);
Console.println("%RH");
Console.print("Soil Moisture:");
Console.println(nodePacket.moisture);
Console.print("Luminosity:");
Console.print(nodePacket.light);
Console.println("%");
Console.print("Battery Voltage:");
Console.print(nodePacket.supplyV);
Console.println("mV");
}
}
void OpenGardenClass::debugRF(void){
if (rf12_recvDone() && rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0) {
nodeID = rf12_hdr & 0x1F; // get node ID
airPacket = *(Payload*) rf12_data;
Console.print("Received packet from node ");
Console.println(nodeID);
Console.print("Temperature:");
Console.print(airPacket.temperature);
Console.println("*C");
Console.print("Humidity:");
Console.print(airPacket.humidity);
Console.println("%RH");
Console.print("Soil Moisture:");
Console.println(airPacket.moisture);
Console.print("Luminosity:");
Console.println(airPacket.light);
Console.print("Battery Voltage:");
Console.print(airPacket.supplyV);
Console.println("mV");
Console.println("***************");
if (RF12_WANTS_ACK) { // Send ACK if requested
rf12_sendStart(RF12_ACK_REPLY, 0, 0);
}
}
}
//RTC functions:
void OpenGardenClass::initRTC(void){
Wire.begin();
RTC.begin();
}
void OpenGardenClass::setTime(void){
RTC.adjust(DateTime(__DATE__, __TIME__));
}
DateTime OpenGardenClass::getTime(void){
return RTC.now();
}
void OpenGardenClass::printTime(DateTime now){
Console.print(now.month(), DEC);
Console.print("/");
Console.print(now.day(), DEC);
Console.print("/");
Console.print(now.year(), DEC);
Console.print(" ");
switch (now.dayOfWeek()) // Friendly printout the weekday
{
case 1:
Console.print("MON");
break;
case 2:
Console.print("TUE");
break;
case 3:
Console.print("WED");
break;
case 4:
Console.print("THU");
break;
case 5:
Console.print("FRI");
break;
case 6:
Console.print("SAT");
break;
case 7:
Console.print("SUN");
break;
}
Console.print(" ");
if(now.hour()<10){
switch (now.hour())
{
case 0:
Console.print("00");
break;
case 1:
Console.print("01");
break;
case 2:
Console.print("02");
break;
case 3:
Console.print("03");
break;
case 4:
Console.print("04");
break;
case 5:
Console.print("05");
break;
case 6:
Console.print("06");
break;
case 7:
Console.print("07");
break;
case 8:
Console.print("08");
break;
case 9:
Console.print("09");
break;
}
}
else{
Console.print(now.hour(), DEC);
}
Console.print(":");
if(now.minute()<10){
switch (now.minute())
{
case 0:
Console.print("00");
break;
case 1:
Console.print("01");
break;
case 2:
Console.print("02");
break;
case 3:
Console.print("03");
break;
case 4:
Console.print("04");
break;
case 5:
Console.print("05");
break;
case 6:
Console.print("06");
break;
case 7:
Console.print("07");
break;
case 8:
Console.print("08");
break;
case 9:
Console.print("09");
break;
}
}
else{
Console.print(now.minute(), DEC);
}
Console.print(":");
if(now.second()<10){
switch (now.second())
{
case 0:
Console.print("00");
break;
case 1:
Console.print("01");
break;
case 2:
Console.print("02");
break;
case 3:
Console.print("03");
break;
case 4:
Console.print("04");
break;
case 5:
Console.print("05");
break;
case 6:
Console.print("06");
break;
case 7:
Console.print("07");
break;
case 8:
Console.print("08");
break;
case 9:
Console.print("09");
break;
}
}
else{
Console.print(now.second(), DEC);
}
}
//Irrigation functions:
void OpenGardenClass::initIrrigation(int out) {
if (out == 1)
{
pinMode(irrigation1, OUTPUT);
digitalWrite(irrigation1, LOW);
}
if (out == 2)
{
pinMode(irrigation2, OUTPUT);
digitalWrite(irrigation2, LOW);
}
if (out == 3)
{
pinMode(irrigation3, OUTPUT);
digitalWrite(irrigation3, LOW);
}
}
void OpenGardenClass::irrigationON(int out) {
if (out == 1)
{
digitalWrite(irrigation1, HIGH);
}
if (out == 2)
{
digitalWrite(irrigation2, HIGH);
}
if (out == 3)
{
digitalWrite(irrigation3, HIGH);
}
}
void OpenGardenClass::irrigationOFF(int out) {
if (out == 1)
{
digitalWrite(irrigation1, LOW);
}
if (out == 2)
{
digitalWrite(irrigation2, LOW);
}
if (out == 3)
{
digitalWrite(irrigation3, LOW);
}
}
//Power strip functions:
void OpenGardenClass::sendPowerStrip(unsigned long plug){
RemoteSwitch::sendTelegram(plug,3);
}
//Sensor reading functions
int OpenGardenClass::readSoilMoisture() {
return analogRead(soilMoisturePin);
}
float OpenGardenClass::readAirTemperature() {
return dht.readTemperature();
}
float OpenGardenClass::readAirHumidity() {
return dht.readHumidity();
}
float OpenGardenClass::readSoilTemperature() {
sensors.requestTemperatures(); // Send the command to get temperatures
delay(500);
return sensors.getTempCByIndex(0);
}
int OpenGardenClass::readLuminosity() {
float luminosity = analogRead(luminosityPin);
return ((luminosity/1023)*100);
}
void OpenGardenClass::calibratepH(int _calibration_point_4,int _calibration_point_7,int _calibration_point_10){
calibration_point_10 = _calibration_point_10;
calibration_point_7 = _calibration_point_7;
calibration_point_4 = _calibration_point_4;
}
int OpenGardenClass::readpH(){
float value_array[FILTER_SAMPLES];
// Take some measurements to filter the signal noise and glitches
for(int i = 0; i < FILTER_SAMPLES; i++)
{
//Read from the ADC channel selected
value_array[i] = analogRead(pHLevelPin);
}
delay(100);
pH = myFilter.median(value_array,FILTER_SAMPLES);
return (pH*5000.0)/1024.0;
}
float OpenGardenClass::pHConversion(int _mvpH) {
float sensitivity;
// Two ranges calibration
if (_mvpH > calibration_point_7 ) {
// The sensitivity is calculated using the other two calibration values
// Asumme that the pH sensor is lineal in the range.
// sensitivity = pHVariation / volts
// Divide by 3 = (pH) 7 - (pH) 4
sensitivity = (calibration_point_4-calibration_point_7)/3;
// The value at pH 7.0 is taken as reference
// => Units balance => 7 (pH) + (volts) / ((pH) / (volts))
return 7.0 + (calibration_point_7-_mvpH) / sensitivity;
// | |
// (pH 7 voltage - Measured volts) = Variation from the reference
}
else {
// The sensitivity is calculated using the other two calibration values
sensitivity = (calibration_point_7-calibration_point_10) / 3;
return 7.0 + (calibration_point_7-_mvpH)/sensitivity;
}
}
void OpenGardenClass::calibrateEC(float _point_1_cond,float _point_1_cal,float _point_2_cond,float _point_2_cal){
point_1_cond = _point_1_cond;
point_1_cal = _point_1_cal;
point_2_cond = _point_2_cond;
point_2_cal = _point_2_cal;
}
float OpenGardenClass::ECConversion(unsigned long _resistanceEC) {
float value;
float SW_condK;
float SW_condOffset;
// Calculates the cell factor of the conductivity sensor and the offset from the calibration values
SW_condK = point_1_cond * point_2_cond * ((point_1_cal-point_2_cal)/(point_2_cond-point_1_cond));
SW_condOffset = (point_1_cond * point_1_cal-point_2_cond*point_2_cal)/(point_2_cond-point_1_cond);
// Converts the resistance of the sensor into a conductivity value
value = SW_condK * 1 / (_resistanceEC+SW_condOffset);
return value;
}
float OpenGardenClass::readResistanceEC(void) {
//Check if sensorPowerPin is HIGH or LOW because
//sensorPowerPin and ecPowerPin NEVER must be connected at the same time
if(digitalRead(sensorPowerPin)==HIGH){
digitalWrite(sensorPowerPin, LOW);
delay(100);
digitalWrite(ecPowerPin, HIGH);
delay(100);
float ecmeasure = calculateResistanceEC();
digitalWrite(ecPowerPin, LOW);
digitalWrite(sensorPowerPin, HIGH);
return ecmeasure;
}
if(digitalRead(sensorPowerPin)==LOW){
digitalWrite(ecPowerPin, HIGH);
delay(100);
float ecmeasure = calculateResistanceEC();
digitalWrite(ecPowerPin, LOW);
digitalWrite(sensorPowerPin, LOW);
return ecmeasure;
}
}
float OpenGardenClass::calculateResistanceEC(void) {
float value_array[FILTER_SAMPLES];
// Take some measurements to filter the signal noise and glitches
for(int i = 0; i < FILTER_SAMPLES; i++)
{
//Read from the ADC channel selected
value_array[i] = analogRead(ecLevelPin);
}
delay(100);
EC = myFilter.median(value_array,FILTER_SAMPLES);
if(EC <= 11){
resistance = EC/0.050;
}
else if((EC > 11) && (EC <= 23)){
resistance = ((EC-0.44)/0.0480);
}
else if((EC > 23) && (EC <= 47)){
resistance = ((EC-1.7170)/0.0453);
}
else if((EC > 47) && (EC <= 94)){
resistance = ((EC-7.8333)/0.0392);
}
else if((EC > 94) && (EC <= 162)){
resistance = ((EC-34.16)/0.0272);
}
else if((EC > 162) && (EC <= 186)){
resistance = ((EC-36.667)/0.0267);
}
else if((EC > 186) && (EC <= 259)){
resistance = ((EC-93.0909)/0.0161);
}
else if((EC > 259) && (EC <= 310)){
resistance = ((EC-157.0)/0.0102);
}
else if((EC > 310) && (EC <= 358)){
resistance = ((EC-207.1429)/0.0069);
}
else if((EC > 358) && (EC <= 401)){
resistance = ((EC-272.0)/0.0039);
}
else if((EC > 401) && (EC <= 432)){
resistance = ((EC-327.9286)/0.0022);
}
else if((EC > 432) && (EC <= 458)){
resistance = ((EC-373.8095)/0.0012);
}
else if((EC > 458) && (EC <= 479)){
resistance = ((EC-413.3750)/0.0007);
}
else if((EC > 479) && (EC <= 525)){
resistance = ((EC-473.8889)/0.0001);
}
else if((EC > 525)){
resistance = ((EC-525.7778)/0.00001);
}
return resistance;
}
OpenGardenClass OpenGarden;