NodeManager.cpp 43.7 KB
Newer Older
DV's avatar
DV committed
1
2
3
4
5
6
/*
 * NodeManager
 */

#include "NodeManager.h"

DV's avatar
DV committed
7
8
9
10
11
12
13
14
15
/*
 * Constants
 */

const char* BATTERY = "BATTERY";
const char* AWAKE = "AWAKE";
const char* REBOOT = "REBOOT";
const char* CLEAR = "CLEAR";
const char* WAKEUP = "WAKEUP";
DV's avatar
DV committed
16
17
18
19
20
21

/***************************************
   PowerManager
*/

// set the vcc and ground pin the sensor is connected to
22
void PowerManager::setPowerPins(int ground_pin, int vcc_pin, long wait) {
DV's avatar
DV committed
23
  #if DEBUG == 1
DV's avatar
DV committed
24
25
26
27
    Serial.print("POWER G=");
    Serial.print(ground_pin);
    Serial.print(" V=");
    Serial.println(vcc_pin);
DV's avatar
DV committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  #endif
  // configure the vcc pin as output and initialize to low (power off)
  _vcc_pin = vcc_pin;
  pinMode(_vcc_pin, OUTPUT);
  digitalWrite(_vcc_pin, LOW);
  // configure the ground pin as output and initialize to low
  _ground_pin = ground_pin;
  pinMode(_ground_pin, OUTPUT);
  digitalWrite(_ground_pin, LOW);
  _wait = wait;
}

// return true if power pins have been configured
bool PowerManager::_hasPowerManager() {
  if (_vcc_pin != -1 && _ground_pin != -1) return true;
  return false;
}

// turn on the sensor by activating its power pins
void PowerManager::powerOn() {
  if (! _hasPowerManager()) return;
  #if DEBUG == 1
DV's avatar
DV committed
50
    Serial.print("ON P=");
DV's avatar
DV committed
51
52
53
54
55
56
57
58
59
60
61
62
    Serial.println(_vcc_pin);
  #endif
  // power on the sensor by turning high the vcc pin
  digitalWrite(_vcc_pin, HIGH);
  // wait a bit for the device to settle down
  if (_wait > 0) sleep(_wait);
}

// turn off the sensor
void PowerManager::powerOff() {
  if (! _hasPowerManager()) return;
  #if DEBUG == 1
DV's avatar
DV committed
63
    Serial.print("OFF P=");
DV's avatar
DV committed
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
    Serial.println(_vcc_pin);
  #endif
  // power off the sensor by turning low the vcc pin
  digitalWrite(_vcc_pin, LOW);
}

/******************************************
    Sensors
*/

/*
   Sensor class
*/
// constructor
Sensor::Sensor(int child_id, int pin) {
  _child_id = child_id;
  _pin = pin;
  _msg = MyMessage(_child_id, _type);
}

// setter/getter
void Sensor::setPin(int value) {
  _pin = value;
}
int Sensor::getPin() {
  return _pin;
}
void Sensor::setChildId(int value) {
  _child_id = value;
}
int Sensor::getChildId() {
  return _child_id;
}
void Sensor::setPresentation(int value) {
  _presentation = value;
}
int Sensor::getPresentation() {
  return _presentation;
}
void Sensor::setType(int value) {
  _type = value;
  _msg.setType(_type);
}
int Sensor::getType() {
  return _type;
}
void Sensor::setRetries(int value) {
  _retries = value;
}
void Sensor::setSamples(int value) {
  _samples = value;
}
void Sensor::setSamplesInterval(int value) {
  _samples_interval = value;
}
void Sensor::setTackLastValue(bool value) {
  _track_last_value = value;
}
void Sensor::setForceUpdate(int value) {
  _force_update = value;
}
void Sensor::setValueType(int value) {
  _value_type = value;
}
void Sensor::setFloatPrecision(int value) {
  _float_precision = value;
}
#if POWER_MANAGER == 1
132
    void Sensor::setPowerPins(int ground_pin, int vcc_pin, long wait) {
DV's avatar
DV committed
133
134
      _powerManager.setPowerPins(ground_pin, vcc_pin, wait);
    }
DV's avatar
DV committed
135
136
137
    void Sensor::setAutoPowerPins(bool value) {
      _auto_power_pins = value;
    }
DV's avatar
DV committed
138
139
140
141
142
143
144
    void Sensor::powerOn() {
      _powerManager.powerOn();
    }
    void Sensor::powerOff() {
      _powerManager.powerOff();
    }
#endif
DV's avatar
DV committed
145
146
147
void Sensor::setSleepBetweenSend(int value) {
  _sleep_between_send = value;
}
DV's avatar
DV committed
148
149
150
151

// present the sensor to the gateway and controller
void Sensor::presentation() {
  #if DEBUG == 1
DV's avatar
DV committed
152
    Serial.print("PRES I=");
DV's avatar
DV committed
153
    Serial.print(_child_id);
DV's avatar
DV committed
154
    Serial.print(" T=");
DV's avatar
DV committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    Serial.println(_presentation);
  #endif
  present(_child_id, _presentation);
}

// call the sensor-specific implementation of before
void Sensor::before() {
  if (_pin == -1) return;
  onBefore();
}

// call the sensor-specific implementation of loop
void Sensor::loop(const MyMessage & message) {
  if (_pin == -1) return;
  #if POWER_MANAGER == 1
    // turn the sensor on
DV's avatar
DV committed
171
    if (_auto_power_pins) powerOn();
DV's avatar
DV committed
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
  #endif
  // for numeric sensor requiring multiple samples, keep track of the total
  float total = 0;
  // keep track of the number of cycles since the last update
  _cycles++;
  // collect multiple samples if needed
  for (int i = 0; i < _samples; i++) {
    // call the sensor-specific implementation of the main task which will store the result in the _value variable
    if (message.sender == 0 && message.sensor == 0 && message.getCommand() == 0 && message.type == 0) {
      // empty message, we'be been called from loop()
      onLoop();
    }
    else {
      // we've been called from receive(), pass the message along
      onReceive(message);
    }
    // for integers and floats, keep track of the total
    if (_value_type == TYPE_INTEGER) total += (float)_value_int;
    else if (_value_type == TYPE_FLOAT) total += _value_float;
    // wait between samples
    if (_samples_interval > 0) sleep(_samples_interval);
  }
  // process the result and send a response back. 
  if (_value_type == TYPE_INTEGER && total > -1) {
    // if the value is an integer, calculate the average value of the samples
    int avg = (int) (total / _samples);
    // if track last value is disabled or if enabled and the current value is different then the old value, send it back
    if (! _track_last_value || (_track_last_value && avg != _last_value_int) || (_track_last_value && _force_update > 0 && _cycles > _force_update)) {
      _cycles = 0;
      _last_value_int = avg;
      _send(_msg.set(avg));
    }
  }
  // process a float value
  else if (_value_type == TYPE_FLOAT && total > -1) {
    // calculate the average value of the samples
    float avg = total / _samples;
    // if track last value is disabled or if enabled and the current value is different then the old value, send it back
    if (! _track_last_value || (_track_last_value && avg != _last_value_float) || (_track_last_value && _cycles >= _force_update)) {
      _cycles = 0;
      _last_value_float = avg;
      _send(_msg.set(avg, _float_precision));
    }
  }
  // process a string value
  else if (_value_type == TYPE_STRING) {
    // if track last value is disabled or if enabled and the current value is different then the old value, send it back
    if (! _track_last_value || (_track_last_value && strcmp(_value_string, _last_value_string) != 0) || (_track_last_value && _cycles >= _force_update)) {
      _cycles = 0;
      _last_value_string = _value_string;
      _send(_msg.set(_value_string));
    }
  }
  // turn the sensor off
  #if POWER_MANAGER == 1
DV's avatar
DV committed
227
    if (_auto_power_pins) powerOff();
DV's avatar
DV committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  #endif
}

// receive a message from the radio network
void Sensor::receive(const MyMessage &message) {
  // return if not for this sensor
  if (message.sensor != _child_id || message.type != _type) return;
  // a request would make the sensor executing its main task
  loop(message);
}

// send a message to the network
void Sensor::_send(MyMessage & message) {
  // send the message, multiple times if requested
  for (int i = 0; i < _retries; i++) {
DV's avatar
DV committed
243
244
    // if configured, sleep beetween each send
    if (_sleep_between_send > 0) sleep(_sleep_between_send);
DV's avatar
DV committed
245
    #if DEBUG == 1
DV's avatar
DV committed
246
      Serial.print("SEND D=");
DV's avatar
DV committed
247
      Serial.print(message.destination);
DV's avatar
DV committed
248
      Serial.print(" I=");
DV's avatar
DV committed
249
      Serial.print(message.sensor);
DV's avatar
DV committed
250
      Serial.print(" C=");
DV's avatar
DV committed
251
      Serial.print(message.getCommand());
DV's avatar
DV committed
252
      Serial.print(" T=");
DV's avatar
DV committed
253
      Serial.print(message.type);
DV's avatar
DV committed
254
      Serial.print(" S=");
DV's avatar
DV committed
255
      Serial.print(message.getString());
DV's avatar
DV committed
256
      Serial.print(" N=");
DV's avatar
DV committed
257
      Serial.print(message.getInt());
DV's avatar
DV committed
258
      Serial.print(" F=");
DV's avatar
DV committed
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
      Serial.println(message.getFloat());
    #endif
    send(message);
  }
}

/*
   SensorAnalogInput
*/

// contructor
SensorAnalogInput::SensorAnalogInput(int child_id, int pin): Sensor(child_id, pin) {
}

// setter/getter
void SensorAnalogInput::setReference(int value) {
  _reference = value;
}
void SensorAnalogInput::setReverse(bool value) {
  _reverse = value;
}
void SensorAnalogInput::setOutputPercentage(bool value) {
  _output_percentage = value;
}
void SensorAnalogInput::setRangeMin(int value) {
  _range_min = value;
}
void SensorAnalogInput::setRangeMax(int value) {
  _range_max = value;
}

// what do to during setup
void SensorAnalogInput::onBefore() {
  // prepare the pin for input
  pinMode(_pin, INPUT);
}

// what do to during loop
void SensorAnalogInput::onLoop() {
  // read the input
  int adc = _getAnalogRead();
  // calculate the percentage
  int percentage = 0;
  if (_output_percentage) percentage = _getPercentage(adc);
  #if DEBUG == 1
DV's avatar
DV committed
304
    Serial.print("A-IN I=");
DV's avatar
DV committed
305
    Serial.print(_child_id);
DV's avatar
DV committed
306
    Serial.print(" V=");
DV's avatar
DV committed
307
    Serial.print(adc);
DV's avatar
DV committed
308
    Serial.print(" %=");
DV's avatar
DV committed
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
    Serial.println(percentage);
  #endif
  // store the result
  _value_int = _output_percentage ? percentage : adc;
}

// what do to during loop
void SensorAnalogInput::onReceive(const MyMessage & message) {
  onLoop();
}

// read the analog input
int SensorAnalogInput::_getAnalogRead() {
  // set the reference
  if (_reference != -1) {
    analogReference(_reference);
    sleep(100);
  }
  // read and return the value
  int value = analogRead(_pin);
  if (_reverse) value = _range_max - value;
  return value;
}

// return a percentage from an analog value
int SensorAnalogInput::_getPercentage(int adc) {
  float value = (float)adc;
DV's avatar
DV committed
336
337
  // restore the original value
  if (_reverse) value = 1024 - value;
DV's avatar
DV committed
338
339
  // scale the percentage based on the range provided
  float percentage = ((value - _range_min) / (_range_max - _range_min)) * 100;
DV's avatar
DV committed
340
  if (_reverse) percentage = 100 - percentage;
DV's avatar
DV committed
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
  if (percentage > 100) percentage = 100;
  if (percentage < 0) percentage = 0;
  return (int)percentage;
}

/*
   SensorLDR
*/

// contructor
SensorLDR::SensorLDR(int child_id, int pin): SensorAnalogInput(child_id, pin) {
  // set presentation and type and reverse (0: no light, 100: max light)
  setPresentation(S_LIGHT_LEVEL);
  setType(V_LIGHT_LEVEL);
  setReverse(true);
}

/*
   SensorThermistor
*/

// contructor
SensorThermistor::SensorThermistor(int child_id, int pin): Sensor(child_id, pin) {
  // set presentation, type and value type
  setPresentation(S_TEMP);
  setType(V_TEMP);
  setValueType(TYPE_FLOAT);
}

// setter/getter
void SensorThermistor::setNominalResistor(int value) {
  _nominal_resistor = value;
}
void SensorThermistor::setNominalTemperature(int value) {
  _nominal_temperature = value;
}
void SensorThermistor::setBCoefficient(int value) {
  _b_coefficient = value;
}
void SensorThermistor::setSeriesResistor(int value) {
  _series_resistor = value;
}
void SensorThermistor::setOffset(float value) {
  _offset = value;
}

// what do to during setup
void SensorThermistor::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

// what do to during loop
void SensorThermistor::onLoop() {
  // read the voltage across the thermistor
  float adc = analogRead(_pin);
  // calculate the temperature
  float reading = (1023 / adc)  - 1;
  reading = _series_resistor / reading;
  float temperature;
  temperature = reading / _nominal_resistor;     // (R/Ro)
  temperature = log(temperature);                  // ln(R/Ro)
  temperature /= _b_coefficient;                   // 1/B * ln(R/Ro)
  temperature += 1.0 / (_nominal_temperature + 273.15); // + (1/To)
  temperature = 1.0 / temperature;                 // Invert
  temperature -= 273.15;                         // convert to C
  if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
  #if DEBUG == 1
DV's avatar
DV committed
409
    Serial.print("THER I=");
DV's avatar
DV committed
410
    Serial.print(_child_id);
DV's avatar
DV committed
411
    Serial.print(" V=");
DV's avatar
DV committed
412
    Serial.print(adc);
DV's avatar
DV committed
413
    Serial.print(" T=");
DV's avatar
DV committed
414
    Serial.println(temperature);
DV's avatar
DV committed
415
    Serial.print(" M=");
DV's avatar
DV committed
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
    Serial.println(getControllerConfig().isMetric);
  #endif
  // store the value
  _value_float = temperature;
}

// what do to as the main task when receiving a message
void SensorThermistor::onReceive(const MyMessage & message) {
  onLoop();
}

/*
   SensorDigitalInput
*/

// contructor
SensorDigitalInput::SensorDigitalInput(int child_id, int pin): Sensor(child_id, pin) {
}

// what do to during setup
void SensorDigitalInput::onBefore() {
  // set the pin for input
  pinMode(_pin, INPUT);
}

// what do to during loop
void SensorDigitalInput::onLoop() {
  // read the value
  int value = digitalRead(_pin);
  #if DEBUG == 1
DV's avatar
DV committed
446
    Serial.print("D-IN I=");
DV's avatar
DV committed
447
    Serial.print(_child_id);
DV's avatar
DV committed
448
    Serial.print(" P=");
DV's avatar
DV committed
449
    Serial.print(_pin);
DV's avatar
DV committed
450
    Serial.print(" V=");
DV's avatar
DV committed
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
    Serial.println(value);
  #endif
  // store the value
  _value_int = value;
}

// what do to as the main task when receiving a message
void SensorDigitalInput::onReceive(const MyMessage & message) {
  onLoop();
}


/*
   SensorDigitalOutput
*/

// contructor
SensorDigitalOutput::SensorDigitalOutput(int child_id, int pin): Sensor(child_id, pin) {
}

void SensorDigitalOutput::onBefore() {
  // set the pin as output and initialize it accordingly
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, _initial_value == LOW ? LOW : HIGH);
  // the initial value is now the current value
  _value_int = _initial_value;
}

// setter/getter
void SensorDigitalOutput::setInitialValue(int value) {
  _initial_value = value;
}
void SensorDigitalOutput::setPulseWidth(int value) {
  _pulse_width = value;
}

// main task
void SensorDigitalOutput::onLoop() {
  // do nothing on loop
}

// what do to as the main task when receiving a message
void SensorDigitalOutput::onReceive(const MyMessage & message) {
  // retrieve from the message the value to set
  int value = message.getInt();
  if (value != 0 && value != 1) return;
  #if DEBUG == 1
DV's avatar
DV committed
498
    Serial.print("DOUT I=");
DV's avatar
DV committed
499
    Serial.print(_child_id);
DV's avatar
DV committed
500
    Serial.print(" P=");
DV's avatar
DV committed
501
    Serial.print(_pin);
DV's avatar
DV committed
502
    Serial.print(" S=");
DV's avatar
DV committed
503
    Serial.print(_initial_value);
DV's avatar
DV committed
504
    Serial.print(" V=");
DV's avatar
DV committed
505
    Serial.print(value);
DV's avatar
DV committed
506
    Serial.print(" P=");
DV's avatar
DV committed
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
    Serial.println(_pulse_width);
  #endif
  // set the value
  digitalWrite(_pin, value);
  if (_pulse_width > 0) {
    // if this is a pulse output, restore the value to the original value after the pulse
    sleep(_pulse_width);
    digitalWrite(_pin, value == 0 ? HIGH: LOW);
  }
  // store the current value
  _value_int = value;
}

/*
   SensorRelay
*/

// contructor
SensorRelay::SensorRelay(int child_id, int pin): SensorDigitalOutput(child_id, pin) {
  // set presentation and type
  setPresentation(S_BINARY);
  setType(V_STATUS);
}

DV's avatar
DV committed
531
532
533
534
535
536
// define what to do during loop
void SensorRelay::onLoop() {
    // set the value to -1 so to avoid reporting to the gateway during loop
    _value_int = -1;
}

DV's avatar
DV committed
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
/*
   SensorLatchingRelay
*/

// contructor
SensorLatchingRelay::SensorLatchingRelay(int child_id, int pin): SensorRelay(child_id, pin) {
  // like a sensor with a default pulse set
  setPulseWidth(50);
}

/*
   SensorDHT
*/
#if MODULE_DHT == 1
// contructor
SensorDHT::SensorDHT(int child_id, int pin, DHT* dht, int sensor_type, int dht_type): Sensor(child_id, pin) {
  // store the dht object
  _dht = dht;
  // store the sensor type (0: temperature, 1: humidity)
  _sensor_type = sensor_type;
  _dht_type = dht_type;
  if (_sensor_type == 0) {
    // temperature sensor
    setPresentation(S_TEMP);
    setType(V_TEMP);
    setValueType(TYPE_FLOAT);
  }
  else if (_sensor_type == 1) {
    // humidity sensor
    setPresentation(S_HUM);
    setType(V_HUM);
    setValueType(TYPE_FLOAT);
  }
}

// what do to during setup
void SensorDHT::onBefore() {
    // initialize the dht library
    _dht->begin();
}

// what do to during loop
void SensorDHT::onLoop() {
  // temperature sensor
  if (_sensor_type == 0) {
    // read the temperature
    float temperature = _dht->readTemperature();
    // convert it
    if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
    #if DEBUG == 1
DV's avatar
DV committed
587
      Serial.print("DHT I=");
DV's avatar
DV committed
588
      Serial.print(_child_id);
DV's avatar
DV committed
589
      Serial.print(" T=");
DV's avatar
DV committed
590
591
592
593
594
595
596
597
598
599
600
      Serial.println(temperature);
    #endif
    // store the value
    if (! isnan(temperature)) _value_float = temperature;
  }
  // humidity sensor
  else if (_sensor_type == 1) {
    // read humidity
    float humidity = _dht->readHumidity();
    if (isnan(humidity)) return;
    #if DEBUG == 1
DV's avatar
DV committed
601
      Serial.print("DHT I=");
DV's avatar
DV committed
602
      Serial.print(_child_id);
DV's avatar
DV committed
603
      Serial.print(" H=");
DV's avatar
DV committed
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
      Serial.println(humidity);
    #endif
    // store the value
    if (! isnan(humidity)) _value_float = humidity;
  }
}

// what do to as the main task when receiving a message
void SensorDHT::onReceive(const MyMessage & message) {
  onLoop();
}
#endif

/*
   SensorSHT21
*/
#if MODULE_SHT21 == 1
// contructor
DV's avatar
DV committed
622
SensorSHT21::SensorSHT21(int child_id, int sensor_type): Sensor(child_id,A2) {
DV's avatar
DV committed
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
  // store the sensor type (0: temperature, 1: humidity)
  _sensor_type = sensor_type;
  if (_sensor_type == 0) {
    // temperature sensor
    setPresentation(S_TEMP);
    setType(V_TEMP);
    setValueType(TYPE_FLOAT);
  }
  else if (_sensor_type == 1) {
    // humidity sensor
    setPresentation(S_HUM);
    setType(V_HUM);
    setValueType(TYPE_FLOAT);
  }
}

// what do to during setup
void SensorSHT21::onBefore() {
  // initialize the library
  Wire.begin();
}

// what do to during loop
void SensorSHT21::onLoop() {
  // temperature sensor
  if (_sensor_type == 0) {
    // read the temperature
    float temperature = SHT2x.GetTemperature();
    // convert it
    if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
    #if DEBUG == 1
DV's avatar
DV committed
654
      Serial.print("SHT I=");
DV's avatar
DV committed
655
      Serial.print(_child_id);
DV's avatar
DV committed
656
      Serial.print(" T=");
DV's avatar
DV committed
657
658
659
660
661
662
663
664
665
666
667
      Serial.println(temperature);
    #endif
    // store the value
    if (! isnan(temperature)) _value_float = temperature;
  }
  // Humidity Sensor
  else if (_sensor_type == 1) {
    // read humidity
    float humidity = SHT2x.GetHumidity();
    if (isnan(humidity)) return;
    #if DEBUG == 1
DV's avatar
DV committed
668
      Serial.print("SHT I=");
DV's avatar
DV committed
669
      Serial.print(_child_id);
DV's avatar
DV committed
670
      Serial.print(" H=");
DV's avatar
DV committed
671
672
673
674
675
676
677
678
679
680
681
682
683
      Serial.println(humidity);
    #endif
    // store the value
    if (! isnan(humidity)) _value_float = humidity;
  }
}

// what do to as the main task when receiving a message
void SensorSHT21::onReceive(const MyMessage & message) {
  onLoop();
}
#endif

DV's avatar
DV committed
684
685
686
687
688
689
690
691
692
/*
 * SensorHTU21D
 */
 #if MODULE_SHT21 == 1
// constructor
SensorHTU21D::SensorHTU21D(int child_id, int pin): SensorSHT21(child_id, pin) {
}
#endif 

DV's avatar
DV committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
 * SensorSwitch
 */
SensorSwitch::SensorSwitch(int child_id, int pin): Sensor(child_id,pin) {
  setType(V_TRIPPED);
}

// setter/getter
void SensorSwitch::setMode(int value) {
  _mode = value;
}
int SensorSwitch::getMode() {
  return _mode;
}
void SensorSwitch::setDebounce(int value) {
  _debounce = value;
}
void SensorSwitch::setTriggerTime(int value) {
  _trigger_time = value;
}
713
714
715
716
717
718
void SensorSwitch::setInitial(int value) {
  _initial = value;
}
int SensorSwitch::getInitial() {
  return _initial;
}
DV's avatar
DV committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735

// what do to during setup
void SensorSwitch::onBefore() {
  // initialize the value
  if (_mode == RISING) _value_int = LOW;
  else if (_mode == FALLING) _value_int = HIGH;
}

// what do to during loop
void SensorSwitch::onLoop() {
  // wait to ensure the the input is not floating
  if (_debounce > 0) sleep(_debounce);
  // read the value of the pin
  int value = digitalRead(_pin);
  // process the value
  if ( (_mode == RISING && value == HIGH ) || (_mode == FALLING && value == LOW) || (_mode == CHANGE) )  {
    #if DEBUG == 1
DV's avatar
DV committed
736
      Serial.print("SWITCH I=");
DV's avatar
DV committed
737
      Serial.print(_child_id);
DV's avatar
DV committed
738
      Serial.print(" P=");
DV's avatar
DV committed
739
      Serial.print(_pin);
DV's avatar
DV committed
740
      Serial.print(" V=");
DV's avatar
DV committed
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
      Serial.println(value);
    #endif
    _value_int = value;
    // allow the signal to be restored to its normal value
    if (_trigger_time > 0) sleep(_trigger_time);
  } else {
    // invalid
    _value_int = -1;
  }
}
// what do to as the main task when receiving a message
void SensorSwitch::onReceive(const MyMessage & message) {
  onLoop();
}

/*
 * SensorDoor
 */
SensorDoor::SensorDoor(int child_id, int pin): SensorSwitch(child_id,pin) {
  setPresentation(S_DOOR);
}

/*
 * SensorMotion
 */
SensorMotion::SensorMotion(int child_id, int pin): SensorSwitch(child_id,pin) {
  setPresentation(S_MOTION);
  // capture only when it triggers
  setMode(RISING);
770
771
  // set initial value to LOW
  setInitial(LOW);
DV's avatar
DV committed
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
}

/*
   SensorDs18b20
*/
#if MODULE_DS18B20 == 1
// contructor
SensorDs18b20::SensorDs18b20(int child_id, int pin, DallasTemperature* sensors, int index): Sensor(child_id, pin) {
  setPresentation(S_TEMP);
  setType(V_TEMP);
  setValueType(TYPE_FLOAT);
  _index = index;
  _sensors = sensors;
}

// what do to during setup
void SensorDs18b20::onBefore() {
}

// what do to during loop
void SensorDs18b20::onLoop() {
  // request the temperature
  _sensors->requestTemperaturesByIndex(_index);
  // read the temperature
  float temperature = _sensors->getTempCByIndex(_index);
  // convert it
  if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
  #if DEBUG == 1
DV's avatar
DV committed
800
    Serial.print("DS18 I=");
DV's avatar
DV committed
801
    Serial.print(_child_id);
DV's avatar
DV committed
802
    Serial.print(" T=");
DV's avatar
DV committed
803
804
805
806
807
808
809
810
811
812
    Serial.println(temperature);
  #endif
  // store the value
  _value_float = temperature;
}

// what do to as the main task when receiving a message
void SensorDs18b20::onReceive(const MyMessage & message) {
  onLoop();
}
DV's avatar
DV committed
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
#endif

/*
   SensorBH1750
*/
#if MODULE_BH1750 == 1
// contructor
SensorBH1750::SensorBH1750(int child_id): Sensor(child_id,A4) {
  setPresentation(S_LIGHT_LEVEL);
  setType(V_LEVEL);
  _lightSensor = new BH1750();
}

// what do to during setup
void SensorBH1750::onBefore() {
  _lightSensor->begin();
}

// what do to during loop
void SensorBH1750::onLoop() {
  // request the light level
  _value_int = _lightSensor->readLightLevel();
  #if DEBUG == 1
    Serial.print("BH1 I=");
    Serial.print(_child_id);
    Serial.print(" L=");
    Serial.println(_value_int);
  #endif
}

// what do to as the main task when receiving a message
void SensorBH1750::onReceive(const MyMessage & message) {
  onLoop();
}
#endif

/*
   SensorMLX90614
*/
#if MODULE_MLX90614 == 1
// contructor
SensorMLX90614::SensorMLX90614(int child_id, Adafruit_MLX90614* mlx, int sensor_type): Sensor(child_id,A4) {
  // store the sensor type (0: ambient, 1: object)
  _sensor_type = sensor_type;
  _mlx = mlx;
  // set presentation and type
  setPresentation(S_TEMP);
  setType(V_TEMP);
  setValueType(TYPE_FLOAT);
}
DV's avatar
DV committed
863

DV's avatar
DV committed
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
// what do to during setup
void SensorMLX90614::onBefore() {
  // initialize the library
  _mlx->begin();
}

// what do to during loop
void SensorMLX90614::onLoop() {
  float temperature = _sensor_type == 0 ? _mlx->readAmbientTempC() : _mlx->readObjectTempC();
  // convert it
  if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
  #if DEBUG == 1
    Serial.print("MLX I=");
    Serial.print(_child_id);
    Serial.print(" T=");
    Serial.println(temperature);
  #endif
  if (! isnan(temperature)) _value_float = temperature;
}
DV's avatar
DV committed
883

DV's avatar
DV committed
884
885
886
887
// what do to as the main task when receiving a message
void SensorMLX90614::onReceive(const MyMessage & message) {
  onLoop();
}
DV's avatar
DV committed
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
#endif

/*******************************************
   NodeManager
*/

// initialize the node manager
NodeManager::NodeManager() {
  // setup the service message container
  _msg = MyMessage(CONFIGURATION_CHILD_ID, V_CUSTOM);
}

// setter/getter
void NodeManager::setRebootPin(int value) {
    _reboot_pin = value;
}
DV's avatar
DV committed
904
905
906
void NodeManager::setRetries(int value) {
  _retries = value;
}
DV's avatar
DV committed
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
#if BATTERY_MANAGER == 1
  void NodeManager::setBatteryMin(float value) {
    _battery_min = value;
  }
  void NodeManager::setBatteryMax(float value) {
    _battery_max = value;
  }
  void NodeManager::setBatteryReportCycles(int value) {
    _battery_report_cycles = value;
  }
#endif
#if SLEEP_MANAGER == 1
  void NodeManager::setSleepMode(int value) {
    _sleep_mode = value;
  }
  void NodeManager::setSleepTime(int value) {
    _sleep_time = value;
  }
  void NodeManager::setSleepUnit(int value) {
    _sleep_unit = value;
  }
  void NodeManager::setSleep(int value1, int value2, int value3) {
    _sleep_mode = value1;
DV's avatar
DV committed
930
    _sleep_time = value2;
DV's avatar
DV committed
931
932
933
934
935
936
    _sleep_unit = value3;
  }
  void NodeManager::setSleepInterruptPin(int value) {
    _sleep_interrupt_pin = value;
  }
#endif
937
void NodeManager::setInterrupt(int pin, int mode, int pull) {
DV's avatar
DV committed
938
939
940
941
942
943
944
945
946
947
  if (pin == INTERRUPT_PIN_1) {
    _interrupt_1_mode = mode;
    _interrupt_1_pull = pull;
  }
  if (pin == INTERRUPT_PIN_2) {
    _interrupt_2_mode = mode;
    _interrupt_2_pull = pull;
  }
}
#if POWER_MANAGER == 1
948
  void NodeManager::setPowerPins(int ground_pin, int vcc_pin, long wait) {
DV's avatar
DV committed
949
950
    _powerManager.setPowerPins(ground_pin, vcc_pin, wait);
  }
DV's avatar
DV committed
951
952
953
  void NodeManager::setAutoPowerPins(bool value) {
    _auto_power_pins = value;
  }
DV's avatar
DV committed
954
955
956
957
958
959
960
  void NodeManager::powerOn() {
    _powerManager.powerOn();
  }
  void NodeManager::powerOff() {
    _powerManager.powerOff();
  }
#endif
DV's avatar
DV committed
961
962
963
void NodeManager::setSleepBetweenSend(int value) {
  _sleep_between_send = value;
}
DV's avatar
DV committed
964
965

// register a sensor to this manager
966
int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
DV's avatar
DV committed
967
968
969
970
971
972
973
974
975
976
  #if DEBUG == 1
    if (_startup) {
      Serial.print("NodeManager v");
      Serial.println(VERSION);
      _startup = false;
    }
  #endif
  // get a child_id if not provided by the user
  if (child_id < 0) child_id = _getAvailableChildId();
  // based on the given sensor type instantiate the appropriate class
977
  if (sensor_type == 0) return -1;
DV's avatar
DV committed
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
  #if MODULE_ANALOG_INPUT == 1
    else if (sensor_type == SENSOR_ANALOG_INPUT) return registerSensor(new SensorAnalogInput(child_id, pin));
    else if (sensor_type == SENSOR_LDR) return registerSensor(new SensorLDR(child_id, pin));
    else if (sensor_type == SENSOR_THERMISTOR) return registerSensor(new SensorThermistor(child_id, pin));
  #endif
  #if MODULE_DIGITAL_INPUT == 1
    else if (sensor_type == SENSOR_DIGITAL_INPUT) return registerSensor(new SensorDigitalInput(child_id, pin));
  #endif
  #if MODULE_DIGITAL_OUTPUT == 1
    else if (sensor_type == SENSOR_DIGITAL_OUTPUT) return registerSensor(new SensorDigitalOutput(child_id, pin));
    else if (sensor_type == SENSOR_RELAY) return registerSensor(new SensorRelay(child_id, pin));
    else if (sensor_type == SENSOR_LATCHING_RELAY) return registerSensor(new SensorLatchingRelay(child_id, pin));
  #endif
  #if MODULE_DHT == 1
    else if (sensor_type == SENSOR_DHT11 || sensor_type == SENSOR_DHT22) {
      DHT* dht = new DHT(pin,DHT22);
      int dht_type = sensor_type == SENSOR_DHT11 ? DHT11 : DHT22;
      registerSensor(new SensorDHT(child_id,pin,dht,0,dht_type));
      child_id = _getAvailableChildId();
997
      return registerSensor(new SensorDHT(child_id,pin,dht,1,dht_type));
DV's avatar
DV committed
998
999
1000
    }
  #endif
  #if MODULE_SHT21 == 1