forked from mysensors/NodeManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeManagerLibrary.h
1994 lines (1895 loc) · 59.4 KB
/
NodeManagerLibrary.h
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
/*
* NodeManager Library
*/
#ifndef NodeManager_h
#define NodeManager_h
#include <Arduino.h>
// define NodeManager version
#define VERSION "1.7"
/***********************************
Constants
*/
// define board sleep status
#define AWAKE 0
#define SLEEP 1
// define time unit
#define SECONDS 0
#define MINUTES 1
#define HOURS 2
#define DAYS 3
// define on/off
#define OFF 0
#define ON 1
/***********************************
Chip type
*/
// 168 and 328 Arduinos
#if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
#define CHIP_TINYX4
#endif
#if defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#define CHIP_TINYX5
#endif
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define CHIP_MEGA
#endif
#if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4)
#define CHIP_STM32
#endif
#if defined(ESP8266) || defined(MY_GATEWAY_ESP8266)
#define CHIP_ESP8266
#endif
#if defined (MYBOARDNRF5)
#define CHIP_NRF5
#endif
#if !defined(CHIP_ESP8266) && !defined(CHIP_STM32) && !defined(CHIP_NRF5)
#define CHIP_AVR
#endif
// define interrupt pins
#if defined(CHIP_STM32)
#define INTERRUPT_PIN_1 PB8
#define INTERRUPT_PIN_2 2
#else
#define INTERRUPT_PIN_1 3
#define INTERRUPT_PIN_2 2
#endif
// define eeprom addresses
#define EEPROM_SLEEP_SAVED 0
#define EEPROM_SLEEP_1 5
#define EEPROM_SLEEP_2 6
#define EEPROM_SLEEP_3 7
#define EEPROM_USER_START 100
/***********************************
Default configuration settings
*/
// define default sketch name and version
#ifndef SKETCH_NAME
#define SKETCH_NAME "NodeManager"
#endif
#ifndef SKETCH_VERSION
#define SKETCH_VERSION "1.0"
#endif
// SensorConfiguration default child_id
#ifndef CONFIGURATION_CHILD_ID
#define CONFIGURATION_CHILD_ID 200
#endif
// SensorBattery default child_id
#ifndef BATTERY_CHILD_ID
#define BATTERY_CHILD_ID 201
#endif
// SensorSignal default child_id
#ifndef SIGNAL_CHILD_ID
#define SIGNAL_CHILD_ID 202
#endif
// default built-in features if not defined
#ifndef FEATURE_DEBUG
#define FEATURE_DEBUG ON
#endif
#ifndef FEATURE_POWER_MANAGER
#define FEATURE_POWER_MANAGER OFF
#endif
#ifndef FEATURE_INTERRUPTS
#define FEATURE_INTERRUPTS ON
#endif
#ifndef FEATURE_CONDITIONAL_REPORT
#define FEATURE_CONDITIONAL_REPORT OFF
#endif
#ifndef FEATURE_EEPROM
#define FEATURE_EEPROM OFF
#endif
#ifndef FEATURE_SLEEP
#define FEATURE_SLEEP ON
#endif
#ifndef FEATURE_RECEIVE
#define FEATURE_RECEIVE ON
#endif
#ifndef FEATURE_TIME
#define FEATURE_TIME OFF
#endif
#ifndef FEATURE_RTC
#define FEATURE_RTC OFF
#endif
#ifndef FEATURE_SD
#define FEATURE_SD OFF
#endif
#ifndef FEATURE_HOOKING
#define FEATURE_HOOKING OFF
#endif
/***********************************
Libraries
*/
// include supporting libraries for enabled sensors
#ifdef MY_USE_UDP
#include <WiFiUdp.h>
#endif
#ifdef CHIP_ESP8266
#include <ESP8266WiFi.h>
#endif
// load MySensors library
#include <MySensors.h>
// include third party libraries
#ifdef USE_SIGNAL
#define MY_SIGNAL_REPORT_ENABLED
#endif
#ifdef USE_DHT
#include <DHT.h>
#endif
#ifdef USE_SHT21
#include <Wire.h>
#include <Sodaq_SHT2x.h>
#endif
#ifdef USE_DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
#endif
#ifdef USE_BH1750
#include <BH1750.h>
#include <Wire.h>
#endif
#ifdef USE_MLX90614
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#endif
#ifdef USE_BME280
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#endif
#ifdef USE_SONOFF
#include <Bounce2.h>
#endif
#ifdef USE_BMP085_180
#include <Wire.h>
#include <Adafruit_BMP085.h>
#endif
#ifdef USE_HCSR04
#include <NewPing.h>
#endif
#ifdef USE_MCP9808
#include <Wire.h>
#include "Adafruit_MCP9808.h"
#endif
#ifdef USE_MHZ19
#include <SoftwareSerial.h>
#endif
#ifdef USE_AM2320
#include <Wire.h>
#include <AM2320.h>
#endif
#ifdef USE_TSL2561
#include <TSL2561.h>
#include <Wire.h>
#endif
#ifdef USE_PT100
#include <DFRobotHighTemperatureSensor.h>
#endif
#ifdef USE_BMP280
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#endif
#ifdef USE_DIMMER
#include <math.h>
#endif
#ifdef USE_PMS
#include <PMS.h>
#include <SoftwareSerial.h>
#endif
#ifdef USE_VL53L0X
#include <Wire.h>
#include <VL53L0X.h>
#endif
#ifdef USE_SSD1306
#include <SSD1306Ascii.h>
#include <SSD1306AsciiAvrI2c.h>
#endif
#ifdef USE_SHT31
#include <Wire.h>
#include "Adafruit_SHT31.h"
#endif
#ifdef USE_SI7021
#include <Wire.h>
#include "SparkFun_Si7021_Breakout_Library.h"
#endif
#ifdef USE_CHIRP
#include <Wire.h>
#include <I2CSoilMoistureSensor.h>
#endif
#ifdef USE_HD44780
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#endif
#ifdef USE_SERVO
#include <Servo.h>
#endif
#ifdef USE_APDS9960
#include <Wire.h>
#include <SparkFun_APDS9960.h>
#endif
#ifdef USE_NEOPIXEL
#if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4)
#include <NeoMaple.h>
#else
#include <Adafruit_NeoPixel.h>
#endif
#endif
// include third party libraries for enabled features
#ifdef MY_GATEWAY_SERIAL
#define FEATURE_SLEEP OFF
#endif
#if FEATURE_TIME == ON
#include <TimeLib.h>
#endif
#if FEATURE_RTC == ON
#define FEATURE_TIME ON
#include <DS3232RTC.h>
#endif
#if FEATURE_CONDITIONAL_REPORT == ON
#include <float.h>
#endif
#if FEATURE_SD == ON
#include <SD.h>
#endif
/*******************************************************************
Classes
*/
class NodeManager;
class Sensor;
/*
* List
*/
template<typename T> class List {
public:
typedef T* iterator;
List() {
_internalArray = NULL;
_endPosition = 0;
_allocBlocks = 0;
}
~List() {
delete[] _internalArray;
_internalArray = NULL;
_endPosition = 0;
_allocBlocks = 0;
}
void push(T item) {
if (_endPosition == _allocBlocks) _AllocOneBlock(false);
_internalArray[_endPosition] = item;
++_endPosition;
}
void pop() {
if (_endPosition == 0) return;
--_endPosition;
_DeAllocOneBlock(false);
}
T get(int position) {
position = position -1;
if (position > _endPosition) position = _endPosition;
return _internalArray[position];
}
void clear() {
T* newArray = NULL;
if (_allocBlocks > 0) newArray = new T[_allocBlocks];
delete[] _internalArray;
_internalArray = newArray;
_endPosition = 0;
}
inline iterator begin() { return _internalArray; }
inline iterator end() { return _internalArray + _endPosition; }
inline bool empty() { return (_endPosition == 0); }
inline int size() { return _endPosition; }
void allocateBlocks(int alloc) {
_allocBlocks = alloc;
T* newArray = new T[_allocBlocks];
for (int i = 0; i < _endPosition; ++i) newArray[i] = _internalArray[i];
delete[] _internalArray;
_internalArray = newArray;
}
private:
T* _internalArray;
int _endPosition;
int _allocBlocks;
void _AllocOneBlock(bool shiftItems) {
++_allocBlocks;
T* newArray = new T[_allocBlocks];
for (int i = 0; i < _endPosition; ++i) newArray[shiftItems ? (i + 1) : i] = _internalArray[i];
delete[] _internalArray;
_internalArray = newArray;
}
void _DeAllocOneBlock(bool shiftItems) {
--_allocBlocks;
if (_allocBlocks == 0) {
delete[] _internalArray;
_internalArray = NULL;
return;
}
T* newArray = new T[_allocBlocks];
for (int i = 0; i < _endPosition; ++i) newArray[i] = _internalArray[shiftItems ? (i + 1) : i];
delete[] _internalArray;
_internalArray = newArray;
}
};
/*
PowerManager
*/
#if FEATURE_POWER_MANAGER == ON
class PowerManager {
public:
PowerManager(int ground_pin, int vcc_pin, int wait_time = 50);
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
// if enabled the pins will be automatically powered on while awake and off during sleeping
// turns the power pins on
void powerOn();
// turns the power pins on
void powerOff();
private:
int _vcc_pin = -1;
int _ground_pin = -1;
long _wait = 0;
};
#endif
/*
Timer
*/
class Timer {
public:
Timer(NodeManager* node_manager);
// start the timer which will be over when the configured target passes by
void start(int target, int unit);
void start();
// stop the timer
void stop();
// reset the timer
void reset();
// reset the timer and start over
void restart();
// set the timer configuration but do not start it
void set(int target, int unit);
void unset();
// update the timer. To be called at every cycle
void update();
// return true if the time is over
bool isOver();
// return true if the timer is running
bool isRunning();
// return true if the timer has been configured
bool isConfigured();
// return true if this is the first time the timer runs
bool isFirstRun();
// return the current elapsed time
float getElapsed();
private:
NodeManager* _node;
int _target = 0;
long _elapsed = 0;
bool _is_running = false;
bool _is_configured = false;
bool _first_run = true;
long _last = 0;
};
/*
Request
*/
class Request {
public:
Request(int child_id, const char* string);
// return the child id the message has been requested to
int getRecipientChildId();
// return the child id the request is for
int getChildId();
// return the parsed function
int getFunction();
// return the value as an int
int getValueInt();
// return the value as a float
float getValueFloat();
private:
int _function = -1;
int _child_id = -1;
int _recipient_child_id = -1;
float _value;
};
/***************************************
Child: child class
*/
class Child {
public:
Child();
Child(Sensor* sensor, int child_id, int presentation, int type, const char* description = "");
// set child id used to communicate with the gateway/controller
void setChildId(int value);
int getChildId();
// set sensor presentation (default: S_CUSTOM)
void setPresentation(int value);
int getPresentation();
// set sensor type (default: V_CUSTOM)
void setType(int value);
int getType();
// set how many decimal digits to use (default: 2 for ChildFloat, 4 for ChildDouble)
void setFloatPrecision(int value);
// set sensor description
void setDescription(const char* value);
const char* getDescription();
#if FEATURE_CONDITIONAL_REPORT == ON
// force to send an update after the configured number of minutes
void setForceUpdateMinutes(int value);
// never report values below this threshold (default: FLT_MIN)
void setMinThreshold(float value);
// never report values above this threshold (default: FLT_MAX)
void setMaxThreshold(float value);
// do not report values if too close to the previous one (default: 0)
void setValueDelta(float value);
#endif
// send the current value to the gateway
virtual void sendValue(bool force);
// print the current value on a LCD display
virtual void print(Print& device);
// reset all the counters
virtual void reset();
protected:
int _samples = 0;
Sensor* _sensor;
int _child_id;
int _presentation = S_CUSTOM;
int _type = V_CUSTOM;
int _float_precision;
const char* _description = "";
#if FEATURE_CONDITIONAL_REPORT == ON
Timer* _force_update_timer;
float _min_threshold = FLT_MIN;
float _max_threshold = FLT_MAX;
float _value_delta = 0;
#endif
};
class ChildInt: public Child {
public:
ChildInt(Sensor* sensor, int child_id, int presentation, int type, const char* description = "");
void setValueInt(int value);
int getValueInt();
void sendValue(bool force);
void print(Print& device);
void reset();
private:
int _value;
#if FEATURE_CONDITIONAL_REPORT == ON
int _last_value = -256;
#endif
int _total = 0;
};
class ChildFloat: public Child {
public:
ChildFloat(Sensor* sensor, int child_id, int presentation, int type, const char* description = "");
void setValueFloat(float value);
float getValueFloat();
void sendValue(bool force);
void print(Print& device);
void reset();
private:
float _value;
#if FEATURE_CONDITIONAL_REPORT == ON
float _last_value = -256;
#endif
float _total = 0;
};
class ChildDouble: public Child {
public:
ChildDouble(Sensor* sensor, int child_id, int presentation, int type, const char* description = "");
void setValueDouble(double value);
double getValueDouble();
void sendValue(bool force);
void print(Print& device);
void reset();
private:
double _value;
#if FEATURE_CONDITIONAL_REPORT == ON
double _last_value = -256;
#endif
double _total = 0;
};
class ChildString: public Child {
public:
ChildString(Sensor* sensor, int child_id, int presentation, int type, const char* description = "");
void setValueString(const char* value);
const char* getValueString();
void sendValue(bool force);
void print(Print& device);
void reset();
private:
const char* _value = "";
#if FEATURE_CONDITIONAL_REPORT == ON
const char* _last_value = "";
#endif
};
/***************************************
Sensor: generic sensor class
*/
class Sensor {
public:
Sensor();
Sensor(NodeManager& node_manager, int pin = -1);
// return the name of the sensor
const char* getName();
// [1] where the sensor is attached to (default: not set)
void setPin(int value);
int getPin();
// [5] For some sensors, the measurement can be queried multiple times and an average is returned (default: 1)
void setSamples(int value);
// [6] If more then one sample has to be taken, set the interval in milliseconds between measurements (default: 0)
void setSamplesInterval(int value);
#if FEATURE_POWER_MANAGER == ON
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
// [13] manually turn the power on
void powerOn();
// [14] manually turn the power off
void powerOff();
#endif
// [17] After how many minutes the sensor will report back its measure (default: 10 minutes)
void setReportIntervalSeconds(int value);
// [16] After how many minutes the sensor will report back its measure (default: 10 minutes)
void setReportIntervalMinutes(int value);
// [19] After how many hours the sensor will report back its measure (default: 10 minutes)
void setReportIntervalHours(int value);
// [20] After how many days the sensor will report back its measure (default: 10 minutes)
void setReportIntervalDays(int value);
// return true if the report interval has been already configured
bool isReportIntervalConfigured();
#if FEATURE_INTERRUPTS == ON
// return the pin the interrupt is attached to
int getInterruptPin();
// listen for interrupts on the given pin so interrupt() will be called when occurring
void setInterrupt(int pin, int mode, int initial);
#endif
#if FEATURE_POWER_MANAGER == ON
// set a previously configured PowerManager to the sensor so to powering it up with custom pins
void setPowerManager(PowerManager& powerManager);
#endif
#if FEATURE_HOOKING == ON
// set a custom hook function to be called when the sensor executes its setup() function
void setSetupHook(void (*function)(Sensor* sensor));
// set a custom hook function to be called just before the sensor executes its loop() function
void setPreLoopHook(void (*function)(Sensor* sensor));
// set a custom hook function to be called just after the sensor executes its loop() function
void setPostLoopHook(void (*function)(Sensor* sensor));
// set a custom hook function to be called when the sensor executes its interrupt() function
void setInterruptHook(void (*function)(Sensor* sensor));
// set a custom hook function to be called when the sensor executes its receive() function
void setReceiveHook(void (*function)(Sensor* sensor, MyMessage* message));
#endif
// list of configured child
List<Child*> children;
#if FEATURE_INTERRUPTS == ON
void interrupt();
#endif
Child* getChild(int child_id);
// register a child
void registerChild(Child* child);
NodeManager* _node;
// define what to do at each stage of the sketch
void before();
void presentation();
void setup();
void loop(MyMessage* message);
#if FEATURE_RECEIVE == ON
void receive(MyMessage* message);
#endif
// abstract functions, subclasses need to implement
virtual void onBefore();
virtual void onSetup();
virtual void onLoop(Child* child);
virtual void onReceive(MyMessage* message);
virtual void onInterrupt();
protected:
const char* _name = "";
int _pin = -1;
int _samples = 1;
int _samples_interval = 0;
#if FEATURE_INTERRUPTS == ON
int _interrupt_pin = -1;
#endif
#if FEATURE_POWER_MANAGER == ON
PowerManager* _powerManager = nullptr;
#endif
Timer* _report_timer;
#if FEATURE_HOOKING == ON
void (*_setup_hook)(Sensor* sensor);
void (*_pre_loop_hook)(Sensor* sensor);
void (*_post_loop_hook)(Sensor* sensor);
void (*_interrupt_hook)(Sensor* sensor);
void (*_receive_hook)(Sensor* sensor, MyMessage* message);
#endif
};
#ifdef USE_BATTERY
/*
SensorBattery: report battery level
*/
class SensorBattery: public Sensor {
public:
SensorBattery(NodeManager& nodeManager, int child_id = BATTERY_CHILD_ID);
// [102] the expected vcc when the batter is fully discharged, used to calculate the percentage (default: 2.7)
void setMinVoltage(float value);
// [103] the expected vcc when the batter is fully charged, used to calculate the percentage (default: 3.3)
void setMaxVoltage(float value);
// [104] if true, the battery level will be evaluated by measuring the internal vcc without the need to connect any pin, if false the voltage divider methon will be used (default: true)
void setBatteryInternalVcc(bool value);
// [105] if setBatteryInternalVcc() is set to false, the analog pin to which the battery's vcc is attached (https://www.mysensors.org/build/battery) (default: -1)
void setBatteryPin(int value);
// [106] if setBatteryInternalVcc() is set to false, the volts per bit ratio used to calculate the battery voltage (default: 0.003363075)
void setBatteryVoltsPerBit(float value);
// define what to do at each stage of the sketch
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
float _battery_min = 2.6;
float _battery_max = 3.3;
bool _battery_internal_vcc = true;
int _battery_pin = -1;
float _battery_volts_per_bit = 0.003363075;
};
#endif
#ifdef USE_SIGNAL
/*
SensorSignal: report RSSI signal strength from the radio
*/
class SensorSignal: public Sensor {
public:
SensorSignal(NodeManager& nodeManager, int child_id = SIGNAL_CHILD_ID);
// [101] define which signal report to send. Possible values are SR_UPLINK_QUALITY, SR_TX_POWER_LEVEL, SR_TX_POWER_PERCENT, SR_TX_RSSI, SR_RX_RSSI, SR_TX_SNR, SR_RX_SNR (default: SR_RX_RSSI)
void setSignalCommand(int value);
// define what to do at each stage of the sketch
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
int _signal_command = SR_RX_RSSI;
};
#endif
#ifdef USE_CONFIGURATION
/*
SensorConfiguration: allow remote configuration of the board and any configured sensor
*/
class SensorConfiguration: public Sensor {
public:
SensorConfiguration(NodeManager& nodeManager);
// define what to do at each stage of the sketch
void onBefore();
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
};
#endif
#ifdef USE_ANALOG_INPUT
/*
SensorAnalogInput: read the analog input of a configured pin
*/
class SensorAnalogInput: public Sensor {
public:
SensorAnalogInput(NodeManager& node_manager, int pin, int child_id = -255);
// [101] the analog reference to use (default: not set, can be either INTERNAL or DEFAULT)
void setReference(int value);
// [102] reverse the value or the percentage (e.g. 70% -> 30%) (default: false)
void setReverse(bool value);
// [103] when true returns the value as a percentage (default: true)
void setOutputPercentage(bool value);
// [104] minimum value for calculating the percentage (default: 0)
void setRangeMin(int value);
// [105] maximum value for calculating the percentage (default: 1024)
void setRangeMax(int value);
// define what to do at each stage of the sketch
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
int _reference = -1;
bool _reverse = false;
bool _output_percentage = true;
int _range_min = 0;
int _range_max = 1024;
int _getPercentage(int value);
int _getAnalogRead();
};
/*
SensorLDR: return the percentage of light from a Light dependent resistor
*/
class SensorLDR: public SensorAnalogInput {
public:
SensorLDR(NodeManager& node_manager, int pin, int child_id = -255);
// define what to do at each stage of the sketch
void onSetup();
};
/*
SensorRain
*/
class SensorRain: public SensorAnalogInput {
public:
SensorRain(NodeManager& node_manager, int pin, int child_id = -255);
// define what to do at each stage of the sketch
void onSetup();
};
/*
SensorSoilMoisture
*/
class SensorSoilMoisture: public SensorAnalogInput {
public:
SensorSoilMoisture(NodeManager& node_manager, int pin, int child_id = -255);
// define what to do at each stage of the sketch
void onSetup();
};
#endif
#ifdef USE_THERMISTOR
/*
SensorThermistor: read the temperature from a thermistor
*/
class SensorThermistor: public Sensor {
public:
SensorThermistor(NodeManager& node_manager, int pin, int child_id = -255);
// [101] resistance at 25 degrees C (default: 10000)
void setNominalResistor(long value);
// [102] temperature for nominal resistance (default: 25)
void setNominalTemperature(int value);
// [103] The beta coefficient of the thermistor (default: 3950)
void setBCoefficient(int value);
// [104] the value of the resistor in series with the thermistor (default: 10000)
void setSeriesResistor(long value);
// [105] set a temperature offset
void setOffset(float value);
// define what to do at each stage of the sketch
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
long _nominal_resistor = 10000;
int _nominal_temperature = 25;
int _b_coefficient = 3950;
long _series_resistor = 10000;
float _offset = 0;
};
#endif
#ifdef USE_ML8511
/*
SensorML8511
*/
class SensorML8511: public Sensor {
public:
SensorML8511(NodeManager& node_Manager, int pin, int child_id = -255);
// define what to do at each stage of the sketch
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
float _mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
};
#endif
#ifdef USE_ACS712
/*
SensorACS712
*/
class SensorACS712: public Sensor {
public:
SensorACS712(NodeManager& node_manager, int pin, int child_id = -255);
// [101] set how many mV are equivalent to 1 Amp. The value depends on the module (100 for 20A Module, 66 for 30A Module) (default: 185);
void setmVPerAmp(int value);
// [102] set ACS offset (default: 2500);
void setOffset(int value);
// define what to do at each stage of the sketch
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
int _ACS_offset = 2500;
int _mv_per_amp = 185;
};
#endif
#ifdef USE_DIGITAL_INPUT
/*
SensorDigitalInput: read the digital input of the configured pin
*/
class SensorDigitalInput: public Sensor {
public:
SensorDigitalInput(NodeManager& node_manager, int pin, int child_id = -255);
// define what to do at each stage of the sketch
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
};
#endif
#ifdef USE_DIGITAL_OUTPUT
/*
SensorDigitalOutput: control a digital output of the configured pin
*/
class SensorDigitalOutput: public Sensor {
public:
SensorDigitalOutput(NodeManager& node_manager, int pin, int child_id = -255);
// [104] when legacy mode is enabled expect a REQ message to trigger, otherwise the default SET (default: false)
void setLegacyMode(bool value);
// [105] automatically turn the output off after the given number of minutes
void setSafeguard(int value);
// [106] if true the input value becomes a duration in minutes after which the output will be automatically turned off (default: false)
void setInputIsElapsed(bool value);
// [107] optionally wait for the given number of milliseconds after changing the status (default: 0)
void setWaitAfterSet(int value);
// [108] when switching on, turns the output off after the given number of milliseconds. For latching relay controls the pulse width (default: 0)
void setPulseWidth(int value);
// [109] Invert the value to write. E.g. if ON is received, write LOW (default: false)
void setInvertValueToWrite(bool value);
// [110] for a 2-pins latching relay, set the pin which turns the relay off (default: -1)
void setPinOff(int value);
// manually switch the output to the provided status (ON or OFF)
void setStatus(int value);
// toggle the status
void toggleStatus();
// get the current state
int getStatus();
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
int _status = OFF;
int _pin_off = -1;
bool _legacy_mode = false;
bool _input_is_elapsed = false;
int _wait_after_set = 0;
int _pulse_width = 0;
bool _invert_value_to_write = false;
Timer* _safeguard_timer = new Timer(_node);
virtual void _switchOutput(int value);
};
/*
SensorRelay
*/
class SensorRelay: public SensorDigitalOutput {
public:
SensorRelay(NodeManager& node_manager, int pin, int child_id = -255);
};
/*
SensorLatchingRelay1Pin
*/
class SensorLatchingRelay1Pin: public SensorRelay {
public:
SensorLatchingRelay1Pin(NodeManager& node_manager, int pin, int child_id = -255);
};
/*
SensorLatchingRelay2Pins
*/
class SensorLatchingRelay2Pins: public SensorRelay {
public:
SensorLatchingRelay2Pins(NodeManager& node_manager, int pin_off, int pin_on, int child_id = -255);
};
#endif
/*
SensorDHT
*/
#ifdef USE_DHT
class SensorDHT: public Sensor {
public:
SensorDHT(NodeManager& node_manager, int pin, int child_id = -255);
// define what to do at each stage of the sketch
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
DHT* _dht;
int _dht_type;
float _offset = 0;
};
/*
SensorDHT11
*/
class SensorDHT11: public SensorDHT {
public:
SensorDHT11(NodeManager& node_manager, int pin, int child_id = -255);
};
/*
SensorDHT22
*/
class SensorDHT22: public SensorDHT {
public:
SensorDHT22(NodeManager& node_manager, int pin, int child_id = -255);
};
#endif
/*
SensorSHT21: temperature and humidity sensor
*/
#ifdef USE_SHT21
class SensorSHT21: public Sensor {
public:
SensorSHT21(NodeManager& node_manager, int child_id = -255);
// define what to do at each stage of the sketch
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
};
/*
SensorHTU21D: temperature and humidity sensor
*/
class SensorHTU21D: public SensorSHT21 {
public:
SensorHTU21D(NodeManager& nodeManager, int child_id = -255);
};
#endif
/*
* SensorInterrupt
*/
#ifdef USE_INTERRUPT
class SensorInterrupt: public Sensor {
public:
SensorInterrupt(NodeManager& node_manager, int pin, int child_id = -255);