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

#include "NodeManager.h"

7
8
9
10
11
12
/***************************************
   Global functions
*/

// return vcc in V
float getVcc() {
13
14
15
16
17
18
19
20
21
22
23
24
  #ifndef MY_GATEWAY_ESP8266
    // Measure Vcc against 1.1V Vref
    #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
      ADMUX = (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1));
    #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
      ADMUX = (_BV(MUX5) | _BV(MUX0));
    #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
      ADMUX = (_BV(MUX3) | _BV(MUX2));
    #else
      ADMUX = (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1));
    #endif
    // Vref settle
user2684's avatar
user2684 committed
25
    wait(70);
26
27
28
29
30
    // Do conversion
    ADCSRA |= _BV(ADSC);
    while (bit_is_set(ADCSRA, ADSC)) {};
    // return Vcc in mV
    return (float)((1125300UL) / ADC) / 1000;
31
  #else
32
    return (float)0;
33
34
  #endif
}
DV's avatar
DV committed
35

36

DV's avatar
DV committed
37
38
39
40
41
/***************************************
   PowerManager
*/

// set the vcc and ground pin the sensor is connected to
user2684's avatar
user2684 committed
42
void PowerManager::setPowerPins(int ground_pin, int vcc_pin, int wait_time) {
DV's avatar
DV committed
43
  #if DEBUG == 1
44
    Serial.print(F("PWR G="));
DV's avatar
DV committed
45
    Serial.print(ground_pin);
46
    Serial.print(F(" V="));
DV's avatar
DV committed
47
    Serial.println(vcc_pin);
DV's avatar
DV committed
48
  #endif
user2684's avatar
user2684 committed
49
  // configure the vcc pin as output and initialize to high (power on)
DV's avatar
DV committed
50
51
  _vcc_pin = vcc_pin;
  pinMode(_vcc_pin, OUTPUT);
user2684's avatar
user2684 committed
52
  digitalWrite(_vcc_pin, HIGH);
DV's avatar
DV committed
53
54
55
56
  // configure the ground pin as output and initialize to low
  _ground_pin = ground_pin;
  pinMode(_ground_pin, OUTPUT);
  digitalWrite(_ground_pin, LOW);
user2684's avatar
user2684 committed
57
  _wait = wait_time;
DV's avatar
DV committed
58
59
60
}

// return true if power pins have been configured
user2684's avatar
user2684 committed
61
bool PowerManager::isConfigured() {
DV's avatar
DV committed
62
63
64
65
66
67
  if (_vcc_pin != -1 && _ground_pin != -1) return true;
  return false;
}

// turn on the sensor by activating its power pins
void PowerManager::powerOn() {
user2684's avatar
user2684 committed
68
  if (! isConfigured()) return;
DV's avatar
DV committed
69
  #if DEBUG == 1
70
    Serial.print(F("ON P="));
DV's avatar
DV committed
71
72
73
74
75
    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
76
  if (_wait > 0) wait(_wait);
DV's avatar
DV committed
77
78
79
80
}

// turn off the sensor
void PowerManager::powerOff() {
user2684's avatar
user2684 committed
81
  if (! isConfigured()) return;
DV's avatar
DV committed
82
  #if DEBUG == 1
83
    Serial.print(F("OFF P="));
DV's avatar
DV committed
84
85
86
87
88
89
    Serial.println(_vcc_pin);
  #endif
  // power off the sensor by turning low the vcc pin
  digitalWrite(_vcc_pin, LOW);
}

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/******************************************
    Timer
*/

Timer::Timer(NodeManager* node_manager) {
  _node_manager = node_manager;
}

// start the timer
void Timer::start(long target, int unit) {
  set(target,unit);
  start();
}
void Timer::start() {
  if (_is_configured) _is_running = true;
}

// stop the timer
void Timer::stop() {
  _is_running = false;
}

// setup the timer
void Timer::set(long target, int unit) {
  // reset the timer
  _elapsed = 0;
  _use_millis = false;
  _last_millis = 0;
  _sleep_time = 0;
  // save the settings
  _target = target;
  _unit = unit;
  if (_unit == MINUTES) {
    if (_node_manager->isSleepingNode()) {
      // this is a sleeping node and millis() is not reliable so calculate how long a sleep/wait cycle would last
      int sleep_unit = _node_manager->getSleepUnit();
      _sleep_time = (float)_node_manager->getSleepTime();
      if (sleep_unit == SECONDS) _sleep_time = _sleep_time/60;
      else if (sleep_unit == HOURS) _sleep_time = _sleep_time*60;
      else if (sleep_unit == DAYS) _sleep_time = _sleep_time*1440;
    }
    else {
      // this is not a sleeping node, use millis() to keep track of the elapsed time
      _use_millis = true;
    }
  }
  _is_configured = true;
}

// update the timer at every cycle
void Timer::update() {
  if (! isRunning()) return;
  if (_unit == CYCLES) {
    // if not a sleeping node, counting the cycles do not make sense
    if (! _node_manager->isSleepingNode()) return;
    // just increase the cycle counter
    _elapsed++;
  }
  else if (_unit == MINUTES) {
    // if using millis(), calculate the elapsed minutes, otherwise add a sleep interval
    if (_use_millis) {
      _elapsed = (float)(millis() - _last_millis)/1000/60;
    }
    else {
      _elapsed += _sleep_time;
    }
  }
}

// return true if the time is over
bool Timer::isOver() {
  if (! isRunning()) return false;
  // time has elapsed
  if (_elapsed >= _target) return true;
  // millis has started over
  if (_elapsed < 0 ) return true;
  return false;
}

// return true if the timer is running
bool Timer::isRunning() {
  return _is_running;
}

// return true if the time is configured
bool Timer::isConfigured() {
  return _is_configured;
}

// restart the timer
void Timer::restart() {
  if (! isRunning()) return;
  // reset elapsed
  _elapsed = 0;
  // if using millis, keep track of the now timestamp
  if (_use_millis) _last_millis = millis();
}

// return elapsed minutes so far
float Timer::getElapsed() {
  return _elapsed;
}

// return the configured unit
int Timer::getUnit() {
  return _unit;
}
197

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

/******************************************
    Request
*/

Request::Request(const char* string) {
  char str[10];
  char* ptr;
  strcpy(str,string);
  // tokenize the string and split function from value
  strtok_r(str,",",&ptr);
  _function = atoi(str);
  strcpy(_value,ptr);
  #if DEBUG == 1
    Serial.print(F("REQ F="));
    Serial.print(getFunction());
    Serial.print(F(" I="));
    Serial.print(getValueInt());
    Serial.print(F(" F="));
    Serial.print(getValueFloat());
    Serial.print(F(" S="));
    Serial.println(getValueString());
  #endif
}

// return the parsed function
int Request::getFunction() {
  return _function;
}

// return the value as an int
int Request::getValueInt() {
  return atoi(_value);
  
}

// return the value as a float
float Request::getValueFloat() {
  return atof(_value);
}

// return the value as a string
char* Request::getValueString() {
  return _value;
}


DV's avatar
DV committed
245
246
247
248
249
250
251
252
/******************************************
    Sensors
*/

/*
   Sensor class
*/
// constructor
253
254
Sensor::Sensor(NodeManager* node_manager, int child_id, int pin) {
  _node_manager = node_manager;
DV's avatar
DV committed
255
256
257
  _child_id = child_id;
  _pin = pin;
  _msg = MyMessage(_child_id, _type);
258
  _msg_service = MyMessage(_child_id, V_CUSTOM);
259
260
  _report_timer = new Timer(_node_manager);
  _force_update_timer = new Timer(_node_manager);
DV's avatar
DV committed
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
}

// 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;
}
user2684's avatar
user2684 committed
289
290
291
void Sensor::setDescription(char* value) {
  _description = value;
}
DV's avatar
DV committed
292
293
294
295
296
297
void Sensor::setSamples(int value) {
  _samples = value;
}
void Sensor::setSamplesInterval(int value) {
  _samples_interval = value;
}
298
void Sensor::setTrackLastValue(bool value) {
DV's avatar
DV committed
299
300
301
  _track_last_value = value;
}
void Sensor::setForceUpdate(int value) {
302
303
304
305
306
307
308
  setForceUpdateCycles(value);
}
void Sensor::setForceUpdateCycles(int value) {
  _force_update_timer->start(value,CYCLES);
}
void Sensor::setForceUpdateMinutes(int value) {
  _force_update_timer->start(value,MINUTES);
DV's avatar
DV committed
309
310
311
312
}
void Sensor::setValueType(int value) {
  _value_type = value;
}
313
314
315
int Sensor::getValueType() {
  return _value_type;
}
DV's avatar
DV committed
316
317
318
319
void Sensor::setFloatPrecision(int value) {
  _float_precision = value;
}
#if POWER_MANAGER == 1
user2684's avatar
user2684 committed
320
321
    void Sensor::setPowerPins(int ground_pin, int vcc_pin, int wait_time) {
      _powerManager.setPowerPins(ground_pin, vcc_pin, wait_time);
DV's avatar
DV committed
322
    }
DV's avatar
DV committed
323
324
325
    void Sensor::setAutoPowerPins(bool value) {
      _auto_power_pins = value;
    }
DV's avatar
DV committed
326
327
328
329
330
331
332
    void Sensor::powerOn() {
      _powerManager.powerOn();
    }
    void Sensor::powerOff() {
      _powerManager.powerOff();
    }
#endif
333
334
335
int Sensor::getInterruptPin() {
  return _interrupt_pin;
}
336
337
338
339
340
341
342
343
344
int Sensor::getValueInt() {
  return _last_value_int;
}
float Sensor::getValueFloat() {
  return _last_value_float;
}
char* Sensor::getValueString() {
  return _last_value_string;
}
DV's avatar
DV committed
345

346
347
348
349
350
351
352
353
354
355
// After how many cycles the sensor will report back its measure (default: 1 cycle)
void Sensor::setReportIntervalCycles(int value) {
  _report_timer->start(value,CYCLES);
}

// After how many minutes the sensor will report back its measure (default: 1 cycle)
void Sensor::setReportIntervalMinutes(int value) {
  _report_timer->start(value,MINUTES);
}

DV's avatar
DV committed
356
357
358
// present the sensor to the gateway and controller
void Sensor::presentation() {
  #if DEBUG == 1
359
    Serial.print(F("PRES I="));
DV's avatar
DV committed
360
    Serial.print(_child_id);
361
    Serial.print(F(" T="));
DV's avatar
DV committed
362
363
    Serial.println(_presentation);
  #endif
364
  present(_child_id, _presentation,_description,_node_manager->getAck());
DV's avatar
DV committed
365
366
367
368
369
370
371
372
}

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

user2684's avatar
user2684 committed
373
374
375
376
377
378
// call the sensor-specific implementation of setup
void Sensor::setup() {
  if (_pin == -1) return;
  onSetup();
}

DV's avatar
DV committed
379
380
381
// call the sensor-specific implementation of loop
void Sensor::loop(const MyMessage & message) {
  if (_pin == -1) return;
382
383
384
385
386
387
388
389
390
391
  // update the timers if within a loop cycle
  if (! _isReceive(message)) {
    if (_report_timer->isRunning()) {
      // update the timer
      _report_timer->update();
      // if it is not the time yet to report a new measure, just return
      if (! _report_timer->isOver()) return;
    }
    if (_force_update_timer->isRunning()) _force_update_timer->update();
  }
DV's avatar
DV committed
392
393
  #if POWER_MANAGER == 1
    // turn the sensor on
DV's avatar
DV committed
394
    if (_auto_power_pins) powerOn();
DV's avatar
DV committed
395
396
397
398
399
400
401
  #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
  // 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
402
    if (_isReceive(message)) {
DV's avatar
DV committed
403
404
405
      // we've been called from receive(), pass the message along
      onReceive(message);
    }
406
407
408
409
    else {
      // we'be been called from loop()
      onLoop();
    }
DV's avatar
DV committed
410
411
412
413
    // 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
414
    if (_samples_interval > 0) wait(_samples_interval);
DV's avatar
DV committed
415
  }
416
  // process the result and send a response back
DV's avatar
DV committed
417
418
419
420
  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
421
    if (_isReceive(message) || _isWorthSending(avg != _last_value_int))  {
DV's avatar
DV committed
422
423
424
425
426
427
428
429
      _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;
430
431
    // report the value back
    if (_isReceive(message) || _isWorthSending(avg != _last_value_float))  {
DV's avatar
DV committed
432
433
434
435
436
437
438
      _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
439
    if (_isReceive(message) || _isWorthSending(strcmp(_value_string, _last_value_string) != 0))  {
DV's avatar
DV committed
440
441
442
443
444
445
      _last_value_string = _value_string;
      _send(_msg.set(_value_string));
    }
  }
  // turn the sensor off
  #if POWER_MANAGER == 1
DV's avatar
DV committed
446
    if (_auto_power_pins) powerOff();
DV's avatar
DV committed
447
  #endif
448
449
  // restart the report timer if over
  if (! _isReceive(message) && _report_timer->isRunning() && _report_timer->isOver()) _report_timer->restart();
DV's avatar
DV committed
450
451
452
453
454
}

// receive a message from the radio network
void Sensor::receive(const MyMessage &message) {
  // return if not for this sensor
455
456
457
458
459
460
461
462
463
464
465
466
467
  if (message.sensor != _child_id) return;
  // check if it is a request for the API
  if (message.getCommand() == C_REQ && message.type == V_CUSTOM) {
    #if REMOTE_CONFIGURATION == 1
      // parse the request
      Request request = Request(message.getString());
      // if it is for a sensor-generic function, call process(), otherwise the sensor-specific onProcess();
      if (request.getFunction() < 100) process(request);
      else onProcess(request);
    #endif
  }
  // return if the type is not correct
  if (message.type != _type) return;
468
  // a request would make the sensor executing its main task passing along the message
DV's avatar
DV committed
469
470
471
  loop(message);
}

472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// process a remote configuration request message
void Sensor::process(Request & request) {
  int function = request.getFunction();
  switch(function) {
    case 1: setPin(request.getValueInt()); break;
    case 2: setChildId(request.getValueInt()); break;
    case 3: setType(request.getValueInt()); break;
    case 4: setDescription(request.getValueString()); break;
    case 5: setSamples(request.getValueInt()); break;
    case 6: setSamplesInterval(request.getValueInt()); break;
    case 7: setTrackLastValue(request.getValueInt()); break;
    case 8: setForceUpdateCycles(request.getValueInt()); break;
    case 9: setForceUpdateMinutes(request.getValueInt()); break;
    case 10: setValueType(request.getValueInt()); break;
    case 11: setFloatPrecision(request.getValueInt()); break;
    #if POWER_MANAGER == 1
      case 12: setAutoPowerPins(request.getValueInt()); break;
      case 13: powerOn(); break;
      case 14: powerOff(); break;
    #endif
    case 15: setReportIntervalCycles(request.getValueInt()); break;
    case 16: setReportIntervalMinutes(request.getValueInt()); break;
    default: return;
  }
  _send(_msg_service.set(function));
}

DV's avatar
DV committed
499
500
501
// send a message to the network
void Sensor::_send(MyMessage & message) {
  // send the message, multiple times if requested
502
  for (int i = 0; i < _node_manager->getRetries(); i++) {
DV's avatar
DV committed
503
    // if configured, sleep beetween each send
504
    if (_node_manager->getSleepBetweenSend() > 0) sleep(_node_manager->getSleepBetweenSend());
DV's avatar
DV committed
505
    #if DEBUG == 1
506
      Serial.print(F("SEND D="));
DV's avatar
DV committed
507
      Serial.print(message.destination);
508
      Serial.print(F(" I="));
DV's avatar
DV committed
509
      Serial.print(message.sensor);
510
      Serial.print(F(" C="));
DV's avatar
DV committed
511
      Serial.print(message.getCommand());
512
      Serial.print(F(" T="));
DV's avatar
DV committed
513
      Serial.print(message.type);
514
      Serial.print(F(" S="));
DV's avatar
DV committed
515
      Serial.print(message.getString());
516
      Serial.print(F(" I="));
DV's avatar
DV committed
517
      Serial.print(message.getInt());
518
      Serial.print(F(" F="));
DV's avatar
DV committed
519
520
      Serial.println(message.getFloat());
    #endif
521
    send(message,_node_manager->getAck());
DV's avatar
DV committed
522
523
524
  }
}

525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// return true if the message is coming from the radio network
bool Sensor::_isReceive(const MyMessage & message) {
  if (message.sender == 0 && message.sensor == 0 && message.getCommand() == 0 && message.type == 0) return false;
  return true;
}

// determine if a value is worth sending back to the controller
bool Sensor::_isWorthSending(bool comparison) {
  // track last value is disabled
  if (! _track_last_value) return true;
  // track value is enabled and the current value is different then the old value
  if (_track_last_value && comparison) return true;
  // track value is enabled and the timer is over
  if (_track_last_value && _force_update_timer->isRunning() && _force_update_timer->isOver()) {
    // restart the timer
    _force_update_timer->restart();
    return true;
  }
  return false;
}

546
#if MODULE_ANALOG_INPUT == 1
DV's avatar
DV committed
547
548
549
550
551
/*
   SensorAnalogInput
*/

// contructor
552
SensorAnalogInput::SensorAnalogInput(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager, child_id, pin) {
DV's avatar
DV committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
}

// 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;
}

572
// what to do during before
DV's avatar
DV committed
573
574
575
576
577
void SensorAnalogInput::onBefore() {
  // prepare the pin for input
  pinMode(_pin, INPUT);
}

578
// what to do during setup
user2684's avatar
user2684 committed
579
580
581
void SensorAnalogInput::onSetup() {
}

582
// what to do during loop
DV's avatar
DV committed
583
584
585
586
587
588
589
void SensorAnalogInput::onLoop() {
  // read the input
  int adc = _getAnalogRead();
  // calculate the percentage
  int percentage = 0;
  if (_output_percentage) percentage = _getPercentage(adc);
  #if DEBUG == 1
590
    Serial.print(F("A-IN I="));
DV's avatar
DV committed
591
    Serial.print(_child_id);
592
    Serial.print(F(" V="));
DV's avatar
DV committed
593
    Serial.print(adc);
594
    Serial.print(F(" %="));
DV's avatar
DV committed
595
596
597
598
599
600
    Serial.println(percentage);
  #endif
  // store the result
  _value_int = _output_percentage ? percentage : adc;
}

601
// what to do during loop
DV's avatar
DV committed
602
void SensorAnalogInput::onReceive(const MyMessage & message) {
603
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
604
605
}

606
607
608
609
610
611
612
613
614
615
616
617
618
619
// what to do when receiving a remote message
void SensorAnalogInput::onProcess(Request & request) {
  int function = request.getFunction();
  switch(function) {
    case 101: setReference(request.getValueInt()); break;
    case 102: setReverse(request.getValueInt()); break;
    case 103: setOutputPercentage(request.getValueInt()); break;
    case 104: setRangeMin(request.getValueInt()); break;
    case 105: setRangeMax(request.getValueInt()); break;
    default: return;
  }
  _send(_msg_service.set(function));
}

DV's avatar
DV committed
620
621
// read the analog input
int SensorAnalogInput::_getAnalogRead() {
622
623
624
625
626
627
628
  #ifndef MY_GATEWAY_ESP8266
    // set the reference
    if (_reference != -1) {
      analogReference(_reference);
      wait(100);
    }
  #endif
DV's avatar
DV committed
629
630
631
632
633
634
635
636
637
  // 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
638
639
  // restore the original value
  if (_reverse) value = 1024 - value;
DV's avatar
DV committed
640
641
  // scale the percentage based on the range provided
  float percentage = ((value - _range_min) / (_range_max - _range_min)) * 100;
DV's avatar
DV committed
642
  if (_reverse) percentage = 100 - percentage;
DV's avatar
DV committed
643
644
645
646
647
648
649
650
651
652
  if (percentage > 100) percentage = 100;
  if (percentage < 0) percentage = 0;
  return (int)percentage;
}

/*
   SensorLDR
*/

// contructor
653
SensorLDR::SensorLDR(NodeManager* node_manager, int child_id, int pin): SensorAnalogInput(node_manager, child_id, pin) {
DV's avatar
DV committed
654
655
656
657
658
659
660
661
662
663
664
  // set presentation and type and reverse (0: no light, 100: max light)
  setPresentation(S_LIGHT_LEVEL);
  setType(V_LIGHT_LEVEL);
  setReverse(true);
}

/*
   SensorThermistor
*/

// contructor
665
SensorThermistor::SensorThermistor(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager, child_id, pin) {
DV's avatar
DV committed
666
667
668
669
670
671
672
  // set presentation, type and value type
  setPresentation(S_TEMP);
  setType(V_TEMP);
  setValueType(TYPE_FLOAT);
}

// setter/getter
673
void SensorThermistor::setNominalResistor(long value) {
DV's avatar
DV committed
674
675
676
677
678
679
680
681
  _nominal_resistor = value;
}
void SensorThermistor::setNominalTemperature(int value) {
  _nominal_temperature = value;
}
void SensorThermistor::setBCoefficient(int value) {
  _b_coefficient = value;
}
682
void SensorThermistor::setSeriesResistor(long value) {
DV's avatar
DV committed
683
684
685
686
687
688
  _series_resistor = value;
}
void SensorThermistor::setOffset(float value) {
  _offset = value;
}

689
// what to do during before
DV's avatar
DV committed
690
691
692
693
694
void SensorThermistor::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

695
// what to do during setup
user2684's avatar
user2684 committed
696
697
698
void SensorThermistor::onSetup() {
}

699
// what to do during loop
DV's avatar
DV committed
700
701
702
703
704
705
706
707
708
709
710
711
712
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
713
  temperature = _node_manager->celsiusToFahrenheit(temperature);
DV's avatar
DV committed
714
  #if DEBUG == 1
715
    Serial.print(F("THER I="));
DV's avatar
DV committed
716
    Serial.print(_child_id);
717
    Serial.print(F(" V="));
DV's avatar
DV committed
718
    Serial.print(adc);
719
    Serial.print(F(" T="));
720
    Serial.print(temperature);
DV's avatar
DV committed
721
722
723
724
725
  #endif
  // store the value
  _value_float = temperature;
}

726
// what to do as the main task when receiving a message
DV's avatar
DV committed
727
void SensorThermistor::onReceive(const MyMessage & message) {
728
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
729
730
}

731
732
733
734
735
736
737
738
739
740
741
742
743
// what to do when receiving a remote message
void SensorThermistor::onProcess(Request & request) {
  int function = request.getFunction();
  switch(function) {
    case 101: setNominalResistor((long)request.getValueInt()); break;
    case 102: setNominalTemperature(request.getValueInt()); break;
    case 103: setBCoefficient(request.getValueInt()); break;
    case 104: setSeriesResistor((long)request.getValueString()); break;
    case 105: setOffset(request.getValueFloat()); break;
    default: return;
  }
  _send(_msg_service.set(function));
}
744
745
746
747
748
749

/*
   SensorML8511
*/

// contructor
750
SensorML8511::SensorML8511(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager, child_id, pin) {
751
752
753
754
755
756
  // set presentation, type and value type
  setPresentation(S_UV);
  setType(V_UV);
  setValueType(TYPE_FLOAT);
}

757
// what to do during before
758
759
760
761
762
void SensorML8511::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

763
// what to do during setup
764
765
766
void SensorML8511::onSetup() {
}

767
// what to do during loop
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
void SensorML8511::onLoop() {
  // read the voltage 
  int uvLevel = analogRead(_pin);
  int refLevel = getVcc()*1024/3.3;
  //Use the 3.3V power pin as a reference to get a very accurate output value from sensor
  float outputVoltage = 3.3 / refLevel * uvLevel;
  //Convert the voltage to a UV intensity level
  float uvIntensity = _mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0); 
  #if DEBUG == 1
    Serial.print(F("UV I="));
    Serial.print(_child_id);
    Serial.print(F(" V="));
    Serial.print(outputVoltage);
    Serial.print(F(" I="));
    Serial.println(uvIntensity);
  #endif
  // store the value
  _value_float = uvIntensity;
}

788
// what to do as the main task when receiving a message
789
void SensorML8511::onReceive(const MyMessage & message) {
790
  if (message.getCommand() == C_REQ) onLoop();
791
792
}

793
794
795
796
// what to do when receiving a remote message
void SensorML8511::onProcess(Request & request) {
}

797
798
799
800
801
// The Arduino Map function but for floats
float SensorML8511::_mapfloat(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

802
803
804
805
806
/*
   SensorACS712
*/

// contructor
807
SensorACS712::SensorACS712(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager, child_id, pin) {
808
809
810
811
812
813
814
815
816
817
818
819
820
821
  // set presentation, type and value type
  setPresentation(S_MULTIMETER);
  setType(V_CURRENT);
  setValueType(TYPE_FLOAT);
}

// setter/getter
void SensorACS712::setmVPerAmp(int value) {
  _mv_per_amp = value;
}
void SensorACS712::setOffset(int value) {
  _ACS_offset = value;
}

822
// what to do during before
823
824
825
826
827
void SensorACS712::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

828
// what to do during setup
829
830
831
void SensorACS712::onSetup() {
}

832
// what to do during loop
833
834
835
836
837
838
839
840
841
842
843
844
845
846
void SensorACS712::onLoop() {
  int value = analogRead(_pin);
  // convert the analog read in mV
  double voltage = (value / 1024.0) * 5000; 
  // convert voltage in amps
  _value_float = ((voltage - _ACS_offset) / _mv_per_amp);
  #if DEBUG == 1
    Serial.print(F("ACS I="));
    Serial.print(_child_id);
    Serial.print(F(" A="));
    Serial.println(_value_float);
  #endif
}

847
// what to do as the main task when receiving a message
848
849
850
851
void SensorACS712::onReceive(const MyMessage & message) {
  if (message.getCommand() == C_REQ) onLoop();
}

852
853
854
855
856
857
858
859
860
861
862
// what to do when receiving a remote message
void SensorACS712::onProcess(Request & request) {
  int function = request.getFunction();
  switch(function) {
    case 100: setmVPerAmp(request.getValueInt()); break;
    case 102: setOffset(request.getValueInt()); break;
    default: return;
  }
  _send(_msg_service.set(function));
}

863
864
865
866
867
/*
   SensorRainGauge
*/

// contructor
868
SensorRainGauge::SensorRainGauge(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager,child_id, pin) {
869
870
871
872
  // set presentation, type and value type
  setPresentation(S_RAIN);
  setType(V_RAIN);
  setValueType(TYPE_FLOAT);
873
874
  // create the timer
  _timer = new Timer(node_manager);
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
}

// initialize static variables
long SensorRainGauge::_last_tip = 0;
long SensorRainGauge::_count = 0;

// setter/getter
void SensorRainGauge::setReportInterval(int value) {
  _report_interval = value;
}
void SensorRainGauge::setSingleTip(float value) {
  _single_tip = value;
}

// what to do during before
void SensorRainGauge::onBefore() {
  // set the pin as input and enabled pull up
  pinMode(_pin, INPUT_PULLUP);
  // attach to the pin's interrupt and execute the routine on falling
  attachInterrupt(digitalPinToInterrupt(_pin), _onTipped, FALLING);
895
896
  // start the timer
  _timer->start(_report_interval,MINUTES);
897
898
899
900
901
902
903
904
}

// what to do during setup
void SensorRainGauge::onSetup() {
}

// what to do when when receiving an interrupt
void SensorRainGauge::_onTipped() {
905
  long now = millis();
906
  // on tipping, two consecutive interrupts are received, ignore the second one
907
  if ( (now - _last_tip > 100) || (now < _last_tip) ){
908
909
910
911
912
913
    // increase the counter
    _count++;
    #if DEBUG == 1
      Serial.println(F("RAIN+"));
    #endif
  }
914
  _last_tip = now;
915
916
917
918
919
920
}

// what to do during loop
void SensorRainGauge::onLoop() {
  // avoid reporting the same value multiple times
  _value_float = -1;
921
922
923
  _timer->update();
  // time to report 
  if (_timer->isOver()) {
924
    // report the total amount of rain for the last period
925
    _value_float = _count * _single_tip;
926
927
928
929
930
931
    #if DEBUG == 1
      Serial.print(F("RAIN I="));
      Serial.print(_child_id);
      Serial.print(F(" T="));
      Serial.println(_value_float);
    #endif
932
933
    // reset the timer
    _timer->restart();
934
935
936
937
938
939
940
  }
}

// what to do as the main task when receiving a message
void SensorRainGauge::onReceive(const MyMessage & message) {
  if (message.getCommand() == C_REQ) {
    // report the total amount of rain for the last period
941
    _value_float = _count * _single_tip;    
942
943
944
  }
}

945
946
947
948
949
950
951
952
953
954
955
// what to do when receiving a remote message
void SensorRainGauge::onProcess(Request & request) {
  int function = request.getFunction();
  switch(function) {
    case 101: setReportInterval(request.getValueInt()); break;
    case 102: setSingleTip(request.getValueFloat()); break;
    default: return;
  }
  _send(_msg_service.set(function));
}

956
957
958
959
960
/*
   SensorRain
*/

// contructor
961
SensorRain::SensorRain(NodeManager* node_manager, int child_id, int pin): SensorAnalogInput(node_manager,child_id, pin) {
962
963
964
965
966
967
968
969
970
971
972
973
974
975
  // set presentation and type and reverse
  setPresentation(S_RAIN);
  setType(V_RAINRATE);
  setReference(DEFAULT);
  setOutputPercentage(true);
  setReverse(true);
  setRangeMin(100);
}

/*
   SensorSoilMoisture
*/

// contructor
976
SensorSoilMoisture::SensorSoilMoisture(NodeManager* node_manager, int child_id, int pin): SensorAnalogInput(node_manager, child_id, pin) {
977
978
979
980
981
982
983
984
985
  // set presentation and type and reverse
  setPresentation(S_MOISTURE);
  setType(V_LEVEL);
  setReference(DEFAULT);
  setOutputPercentage(true);
  setReverse(true);
  setRangeMin(100);
}

986
#endif
987

988
#if MODULE_DIGITAL_INPUT == 1
DV's avatar
DV committed
989
990
991
992
993
/*
   SensorDigitalInput
*/

// contructor
994
SensorDigitalInput::SensorDigitalInput(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager,child_id, pin) {
DV's avatar
DV committed
995
996
}

997
// what to do during before
DV's avatar
DV committed
998
999
1000
1001
1002
void SensorDigitalInput::onBefore() {
  // set the pin for input
  pinMode(_pin, INPUT);
}

1003
// what to do during setup
user2684's avatar
user2684 committed
1004
1005
1006
void SensorDigitalInput::onSetup() {
}

1007
// what to do during loop
DV's avatar
DV committed
1008
1009
1010
1011
void SensorDigitalInput::onLoop() {
  // read the value
  int value = digitalRead(_pin);
  #if DEBUG == 1
1012
    Serial.print(F("D-IN I="));
DV's avatar
DV committed
1013
    Serial.print(_child_id);
1014
    Serial.print(F(" P="));
DV's avatar
DV committed
1015
    Serial.print(_pin);
1016
    Serial.print(F(" V="));
DV's avatar
DV committed
1017
1018
1019
1020
1021
1022
    Serial.println(value);
  #endif
  // store the value
  _value_int = value;
}

1023
// what to do as the main task when receiving a message
DV's avatar
DV committed
1024
void SensorDigitalInput::onReceive(const MyMessage & message) {
1025
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
1026
1027
}

1028
1029
1030
1031
// what to do when receiving a remote message
void SensorDigitalInput::onProcess(Request & request) {
}
#endif
DV's avatar
DV committed
1032

1033
#if MODULE_DIGITAL_OUTPUT == 1
DV's avatar
DV committed
1034
1035
1036
1037
/*
   SensorDigitalOutput
*/

1038
SensorDigitalOutput::SensorDigitalOutput(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager,child_id, pin) {
1039
  _safeguard_timer = new Timer(node_manager);
DV's avatar
DV committed
1040
1041
}

1042
// what to do during before
DV's avatar
DV committed
1043
1044
1045
void SensorDigitalOutput::onBefore() {
  // set the pin as output and initialize it accordingly
  pinMode(_pin, OUTPUT);
1046
1047
  _state = _initial_value == LOW ? LOW : HIGH;
  digitalWrite(_pin, _state);
DV's avatar
DV committed
1048
1049
  // the initial value is now the current value
  _value_int = _initial_value;
1050
  // create the safeguard timer
DV's avatar
DV committed
1051
1052
}

1053
// what to do during setup
user2684's avatar
user2684 committed
1054
1055
1056
void SensorDigitalOutput::onSetup() {
}

DV's avatar
DV committed
1057
1058
1059
1060
1061
1062
1063
// setter/getter
void SensorDigitalOutput::setInitialValue(int value) {
  _initial_value = value;
}
void SensorDigitalOutput::setPulseWidth(int value) {
  _pulse_width = value;
}
1064
1065
1066
void SensorDigitalOutput::setOnValue(int value) {
  _on_value = value;
}
1067
1068
1069
void SensorDigitalOutput::setLegacyMode(bool value) {
  _legacy_mode = value;
}
1070
1071
1072
1073
1074
1075
1076
1077
1078
void SensorDigitalOutput::setSafeguard(int value) {
  _safeguard_timer->set(value,MINUTES);
}
int SensorDigitalOutput::getState() {
  return _state;
}
void SensorDigitalOutput::setInputIsElapsed(bool value) {
  _input_is_elapsed = value;
}
DV's avatar
DV committed
1079
1080
1081

// main task
void SensorDigitalOutput::onLoop() {
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
    // set the value to -1 so to avoid reporting to the gateway during loop
    _value_int = -1;
    _last_value_int = -1;
  // if a safeguard is set, check if it is time for it
  if (_safeguard_timer->isRunning()) {
    // update the timer
    _safeguard_timer->update();
    // if the time is over, turn the output off
    if (_safeguard_timer->isOver()) set(LOW);
  }
DV's avatar
DV committed
1092
1093
}

1094
// what to do as the main task when receiving a message
DV's avatar
DV committed
1095
void SensorDigitalOutput::onReceive(const MyMessage & message) {
1096
1097
  // by default handle a SET message but when legacy mode is set when a REQ message is expected instead
  if ( (message.getCommand() == C_SET && ! _legacy_mode) || (message.getCommand() == C_REQ && _legacy_mode)) {
1098
1099
    // switch the output
    set(message.getInt());
1100
  }
1101
  if (message.getCommand() == C_REQ && ! _legacy_mode) {
1102
1103
1104
    // return the current status
    _value_int = _state;
  }
DV's avatar
DV committed
1105
1106
}

1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
// what to do when receiving a remote message
void SensorDigitalOutput::onProcess(Request & request) {
  int function = request.getFunction();
  switch(function) {
    case 101: setInitialValue(request.getValueInt()); break;
    case 102: setPulseWidth(request.getValueInt()); break;
    case 103: setOnValue(request.getValueInt()); break;
    case 104: setLegacyMode(request.getValueInt()); break;
    case 105: setSafeguard(request.getValueInt()); break;
    case 106: setInputIsElapsed(request.getValueInt()); break;
    default: return;
  }
  _send(_msg_service.set(function));
}

1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
// write the value to the output
void SensorDigitalOutput::set(int value) {
  if (_input_is_elapsed) {
    if (value == LOW) {
      // stop the timer
      _safeguard_timer->stop();
    } else {
      // configure and start the timer
      _safeguard_timer->start(value,MINUTES);
      // if the input is an elapsed time, unless the value is LOW, the output will be always on
      value = HIGH;
    }
  } else {
    // if turning the output on and a safeguard timer is configured, start it
    if (value == HIGH && _safeguard_timer->isConfigured() && ! _safeguard_timer->isRunning()) _safeguard_timer->start();
  }
  #if DEBUG == 1
    Serial.print(F("DOUT I="));
    Serial.print(_child_id);
    Serial.print(F(" P="));
    Serial.print(_pin);
    Serial.print(F(" V="));
    Serial.print(value);
    Serial.print(F(" P="));
    Serial.println(_pulse_width);
  #endif
  // reverse the value if needed
  int value_to_write = value;
  if (_on_value == LOW) {
    if (value == HIGH) value_to_write = LOW;
    if (value == LOW) value_to_write = HIGH;
  }
  // set the value
  digitalWrite(_pin, value_to_write);
  if (_pulse_width > 0) {
    // if this is a pulse output, restore the value to the original value after the pulse
    wait(_pulse_width);
    digitalWrite(_pin, value_to_write == 0 ? HIGH: LOW);
  }
  // store the current value so it will be sent to the controller
  _state = value;
  _value_int = value;
}


DV's avatar
DV committed
1167
1168
1169
1170
1171
/*
   SensorRelay
*/

// contructor
1172
SensorRelay::SensorRelay(NodeManager* node_manager, int child_id, int pin): SensorDigitalOutput(node_manager, child_id, pin) {
DV's avatar
DV committed
1173
1174
1175
1176
  // set presentation and type
  setPresentation(S_BINARY);
  setType(V_STATUS);
}
1177

DV's avatar
DV committed
1178
1179
1180
1181
1182
/*
   SensorLatchingRelay
*/

// contructor
1183
SensorLatchingRelay::SensorLatchingRelay(NodeManager* node_manager, int child_id, int pin): SensorRelay(node_manager, child_id, pin) {
DV's avatar
DV committed
1184
1185
1186
1187
  // like a sensor with a default pulse set
  setPulseWidth(50);
}

1188
#endif
DV's avatar
DV committed
1189
1190
1191
1192
1193
/*
   SensorDHT
*/
#if MODULE_DHT == 1
// contructor
1194
SensorDHT::SensorDHT(NodeManager* node_manager, int child_id, int pin, DHT* dht, int sensor_type, int dht_type): Sensor(node_manager, child_id, pin) {
DV's avatar
DV committed
1195
1196
1197
1198
  // store the dht object
  _dht = dht;
  _sensor_type = sensor_type;
  _dht_type = dht_type;
1199
  if (_sensor_type == SensorDHT::TEMPERATURE) {
DV's avatar
DV committed
1200
1201
1202
1203
1204
    // temperature sensor
    setPresentation(S_TEMP);
    setType(V_TEMP);
    setValueType(TYPE_FLOAT);
  }
1205
  else if (_sensor_type == SensorDHT::HUMIDITY) {
DV's avatar
DV committed
1206
1207
1208
1209
1210
1211
1212
    // humidity sensor
    setPresentation(S_HUM);
    setType(V_HUM);
    setValueType(TYPE_FLOAT);
  }
}

1213
// what to do during before
DV's avatar
DV committed
1214
1215
1216
1217
1218
void SensorDHT::onBefore() {
    // initialize the dht library
    _dht->begin();
}

1219
// what to do during setup
user2684's avatar
user2684 committed
1220
1221
1222
void SensorDHT::onSetup() {
}

1223
// what to do during loop
DV's avatar
DV committed
1224
1225
void SensorDHT::onLoop() {
  // temperature sensor
1226
  if (_sensor_type == SensorDHT::TEMPERATURE) {
DV's avatar
DV committed
1227
1228
1229
    // read the temperature
    float temperature = _dht->readTemperature();
    // convert it
1230
    temperature = _node_manager->celsiusToFahrenheit(temperature);
DV's avatar
DV committed
1231
    #if DEBUG == 1
1232
      Serial.print(F("DHT I="));
DV's avatar
DV committed
1233
      Serial.print(_child_id);
1234
      Serial.print(F(" T="));
DV's avatar
DV committed
1235
1236
1237
1238
1239
1240
      Serial.println(temperature);
    #endif
    // store the value
    if (! isnan(temperature)) _value_float = temperature;
  }
  // humidity sensor
1241
  else if (_sensor_type == SensorDHT::HUMIDITY) {
DV's avatar
DV committed
1242
1243
1244
1245
    // read humidity
    float humidity = _dht->readHumidity();
    if (isnan(humidity)) return;
    #if DEBUG == 1
1246
      Serial.print(F("DHT I="));
DV's avatar
DV committed
1247
      Serial.print(_child_id);
1248
      Serial.print(F(" H="));
DV's avatar
DV committed
1249
1250
1251
1252
1253
1254
1255
      Serial.println(humidity);
    #endif
    // store the value
    if (! isnan(humidity)) _value_float = humidity;
  }
}

1256
// what to do as the main task when receiving a message
DV's avatar
DV committed
1257
void SensorDHT::onReceive(const MyMessage & message) {
1258
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
1259
}
1260
1261
1262
1263

// what to do when receiving a remote message
void SensorDHT::onProcess(Request & request) {
}
DV's avatar
DV committed
1264
1265
1266
1267
1268
1269
1270
#endif

/*
   SensorSHT21
*/
#if MODULE_SHT21 == 1
// contructor
1271
SensorSHT21::SensorSHT21(NodeManager* node_manager, int child_id, int sensor_type): Sensor(node_manager,child_id,A2) {
DV's avatar
DV committed
1272
1273
  // store the sensor type (0: temperature, 1: humidity)
  _sensor_type = sensor_type;
1274
  if (_sensor_type == SensorSHT21::TEMPERATURE) {
DV's avatar
DV committed
1275
1276
1277
1278
1279
    // temperature sensor
    setPresentation(S_TEMP);
    setType(V_TEMP);
    setValueType(TYPE_FLOAT);
  }
1280
  else if (_sensor_type == SensorSHT21::HUMIDITY) {
DV's avatar
DV committed
1281
1282
1283
1284
1285
1286
1287
    // humidity sensor
    setPresentation(S_HUM);
    setType(V_HUM);
    setValueType(TYPE_FLOAT);
  }
}

1288
// what to do during before
DV's avatar
DV committed
1289
1290
1291
1292
1293
void SensorSHT21::onBefore() {
  // initialize the library
  Wire.begin();
}

1294
// what to do during setup
user2684's avatar
user2684 committed
1295
1296
1297
void SensorSHT21::onSetup() {
}

1298
// what to do during loop
DV's avatar
DV committed
1299
1300
void SensorSHT21::onLoop() {
  // temperature sensor
1301
  if (_sensor_type == SensorSHT21::TEMPERATURE) {
DV's avatar
DV committed
1302
1303
1304
    // read the temperature
    float temperature = SHT2x.GetTemperature();
    // convert it
1305
    temperature = _node_manager->celsiusToFahrenheit(temperature);
DV's avatar
DV committed
1306
    #if DEBUG == 1
1307
      Serial.print(F("SHT I="));
DV's avatar
DV committed
1308
      Serial.print(_child_id);
1309
      Serial.print(F(" T="));
DV's avatar
DV committed
1310
1311
1312
1313
1314
1315
      Serial.println(temperature);
    #endif
    // store the value
    if (! isnan(temperature)) _value_float = temperature;
  }
  // Humidity Sensor
1316
  else if (_sensor_type == SensorSHT21::HUMIDITY) {
DV's avatar
DV committed
1317
1318
1319
1320
    // read humidity
    float humidity = SHT2x.GetHumidity();
    if (isnan(humidity)) return;
    #if DEBUG == 1
1321
      Serial.print(F("SHT I="));
DV's avatar
DV committed
1322
      Serial.print(_child_id);
1323
      Serial.print(F(" H="));
DV's avatar
DV committed
1324
1325
1326
1327
1328
1329
1330
      Serial.println(humidity);
    #endif
    // store the value
    if (! isnan(humidity)) _value_float = humidity;
  }
}

1331
// what to do as the main task when receiving a message
DV's avatar
DV committed
1332
void SensorSHT21::onReceive(const MyMessage & message) {
1333
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
1334
}
1335
1336
1337
1338

// what to do when receiving a remote message
void SensorSHT21::onProcess(Request & request) {
}
DV's avatar
DV committed
1339
1340
#endif

DV's avatar
DV committed
1341
1342
1343
1344
1345
/*
 * SensorHTU21D
 */
 #if MODULE_SHT21 == 1
// constructor
1346
SensorHTU21D::SensorHTU21D(NodeManager* node_manager, int child_id, int pin): SensorSHT21(node_manager, child_id, pin) {
DV's avatar
DV committed
1347
1348
1349
}
#endif 

1350
#if MODULE_SWITCH == 1
DV's avatar
DV committed
1351
1352
1353
/*
 * SensorSwitch
 */
1354
SensorSwitch::SensorSwitch(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager,child_id,pin) {
DV's avatar
DV committed
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
  setType(V_TRIPPED);
}

// setter/getter
void SensorSwitch::setMode(int value) {
  _mode = value;
}
void SensorSwitch::setDebounce(int value) {
  _debounce = value;
}
void SensorSwitch::setTriggerTime(int value) {
  _trigger_time = value;
}
1368
1369
1370
void SensorSwitch::setInitial(int value) {
  _initial = value;
}
DV's avatar
DV committed
1371

1372
// what to do during before
DV's avatar
DV committed
1373
1374
1375
1376
void SensorSwitch::onBefore() {
  // initialize the value
  if (_mode == RISING) _value_int = LOW;
  else if (_mode == FALLING) _value_int = HIGH;
1377
1378
1379
  // set the interrupt pin so it will be called only when waking up from that interrupt
  _interrupt_pin = _pin;
  _node_manager->setInterrupt(_pin,_mode,_initial);
DV's avatar
DV committed
1380
1381
}

1382
// what to do during setup
user2684's avatar
user2684 committed
1383
1384
1385
void SensorSwitch::onSetup() {
}

1386
// what to do during loop
DV's avatar
DV committed
1387
1388
void SensorSwitch::onLoop() {
  // wait to ensure the the input is not floating
1389
  if (_debounce > 0) wait(_debounce);
DV's avatar
DV committed
1390
1391
1392
1393
1394
  // 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
1395
      Serial.print(F("SWITCH I="));
DV's avatar
DV committed
1396
      Serial.print(_child_id);
1397
      Serial.print(F(" P="));
DV's avatar
DV committed
1398
      Serial.print(_pin);