NodeManager.h 40.5 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

// define time unit
#define SECONDS 0
#define MINUTES 1
#define HOURS 2
#define DAYS 3
27
#define CYCLES 4
DV's avatar
DV committed
28

29
30
31
32
// define on/off
#define OFF 0
#define ON 1

DV's avatar
DV committed
33
34
35
36
37
38
39
40
41
// 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

42
43
44
45
46
// define configuration settings that can be saved and loaded from the EEPROM
#define SAVE_SLEEP_MODE 0
#define SAVE_SLEEP_TIME 1
#define SAVE_SLEEP_UNIT 2

DV's avatar
DV committed
47
48
49
50
51
52
// define eeprom addresses
#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
53
54
55
#define EEPROM_USER_START 100

// define requests
DV's avatar
DV committed
56

DV's avatar
DV committed
57
58
59
60
61
/************************************
 * Include user defined configuration settings
 */
 
#include "config.h"
DV's avatar
DV committed
62
63

/***********************************
DV's avatar
DV committed
64
   Default configuration settings
DV's avatar
DV committed
65
*/
66
67
68
69
// if enabled, enable debug messages on serial port
#ifndef DEBUG
  #define DEBUG 1
#endif
DV's avatar
DV committed
70
71

// if enabled, enable the capability to power on sensors with the arduino's pins to save battery while sleeping
DV's avatar
DV committed
72
73
74
#ifndef POWER_MANAGER
  #define POWER_MANAGER 1
#endif
DV's avatar
DV committed
75
// 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
76
77
78
#ifndef BATTERY_MANAGER
  #define BATTERY_MANAGER 1
#endif
DV's avatar
DV committed
79
// if enabled, allow modifying the configuration remotely by interacting with the configuration child id
DV's avatar
DV committed
80
81
82
#ifndef REMOTE_CONFIGURATION
  #define REMOTE_CONFIGURATION 1
#endif
DV's avatar
DV committed
83
// if enabled, persist the configuration settings on EEPROM
DV's avatar
DV committed
84
85
86
#ifndef PERSIST
  #define PERSIST 0
#endif
DV's avatar
DV committed
87
88

// if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle
DV's avatar
DV committed
89
#ifndef SERVICE_MESSAGES
90
  #define SERVICE_MESSAGES 0
DV's avatar
DV committed
91
#endif
DV's avatar
DV committed
92
// 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
93
94
95
#ifndef BATTERY_SENSOR
  #define BATTERY_SENSOR 1
#endif
DV's avatar
DV committed
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// the child id used to allow remote configuration
#ifndef CONFIGURATION_CHILD_ID
  #define CONFIGURATION_CHILD_ID 200
#endif
// the child id used to report the battery voltage to the controller
#ifndef BATTERY_CHILD_ID
  #define BATTERY_CHILD_ID 201
#endif
// define the maximum number of sensors that can be managed
#ifndef MAX_SENSORS
  #define MAX_SENSORS 10
#endif

/***********************************
   Default module settings
*/

114
// Enable this module to use one of the following sensors: SENSOR_ANALOG_INPUT, SENSOR_LDR, SENSOR_THERMISTOR, SENSOR_ACS712
DV's avatar
DV committed
115
116
117
#ifndef MODULE_ANALOG_INPUT
  #define MODULE_ANALOG_INPUT 0
#endif
DV's avatar
DV committed
118
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_INPUT
DV's avatar
DV committed
119
120
121
#ifndef MODULE_DIGITAL_INPUT
  #define MODULE_DIGITAL_INPUT 0
#endif
DV's avatar
DV committed
122
// Enable this module to use one of the following sensors: SENSOR_DIGITAL_OUTPUT, SENSOR_RELAY, SENSOR_LATCHING_RELAY
DV's avatar
DV committed
123
124
125
#ifndef MODULE_DIGITAL_OUTPUT
  #define MODULE_DIGITAL_OUTPUT 0
#endif
DV's avatar
DV committed
126
// Enable this module to use one of the following sensors: SENSOR_SHT21, SENSOR_HTU21D
DV's avatar
DV committed
127
128
129
#ifndef MODULE_SHT21
  #define MODULE_SHT21 0
#endif
DV's avatar
DV committed
130
// Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22
DV's avatar
DV committed
131
132
133
#ifndef MODULE_DHT
  #define MODULE_DHT 0
#endif
DV's avatar
DV committed
134
// Enable this module to use one of the following sensors: SENSOR_SWITCH, SENSOR_DOOR, SENSOR_MOTION
DV's avatar
DV committed
135
136
137
#ifndef MODULE_SWITCH
  #define MODULE_SWITCH 0
#endif
DV's avatar
DV committed
138
// Enable this module to use one of the following sensors: SENSOR_DS18B20
DV's avatar
DV committed
139
140
141
#ifndef MODULE_DS18B20
  #define MODULE_DS18B20 0
#endif
DV's avatar
DV committed
142
143
144
145
146
147
148
149
// 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
150
151
152
153
// Enable this module to use one of the following sensors: SENSOR_BME280
#ifndef MODULE_BME280
  #define MODULE_BME280 0
#endif
154
155
156
157
158
159
160
161
162
163
164
165
// 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
166
167
168
169
// Enable this module to use one of the following sensors: SENSOR_MCP9808
#ifndef MODULE_MCP9808
  #define MODULE_MCP9808 0
#endif
170
171
172
173
// Enable this module to use one of the following sensors: SENSOR_MQ
#ifndef MODULE_MQ
  #define MODULE_MQ 0
#endif
174
175
176
177
// Enable this module to use one of the following sensors: SENSOR_MHZ19
#ifndef MODULE_MHZ19
  #define MODULE_MHZ19 0
#endif
178

DV's avatar
DV committed
179
/***********************************
180
   Supported Sensors
DV's avatar
DV committed
181
*/
182
183
184
185
186
187
188
189
190
191
192
193
194
195
enum supported_sensors {
  #if MODULE_ANALOG_INPUT == 1
    // Generic analog sensor, return a pin's analog value or its percentage
    SENSOR_ANALOG_INPUT,
    // LDR sensor, return the light level of an attached light resistor in percentage
    SENSOR_LDR,
    // Thermistor sensor, return the temperature based on the attached thermistor
    SENSOR_THERMISTOR,
    // ML8511 UV sensor
    SENSOR_ML8511,
    // Current sensor
    SENSOR_ACS712,
    // rain gauge sensor
    SENSOR_RAIN_GAUGE,
196
197
198
199
    // Rain sensor, return the percentage of rain from an attached analog sensor
    SENSOR_RAIN,
    // Soil moisture sensor, return the percentage of moisture from an attached analog sensor
    SENSOR_SOIL_MOISTURE,
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
  #endif
  #if MODULE_DIGITAL_INPUT == 1
    // Generic digital sensor, return a pin's digital value
    SENSOR_DIGITAL_INPUT,
  #endif
  #if MODULE_DIGITAL_OUTPUT == 1
    // Generic digital output sensor, allows setting the digital output of a pin to the requested value
    SENSOR_DIGITAL_OUTPUT,
    // Relay sensor, allows activating the relay
    SENSOR_RELAY,
    // Latching Relay sensor, allows activating the relay with a pulse
    SENSOR_LATCHING_RELAY,
  #endif
  #if MODULE_DHT == 1
    // DHT11/DHT22 sensors, return temperature/humidity based on the attached DHT sensor
    SENSOR_DHT11,
    SENSOR_DHT22,
  #endif
  #if MODULE_SHT21 == 1
    // SHT21 sensor, return temperature/humidity based on the attached SHT21 sensor
    SENSOR_SHT21,
    SENSOR_HTU21D,
  #endif
  #if MODULE_SWITCH == 1
    // Generic switch, wake up the board when a pin changes status
    SENSOR_SWITCH,
    // Door sensor, wake up the board and report when an attached magnetic sensor has been opened/closed
    SENSOR_DOOR,
    // Motion sensor, wake up the board and report when an attached PIR has triggered
    SENSOR_MOTION,
  #endif
  #if MODULE_DS18B20 == 1
    // DS18B20 sensor, return the temperature based on the attached sensor
    SENSOR_DS18B20,
  #endif
  #if MODULE_BH1750 == 1
    // BH1750 sensor, return light in lux
    SENSOR_BH1750,
  #endif
  #if MODULE_MLX90614 == 1
    // MLX90614 sensor, contactless temperature sensor
    SENSOR_MLX90614,
  #endif
  #if MODULE_BME280 == 1
    // MLX90614 sensor, contactless temperature sensor
    SENSOR_BME280,
  #endif
  #if MODULE_SONOFF == 1
    // Sonoff wireless smart switch
    SENSOR_SONOFF,
  #endif
  #if MODULE_BMP085 == 1
    // BMP085/BMP180 sensor, return temperature and pressure
    SENSOR_BMP085,
  #endif
  #if MODULE_HCSR04 == 1
    // HC-SR04 sensor, return the distance between the sensor and an object
    SENSOR_HCSR04,
  #endif
  #if MODULE_MCP9808 == 1
    // MCP9808 sensor, precision temperature sensor
    SENSOR_MCP9808,
  #endif
263
264
265
266
  #if MODULE_MQ == 1
    // MQ2 air quality sensor
    SENSOR_MQ,
  #endif
267
268
269
270
  #if MODULE_MHZ19 == 1
    // MH-Z19 CO2 sensor
    SENSOR_MHZ19,
  #endif
271
};
DV's avatar
DV committed
272
273
274
275
/***********************************
  Libraries
*/

276
277
278
279
280
281
282
283
// include supporting libraries
#ifdef MY_USE_UDP
    #include <WiFiUdp.h>
#endif
#ifdef MY_GATEWAY_ESP8266
  #include <ESP8266WiFi.h>
#endif

DV's avatar
DV committed
284
285
// include MySensors libraries
#include <core/MySensorsCore.h>
286
#include <core/MyCapabilities.h>
DV's avatar
DV committed
287
288
289
290
291
292
293
294
295
296
297
298
299

// 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
300
301
302
303
304
305
306
307
#if MODULE_BH1750 == 1
  #include <BH1750.h>
  #include <Wire.h>
#endif
#if MODULE_MLX90614 == 1
  #include <Wire.h>
  #include <Adafruit_MLX90614.h>
#endif
308
309
310
311
312
313
#if MODULE_BME280 == 1
  #include <Wire.h>
  #include <SPI.h>
  #include <Adafruit_Sensor.h>
  #include <Adafruit_BME280.h>
#endif
314
315
316
317
318
319
320
321
322
323
#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
324
325
326
327
#if MODULE_MCP9808 == 1
  #include <Wire.h>
  #include "Adafruit_MCP9808.h"
#endif
328
329
330
#if MODULE_MHZ19 == 1
  #include <SoftwareSerial.h>
#endif
DV's avatar
DV committed
331

332
/*******************************************************************
DV's avatar
DV committed
333
334
   Classes
*/
335
class NodeManager;
DV's avatar
DV committed
336
337
338
339
340
341
342
343
344

/*
   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
345
    void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
346
    // turns the power pins on
DV's avatar
DV committed
347
    void powerOn();
348
    // turns the power pins on
DV's avatar
DV committed
349
350
351
352
353
354
355
    void powerOff();
  private:
    int _vcc_pin = -1;
    int _ground_pin = -1;
    long _wait = 0;
};

356
357
358
/*
   Timer
*/
DV's avatar
DV committed
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
class Timer {
  public:
    Timer(NodeManager* node_manager);
    // start the timer which will be over when interval passes by. Unit can be either CYCLES or MINUTES
    void start(long target, int unit);
    void start();
    // stop the timer
    void stop();
    // set the timer configuration but do not start it
    void set(long target, int unit);
    // update the timer. To be called at every cycle
    void update();
    // returns true if the time is over
    bool isOver();
    // return true if the timer is running
    bool isRunning();
    // returns true if the timer has been configured
    bool isConfigured();
    // reset the timer and start over
    void restart();
    // return the current elapsed time
    float getElapsed();
    // return the configured unit
    int getUnit();
    // return the configured target
    int getTarget();
   private:
    NodeManager* _node_manager;
    long _target = 0;
    int _unit = 0;
    float _elapsed = 0;
    bool _use_millis = false;
    long _last_millis = 0;
    float _sleep_time = 0;
    bool _is_running = false;
    bool _is_configured = false;
};
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

/*
   Request
*/

class Request {
  public:
    Request(const char* string);
    // return the parsed function
    int getFunction();
    // return the value as an int
    int getValueInt();
    // return the value as a float
    float getValueFloat();
    // return the value as a string
    char* getValueString();
   private:
    NodeManager* _node_manager;
    int _function;
    char* _value;
};

DV's avatar
DV committed
419
420
421
422
423
/***************************************
   Sensor: generic sensor class
*/
class Sensor {
  public:
424
    Sensor(NodeManager* node_manager, int child_id, int pin);
425
    // [1] where the sensor is attached to (default: not set)
DV's avatar
DV committed
426
427
    void setPin(int value);
    int getPin();
428
    // [2] child_id of this sensor (default: not set)
DV's avatar
DV committed
429
430
431
432
433
    void setChildId(int value);
    int getChildId();
    // presentation of this sensor (default: S_CUSTOM)
    void setPresentation(int value);
    int getPresentation();
434
    // [3] type of this sensor (default: V_CUSTOM)
DV's avatar
DV committed
435
436
    void setType(int value);
    int getType();
437
    // [4] description of the sensor (default: '')
user2684's avatar
user2684 committed
438
    void setDescription(char *value);
439
    // [5] For some sensors, the measurement can be queried multiple times and an average is returned (default: 1)
DV's avatar
DV committed
440
    void setSamples(int value);
441
    // [6] If more then one sample has to be taken, set the interval in milliseconds between measurements (default: 0)
DV's avatar
DV committed
442
    void setSamplesInterval(int value);
443
    // [7] if true will report the measure only if different than the previous one (default: false)
444
    void setTrackLastValue(bool value);
445
    // [8] if track last value is enabled, force to send an update after the configured number of cycles (default: -1)
DV's avatar
DV committed
446
    void setForceUpdate(int value);
447
    void setForceUpdateCycles(int value);
448
    // [9] if track last value is enabled, force to send an update after the configured number of minutes (default: -1)
449
    void setForceUpdateMinutes(int value);
450
    // [10] the value type of this sensor (default: TYPE_INTEGER)
DV's avatar
DV committed
451
    void setValueType(int value);
452
    int getValueType();
453
454
    // [11] for float values, set the float precision (default: 2)
    void  setFloatPrecision(int value);
DV's avatar
DV committed
455
456
    #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
457
      void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
458
      // [12] if enabled the pins will be automatically powered on while awake and off during sleeping (default: true)
DV's avatar
DV committed
459
      void setAutoPowerPins(bool value);
460
      // [13] manually turn the power on
DV's avatar
DV committed
461
      void powerOn();
462
      // [14] manually turn the power off
DV's avatar
DV committed
463
464
      void powerOff();
    #endif
465
466
467
468
    // get the latest recorded value from the sensor
    int getValueInt();
    float getValueFloat();
    char* getValueString();
469
    // [15] After how many cycles the sensor will report back its measure (default: 1 cycle)
470
    void setReportIntervalCycles(int value);
471
    // [16] After how many minutes the sensor will report back its measure (default: 1 cycle)
472
    void setReportIntervalMinutes(int value);
473
474
    // process a remote request
    void process(Request & request);
475
476
    // return the pin the interrupt is attached to
    int getInterruptPin();
DV's avatar
DV committed
477
478
479
    // define what to do at each stage of the sketch
    virtual void before();
    virtual void presentation();
user2684's avatar
user2684 committed
480
    virtual void setup();
DV's avatar
DV committed
481
482
483
484
    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
485
    virtual void onSetup() = 0;
DV's avatar
DV committed
486
487
    virtual void onLoop() = 0;
    virtual void onReceive(const MyMessage & message) = 0;
488
    virtual void onProcess(Request & request) = 0;
DV's avatar
DV committed
489
490
  protected:
    MyMessage _msg;
491
    MyMessage _msg_service;
492
    NodeManager* _node_manager;
DV's avatar
DV committed
493
494
495
496
    int _pin = -1;
    int _child_id;
    int _presentation = S_CUSTOM;
    int _type = V_CUSTOM;
user2684's avatar
user2684 committed
497
    char* _description = "";
DV's avatar
DV committed
498
499
500
501
502
503
504
505
506
507
508
    int _samples = 1;
    int _samples_interval = 0;
    bool _track_last_value = false;
    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 = "";
509
510
511
512
513
    int _interrupt_pin = -1;
    #if POWER_MANAGER  == 1
      PowerManager _powerManager;
      bool _auto_power_pins = true;
    #endif
514
515
    Timer* _report_timer;
    Timer* _force_update_timer;
516
    void _send(MyMessage & msg);
517
518
    bool _isReceive(const MyMessage & message);
    bool _isWorthSending(bool comparison);
DV's avatar
DV committed
519
520
};

521
#if MODULE_ANALOG_INPUT == 1
DV's avatar
DV committed
522
523
524
525
526
/*
   SensorAnalogInput: read the analog input of a configured pin
*/
class SensorAnalogInput: public Sensor {
  public:
527
    SensorAnalogInput(NodeManager* node_manager, int child_id, int pin);
528
    // [101] the analog reference to use (default: not set, can be either INTERNAL or DEFAULT)
DV's avatar
DV committed
529
    void setReference(int value);
530
    // [102] reverse the value or the percentage (e.g. 70% -> 30%) (default: false)
DV's avatar
DV committed
531
    void setReverse(bool value);
532
    // [103] when true returns the value as a percentage (default: true)
DV's avatar
DV committed
533
    void setOutputPercentage(bool value);
534
    // [104] minimum value for calculating the percentage (default: 0)
DV's avatar
DV committed
535
    void setRangeMin(int value);
536
    // [105] maximum value for calculating the percentage (default: 1024)
DV's avatar
DV committed
537
538
539
    void setRangeMax(int value);
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
540
    void onSetup();
DV's avatar
DV committed
541
542
    void onLoop();
    void onReceive(const MyMessage & message);
543
    void onProcess(Request & request);
DV's avatar
DV committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  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:
559
    SensorLDR(NodeManager* node_manager, int child_id, int pin);
DV's avatar
DV committed
560
561
562
563
564
565
566
};

/*
   SensorThermistor: read the temperature from a thermistor
*/
class SensorThermistor: public Sensor {
  public:
567
    SensorThermistor(NodeManager* node_manager, int child_id, int pin);
568
    // [101] resistance at 25 degrees C (default: 10000)
569
    void setNominalResistor(long value);
570
    // [102] temperature for nominal resistance (default: 25)
DV's avatar
DV committed
571
    void setNominalTemperature(int value);
572
    // [103] The beta coefficient of the thermistor (default: 3950)
DV's avatar
DV committed
573
    void setBCoefficient(int value);
574
    // [104] the value of the resistor in series with the thermistor (default: 10000)
575
    void setSeriesResistor(long value);
576
    // [105] set a temperature offset
DV's avatar
DV committed
577
578
579
    void setOffset(float value);
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
580
    void onSetup();
DV's avatar
DV committed
581
582
    void onLoop();
    void onReceive(const MyMessage & message);
583
    void onProcess(Request & request);
DV's avatar
DV committed
584
  protected:
585
    long _nominal_resistor = 10000;
DV's avatar
DV committed
586
587
    int _nominal_temperature = 25;
    int _b_coefficient = 3950;
588
    long _series_resistor = 10000;
DV's avatar
DV committed
589
590
591
    float _offset = 0;
};

592
593
/*
    SensorML8511
594
*/
595
596
597

class SensorML8511: public Sensor {
  public:
598
    SensorML8511(NodeManager* node_manager, int child_id, int pin);
599
600
601
602
603
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
604
    void onProcess(Request & request);
605
606
607
  protected:
    float _mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
};
608
609
610
611
612
613
614

/*
    SensorACS712
*/

class SensorACS712: public Sensor {
  public:
615
    SensorACS712(NodeManager* node_manager, int child_id, int pin);
616
    // [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);
617
    void setmVPerAmp(int value);
618
    // [102] set ACS offset (default: 2500);
619
620
621
622
623
624
    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);
625
    void onProcess(Request & request);
626
627
628
629
  protected:
    int _ACS_offset = 2500;
    int _mv_per_amp = 185;
};
630
631
632
633
634
635
636

/*
    SensorRainGauge
*/

class SensorRainGauge: public Sensor {
  public:
637
    SensorRainGauge(NodeManager* node_manager, int child_id, int pin);
638
    // [101] set how frequently to report back to the controller in minutes. After reporting the measure is resetted (default: 60)
639
    void setReportInterval(int value);
640
    // [102] set how many mm of rain to count for each tip (default: 0.11)
641
642
643
644
645
646
    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);
647
    void onProcess(Request & request);
648
649
650
651
652
  public:
    static void _onTipped();
    static long _last_tip;
    static long _count;
  protected:
653
    int _report_interval = 60;
654
    float _single_tip = 0.11;
655
    Timer* _timer;
656
};
657

658
659
660
661
662
/*
   SensorRain
*/
class SensorRain: public SensorAnalogInput {
  public:
663
    SensorRain(NodeManager* node_manager, int child_id, int pin);
664
665
666
667
668
669
670
};

/*
   SensorSoilMoisture
*/
class SensorSoilMoisture: public SensorAnalogInput {
  public:
671
    SensorSoilMoisture(NodeManager* node_manager, int child_id, int pin);
672
};
673
#endif
674

675
676

#if MODULE_DIGITAL_INPUT == 1
DV's avatar
DV committed
677
678
679
680
681
/*
   SensorDigitalInput: read the digital input of the configured pin
*/
class SensorDigitalInput: public Sensor {
  public:
682
    SensorDigitalInput(NodeManager* node_manager, int child_id, int pin);
DV's avatar
DV committed
683
684
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
685
    void onSetup();
DV's avatar
DV committed
686
687
    void onLoop();
    void onReceive(const MyMessage & message);
688
    void onProcess(Request & request);
DV's avatar
DV committed
689
};
690
#endif
DV's avatar
DV committed
691

692
#if MODULE_DIGITAL_OUTPUT == 1
DV's avatar
DV committed
693
694
695
696
697
/*
   SensorDigitalOutput: control a digital output of the configured pin
*/
class SensorDigitalOutput: public Sensor {
  public:
698
    SensorDigitalOutput(NodeManager* node_manager, int child_id, int pin);
699
    // [103] define which value to set to the output when set to on (default: HIGH)
700
    void setOnValue(int value);
701
    // [104] when legacy mode is enabled expect a REQ message to trigger, otherwise the default SET (default: false)
702
    void setLegacyMode(bool value);
703
    // [105] automatically turn the output off after the given number of minutes
704
    void setSafeguard(int value);
705
    // [106] if true the input value becomes a duration in minutes after which the output will be automatically turned off (default: false)
706
    void setInputIsElapsed(bool value);
707
708
    // [107] optionally wait for the given number of milliseconds after changing the status (default: 0)
    void setWaitAfterSet(int value);
709
    // manually switch the output to the provided value
710
    void setStatus(int value);
711
    // get the current state
712
    int getStatus();
DV's avatar
DV committed
713
714
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
715
    void onSetup();
DV's avatar
DV committed
716
717
    void onLoop();
    void onReceive(const MyMessage & message);
718
    void onProcess(Request & request);
DV's avatar
DV committed
719
  protected:
720
    int _on_value = HIGH;
721
    int _status = OFF;
722
    bool _legacy_mode = false;
723
    bool _input_is_elapsed = false;
724
    int _wait_after_set = 0;
725
    Timer* _safeguard_timer;
726
727
728
    void _setupPin(int pin);
    virtual void _setStatus(int value);
    int _getValueToWrite(int value);
DV's avatar
DV committed
729
730
731
732
733
734
735
736
};


/*
   SensorRelay
*/
class SensorRelay: public SensorDigitalOutput {
  public:
737
    SensorRelay(NodeManager* node_manager, int child_id, int pin);
DV's avatar
DV committed
738
739
740
741
742
743
744
};

/*
   SensorLatchingRelay
*/
class SensorLatchingRelay: public SensorRelay {
  public:
745
    SensorLatchingRelay(NodeManager* node_manager, int child_id, int pin);
746
747
748
749
750
751
752
753
754
755
756
757
758
759
    // [201] set the duration of the pulse to send in ms to activate the relay (default: 50)
    void setPulseWidth(int value);
    // [202] set the pin which turns the relay off (default: the pin provided while registering the sensor)
    void setPinOff(int value);
    // [203] set the pin which turns the relay on (default: the pin provided while registering the sensor + 1)
    void setPinOn(int value);
    // define what to do at each stage of the sketch
    void onBefore();
    void onProcess(Request & request);
  protected:
    int _pin_on;
    int _pin_off;
    int _pulse_width = 50;
    void _setStatus(int value);
DV's avatar
DV committed
760
};
761
#endif
DV's avatar
DV committed
762
763
764
765
766
767
768

/*
   SensorDHT
*/
#if MODULE_DHT == 1
class SensorDHT: public Sensor {
  public:
769
    SensorDHT(NodeManager* node_manager, int child_id, int pin, DHT* dht, int sensor_type, int dht_type);
DV's avatar
DV committed
770
771
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
772
    void onSetup();
DV's avatar
DV committed
773
774
    void onLoop();
    void onReceive(const MyMessage & message);
775
    void onProcess(Request & request);
776
777
778
    // constants
    const static int TEMPERATURE = 0;
    const static int HUMIDITY = 1;
DV's avatar
DV committed
779
780
781
782
783
784
785
786
787
  protected:
    DHT* _dht;
    int _dht_type = DHT11;
    float _offset = 0;
    int _sensor_type = 0;
};
#endif

/*
DV's avatar
DV committed
788
   SensorSHT21: temperature and humidity sensor
DV's avatar
DV committed
789
790
791
792
*/
#if MODULE_SHT21 == 1
class SensorSHT21: public Sensor {
  public:
793
    SensorSHT21(NodeManager* node_manager, int child_id, int sensor_type);
DV's avatar
DV committed
794
795
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
796
    void onSetup();
DV's avatar
DV committed
797
798
    void onLoop();
    void onReceive(const MyMessage & message);
799
    void onProcess(Request & request);
800
801
802
    // constants
    const static int TEMPERATURE = 0;
    const static int HUMIDITY = 1;
DV's avatar
DV committed
803
804
805
806
  protected:
    float _offset = 0;
    int _sensor_type = 0;
};
DV's avatar
DV committed
807
808
809
810
811
812
813

/*
   SensorHTU21D: temperature and humidity sensor
*/

class SensorHTU21D: public SensorSHT21 {
  public:
814
    SensorHTU21D(NodeManager* node_manager, int child_id, int pin);
DV's avatar
DV committed
815
};
DV's avatar
DV committed
816
817
818
819
820
#endif

/*
 * SensorSwitch
 */
821
#if MODULE_SWITCH == 1
DV's avatar
DV committed
822
823
class SensorSwitch: public Sensor {
  public:
824
    SensorSwitch(NodeManager* node_manager, int child_id, int pin);
825
    // [101] set the interrupt mode. Can be CHANGE, RISING, FALLING (default: CHANGE)
DV's avatar
DV committed
826
    void setMode(int value);
827
    // [102] milliseconds to wait before reading the input (default: 0)
DV's avatar
DV committed
828
    void setDebounce(int value);
829
    // [103] time to wait in milliseconds after a change is detected to allow the signal to be restored to its normal value (default: 0)
DV's avatar
DV committed
830
    void setTriggerTime(int value);
831
    // [104] Set initial value on the interrupt pin (default: HIGH)
832
    void setInitial(int value);
DV's avatar
DV committed
833
834
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
835
    void onSetup();
DV's avatar
DV committed
836
837
    void onLoop();
    void onReceive(const MyMessage & message);
838
    void onProcess(Request & request);
DV's avatar
DV committed
839
840
841
842
  protected:
    int _debounce = 0;
    int _trigger_time = 0;
    int _mode = CHANGE;
843
    int _initial = HIGH;
DV's avatar
DV committed
844
845
846
847
848
849
850
};

/*
 * SensorDoor
 */
class SensorDoor: public SensorSwitch {
  public:
851
    SensorDoor(NodeManager* node_manager, int child_id, int pin);
DV's avatar
DV committed
852
853
854
855
856
857
858
};

/*
 * SensorMotion
 */
class SensorMotion: public SensorSwitch {
  public:
859
    SensorMotion(NodeManager* node_manager, int child_id, int pin);
DV's avatar
DV committed
860
};
861
#endif
DV's avatar
DV committed
862
863
864
865
866
867
/*
   SensorDs18b20
*/
#if MODULE_DS18B20 == 1
class SensorDs18b20: public Sensor {
  public:
868
    SensorDs18b20(NodeManager* node_manager, int child_id, int pin, DallasTemperature* sensors, int index);
user2684's avatar
user2684 committed
869
870
    // returns the sensor's resolution in bits
    int getResolution();
871
    // [101] set the sensor's resolution in bits
user2684's avatar
user2684 committed
872
    void setResolution(int value);
873
    // [102] sleep while DS18B20 calculates temperature (default: false)
874
    void setSleepDuringConversion(bool value);
875
876
    // return the sensors' device address
    DeviceAddress* getDeviceAddress();
877
878
879
880
881
    // define what to do at each stage of the sketch
    void onBefore();
    void onSetup();
    void onLoop();
    void onReceive(const MyMessage & message);
882
    void onProcess(Request & request);
DV's avatar
DV committed
883
884
885
  protected:
    float _offset = 0;
    int _index;
886
    bool _sleep_during_conversion = false;
DV's avatar
DV committed
887
    DallasTemperature* _sensors;
user2684's avatar
user2684 committed
888
    DeviceAddress _device_address;
DV's avatar
DV committed
889
890
891
};
#endif

DV's avatar
DV committed
892
893
894
895
896
897
/*
   SensorBH1750
*/
#if MODULE_BH1750 == 1
class SensorBH1750: public Sensor {
  public:
898
    SensorBH1750(NodeManager* node_manager, int child_id);
DV's avatar
DV committed
899
900
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
901
    void onSetup();
DV's avatar
DV committed
902
903
    void onLoop();
    void onReceive(const MyMessage & message);
904
    void onProcess(Request & request);
DV's avatar
DV committed
905
906
907
908
909
910
911
912
913
914
915
  protected:
    BH1750* _lightSensor;
};
#endif

/*
   SensorMLX90614
*/
#if MODULE_MLX90614 == 1
class SensorMLX90614: public Sensor {
  public:
916
    SensorMLX90614(NodeManager* node_manager, int child_id, Adafruit_MLX90614* mlx, int sensor_type);
DV's avatar
DV committed
917
918
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
919
    void onSetup();
DV's avatar
DV committed
920
921
    void onLoop();
    void onReceive(const MyMessage & message);
922
    void onProcess(Request & request);
923
924
925
    // constants
    const static int TEMPERATURE_AMBIENT = 0;
    const static int TEMPERATURE_OBJECT = 1;
DV's avatar
DV committed
926
927
928
929
930
  protected:
    Adafruit_MLX90614* _mlx;
    int _sensor_type;
};
#endif
DV's avatar
DV committed
931

932

933
/*
934
 * SensorBosch
935
*/
936
937
938

#if MODULE_BME280 == 1 || MODULE_BMP085 == 1
class SensorBosch: public Sensor {
939
  public:
940
    SensorBosch(NodeManager* node_manager, int child_id, int sensor_type);
941
    // [101] define how many pressure samples to keep track of for calculating the forecast (default: 5)
user2684's avatar
user2684 committed
942
    void setForecastSamplesCount(int value);
943
944
    // define what to do at each stage of the sketch
    void onBefore();
user2684's avatar
user2684 committed
945
    void onSetup();
946
947
    void onLoop();
    void onReceive(const MyMessage & message);
948
    void onProcess(Request & request);
949
950
951
952
953
954
    // 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);
955
956
  protected:
    int _sensor_type;
user2684's avatar
user2684 committed
957
958
959
960
961
962
963
964
965
    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();
966
967
968
969
970
971
972
973
974
975
    void _forecast(float pressure);
};
#endif

/*
   SensorBME280
*/
#if MODULE_BME280 == 1
class SensorBME280: public SensorBosch {
  public:
976
    SensorBME280(NodeManager* node_manager, int child_id, Adafruit_BME280* bme, int sensor_type);
977
978
979
980
981
982
983
984
985
986
987
988
    void onLoop();
  protected:
    Adafruit_BME280* _bme;
};
#endif

/*
   SensorBMP085
*/
#if MODULE_BMP085 == 1
class SensorBMP085: public SensorBosch {
  public:
989
    SensorBMP085(NodeManager* node_manager, int child_id, Adafruit_BMP085* bmp, int sensor_type);
990
991
992
    void onLoop();
  protected:
    Adafruit_BMP085* _bmp;
993
994
995
};
#endif

996
997
998
999
1000
/*
   SensorHCSR04
*/
#if MODULE_HCSR04 == 1
class SensorHCSR04: public Sensor {
For faster browsing, not all history is shown. View entire blame