-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoolAndSaunaController.ino
1424 lines (1229 loc) · 48.8 KB
/
poolAndSaunaController.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
// This is a human-readable summary of (and not a substitute for) the license.
// Disclaimer
//
// You are free to:
// Share — copy and redistribute the material in any medium or format
// Adapt — remix, transform, and build upon the material
// The licensor cannot revoke these freedoms as long as you follow the license terms.
//
// Under the following terms:
// Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
// NonCommercial — You may not use the material for commercial purposes.
// ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
// No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
//
// Notices:
// You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
// No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
//
// github: https://github.com/gusgonnet/poolAndSaunaController
// hackster: https://www.hackster.io/gusgonnet/pool-and-sauna-controller-b24a9a
//
// Free for personal use.
//
// https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// Uses this library for the DS18b20 sensor: https://github.com/LukeUSMC/ds18b20-photon
// Datasheet: http://cdn.sparkfun.com/datasheets/Sensors/Temp/DS18B20.pdf
//
/*
NOTE: To draw the FSM image, copy paste this uml code below in http://www.plantuml.com/plantuml/uml
@startuml
skinparam backgroundColor LightYellow
skinparam state {
BackgroundColor LightBlue
BorderColor Gray
FontName Impact
}
[*] --> initState
note left of initState : The system boots\nin this state
initState: 10 seconds
initState -down--> offState: if system was\npreviously off
initState -down--> idleState: if system was\npreviously on
offState: system off
offState -left-> idleState: setOnOff("on")
idleState: heating off
idleState -down-> onState: temperature\n< target
idleState -right-> offState: setOnOff("off")
onState: heating on
onState -up-> idleState: temperature\n> target
onState -up-> offState: setOnOff("off")
@enduml
*/
#include "Particle-OneWire.h"
#include "DS18B20.h"
#include "NCD4Relay.h"
#include "PietteTech_DHT.h"
#include "elapsedMillis.h"
#include "FiniteStateMachine.h"
#define APP_NAME "poolAndSaunaController"
String VERSION = "Version 0.09";
SYSTEM_MODE(AUTOMATIC);
/*******************************************************************************
* changes in version 0.01:
* Initial version
* changes in version 0.02:
* Logic is working now
* pool ds18b20 sensor on D2
* changes in version 0.03:
* Fixing unstable temperature samples
* adding minimum time in state
* adding setCalibration function
* added turnOffAllRelays at boot time
* changes in version 0.04:
* Storing settings in EEPROM
* changes in version 0.05:
* duplicating FSM and others to control pool and sauna
* pool ds18b20 sensor on D3
* changes in version 0.06:
* adding system on/off
* changes in version 0.07:
* addding user control for relays 3 and 4 (via a cloud function)
* adding third ds18b20 sensor for sensing ambient temperature on D4
* changes in version 0.08:
* upgrading control and adding function to get status of relays 3 and 4
* changes in version 0.09:
* Particle build share link: https://go.particle.io/shared_apps/5aea75975ae4dfa4a4000e46
* adding DHT22 sensor for sensing ambient temperature and humidity on D5
*******************************************************************************/
// Argentina time zone GMT-3
const int TIME_ZONE = -3;
/*******************************************************************************
declare FSM states with proper enter, update and exit functions
we currently have the following states:
- init: when the system boots up it starts in this state.
After some time (ten seconds), the system transitions to the off or idle state.
- off: the system is off until the user sets it to on
No relays are activated. Water pump is off
- idle: the target temperature is being monitored.
No relays are activated. Water pump is off
- on : the water pump is activated until the target temperature is reached.
Relay for the water pump is on.
State changes:
Most likely: off -> idle -> on -> idle -> on and so on
States at power up: init -> off if previous state was off
init -> idle if previous state was on or idle
*******************************************************************************/
State initState = State(initEnterFunction, initUpdateFunction, initExitFunction);
State offState = State(offEnterFunction, offUpdateFunction, offExitFunction);
State idleState = State(idleEnterFunction, idleUpdateFunction, idleExitFunction);
State onState = State(onEnterFunction, onUpdateFunction, onExitFunction);
//initialize state machine for the pool, start in state: init
FSM stateMachine1 = FSM(initState);
// Sample FSM every now and then - 100 milliseconds means 10 times per second
#define QUICK_LOOP_INTERVAL 100
elapsedMillis quickLoopTimer;
// FSM states constants
#define STATE_INIT "Initializing"
#define STATE_OFF "Off"
#define STATE_IDLE "Idle"
#define STATE_ON "On"
String state = STATE_INIT;
String lastState = "";
// timers work on millis, so we adjust the value with this constant
#define MILLISECONDS_TO_MINUTES 60000
#define MILLISECONDS_TO_SECONDS 1000
// 10 seconds for the init cycle, so sensor samples get stabilized
// units: seconds
int initTimeout = 10;
// a state has to have a minimum duration time: this is now one minute
// units: minutes
int minimumTimeInState = 1;
/*******************************************************************************
temperature sensor and variables
*******************************************************************************/
//Sets Pin D2 for Temp Sensor
DS18B20 ds18b20 = DS18B20(D2);
// Sample temperature sensor every 30 seconds
#define TEMPERATURE_SAMPLE_INTERVAL 30 * MILLISECONDS_TO_SECONDS
elapsedMillis temperatureSampleInterval;
//temperature related variables
#define INVALID -1
double temperatureCurrent = INVALID;
double temperatureTarget = 30.0;
//sensor difference with real temperature (if none set to zero)
//use this variable to align measurements with your existing displays
double temperatureCalibration = 0;
//you can change this to your liking
// a smaller value will make your temperature more constant at the price of
// starting the pump more often
// a larger value will reduce the number of times the pump turns on but will leave it on a longer time
double temperatureMargin = 0.25;
// Celsius is the default unit, set this boolean to true if you want to use Fahrenheit
const bool useFahrenheit = false;
/*******************************************************************************
declare second FSM, with second sensor for the sauna
*******************************************************************************/
State initState2 = State(initEnterFunction2, initUpdateFunction2, initExitFunction2);
State offState2 = State(offEnterFunction2, offUpdateFunction2, offExitFunction2);
State idleState2 = State(idleEnterFunction2, idleUpdateFunction2, idleExitFunction2);
State onState2 = State(onEnterFunction2, onUpdateFunction2, onExitFunction2);
//initialize state machine for the sauna, start in state: init
FSM stateMachine2 = FSM(initState2);
//Sets Pin D3 for Temp Sensor2
DS18B20 ds18b20_2 = DS18B20(D3);
double temperatureCurrent2 = INVALID;
double temperatureTarget2 = 37.0;
double temperatureCalibration2 = 0;
String state2 = STATE_INIT;
String lastState2 = "";
/*******************************************************************************
temperature sensor and variables for ambient sensing
*******************************************************************************/
//Sets Pin D4 for Ambient Temp Sensor
DS18B20 ds18b20_3 = DS18B20(D4);
double temperatureCurrent3 = INVALID;
/*******************************************************************************
DHT sensor for ambient sensing
*******************************************************************************/
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN 5 // Digital pin for communications
void dht_wrapper(); // must be declared before the lib initialization
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
bool bDHTstarted; // flag to indicate we started acquisition
double temperatureCurrent4 = INVALID;
double humidityCurrent4 = INVALID;
// This wrapper is in charge of calling the DHT sensor lib
void dht_wrapper() { DHT.isrCallback(); }
/*******************************************************************************
relay variables
*******************************************************************************/
NCD4Relay relayController;
// timers for switching off the relays
#define MILLISECONDS_TO_MINUTES 60000
elapsedMillis timerOnRelay1;
elapsedMillis timerOnRelay2;
elapsedMillis timerOnRelay3;
elapsedMillis timerOnRelay4;
int turnOffRelay1AfterTime = 0;
int turnOffRelay2AfterTime = 0;
int turnOffRelay3AfterTime = 0;
int turnOffRelay4AfterTime = 0;
/*******************************************************************************
structure for writing thresholds in eeprom
https://docs.particle.io/reference/firmware/photon/#eeprom
*******************************************************************************/
//randomly chosen value here. The only thing that matters is that it's not 255
// since 255 is the default value for uninitialized eeprom
// value 137 will be used in version 0.4
// value 138 will be used in version 0.5
// value 139 will be used in version 0.6
#define EEPROM_VERSION 139
#define EEPROM_ADDRESS 0
struct EepromMemoryStructure
{
uint8_t version = EEPROM_VERSION;
double temperatureTarget;
double temperatureCalibration;
uint8_t lastState;
double temperatureTarget2;
double temperatureCalibration2;
uint8_t lastState2;
};
EepromMemoryStructure eepromMemory;
/*******************************************************************************
* Function Name : setup
* Description : this function runs once at system boot
*******************************************************************************/
void setup()
{
// publish startup message with firmware version
Particle.publish(APP_NAME, VERSION, PRIVATE);
// declare cloud variables
// https://docs.particle.io/reference/firmware/photon/#particle-variable-
// Up to 20 cloud variables may be registered and each variable name is limited to a maximum of 12 characters.
Particle.variable("temperature", temperatureCurrent);
Particle.variable("target", temperatureTarget);
Particle.variable("calibration", temperatureCalibration);
// This variable informs the state of the system
Particle.variable("state", state);
// declare cloud functions
// https://docs.particle.io/reference/firmware/photon/#particle-function-
// Up to 15 cloud functions may be registered and each function name is limited to a maximum of 12 characters.
Particle.function("setOnOff", setOnOff);
Particle.function("setTarget", setTarget);
Particle.function("setCalbrtion", setCalibration);
/*******************************************************************************
cloud variables and functions for the second FSM, with second sensor
*******************************************************************************/
Particle.variable("temperature2", temperatureCurrent2);
Particle.variable("target2", temperatureTarget2);
Particle.variable("calibration2", temperatureCalibration2);
Particle.variable("state2", state2);
Particle.function("setOnOff2", setOnOff2);
Particle.function("setTarget2", setTarget2);
Particle.function("setCalbrtn2", setCalibration2);
/*******************************************************************************
cloud variables and functions for the ambient temperature
*******************************************************************************/
Particle.variable("tempAmbient", temperatureCurrent3);
Particle.variable("tempAmbieDHT", temperatureCurrent4);
Particle.variable("humiAmbieDHT", humidityCurrent4);
Time.zone(TIME_ZONE);
// this function allows the user to control relays 3 and 4 (from IFTTT for example)
Particle.function("controlRelay", triggerRelay);
// this function allows the user to query the status of relays 3 and 4 (from IFTTT for example)
Particle.function("relayStatus", relayStatus);
relayController.setAddress(0, 0, 0);
// this is needed if you are flashing new versions while there is one or more relays activated
// they will remain active if we don't turn them off
relayController.turnOffAllRelays();
//restore settings from eeprom, if there were any saved before
readFromEeprom();
}
/*******************************************************************************
* Function Name : loop
* Description : this function runs continuously while the project is running
*******************************************************************************/
void loop()
{
quickLoop();
}
/*******************************************************************************
* Function Name : quickLoop
* Description : this function runs every 100 milliseconds (10 times a second)
to save power
*******************************************************************************/
void quickLoop()
{
// is time up? no, then come back later
if (quickLoopTimer < QUICK_LOOP_INTERVAL)
{
return;
}
// time is up, reset timer
quickLoopTimer = 0;
// read the temperature sensors
readTemperature();
// update the FSMs
// the FSM is the heart of the program - all actions are defined by its states
stateMachine1.update();
stateMachine2.update();
// check if there is any relay to turn off automatically
turnOffRelay();
}
/*******************************************************************************
********************************************************************************
********************************************************************************
CONTROL FUNCTIONS
********************************************************************************
********************************************************************************
*******************************************************************************/
/*******************************************************************************
* Function Name : setOnOff
* Description : this function sets the system on or off
* Parameters : String parameter: on/ON/On or off/OFF/Off
* Return : 0 if success, -1 if fails
*******************************************************************************/
int setOnOff(String parameter)
{
if (parameter.equalsIgnoreCase("on"))
{
// transition to on only if the system is off
if (state == STATE_OFF) {
stateMachine1.transitionTo(idleState);
}
return 0;
}
if (parameter.equalsIgnoreCase("off"))
{
// transition to off only if the system is on
if ( (state == STATE_IDLE) || (state == STATE_ON) ) {
stateMachine1.transitionTo(offState);
}
return 0;
}
Particle.publish(APP_NAME, "Failed to set system FSM1 to " + parameter, PRIVATE);
return -1;
}
/*******************************************************************************
* Function Name : setOnOff2
* Description : this function sets the system on or off for FSM2
* Parameters : String parameter: on/ON/On or off/OFF/Off
* Return : 0 if success, -1 if fails
*******************************************************************************/
int setOnOff2(String parameter)
{
if (parameter.equalsIgnoreCase("on"))
{
// transition to on only if the system is off
if (state2 == STATE_OFF) {
stateMachine2.transitionTo(idleState2);
}
return 0;
}
if (parameter.equalsIgnoreCase("off"))
{
// transition to off only if the system is on
if ( (state2 == STATE_IDLE) || (state2 == STATE_ON) ) {
stateMachine2.transitionTo(offState2);
}
return 0;
}
Particle.publish(APP_NAME, "Failed to set system FSM2 to " + parameter, PRIVATE);
return -1;
}
/*******************************************************************************
* Function Name : setTarget
* Description : this function sets the target temperature
* Parameters : String parameter: the target temperature
Value has to be between 0 and 125. Anything else is ignored
* Return : 0 if success, -1 if fails
*******************************************************************************/
int setTarget(String parameter)
{
double localValue = atoi(parameter);
if ((localValue >= 0) && (localValue <= 125))
{
temperatureTarget = localValue;
Particle.publish(APP_NAME, "Setting temperature target to " + double2string(temperatureTarget), PRIVATE);
saveSettingsInEeprom();
return 0;
}
Particle.publish(APP_NAME, "Failed to set temperature target to " + parameter + " (0<=value<=125)", PRIVATE);
return -1;
}
/*******************************************************************************
* Function Name : setCalibration
* Description : this function sets the calibration of the temperature
* Parameters : String parameter: the calibration value
Value has to be between -50 and 50. Anything else is ignored
* Return : 0 if success, -1 if fails
*******************************************************************************/
int setCalibration(String parameter)
{
double localValue = atoi(parameter);
if ((localValue >= -50) && (localValue <= 50))
{
// rollback previous calibration adjustment
temperatureCurrent = temperatureCurrent - temperatureCalibration;
// store new calibration value
temperatureCalibration = localValue;
// apply new calibration adjustment
temperatureCurrent = temperatureCurrent + temperatureCalibration;
Particle.publish(APP_NAME, "Setting temperature calibration to " + double2string(temperatureCalibration), PRIVATE);
saveSettingsInEeprom();
return 0;
}
Particle.publish(APP_NAME, "Failed to set temperature calibration to " + parameter + " (-50<=value<=50)", PRIVATE);
return -1;
}
/*******************************************************************************
* Function Name : setTarget2
* Description : this function sets the target temperature for the sauna
* Parameters : String parameter: the target temperature
Value has to be between 0 and 125. Anything else is ignored
* Return : 0 if success, -1 if fails
*******************************************************************************/
int setTarget2(String parameter)
{
double localValue = atoi(parameter);
if ((localValue >= 0) && (localValue <= 125))
{
temperatureTarget2 = localValue;
Particle.publish(APP_NAME, "Setting temperature2 target to " + double2string(temperatureTarget2), PRIVATE);
saveSettingsInEeprom();
return 0;
}
Particle.publish(APP_NAME, "Failed to set temperature2 target to " + parameter + " (0<=value<=125)", PRIVATE);
return -1;
}
/*******************************************************************************
* Function Name : setCalibration2
* Description : this function sets the calibration of the temperature2
* Parameters : String parameter: the calibration value
Value has to be between -50 and 50. Anything else is ignored
* Return : 0 if success, -1 if fails
*******************************************************************************/
int setCalibration2(String parameter)
{
double localValue = atoi(parameter);
if ((localValue >= -50) && (localValue <= 50))
{
// rollback previous calibration adjustment
temperatureCurrent2 = temperatureCurrent2 - temperatureCalibration2;
// store new calibration value
temperatureCalibration2 = localValue;
// apply new calibration adjustment
temperatureCurrent2 = temperatureCurrent2 + temperatureCalibration2;
Particle.publish(APP_NAME, "Setting temperature2 calibration to " + double2string(temperatureCalibration2), PRIVATE);
saveSettingsInEeprom();
return 0;
}
Particle.publish(APP_NAME, "Failed to set temperature2 calibration to " + parameter + " (-50<=value<=50)", PRIVATE);
return -1;
}
/*******************************************************************************
********************************************************************************
********************************************************************************
SENSOR FUNCTIONS
********************************************************************************
********************************************************************************
*******************************************************************************/
/*******************************************************************************
* Function Name : readTemperature
* Description : reads the temperature sensor in D2, D3 and D4 at every TEMPERATURE_SAMPLE_INTERVAL
* Return : none
*******************************************************************************/
void readTemperature()
{
// is time up? no, then come back later
if (temperatureSampleInterval < TEMPERATURE_SAMPLE_INTERVAL)
{
return;
}
//is time up, reset timer
temperatureSampleInterval = 0;
getTemp();
getTemp2();
getTempAmb();
getTempAmbDHT();
Particle.publish(APP_NAME, "Pool: " + double2string(temperatureCurrent) \
+ ", Sauna: " + double2string(temperatureCurrent2) \
+ ", TAmb: " + double2string(temperatureCurrent3) \
+ ", TAmbDHT: " + double2string(temperatureCurrent4) \
+ ", HAmbDHT: " + double2string(humidityCurrent4) \
, PRIVATE);
}
/*******************************************************************************
* Function Name : getTemp
* Description : reads the DS18B20 sensor
* Return : nothing
*******************************************************************************/
void getTemp()
{
int dsAttempts = 0;
double temperatureLocal = INVALID;
if (!ds18b20.search())
{
ds18b20.resetsearch();
temperatureLocal = ds18b20.getTemperature();
while (!ds18b20.crcCheck() && dsAttempts < 4)
{
dsAttempts++;
if (dsAttempts == 3)
{
delay(1000);
}
ds18b20.resetsearch();
temperatureLocal = ds18b20.getTemperature();
continue;
}
dsAttempts = 0;
if (useFahrenheit)
{
temperatureLocal = ds18b20.convertToFahrenheit(temperatureLocal);
}
// calibrate values
temperatureLocal = temperatureLocal + temperatureCalibration;
// if reading is valid, take it
if ((temperatureLocal != INVALID) && (ds18b20.crcCheck()))
{
temperatureCurrent = temperatureLocal;
}
}
}
/*******************************************************************************
* Function Name : getTemp2
* Description : reads the second DS18B20 sensor
* Return : nothing
*******************************************************************************/
void getTemp2()
{
int dsAttempts = 0;
double temperatureLocal = INVALID;
if (!ds18b20_2.search())
{
ds18b20_2.resetsearch();
temperatureLocal = ds18b20_2.getTemperature();
while (!ds18b20_2.crcCheck() && dsAttempts < 4)
{
dsAttempts++;
if (dsAttempts == 3)
{
delay(1000);
}
ds18b20_2.resetsearch();
temperatureLocal = ds18b20_2.getTemperature();
continue;
}
dsAttempts = 0;
if (useFahrenheit)
{
temperatureLocal = ds18b20_2.convertToFahrenheit(temperatureLocal);
}
// calibrate values
temperatureLocal = temperatureLocal + temperatureCalibration2;
// if reading is valid, take it
if ((temperatureLocal != INVALID) && (ds18b20_2.crcCheck()))
{
temperatureCurrent2 = temperatureLocal;
}
}
}
/*******************************************************************************
* Function Name : getTempAmb
* Description : reads the third DS18B20 sensor (ambient)
* Return : nothing
*******************************************************************************/
void getTempAmb()
{
int dsAttempts = 0;
double temperatureLocal = INVALID;
if (!ds18b20_3.search())
{
ds18b20_3.resetsearch();
temperatureLocal = ds18b20_3.getTemperature();
while (!ds18b20_3.crcCheck() && dsAttempts < 4)
{
dsAttempts++;
if (dsAttempts == 3)
{
delay(1000);
}
ds18b20_3.resetsearch();
temperatureLocal = ds18b20_3.getTemperature();
continue;
}
dsAttempts = 0;
if (useFahrenheit)
{
temperatureLocal = ds18b20_3.convertToFahrenheit(temperatureLocal);
}
// if reading is valid, take it
if ((temperatureLocal != INVALID) && (ds18b20_3.crcCheck()))
{
temperatureCurrent3 = temperatureLocal;
}
}
}
/*******************************************************************************
* Function Name : getTempAmbDHT
* Description : reads the temperature of the DHT22 sensor
* Return : none
*******************************************************************************/
void getTempAmbDHT() {
// start the sample
if (!bDHTstarted) {
DHT.acquireAndWait(5);
bDHTstarted = true;
}
//still acquiring sample? go away and come back later
if (DHT.acquiring()) {
return;
}
//I observed my dht22 measuring below 0 from time to time, so let's discard that sample
if ( DHT.getCelsius() < 0 ) {
//reset the sample flag so we can take another
bDHTstarted = false;
return;
}
if (useFahrenheit)
{
temperatureCurrent4 = DHT.getFahrenheit();
}
else
{
temperatureCurrent4 = DHT.getCelsius();
}
humidityCurrent4 = DHT.getHumidity();
//reset the sample flag so we can take another
bDHTstarted = false;
}
/*******************************************************************************
********************************************************************************
********************************************************************************
FINITE STATE MACHINE FUNCTIONS
********************************************************************************
********************************************************************************
*******************************************************************************/
/*******************************************************************************
* FSM state Name : init
* Description : when the system boots starts in this state to stabilize sensors.
After some time (ten seconds), the system transitions to the idle state or off state,
depending on previous system state
* Status of the pump : off
*******************************************************************************/
void initEnterFunction()
{
// set the new state and publish the change, do not set lastState since we need the value from the eeprom
setState(STATE_INIT, false);
}
void initUpdateFunction()
{
// is time up? no, then come back later
if (stateMachine1.timeInCurrentState() < (initTimeout * MILLISECONDS_TO_SECONDS))
{
return;
}
if (lastState==STATE_IDLE)
{
stateMachine1.transitionTo(idleState);
}
else
{
stateMachine1.transitionTo(offState);
}
}
void initExitFunction()
{
}
/*******************************************************************************
* FSM state Name : off
* Description : the system is off until the user sets it to on
* Status of the pump : off
*******************************************************************************/
void offEnterFunction()
{
// set the new state and publish the change
setState(STATE_OFF, true);
}
void offUpdateFunction()
{
}
void offExitFunction()
{
}
/*******************************************************************************
* FSM state Name : idle
* Description : the system is checking the temperature. When it goes under a threshold, then
the On state is triggered.
* Status of the pump : off
*******************************************************************************/
void idleEnterFunction()
{
// set the new state and publish the change
setState(STATE_IDLE, true);
}
void idleUpdateFunction()
{
// is minimum time up? no, then come back later
if (stateMachine1.timeInCurrentState() < (minimumTimeInState * MILLISECONDS_TO_MINUTES))
{
return;
}
//if the temperature is lower than the target, transition to onState
// temperature is equal to INVALID at boot time until a valid temperature is measured
if ((temperatureCurrent != INVALID) && (temperatureCurrent <= temperatureTarget - temperatureMargin))
{
stateMachine1.transitionTo(onState);
}
}
void idleExitFunction()
{
}
/*******************************************************************************
* FSM state Name : On
* Description : the pump will be turned on until the temperature reaches the target.
It then transitions to the off state.
* Status of the pump : on
*******************************************************************************/
void onEnterFunction()
{
// set the new state and publish the change
setState(STATE_ON, true);
turnOnRelay(1);
}
void onUpdateFunction()
{
// is minimum time up? no, then come back later
if (stateMachine1.timeInCurrentState() < (minimumTimeInState * MILLISECONDS_TO_MINUTES))
{
return;
}
//if the temperature is higher than the target, transition to offState
// temperature is equal to INVALID at boot time until a valid temperature is measured
if ((temperatureCurrent != INVALID) && (temperatureCurrent >= temperatureTarget + temperatureMargin))
{
stateMachine1.transitionTo(idleState);
}
}
void onExitFunction()
{
turnOffRelay(1);
}
/*******************************************************************************
* FSM state Name : init2
* Description : when the system boots starts in this state to stabilize sensors.
After some time (ten seconds), the system transitions to the idle state or off state,
depending on previous system state
* Status of the pump : off
*******************************************************************************/
void initEnterFunction2()
{
// set the new state and publish the change
setState2(STATE_INIT, false);
}
void initUpdateFunction2()
{
// is time up? no, then come back later
if (stateMachine2.timeInCurrentState() < (initTimeout * MILLISECONDS_TO_SECONDS))
{
return;
}
if (lastState2==STATE_IDLE)
{
stateMachine2.transitionTo(idleState2);
}
else
{
stateMachine2.transitionTo(offState2);
}
}
void initExitFunction2()
{
}
/*******************************************************************************
* FSM state Name : off2
* Description : the system is off until the user sets it to on
* Status of the pump : off
*******************************************************************************/
void offEnterFunction2()
{
// set the new state and publish the change
setState2(STATE_OFF, true);
}
void offUpdateFunction2()
{
}
void offExitFunction2()
{
}
/*******************************************************************************
* FSM state Name : idle2
* Description : the system is checking the temperature. When it goes under a threshold, then
the On state is triggered.
* Status of the pump : off
*******************************************************************************/
void idleEnterFunction2()
{
// set the new state and publish the change
setState2(STATE_IDLE, true);
}
void idleUpdateFunction2()
{
// is minimum time up? no, then come back later
if (stateMachine2.timeInCurrentState() < (minimumTimeInState * MILLISECONDS_TO_MINUTES))
{
return;
}
//if the temperature2 is lower than the target2, transition to onState2
// temperature is equal to INVALID at boot time until a valid temperature is measured
if ((temperatureCurrent2 != INVALID) && (temperatureCurrent2 <= temperatureTarget2 - temperatureMargin))
{
stateMachine2.transitionTo(onState2);
}
}
void idleExitFunction2()
{
}
/*******************************************************************************
* FSM state Name : On2
* Description : the pump will be turned on until the temperature reaches the target.
It then transitions to the off state.
* Status of the pump : on
*******************************************************************************/
void onEnterFunction2()
{
// set the new state and publish the change
setState2(STATE_ON, true);
turnOnRelay(2);
}
void onUpdateFunction2()
{
// is minimum time up? no, then come back later
if (stateMachine2.timeInCurrentState() < (minimumTimeInState * MILLISECONDS_TO_MINUTES))
{
return;
}
//if the temperature2 is higher than the target2, transition to offState2
// temperature is equal to INVALID at boot time until a valid temperature is measured
if ((temperatureCurrent2 != INVALID) && (temperatureCurrent2 >= temperatureTarget2 + temperatureMargin))
{
stateMachine2.transitionTo(idleState2);
}
}
void onExitFunction2()
{
turnOffRelay(2);
}
/*******************************************************************************
********************************************************************************
********************************************************************************
HELPER FUNCTIONS
********************************************************************************
********************************************************************************
*******************************************************************************/
/*******************************************************************************