NodeManager.h 32.6 KB
Newer Older
DV's avatar
DV committed
1
2
3
4
5
6
7
8
/*
 * NodeManager
 */
#ifndef NodeManager_h
#define NodeManager_h

#include <Arduino.h>

user2684's avatar
user2684 committed
9
// define NodeManager version
user2684's avatar
user2684 committed
10
#define VERSION "1.6-dev"
DV's avatar
DV committed
11
12
13
14
15
16
17
18
19

/***********************************
   Constants
*/

// define sleep mode
#define IDLE 0
#define SLEEP 1
#define WAIT 2
20
#define ALWAYS_ON 3
DV's avatar
DV committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

// define time unit
#define SECONDS 0
#define MINUTES 1
#define HOURS 2
#define DAYS 3

// define value type
#define TYPE_INTEGER 0
#define TYPE_FLOAT 1
#define TYPE_STRING 2

// define interrupt pins
#define INTERRUPT_PIN_1 3
#define INTERRUPT_PIN_2 2

// define eeprom addresses
#define EEPROM_LAST_ID 4
#define EEPROM_SLEEP_SAVED 0
#define EEPROM_SLEEP_MODE 1
#define EEPROM_SLEEP_TIME_MAJOR 2
#define EEPROM_SLEEP_TIME_MINOR 3
#define EEPROM_SLEEP_UNIT 4

DV's avatar
DV committed
45
46
47
48
49
/************************************
 * Include user defined configuration settings
 */
 
#include "config.h"
DV's avatar
DV committed
50
51

/***********************************
DV's avatar
DV committed
52
   Default configuration settings
DV's avatar
DV committed
53
54
55
*/

// if enabled, enable the capability to power on sensors with the arduino's pins to save battery while sleeping
DV's avatar
DV committed
56
57
58
#ifndef POWER_MANAGER
  #define POWER_MANAGER 1
#endif
DV's avatar
DV committed
59
// if enabled, will load the battery manager library to allow the battery level to be reported automatically or on demand
DV's avatar
DV committed
60
61
62
#ifndef BATTERY_MANAGER
  #define BATTERY_MANAGER 1
#endif
DV's avatar
DV committed
63
// if enabled, allow modifying the configuration remotely by interacting with the configuration child id
DV's avatar
DV committed
64
65
66
#ifndef REMOTE_CONFIGURATION
  #define REMOTE_CONFIGURATION 1
#endif
DV's avatar
DV committed
67
// if enabled, persist the configuration settings on EEPROM
DV's avatar
DV committed
68
69
70
#ifndef PERSIST
  #define PERSIST 0
#endif
DV's avatar
DV committed
71
72

// if enabled, enable debug messages on serial port
DV's avatar
DV committed
73
74
75
#ifndef DEBUG
  #define DEBUG 1
#endif
DV's avatar
DV committed
76
77

// if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle
DV's avatar
DV committed
78
79
80
#ifndef SERVICE_MESSAGES
  #define SERVICE_MESSAGES 1
#endif
DV's avatar
DV committed
81
// if enabled, a battery sensor will be created at BATTERY_CHILD_ID and will report vcc voltage together with the battery level percentage
DV's avatar
DV committed
82
83
84
#ifndef BATTERY_SENSOR
  #define BATTERY_SENSOR 1
#endif
DV's avatar
DV committed
85
86

// the child id used to allow remote configuration
DV's avatar
DV committed
87
88
89
#ifndef CONFIGURATION_CHILD_ID
  #define CONFIGURATION_CHILD_ID 200
#endif
DV's avatar
DV committed
90
// the child id used to report the battery voltage to the controller
DV's avatar
DV committed
91
92
93
#ifndef BATTERY_CHILD_ID
  #define BATTERY_CHILD_ID 201
#endif
DV's avatar
DV committed
94

95
// Enable this module to use one of the following sensors: SENSOR_ANALOG_INPUT, SENSOR_LDR, SENSOR_THERMISTOR, SENSOR_MQ, SENSOR_ACS712
DV's avatar
DV committed
96
97
98
#ifndef MODULE_ANALOG_INPUT
  #define MODULE_ANALOG_INPUT 0
#endif
DV's avatar
DV committed
99
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_INPUT
DV's avatar
DV committed
100
101
102
#ifndef MODULE_DIGITAL_INPUT
  #define MODULE_DIGITAL_INPUT 0
#endif
DV's avatar
DV committed
103
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_OUTPUT, SENSOR_RELAY, SENSOR_LATCHING_RELAY
DV's avatar
DV committed
104
105
106
#ifndef MODULE_DIGITAL_OUTPUT
  #define MODULE_DIGITAL_OUTPUT 0
#endif
DV's avatar
DV committed
107
// Enable this module to use one of the following sensors: SENSOR_SHT21, SENSOR_HTU21D
DV's avatar
DV committed
108
109
110
#ifndef MODULE_SHT21
  #define MODULE_SHT21 0
#endif
DV's avatar
DV committed
111
// Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22
DV's avatar
DV committed
112
113
114
#ifndef MODULE_DHT
  #define MODULE_DHT 0
#endif
DV's avatar
DV committed
115
// Enable this module to use one of the following sensors: SENSOR_SWITCH, SENSOR_DOOR, SENSOR_MOTION
DV's avatar
DV committed
116
117
118
#ifndef MODULE_SWITCH
  #define MODULE_SWITCH 0
#endif
DV's avatar
DV committed
119
// Enable this module to use one of the following sensors: SENSOR_DS18B20
DV's avatar
DV committed
120
121
122
#ifndef MODULE_DS18B20
  #define MODULE_DS18B20 0
#endif
DV's avatar
DV committed
123
124
125
126
127
128
129
130
// Enable this module to use one of the following sensors: SENSOR_BH1750
#ifndef MODULE_BH1750
  #define MODULE_BH1750 0
#endif
// Enable this module to use one of the following sensors: SENSOR_MLX90614
#ifndef MODULE_MLX90614
  #define MODULE_MLX90614 0
#endif
131
132
133
134
// Enable this module to use one of the following sensors: SENSOR_BME280
#ifndef MODULE_BME280
  #define MODULE_BME280 0
#endif
135
136
137
138
139
140
141
142
143
144
145
146
// Enable this module to use one of the following sensors: SENSOR_SONOFF
#ifndef MODULE_SONOFF
  #define MODULE_SONOFF 0
#endif
// Enable this module to use one of the following sensors: SENSOR_BMP085
#ifndef MODULE_BMP085
  #define MODULE_BMP085 0
#endif
// Enable this module to use one of the following sensors: SENSOR_HCSR04
#ifndef MODULE_HCSR04
  #define MODULE_HCSR04 0
#endif
147
148
149
150
151
// Enable this module to use one of the following sensors: SENSOR_MCP9808
#ifndef MODULE_MCP9808
  #define MODULE_MCP9808 0
#endif

DV's avatar
DV committed
152
153
154
155
156
157
158
159
160
161
/***********************************
   Sensors types
*/
#if MODULE_ANALOG_INPUT == 1
  // Generic analog sensor, return a pin's analog value or its percentage
  #define SENSOR_ANALOG_INPUT 1
  // LDR sensor, return the light level of an attached light resistor in percentage
  #define SENSOR_LDR 2
  // Thermistor sensor, return the temperature based on the attached thermistor
  #define SENSOR_THERMISTOR 3
user2684's avatar
user2684 committed
162
163
  // MQ2 air quality sensor
  #define SENSOR_MQ 19
164
165
  // ML8511 UV sensor
  #define SENSOR_ML8511 20
166
167
  // Current sensor
  #define SENSOR_ACS712 24
168
169
  // rain gauge sensor
  #define SENSOR_RAIN_GAUGE 26
DV's avatar
DV committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#endif
#if MODULE_DIGITAL_INPUT == 1
  // Generic digital sensor, return a pin's digital value
  #define SENSOR_DIGITAL_INPUT 4
#endif
#if MODULE_DIGITAL_OUTPUT == 1
  // Generic digital output sensor, allows setting the digital output of a pin to the requested value
  #define SENSOR_DIGITAL_OUTPUT 5
  // Relay sensor, allows activating the relay
  #define SENSOR_RELAY 6
  // Latching Relay sensor, allows activating the relay with a pulse
  #define SENSOR_LATCHING_RELAY 7
#endif
#if MODULE_DHT == 1
  // DHT11/DHT22 sensors, return temperature/humidity based on the attached DHT sensor
  #define SENSOR_DHT11 8
  #define SENSOR_DHT22 9
#endif
#if MODULE_SHT21 == 1
  // SHT21 sensor, return temperature/humidity based on the attached SHT21 sensor
  #define SENSOR_SHT21 10
DV's avatar
DV committed
191
  #define SENSOR_HTU21D 15
DV's avatar
DV committed
192
193
194
195
196
197
198
199
200
201
202
203
204
#endif
#if MODULE_SWITCH == 1
  // Generic switch, wake up the board when a pin changes status
  #define SENSOR_SWITCH 11
  // Door sensor, wake up the board and report when an attached magnetic sensor has been opened/closed
  #define SENSOR_DOOR 12
  // Motion sensor, wake up the board and report when an attached PIR has triggered
  #define SENSOR_MOTION 13
#endif
#if MODULE_DS18B20 == 1
  // DS18B20 sensor, return the temperature based on the attached sensor
  #define SENSOR_DS18B20 14
#endif
DV's avatar
DV committed
205
206
207
208
209
210
211
212
#if MODULE_BH1750 == 1
  // BH1750 sensor, return light in lux
  #define SENSOR_BH1750 16
#endif
#if MODULE_MLX90614 == 1
  // MLX90614 sensor, contactless temperature sensor
  #define SENSOR_MLX90614 17
#endif
213
214
215
216
#if MODULE_BME280 == 1
  // MLX90614 sensor, contactless temperature sensor
  #define SENSOR_BME280 18
#endif
217
218
219
220
221
222
223
224
225
226
227
228
#if MODULE_SONOFF == 1
  // Sonoff wireless smart switch
  #define SENSOR_SONOFF 21
#endif
#if MODULE_BMP085 == 1
  // BMP085/BMP180 sensor, return temperature and pressure
  #define SENSOR_BMP085 22
#endif
#if MODULE_HCSR04 == 1
  // HC-SR04 sensor, return the distance between the sensor and an object
  #define SENSOR_HCSR04 23
#endif
229
230
231
232
#if MODULE_MCP9808 == 1
  // MCP9808 sensor, precision temperature sensor
  #define SENSOR_MCP9808 25
#endif
233
// last Id: 26
DV's avatar
DV committed
234
235
236
237
/***********************************
  Libraries
*/

238
239
240
241
242
243
244
245
// include supporting libraries
#ifdef MY_USE_UDP
    #include <WiFiUdp.h>
#endif
#ifdef MY_GATEWAY_ESP8266
  #include <ESP8266WiFi.h>
#endif

DV's avatar
DV committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// include MySensors libraries
#include <core/MySensorsCore.h>

// include third party libraries
#if MODULE_DHT == 1
  #include <DHT.h>
#endif
#if MODULE_SHT21 == 1
  #include <Wire.h>
  #include <Sodaq_SHT2x.h>
#endif
#if MODULE_DS18B20 == 1
  #include <OneWire.h>
  #include <DallasTemperature.h>
#endif
DV's avatar
DV committed
261
262
263
264
265
266
267
268
#if MODULE_BH1750 == 1
  #include <BH1750.h>
  #include <Wire.h>
#endif
#if MODULE_MLX90614 == 1
  #include <Wire.h>
  #include <Adafruit_MLX90614.h>
#endif
269
270
271
272
273
274
#if MODULE_BME280 == 1
  #include <Wire.h>
  #include <SPI.h>
  #include <Adafruit_Sensor.h>
  #include <Adafruit_BME280.h>
#endif
275
276
277
278
279
280
281
282
283
284
#if MODULE_SONOFF == 1
  #include <Bounce2.h>
#endif
#if MODULE_BMP085 == 1
  #include <Wire.h>
  #include <Adafruit_BMP085.h>
#endif
#if MODULE_HCSR04 == 1
  #include <NewPing.h>
#endif
285
286
287
288
#if MODULE_MCP9808 == 1
  #include <Wire.h>
  #include "Adafruit_MCP9808.h"
#endif
DV's avatar
DV committed
289
290
291
292
293
294
295
296
297
298
299
300
301

/**************************************
   Classes
*/

/*
   PowerManager
*/

class PowerManager {
  public:
    PowerManager() {};
    // to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
user2684's avatar
user2684 committed
302
    void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
DV's avatar
DV committed
303
304
    void powerOn();
    void powerOff();
305
    float getVcc();
user2684's avatar
user2684 committed
306
    bool isConfigured();
DV's avatar
DV committed
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
  private:
    int _vcc_pin = -1;
    int _ground_pin = -1;
    long _wait = 0;
};


/***************************************
   Sensor: generic sensor class
*/
class Sensor {
  public:
    Sensor(int child_id, int pin);
    // where the sensor is attached to (default: not set)
    void setPin(int value);
    int getPin();
    // child_id of this sensor (default: not set)
    void setChildId(int value);
    int getChildId();
    // presentation of this sensor (default: S_CUSTOM)
    void setPresentation(int value);
    int getPresentation();
    // type of this sensor (default: V_CUSTOM)
    void setType(int value);
    int getType();
user2684's avatar
user2684 committed
332
333
    // description of the sensor (default: '')
    void setDescription(char *value);
user2684's avatar
user2684 committed
334
335
    // set this to true if you want destination node to send ack back to this node (default: false)
    void setAck(bool value);
DV's avatar
DV committed
336
337
338
339
340
341
342
343
344
345
346
347
    // when queried, send the message multiple times (default: 1)
    void setRetries(int value);
    // For some sensors, the measurement can be queried multiple times and an average is returned (default: 1)
    void setSamples(int value);
    // If more then one sample has to be taken, set the interval in milliseconds between measurements (default: 0)
    void setSamplesInterval(int value);
    // if true will report the measure only if different then the previous one (default: false)
    void setTackLastValue(bool value);
    // if track last value is enabled, force to send an update after the configured number of cycles (default: -1)
    void setForceUpdate(int value);
    // the value type of this sensor (default: TYPE_INTEGER)
    void setValueType(int value);
348
    int getValueType();
DV's avatar
DV committed
349
350
    // for float values, set the float precision (default: 2)
    void setFloatPrecision(int value);
DV's avatar
DV committed
351
352
    // optionally sleep interval in milliseconds before sending each message to the radio network (default: 0)
    void setSleepBetweenSend(int value);
353
354
355
    // set the interrupt pin the sensor is attached to so its loop() will be executed only upon that interrupt (default: -1)
    void setInterruptPin(int value);
    int getInterruptPin();
DV's avatar
DV committed
356
357
    #if POWER_MANAGER == 1
      // to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
user2684's avatar
user2684 committed
358
      void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
DV's avatar
DV committed
359
360
361
      // if enabled the pins will be automatically powered on while awake and off during sleeping (default: true)
      void setAutoPowerPins(bool value);
      // manually turn the power on
DV's avatar
DV committed
362
      void powerOn();
DV's avatar
DV committed
363
      // manually turn the power off
DV's avatar
DV committed
364
365
      void powerOff();
    #endif
366
367
368
369
    // get the latest recorded value from the sensor
    int getValueInt();
    float getValueFloat();
    char* getValueString();
DV's avatar
DV committed
370
371
372
    // define what to do at each stage of the sketch
    virtual void before();
    virtual void presentation();
user2684's avatar
user2684 committed
373
    virtual void setup();
DV's avatar
DV committed
374
375
376
377
    virtual void loop(const MyMessage & message);
    virtual void receive(const MyMessage & message);
    // abstract functions, subclasses need to implement
    virtual void onBefore() = 0;
user2684's avatar
user2684 committed
378
    virtual void onSetup() = 0;
DV's avatar
DV committed
379
380
381
382
    virtual void onLoop() = 0;
    virtual void onReceive(const MyMessage & message) = 0;
  protected:
    MyMessage _msg;
DV's avatar
DV committed
383
    int _sleep_between_send = 0;
DV's avatar
DV committed
384
385
386
387
    int _pin = -1;
    int _child_id;
    int _presentation = S_CUSTOM;
    int _type = V_CUSTOM;
user2684's avatar
user2684 committed
388
    char* _description = "";
user2684's avatar
user2684 committed
389
    bool _ack = false;
DV's avatar
DV committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
    int _retries = 1;
    int _samples = 1;
    int _samples_interval = 0;
    bool _track_last_value = false;
    int _cycles = 0;
    int _force_update = -1;
    int _value_type = TYPE_INTEGER;
    int _float_precision = 2;
    int _value_int = -1;
    float _value_float = -1;
    char * _value_string = "";
    int _last_value_int = -1;
    float _last_value_float = -1;
    char * _last_value_string = "";
404
405
406
407
408
409
    int _interrupt_pin = -1;
    #if POWER_MANAGER  == 1
      PowerManager _powerManager;
      bool _auto_power_pins = true;
    #endif
    void _send(MyMessage & msg);
DV's avatar
DV committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
};

/*
   SensorAnalogInput: read the analog input of a configured pin
*/
class SensorAnalogInput: public Sensor {
  public:
    SensorAnalogInput(int child_id, int pin);
    // the analog reference to use (default: not set, can be either INTERNAL or DEFAULT)
    void setReference(int value);
    // reverse the value or the percentage (e.g. 70% -> 30%) (default: false)
    void setReverse(bool value);
    // when true returns the value as a percentage (default: true)
    void setOutputPercentage(bool value);
    // minimum value for calculating the percentage (default: 0)
    void setRangeMin(int value);
    // maximum value for calculating the percentage (default: 1024)
    void setRangeMax(int value);
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
430
    void onSetup();
DV's avatar
DV committed
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
    void onLoop();
    void onReceive(const 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(int child_id, int pin);
};

/*
   SensorThermistor: read the temperature from a thermistor
*/
class SensorThermistor: public Sensor {
  public:
    SensorThermistor(int child_id, int pin);
    // resistance at 25 degrees C (default: 10000)
458
    void setNominalResistor(long value);
DV's avatar
DV committed
459
460
461
462
463
    // temperature for nominal resistance (default: 25)
    void setNominalTemperature(int value);
    // The beta coefficient of the thermistor (default: 3950)
    void setBCoefficient(int value);
    // the value of the resistor in series with the thermistor (default: 10000)
464
    void setSeriesResistor(long value);
DV's avatar
DV committed
465
466
467
468
    // set a temperature offset
    void setOffset(float value);
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
469
    void onSetup();
DV's avatar
DV committed
470
471
472
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
473
    long _nominal_resistor = 10000;
DV's avatar
DV committed
474
475
    int _nominal_temperature = 25;
    int _b_coefficient = 3950;
476
    long _series_resistor = 10000;
DV's avatar
DV committed
477
478
479
    float _offset = 0;
};

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
/*
    SensorMQ
 */
class SensorMQ: public Sensor {
  public:
    SensorMQ(int child_id, int pin);
    // define the target gas whose ppm has to be returned. 0: LPG, 1: CO, 2: Smoke (default: 1);
    void setTargetGas(int value);
    // define the load resistance on the board, in kilo ohms (default: 1);
    void setRlValue(float value);
    // define the Ro resistance on the board (default: 10000);
    void setRoValue(float value);
    // Sensor resistance in clean air (default: 9.83);
    void setCleanAirFactor(float value);
    // define how many samples you are going to take in the calibration phase (default: 50);
    void setCalibrationSampleTimes(int value);
    // define the time interal(in milisecond) between each samples in the cablibration phase (default: 500);
    void setCalibrationSampleInterval(int value);
    // define how many samples you are going to take in normal operation (default: 50);
    void setReadSampleTimes(int value);
    // define the time interal(in milisecond) between each samples in the normal operations (default: 5);
    void setReadSampleInterval(int value);
    // set the LPGCurve array (default: {2.3,0.21,-0.47})
    void setLPGCurve(float *value);
    // set the COCurve array (default: {2.3,0.72,-0.34})
    void setCOCurve(float *value);
    // set the SmokeCurve array (default: {2.3,0.53,-0.44})
    void setSmokeCurve(float *value);
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
    float _rl_value = 1.0;
    float _ro_clean_air_factor = 9.83;
    int _calibration_sample_times = 50;
    int _calibration_sample_interval = 500;
    int _read_sample_interval = 50;
    int _read_sample_times = 5;
    float _ro = 10000.0;
    float _LPGCurve[3] = {2.3,0.21,-0.47};
    float _COCurve[3] = {2.3,0.72,-0.34};
    float _SmokeCurve[3] = {2.3,0.53,-0.44};
    float _MQResistanceCalculation(int raw_adc);
    float _MQCalibration();
    float _MQRead();
    int _MQGetGasPercentage(float rs_ro_ratio, int gas_id);
    int  _MQGetPercentage(float rs_ro_ratio, float *pcurve);
    int _gas_lpg = 0;
    int _gas_co = 1;
    int _gas_smoke = 2;
    int _target_gas = _gas_co;
};

/*
    SensorML8511
537
*/
538
539
540
541
542
543
544
545
546
547
548
549

class SensorML8511: public Sensor {
  public:
    SensorML8511(int child_id, int pin);
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
    float _mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
};
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570

/*
    SensorACS712
*/

class SensorACS712: public Sensor {
  public:
    SensorACS712(int child_id, int pin);
    // 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);
    // set ACS offset (default: 2500);
    void setOffset(int value);
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
    int _ACS_offset = 2500;
    int _mv_per_amp = 185;
};
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592

/*
    SensorRainGauge
*/

class SensorRainGauge: public Sensor {
  public:
    SensorRainGauge(int child_id, int pin);
    // set how frequently to report back to the controller in minutes. After reporting the measure is resetted (default: 60);
    void setReportInterval(int value);
    // set how many mm of rain to count for each tip (default: 0.11);
    void setSingleTip(float value);
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
  public:
    static void _onTipped();
    static long _last_tip;
    static long _count;
  protected:
593
    int _report_interval = 60;
594
595
596
    float _single_tip = 0.11;
    long _last_report = 0;
};
597

DV's avatar
DV committed
598
599
600
601
602
603
604
605
/*
   SensorDigitalInput: read the digital input of the configured pin
*/
class SensorDigitalInput: public Sensor {
  public:
    SensorDigitalInput(int child_id, int pin);
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
606
    void onSetup();
DV's avatar
DV committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
    void onLoop();
    void onReceive(const MyMessage & message);
};

/*
   SensorDigitalOutput: control a digital output of the configured pin
*/
class SensorDigitalOutput: public Sensor {
  public:
    SensorDigitalOutput(int child_id, int pin);
    // set how to initialize the output (default: LOW)
    void setInitialValue(int value);
    // if greater than 0, send a pulse of the given duration in ms and then restore the output back to the original value (default: 0)
    void setPulseWidth(int value);
621
622
    // define which value to set to the output when set to on (default: HIGH)
    void setOnValue(int value);
623
624
    // when legacy mode is enabled expect a REQ message to trigger, otherwise the default SET (default: false)
    void setLegacyMode(bool value);
DV's avatar
DV committed
625
626
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
627
    void onSetup();
DV's avatar
DV committed
628
629
630
631
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
    int _initial_value = LOW;
632
    int _on_value = HIGH;
633
    int _state = 0;
DV's avatar
DV committed
634
    int _pulse_width = 0;
635
    bool _legacy_mode = false;
DV's avatar
DV committed
636
637
638
639
640
641
642
643
644
};


/*
   SensorRelay
*/
class SensorRelay: public SensorDigitalOutput {
  public:
    SensorRelay(int child_id, int pin);
DV's avatar
DV committed
645
646
    // define what to do at each stage of the sketch
    void onLoop();
DV's avatar
DV committed
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
};

/*
   SensorLatchingRelay
*/
class SensorLatchingRelay: public SensorRelay {
  public:
    SensorLatchingRelay(int child_id, int pin);
};

/*
   SensorDHT
*/
#if MODULE_DHT == 1
class SensorDHT: public Sensor {
  public:
    SensorDHT(int child_id, int pin, DHT* dht, int sensor_type, int dht_type);
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
666
    void onSetup();
DV's avatar
DV committed
667
668
    void onLoop();
    void onReceive(const MyMessage & message);
669
670
671
    // constants
    const static int TEMPERATURE = 0;
    const static int HUMIDITY = 1;
DV's avatar
DV committed
672
673
674
675
676
677
678
679
680
  protected:
    DHT* _dht;
    int _dht_type = DHT11;
    float _offset = 0;
    int _sensor_type = 0;
};
#endif

/*
DV's avatar
DV committed
681
   SensorSHT21: temperature and humidity sensor
DV's avatar
DV committed
682
683
684
685
686
687
688
*/
#if MODULE_SHT21 == 1
class SensorSHT21: public Sensor {
  public:
    SensorSHT21(int child_id, int sensor_type);
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
689
    void onSetup();
DV's avatar
DV committed
690
691
    void onLoop();
    void onReceive(const MyMessage & message);
692
693
694
    // constants
    const static int TEMPERATURE = 0;
    const static int HUMIDITY = 1;
DV's avatar
DV committed
695
696
697
698
  protected:
    float _offset = 0;
    int _sensor_type = 0;
};
DV's avatar
DV committed
699
700
701
702
703
704
705
706
707

/*
   SensorHTU21D: temperature and humidity sensor
*/

class SensorHTU21D: public SensorSHT21 {
  public:
    SensorHTU21D(int child_id, int pin);
};
DV's avatar
DV committed
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#endif

/*
 * SensorSwitch
 */
class SensorSwitch: public Sensor {
  public:
    SensorSwitch(int child_id, int pin);
    // set the interrupt mode. Can be CHANGE, RISING, FALLING (default: CHANGE)
    void setMode(int value);
    int getMode();
    // milliseconds to wait before reading the input (default: 0)
    void setDebounce(int value);
    // time to wait in milliseconds after a change is detected to allow the signal to be restored to its normal value (default: 0)
    void setTriggerTime(int value);
723
724
725
    // Set initial value on the interrupt pin (default: HIGH)
    void setInitial(int value);
    int getInitial();
DV's avatar
DV committed
726
727
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
728
    void onSetup();
DV's avatar
DV committed
729
730
731
732
733
734
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
    int _debounce = 0;
    int _trigger_time = 0;
    int _mode = CHANGE;
735
    int _initial = HIGH;
DV's avatar
DV committed
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
};

/*
 * SensorDoor
 */
class SensorDoor: public SensorSwitch {
  public:
    SensorDoor(int child_id, int pin);
};

/*
 * SensorMotion
 */
class SensorMotion: public SensorSwitch {
  public:
    SensorMotion(int child_id, int pin);
};

/*
   SensorDs18b20
*/
#if MODULE_DS18B20 == 1
class SensorDs18b20: public Sensor {
  public:
    SensorDs18b20(int child_id, int pin, DallasTemperature* sensors, int index);
user2684's avatar
user2684 committed
761
762
763
764
765
766
    // return the sensors' device address
    DeviceAddress* getDeviceAddress();
    // returns the sensor's resolution in bits
    int getResolution();
    // set the sensor's resolution in bits
    void setResolution(int value);
767
768
769
770
771
772
773
    // sleep while DS18B20 calculates temperature (default: false)
    void setSleepDuringConversion(bool value);
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
DV's avatar
DV committed
774
775
776
  protected:
    float _offset = 0;
    int _index;
777
    bool _sleep_during_conversion = false;
DV's avatar
DV committed
778
    DallasTemperature* _sensors;
user2684's avatar
user2684 committed
779
    DeviceAddress _device_address;
DV's avatar
DV committed
780
781
782
};
#endif

DV's avatar
DV committed
783
784
785
786
787
788
789
790
791
/*
   SensorBH1750
*/
#if MODULE_BH1750 == 1
class SensorBH1750: public Sensor {
  public:
    SensorBH1750(int child_id);
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
792
    void onSetup();
DV's avatar
DV committed
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
    BH1750* _lightSensor;
};
#endif

/*
   SensorMLX90614
*/
#if MODULE_MLX90614 == 1
class SensorMLX90614: public Sensor {
  public:
    SensorMLX90614(int child_id, Adafruit_MLX90614* mlx, int sensor_type);
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
809
    void onSetup();
DV's avatar
DV committed
810
811
    void onLoop();
    void onReceive(const MyMessage & message);
812
813
814
    // constants
    const static int TEMPERATURE_AMBIENT = 0;
    const static int TEMPERATURE_OBJECT = 1;
DV's avatar
DV committed
815
816
817
818
819
  protected:
    Adafruit_MLX90614* _mlx;
    int _sensor_type;
};
#endif
DV's avatar
DV committed
820

821

822
/*
823
 * SensorBosch
824
*/
825
826
827

#if MODULE_BME280 == 1 || MODULE_BMP085 == 1
class SensorBosch: public Sensor {
828
  public:
829
    SensorBosch(int child_id, int sensor_type);
user2684's avatar
user2684 committed
830
831
    // define how many pressure samples to keep track of for calculating the forecast (default: 5)
    void setForecastSamplesCount(int value);
832
833
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
834
    void onSetup();
835
836
    void onLoop();
    void onReceive(const MyMessage & message);
837
838
839
840
841
842
    // constants
    const static int TEMPERATURE = 0;
    const static int HUMIDITY = 1;
    const static int PRESSURE = 2;
    const static int FORECAST = 3;
    static uint8_t GetI2CAddress(uint8_t chip_id);
843
844
  protected:
    int _sensor_type;
user2684's avatar
user2684 committed
845
846
847
848
849
850
851
852
853
    char* _weather[6] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" };
    int _forecast_samples_count = 5;
    float* _forecast_samples;
    int _minute_count = 0;
    float _pressure_avg;
    float _pressure_avg2;
    float _dP_dt;
    bool _first_round = true;
    float _getLastPressureSamplesAverage();
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
    void _forecast(float pressure);
};
#endif

/*
   SensorBME280
*/
#if MODULE_BME280 == 1
class SensorBME280: public SensorBosch {
  public:
    SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type);
    void onLoop();
  protected:
    Adafruit_BME280* _bme;
};
#endif

/*
   SensorBMP085
*/
#if MODULE_BMP085 == 1
class SensorBMP085: public SensorBosch {
  public:
    SensorBMP085(int child_id, Adafruit_BMP085* bmp, int sensor_type);
    void onLoop();
  protected:
    Adafruit_BMP085* _bmp;
881
882
883
};
#endif

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
/*
   SensorHCSR04
*/
#if MODULE_HCSR04 == 1
class SensorHCSR04: public Sensor {
  public:
    SensorHCSR04(int child_id, int pin);
    // Arduino pin tied to trigger pin on the ultrasonic sensor (default: the pin set while registering the sensor)
    void setTriggerPin(int value);
    // Arduino pin tied to echo pin on the ultrasonic sensor (default: the pin set while registering the sensor)
    void setEchoPin(int value);
    // Maximum distance we want to ping for (in centimeters) (default: 300)
    void setMaxDistance(int value);
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
    int _trigger_pin;
    int _echo_pin;
    int _max_distance = 300;
    NewPing* _sonar;
};
#endif
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
/*
   SensorSonoff
*/
#if MODULE_SONOFF == 1
class SensorSonoff: public Sensor {
  public:
    SensorSonoff(int child_id);
    // set the button's pin (default: 0)
    void setButtonPin(int value);
    // set the relay's pin (default: 12)
    void setRelayPin(int value);
    // set the led's pin (default: 13)
    void setLedPin(int value);
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
    Bounce _debouncer = Bounce();
    int _button_pin = 0;
    int _relay_pin = 12;
    int _led_pin = 13;
    int _old_value = 0;
    bool _state = false;
    int _relay_on = 1;
    int _relay_off = 0;
    int _led_on = 0;
    int _led_off = 1;
    void _blink();
    void _toggle();
};
#endif

944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
/*
   SensorMCP9808
*/
#if MODULE_MCP9808 == 1
class SensorMCP9808: public Sensor {
  public:
    SensorMCP9808(int child_id, Adafruit_MCP9808* mcp);
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
  protected:
    Adafruit_MCP9808* _mcp;
};
#endif

user2684's avatar
user2684 committed
961

DV's avatar
DV committed
962
963
964
965
966
967
968
969
/***************************************
   NodeManager: manages all the aspects of the node
*/
class NodeManager {
  public:
    NodeManager();
    // the pin to connect to the RST pin to reboot the board (default: 4)
    void setRebootPin(int value);
DV's avatar
DV committed
970
971
    // send the same service message multiple times (default: 1)
    void setRetries(int value);
DV's avatar
DV committed
972
973
974
975
976
    #if BATTERY_MANAGER == 1
      // the expected vcc when the batter is fully discharged, used to calculate the percentage (default: 2.7)
      void setBatteryMin(float value);
      // the expected vcc when the batter is fully charged, used to calculate the percentage (default: 3.3)
      void setBatteryMax(float value);
977
      // after how many sleeping cycles report the battery level to the controller. When reset the battery is always reported (default: 10)
DV's avatar
DV committed
978
      void setBatteryReportCycles(int value);
979
980
981
982
983
984
      // 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);
      // 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);
      // if setBatteryInternalVcc() is set to false, the volts per bit ratio used to calculate the battery voltage (default: 0.003363075)
      void setBatteryVoltsPerBit(float value);
user2684's avatar
user2684 committed
985
      // If true, wake up by an interrupt counts as a valid cycle for battery reports otherwise only uninterrupted sleep cycles would contribute (default: true)
986
      void setBatteryReportWithInterrupt(bool value);
DV's avatar
DV committed
987
    #endif
988
989
990
991
992
993
994
995
996
997
998
    // define the way the node should behave. It can be IDLE (stay awake withtout executing each sensors' loop), SLEEP (go to sleep for the configured interval), WAIT (wait for the configured interval), ALWAYS_ON (stay awake and execute each sensors' loop)
    void setSleepMode(int value);
    void setMode(int value);
    // define for how long the board will sleep (default: 0)
    void setSleepTime(int value);
    // define the unit of SLEEP_TIME. It can be SECONDS, MINUTES, HOURS or DAYS (default: MINUTES)
    void setSleepUnit(int value);
    // configure the node's behavior, parameters are mode, time and unit
    void setSleep(int value1, int value2, int value3);
    // if enabled, when waking up from the interrupt, the board stops sleeping. Disable it when attaching e.g. a motion sensor (default: true)
    void setSleepInterruptPin(int value);
DV's avatar
DV committed
999
1000
    // configure the interrupt pin and mode. Mode can be CHANGE, RISING, FALLING (default: MODE_NOT_DEFINED)
    void setInterrupt(int pin, int mode, int pull = -1);