NodeManager.cpp 75.3 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

DV's avatar
DV committed
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
/******************************************
    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;
}
user2684's avatar
user2684 committed
131
132
133
void Sensor::setDescription(char* value) {
  _description = value;
}
user2684's avatar
user2684 committed
134
135
136
void Sensor::setAck(bool value) {
  _ack = value;
}
DV's avatar
DV committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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;
}
155
156
157
int Sensor::getValueType() {
  return _value_type;
}
DV's avatar
DV committed
158
159
160
161
void Sensor::setFloatPrecision(int value) {
  _float_precision = value;
}
#if POWER_MANAGER == 1
user2684's avatar
user2684 committed
162
163
    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
164
    }
DV's avatar
DV committed
165
166
167
    void Sensor::setAutoPowerPins(bool value) {
      _auto_power_pins = value;
    }
DV's avatar
DV committed
168
169
170
171
172
173
174
    void Sensor::powerOn() {
      _powerManager.powerOn();
    }
    void Sensor::powerOff() {
      _powerManager.powerOff();
    }
#endif
DV's avatar
DV committed
175
176
177
void Sensor::setSleepBetweenSend(int value) {
  _sleep_between_send = value;
}
178
179
180
181
182
183
void Sensor::setInterruptPin(int value) {
  _interrupt_pin = value;
}
int Sensor::getInterruptPin() {
  return _interrupt_pin;
}
184
185
186
187
188
189
190
191
192
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
193
194
195
196

// present the sensor to the gateway and controller
void Sensor::presentation() {
  #if DEBUG == 1
197
    Serial.print(F("PRES I="));
DV's avatar
DV committed
198
    Serial.print(_child_id);
199
    Serial.print(F(" T="));
DV's avatar
DV committed
200
201
    Serial.println(_presentation);
  #endif
user2684's avatar
user2684 committed
202
  present(_child_id, _presentation,_description,_ack);
DV's avatar
DV committed
203
204
205
206
207
208
209
210
}

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

user2684's avatar
user2684 committed
211
212
213
214
215
216
// call the sensor-specific implementation of setup
void Sensor::setup() {
  if (_pin == -1) return;
  onSetup();
}

DV's avatar
DV committed
217
218
219
220
221
// 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
222
    if (_auto_power_pins) powerOn();
DV's avatar
DV committed
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
227
  if (_force_update > 0) _cycles++;
DV's avatar
DV committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  // 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
243
    if (_samples_interval > 0) wait(_samples_interval);
DV's avatar
DV committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  }
  // 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
278
    if (_auto_power_pins) powerOff();
DV's avatar
DV committed
279
280
281
282
283
284
285
  #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;
286
  // a request would make the sensor executing its main task passing along the message
DV's avatar
DV committed
287
288
289
290
291
292
293
  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
294
    // if configured, sleep beetween each send
295
    if (_sleep_between_send > 0) sleep(_sleep_between_send);
DV's avatar
DV committed
296
    #if DEBUG == 1
297
      Serial.print(F("SEND D="));
DV's avatar
DV committed
298
      Serial.print(message.destination);
299
      Serial.print(F(" I="));
DV's avatar
DV committed
300
      Serial.print(message.sensor);
301
      Serial.print(F(" C="));
DV's avatar
DV committed
302
      Serial.print(message.getCommand());
303
      Serial.print(F(" T="));
DV's avatar
DV committed
304
      Serial.print(message.type);
305
      Serial.print(F(" S="));
DV's avatar
DV committed
306
      Serial.print(message.getString());
307
      Serial.print(F(" I="));
DV's avatar
DV committed
308
      Serial.print(message.getInt());
309
      Serial.print(F(" F="));
DV's avatar
DV committed
310
311
      Serial.println(message.getFloat());
    #endif
user2684's avatar
user2684 committed
312
    send(message,_ack);
DV's avatar
DV committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  }
}

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

341
// what to do during before
DV's avatar
DV committed
342
343
344
345
346
void SensorAnalogInput::onBefore() {
  // prepare the pin for input
  pinMode(_pin, INPUT);
}

347
// what to do during setup
user2684's avatar
user2684 committed
348
349
350
void SensorAnalogInput::onSetup() {
}

351
// what to do during loop
DV's avatar
DV committed
352
353
354
355
356
357
358
void SensorAnalogInput::onLoop() {
  // read the input
  int adc = _getAnalogRead();
  // calculate the percentage
  int percentage = 0;
  if (_output_percentage) percentage = _getPercentage(adc);
  #if DEBUG == 1
359
    Serial.print(F("A-IN I="));
DV's avatar
DV committed
360
    Serial.print(_child_id);
361
    Serial.print(F(" V="));
DV's avatar
DV committed
362
    Serial.print(adc);
363
    Serial.print(F(" %="));
DV's avatar
DV committed
364
365
366
367
368
369
    Serial.println(percentage);
  #endif
  // store the result
  _value_int = _output_percentage ? percentage : adc;
}

370
// what to do during loop
DV's avatar
DV committed
371
void SensorAnalogInput::onReceive(const MyMessage & message) {
372
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
373
374
375
376
}

// read the analog input
int SensorAnalogInput::_getAnalogRead() {
377
378
379
380
381
382
383
  #ifndef MY_GATEWAY_ESP8266
    // set the reference
    if (_reference != -1) {
      analogReference(_reference);
      wait(100);
    }
  #endif
DV's avatar
DV committed
384
385
386
387
388
389
390
391
392
  // 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
393
394
  // restore the original value
  if (_reverse) value = 1024 - value;
DV's avatar
DV committed
395
396
  // scale the percentage based on the range provided
  float percentage = ((value - _range_min) / (_range_max - _range_min)) * 100;
DV's avatar
DV committed
397
  if (_reverse) percentage = 100 - percentage;
DV's avatar
DV committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  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
428
void SensorThermistor::setNominalResistor(long value) {
DV's avatar
DV committed
429
430
431
432
433
434
435
436
  _nominal_resistor = value;
}
void SensorThermistor::setNominalTemperature(int value) {
  _nominal_temperature = value;
}
void SensorThermistor::setBCoefficient(int value) {
  _b_coefficient = value;
}
437
void SensorThermistor::setSeriesResistor(long value) {
DV's avatar
DV committed
438
439
440
441
442
443
  _series_resistor = value;
}
void SensorThermistor::setOffset(float value) {
  _offset = value;
}

444
// what to do during before
DV's avatar
DV committed
445
446
447
448
449
void SensorThermistor::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

450
// what to do during setup
user2684's avatar
user2684 committed
451
452
453
void SensorThermistor::onSetup() {
}

454
// what to do during loop
DV's avatar
DV committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
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
470
    Serial.print(F("THER I="));
DV's avatar
DV committed
471
    Serial.print(_child_id);
472
    Serial.print(F(" V="));
DV's avatar
DV committed
473
    Serial.print(adc);
474
    Serial.print(F(" T="));
475
    Serial.print(temperature);
476
    Serial.print(F(" M="));
DV's avatar
DV committed
477
478
479
480
481
482
    Serial.println(getControllerConfig().isMetric);
  #endif
  // store the value
  _value_float = temperature;
}

483
// what to do as the main task when receiving a message
DV's avatar
DV committed
484
void SensorThermistor::onReceive(const MyMessage & message) {
485
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
486
487
}

488
489
490
491
492
493
494
495
496
497
498
499
500

/*
   SensorML8511
*/

// contructor
SensorML8511::SensorML8511(int child_id, int pin): Sensor(child_id, pin) {
  // set presentation, type and value type
  setPresentation(S_UV);
  setType(V_UV);
  setValueType(TYPE_FLOAT);
}

501
// what to do during before
502
503
504
505
506
void SensorML8511::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

507
// what to do during setup
508
509
510
void SensorML8511::onSetup() {
}

511
// what to do during loop
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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;
}

532
// what to do as the main task when receiving a message
533
void SensorML8511::onReceive(const MyMessage & message) {
534
  if (message.getCommand() == C_REQ) onLoop();
535
536
537
538
539
540
541
}

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

542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
   SensorACS712
*/

// contructor
SensorACS712::SensorACS712(int child_id, int pin): Sensor(child_id, pin) {
  // 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;
}

562
// what to do during before
563
564
565
566
567
void SensorACS712::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

568
// what to do during setup
569
570
571
void SensorACS712::onSetup() {
}

572
// what to do during loop
573
574
575
576
577
578
579
580
581
582
583
584
585
586
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
}

587
// what to do as the main task when receiving a message
588
589
590
591
void SensorACS712::onReceive(const MyMessage & message) {
  if (message.getCommand() == C_REQ) onLoop();
}

592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
   SensorRainGauge
*/

// contructor
SensorRainGauge::SensorRainGauge(int child_id, int pin): Sensor(child_id, pin) {
  // set presentation, type and value type
  setPresentation(S_RAIN);
  setType(V_RAIN);
  setValueType(TYPE_FLOAT);

}

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

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

// what to do when when receiving an interrupt
void SensorRainGauge::_onTipped() {
631
  long now = millis();
632
  // on tipping, two consecutive interrupts are received, ignore the second one
633
  if ( (now - _last_tip > 100) || (now < _last_tip) ){
634
635
636
637
638
639
    // increase the counter
    _count++;
    #if DEBUG == 1
      Serial.println(F("RAIN+"));
    #endif
  }
640
  _last_tip = now;
641
642
643
644
645
646
}

// what to do during loop
void SensorRainGauge::onLoop() {
  // avoid reporting the same value multiple times
  _value_float = -1;
647
  long now = millis();
648
  // time elapsed since the last report
649
  long elapsed = now - _last_report;
650
651
  // minimum time interval between reports
  long min_interval = ((long)_report_interval*1000)*60;
652
653
  // time to report or millis() reset
  if ( (elapsed > min_interval) || (now < _last_report)) {
654
655
656
657
658
659
660
661
662
663
    // report the total amount of rain for the last period
    _value_float = _count*_single_tip;
    #if DEBUG == 1
      Serial.print(F("RAIN I="));
      Serial.print(_child_id);
      Serial.print(F(" T="));
      Serial.println(_value_float);
    #endif
    // reset the counters
    _count = 0;
664
    _last_report = now;
665
666
667
668
669
670
671
672
673
674
675
  }
}

// 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
    _value_float = _count*_single_tip;    
  }
}

676

677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
 * SensorMQ
 */
SensorMQ::SensorMQ(int child_id, int pin): Sensor(child_id,pin) {
  setPresentation(S_AIR_QUALITY);
  setType(V_LEVEL);
}

//setter/getter
void SensorMQ::setRlValue(float value) {
  _rl_value = value;
}
void SensorMQ::setRoValue(float value) {
  _ro = value;
}
void SensorMQ::setCleanAirFactor(float value) {
  _ro_clean_air_factor = value;
}
void SensorMQ::setCalibrationSampleTimes(int value) {
  _calibration_sample_times = value;
}
void SensorMQ::setCalibrationSampleInterval(int value){
  _calibration_sample_interval = value;
}
void SensorMQ::setReadSampleTimes(int value) {
  _read_sample_times = value;
}
void SensorMQ::setReadSampleInterval(int value) {
  _read_sample_interval = value;
}
void SensorMQ::setLPGCurve(float *value) {
  _LPGCurve[0] = value[0];
  _LPGCurve[2] = value[1];
  _LPGCurve[2] = value[2];
}
void SensorMQ::setCOCurve(float *value) {
  _COCurve[0] = value[0];
  _COCurve[2] = value[1];
  _COCurve[2] = value[2];
}
void SensorMQ::setSmokeCurve(float *value) {
  _SmokeCurve[0] = value[0];
  _SmokeCurve[2] = value[1];
  _SmokeCurve[2] = value[2];
}

723
// what to do during before
724
725
726
727
728
void SensorMQ::onBefore() {
  // prepare the pin for input
  pinMode(_pin, INPUT);
}

729
// what to do during setup
730
731
732
733
void SensorMQ::onSetup() {
  _ro = _MQCalibration();
}

734
// what to do during loop
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
void SensorMQ::onLoop() {
  if (_pin == -1) return;
  // calculate rs/ro
  float mq = _MQRead()/_ro;
  // calculate the ppm
  float lpg = _MQGetGasPercentage(mq,_gas_lpg);
  float co = _MQGetGasPercentage(mq,_gas_co);
  float smoke = _MQGetGasPercentage(mq,_gas_smoke);
  // assign to the value the requested gas
  uint16_t value;
  if (_target_gas == _gas_lpg) value = lpg;
  if (_target_gas == _gas_co) value = co;
  if (_target_gas == _gas_smoke) value = smoke;
  #if DEBUG == 1
    Serial.print(F("MQ I="));
    Serial.print(_child_id);
    Serial.print(F(" V="));
    Serial.print(value);
    Serial.print(F(" LPG="));
    Serial.print(lpg);
    Serial.print(F(" CO="));
    Serial.print(co);
    Serial.print(F(" SMOKE="));
    Serial.println(smoke);
  #endif
  // store the value
  _value_int = (int16_t)ceil(value);
}

764
// what to do as the main task when receiving a message
765
void SensorMQ::onReceive(const MyMessage & message) {
766
  if (message.getCommand() == C_REQ) onLoop();
767
768
769
770
771
772
773
774
775
776
777
778
779
780
}

// returns the calculated sensor resistance
float SensorMQ::_MQResistanceCalculation(int raw_adc) {
  return ( ((float)_rl_value*(1023-raw_adc)/raw_adc));
}

//  This function assumes that the sensor is in clean air
float SensorMQ::_MQCalibration() {
  int i;
  float val=0;
  //take multiple samples
  for (i=0; i< _calibration_sample_times; i++) {  
    val += _MQResistanceCalculation(analogRead(_pin));
user2684's avatar
user2684 committed
781
    wait(_calibration_sample_interval);
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
  }
  //calculate the average value
  val = val/_calibration_sample_times;                   
  //divided by RO_CLEAN_AIR_FACTOR yields the Ro
  val = val/_ro_clean_air_factor;
  //according to the chart in the datasheet
  return val;
}

// This function use MQResistanceCalculation to caculate the sensor resistenc (Rs).
float SensorMQ::_MQRead() {
  int i;
  float rs=0;
  for (i=0; i<_read_sample_times; i++) {
    rs += _MQResistanceCalculation(analogRead(_pin));
user2684's avatar
user2684 committed
797
    wait(_read_sample_interval);
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
  }
  rs = rs/_read_sample_times;
  return rs;
}

// This function passes different curves to the MQGetPercentage function which calculates the ppm (parts per million) of the target gas.
int SensorMQ::_MQGetGasPercentage(float rs_ro_ratio, int gas_id) {
  if ( gas_id == _gas_lpg ) {
    return _MQGetPercentage(rs_ro_ratio,_LPGCurve);
  } else if ( gas_id == _gas_co) {
    return _MQGetPercentage(rs_ro_ratio,_COCurve);
  } else if ( gas_id == _gas_smoke) {
    return _MQGetPercentage(rs_ro_ratio,_SmokeCurve);
  }
  return 0;
}

// returns ppm of the target gas
int SensorMQ::_MQGetPercentage(float rs_ro_ratio, float *pcurve) {
  return (pow(10,( ((log10(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}


DV's avatar
DV committed
821
822
823
824
825
826
827
828
/*
   SensorDigitalInput
*/

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

829
// what to do during before
DV's avatar
DV committed
830
831
832
833
834
void SensorDigitalInput::onBefore() {
  // set the pin for input
  pinMode(_pin, INPUT);
}

835
// what to do during setup
user2684's avatar
user2684 committed
836
837
838
void SensorDigitalInput::onSetup() {
}

839
// what to do during loop
DV's avatar
DV committed
840
841
842
843
void SensorDigitalInput::onLoop() {
  // read the value
  int value = digitalRead(_pin);
  #if DEBUG == 1
844
    Serial.print(F("D-IN I="));
DV's avatar
DV committed
845
    Serial.print(_child_id);
846
    Serial.print(F(" P="));
DV's avatar
DV committed
847
    Serial.print(_pin);
848
    Serial.print(F(" V="));
DV's avatar
DV committed
849
850
851
852
853
854
    Serial.println(value);
  #endif
  // store the value
  _value_int = value;
}

855
// what to do as the main task when receiving a message
DV's avatar
DV committed
856
void SensorDigitalInput::onReceive(const MyMessage & message) {
857
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
858
859
860
861
862
863
864
865
866
867
868
}


/*
   SensorDigitalOutput
*/

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

869
// what to do during before
DV's avatar
DV committed
870
871
872
void SensorDigitalOutput::onBefore() {
  // set the pin as output and initialize it accordingly
  pinMode(_pin, OUTPUT);
873
874
  _state = _initial_value == LOW ? LOW : HIGH;
  digitalWrite(_pin, _state);
DV's avatar
DV committed
875
876
877
878
  // the initial value is now the current value
  _value_int = _initial_value;
}

879
// what to do during setup
user2684's avatar
user2684 committed
880
881
882
void SensorDigitalOutput::onSetup() {
}

DV's avatar
DV committed
883
884
885
886
887
888
889
// setter/getter
void SensorDigitalOutput::setInitialValue(int value) {
  _initial_value = value;
}
void SensorDigitalOutput::setPulseWidth(int value) {
  _pulse_width = value;
}
890
891
892
void SensorDigitalOutput::setOnValue(int value) {
  _on_value = value;
}
893
894
895
void SensorDigitalOutput::setLegacyMode(bool value) {
  _legacy_mode = value;
}
DV's avatar
DV committed
896
897
898
899
900
901

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

902
// what to do as the main task when receiving a message
DV's avatar
DV committed
903
void SensorDigitalOutput::onReceive(const MyMessage & message) {
904
905
  // 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)) {
906
907
908
909
910
911
912
913
914
915
916
917
918
    // retrieve from the message the value to set
    int value = message.getInt();
    if (value != 0 && value != 1) return;
    #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
919
920
921
922
923
924
    // 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;
    }
925
    // set the value
926
    digitalWrite(_pin, value_to_write);
927
928
929
    if (_pulse_width > 0) {
      // if this is a pulse output, restore the value to the original value after the pulse
      wait(_pulse_width);
930
      digitalWrite(_pin, value_to_write == 0 ? HIGH: LOW);
931
932
    }
    // store the current value so it will be sent to the controller
933
    _state = value;
934
935
    _value_int = value;
  }
936
  if (message.getCommand() == C_REQ && ! _legacy_mode) {
937
938
939
    // return the current status
    _value_int = _state;
  }
DV's avatar
DV committed
940
941
942
943
944
945
946
947
948
949
950
951
952
}

/*
   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
953
954
955
956
957
958
// 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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/*
   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;
  _sensor_type = sensor_type;
  _dht_type = dht_type;
979
  if (_sensor_type == SensorDHT::TEMPERATURE) {
DV's avatar
DV committed
980
981
982
983
984
    // temperature sensor
    setPresentation(S_TEMP);
    setType(V_TEMP);
    setValueType(TYPE_FLOAT);
  }
985
  else if (_sensor_type == SensorDHT::HUMIDITY) {
DV's avatar
DV committed
986
987
988
989
990
991
992
    // humidity sensor
    setPresentation(S_HUM);
    setType(V_HUM);
    setValueType(TYPE_FLOAT);
  }
}

993
// what to do during before
DV's avatar
DV committed
994
995
996
997
998
void SensorDHT::onBefore() {
    // initialize the dht library
    _dht->begin();
}

999
// what to do during setup
user2684's avatar
user2684 committed
1000
1001
1002
void SensorDHT::onSetup() {
}

1003
// what to do during loop
DV's avatar
DV committed
1004
1005
void SensorDHT::onLoop() {
  // temperature sensor
1006
  if (_sensor_type == SensorDHT::TEMPERATURE) {
DV's avatar
DV committed
1007
1008
1009
1010
1011
    // read the temperature
    float temperature = _dht->readTemperature();
    // convert it
    if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
    #if DEBUG == 1
1012
      Serial.print(F("DHT I="));
DV's avatar
DV committed
1013
      Serial.print(_child_id);
1014
      Serial.print(F(" T="));
DV's avatar
DV committed
1015
1016
1017
1018
1019
1020
      Serial.println(temperature);
    #endif
    // store the value
    if (! isnan(temperature)) _value_float = temperature;
  }
  // humidity sensor
1021
  else if (_sensor_type == SensorDHT::HUMIDITY) {
DV's avatar
DV committed
1022
1023
1024
1025
    // read humidity
    float humidity = _dht->readHumidity();
    if (isnan(humidity)) return;
    #if DEBUG == 1
1026
      Serial.print(F("DHT I="));
DV's avatar
DV committed
1027
      Serial.print(_child_id);
1028
      Serial.print(F(" H="));
DV's avatar
DV committed
1029
1030
1031
1032
1033
1034
1035
      Serial.println(humidity);
    #endif
    // store the value
    if (! isnan(humidity)) _value_float = humidity;
  }
}

1036
// what to do as the main task when receiving a message
DV's avatar
DV committed
1037
void SensorDHT::onReceive(const MyMessage & message) {
1038
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
1039
1040
1041
1042
1043
1044
1045
1046
}
#endif

/*
   SensorSHT21
*/
#if MODULE_SHT21 == 1
// contructor
DV's avatar
DV committed
1047
SensorSHT21::SensorSHT21(int child_id, int sensor_type): Sensor(child_id,A2) {
DV's avatar
DV committed
1048
1049
  // store the sensor type (0: temperature, 1: humidity)
  _sensor_type = sensor_type;
1050
  if (_sensor_type == SensorSHT21::TEMPERATURE) {
DV's avatar
DV committed
1051
1052
1053
1054
1055
    // temperature sensor
    setPresentation(S_TEMP);
    setType(V_TEMP);
    setValueType(TYPE_FLOAT);
  }
1056
  else if (_sensor_type == SensorSHT21::HUMIDITY) {
DV's avatar
DV committed
1057
1058
1059
1060
1061
1062
1063
    // humidity sensor
    setPresentation(S_HUM);
    setType(V_HUM);
    setValueType(TYPE_FLOAT);
  }
}

1064
// what to do during before
DV's avatar
DV committed
1065
1066
1067
1068
1069
void SensorSHT21::onBefore() {
  // initialize the library
  Wire.begin();
}

1070
// what to do during setup
user2684's avatar
user2684 committed
1071
1072
1073
void SensorSHT21::onSetup() {
}

1074
// what to do during loop
DV's avatar
DV committed
1075
1076
void SensorSHT21::onLoop() {
  // temperature sensor
1077
  if (_sensor_type == SensorSHT21::TEMPERATURE) {
DV's avatar
DV committed
1078
1079
1080
1081
1082
    // read the temperature
    float temperature = SHT2x.GetTemperature();
    // convert it
    if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
    #if DEBUG == 1
1083
      Serial.print(F("SHT I="));
DV's avatar
DV committed
1084
      Serial.print(_child_id);
1085
      Serial.print(F(" T="));
DV's avatar
DV committed
1086
1087
1088
1089
1090
1091
      Serial.println(temperature);
    #endif
    // store the value
    if (! isnan(temperature)) _value_float = temperature;
  }
  // Humidity Sensor
1092
  else if (_sensor_type == SensorSHT21::HUMIDITY) {
DV's avatar
DV committed
1093
1094
1095
1096
    // read humidity
    float humidity = SHT2x.GetHumidity();
    if (isnan(humidity)) return;
    #if DEBUG == 1
1097
      Serial.print(F("SHT I="));
DV's avatar
DV committed
1098
      Serial.print(_child_id);
1099
      Serial.print(F(" H="));
DV's avatar
DV committed
1100
1101
1102
1103
1104
1105
1106
      Serial.println(humidity);
    #endif
    // store the value
    if (! isnan(humidity)) _value_float = humidity;
  }
}

1107
// what to do as the main task when receiving a message
DV's avatar
DV committed
1108
void SensorSHT21::onReceive(const MyMessage & message) {
1109
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
1110
1111
1112
}
#endif

DV's avatar
DV committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
/*
 * SensorHTU21D
 */
 #if MODULE_SHT21 == 1
// constructor
SensorHTU21D::SensorHTU21D(int child_id, int pin): SensorSHT21(child_id, pin) {
}
#endif 

DV's avatar
DV committed
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
/*
 * 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;
}
1142
1143
1144
1145
1146
1147
void SensorSwitch::setInitial(int value) {
  _initial = value;
}
int SensorSwitch::getInitial() {
  return _initial;
}
DV's avatar
DV committed
1148

1149
// what to do during before
DV's avatar
DV committed
1150
1151
1152
1153
1154
1155
void SensorSwitch::onBefore() {
  // initialize the value
  if (_mode == RISING) _value_int = LOW;
  else if (_mode == FALLING) _value_int = HIGH;
}

1156
// what to do during setup
user2684's avatar
user2684 committed
1157
1158
1159
void SensorSwitch::onSetup() {
}

1160
// what to do during loop
DV's avatar
DV committed
1161
1162
void SensorSwitch::onLoop() {
  // wait to ensure the the input is not floating
1163
  if (_debounce > 0) wait(_debounce);
DV's avatar
DV committed
1164
1165
1166
1167
1168
  // 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
1169
      Serial.print(F("SWITCH I="));
DV's avatar
DV committed
1170
      Serial.print(_child_id);
1171
      Serial.print(F(" P="));
DV's avatar
DV committed
1172
      Serial.print(_pin);
1173
      Serial.print(F(" V="));
DV's avatar
DV committed
1174
1175
1176
1177
      Serial.println(value);
    #endif
    _value_int = value;
    // allow the signal to be restored to its normal value
1178
    if (_trigger_time > 0) wait(_trigger_time);
DV's avatar
DV committed
1179
1180
1181
1182
1183
  } else {
    // invalid
    _value_int = -1;
  }
}
1184
// what to do as the main task when receiving a message
DV's avatar
DV committed
1185
void SensorSwitch::onReceive(const MyMessage & message) {
1186
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
}

/*
 * 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);
1203
1204
  // set initial value to LOW
  setInitial(LOW);
DV's avatar
DV committed
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
}

/*
   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;
user2684's avatar
user2684 committed
1218
1219
  // retrieve and store the address from the index
  _sensors->getAddress(_device_address, index);
DV's avatar
DV committed
1220
1221
}

1222
// what to do during before
DV's avatar
DV committed
1223
1224
1225
void SensorDs18b20::onBefore() {
}

1226
// what to do during setup
user2684's avatar
user2684 committed
1227
1228
1229
void SensorDs18b20::onSetup() {
}

1230
// what to do during loop
DV's avatar
DV committed
1231
void SensorDs18b20::onLoop() {
1232
1233
  // do not wait for conversion, will sleep manually during it
  if (_sleep_during_conversion) _sensors->setWaitForConversion(false);
DV's avatar
DV committed
1234
  // request the temperature
user2684's avatar
user2684 committed
1235
  _sensors->requestTemperatures();
1236
1237
1238
1239
1240
  if (_sleep_during_conversion) {
    // calculate conversion time and sleep
    int16_t conversion_time = _sensors->millisToWaitForConversion(_sensors->getResolution());
    sleep(conversion_time);
  }
DV's avatar
DV committed
1241
1242
1243
1244
1245
  // read the temperature
  float temperature = _sensors->getTempCByIndex(_index);
  // convert it
  if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
  #if DEBUG == 1
user2684's avatar
user2684 committed
1246
    Serial.print(F("DS18B20 I="));
DV's avatar
DV committed
1247
    Serial.print(_child_id);
1248
    Serial.print(F(" T="));
DV's avatar
DV committed
1249
1250
1251
1252
1253
1254
    Serial.println(temperature);
  #endif
  // store the value
  _value_float = temperature;
}

1255
// what to do as the main task when receiving a message
DV's avatar
DV committed
1256
void SensorDs18b20::onReceive(const MyMessage & message) {
1257
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
1258
}
user2684's avatar
user2684 committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274

// function to print a device address
DeviceAddress* SensorDs18b20::getDeviceAddress() {
  return &_device_address;
}

// returns the sensor's resolution in bits
int SensorDs18b20::getResolution() {
  return _sensors->getResolution(_device_address);
}

// set the sensor's resolution in bits
void SensorDs18b20::setResolution(int value) {
   _sensors->setResolution(_device_address, value);
}

1275
1276
1277
1278
1279
// sleep while DS18B20 calculates temperature
void SensorDs18b20::setSleepDuringConversion(bool value) {
   _sleep_during_conversion = value;
}

DV's avatar
DV committed
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
#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();
}

1293
// what to do during before
DV's avatar
DV committed
1294
1295
1296
1297
void SensorBH1750::onBefore() {
  _lightSensor->begin();
}

1298
// what to do during setup
user2684's avatar
user2684 committed
1299
1300
1301
void SensorBH1750::onSetup() {
}

1302
// what to do during loop
DV's avatar
DV committed
1303
1304
1305
1306
void SensorBH1750::onLoop() {
  // request the light level
  _value_int = _lightSensor->readLightLevel();
  #if DEBUG == 1
1307
    Serial.print(F("BH1 I="));
DV's avatar
DV committed
1308
    Serial.print(_child_id);
1309
    Serial.print(F(" L="));
DV's avatar
DV committed
1310
1311
1312
1313
    Serial.println(_value_int);
  #endif
}

1314
// what to do as the main task when receiving a message
DV's avatar
DV committed
1315
void SensorBH1750::onReceive(const MyMessage & message) {
1316
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
}
#endif

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

1334
// what to do during before
DV's avatar
DV committed
1335
1336
1337
1338
1339
void SensorMLX90614::onBefore() {
  // initialize the library
  _mlx->begin();
}

1340
// what to do during setup
user2684's avatar
user2684 committed
1341
1342
1343
void SensorMLX90614::onSetup() {
}

1344
// what to do during loop
DV's avatar
DV committed
1345
void SensorMLX90614::onLoop() {
1346
  float temperature = _sensor_type == SensorMLX90614::TEMPERATURE_OBJECT ? _mlx->readAmbientTempC() : _mlx->readObjectTempC();
DV's avatar
DV committed
1347
1348
1349
  // convert it
  if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
  #if DEBUG == 1
1350
    Serial.print(F("MLX I="));
DV's avatar
DV committed
1351
    Serial.print(_child_id);
1352
    Serial.print(F(" T="));
DV's avatar
DV committed
1353
1354
1355
1356
    Serial.println(temperature);
  #endif
  if (! isnan(temperature)) _value_float = temperature;
}
DV's avatar
DV committed
1357

1358
// what to do as the main task when receiving a message
DV's avatar
DV committed
1359
void SensorMLX90614::onReceive(const MyMessage & message) {
1360
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
1361
}
DV's avatar
DV committed
1362
1363
#endif

1364
1365

/*
1366
   SensorBosch
1367
*/
1368
#if MODULE_BME280 == 1 || MODULE_BMP085 == 1
1369
// contructor
1370
SensorBosch::SensorBosch(int child_id, int sensor_type): Sensor(child_id,A4) {
1371
  _sensor_type = sensor_type;
1372
  if (_sensor_type == SensorBosch::TEMPERATURE) {
1373
1374
1375
1376
1377
    // temperature sensor
    setPresentation(S_TEMP);
    setType(V_TEMP);
    setValueType(TYPE_FLOAT);
  }
1378
  else if (_sensor_type == SensorBosch::HUMIDITY) {
1379
1380
1381
1382
1383
    // humidity sensor
    setPresentation(S_HUM);
    setType(V_HUM);
    setValueType(TYPE_FLOAT);
  }
1384
  else if (_sensor_type == SensorBosch::PRESSURE) {
1385
1386
1387
1388
1389
    // pressure sensor
    setPresentation(S_BARO);
    setType(V_PRESSURE);
    setValueType(TYPE_FLOAT);
  }
1390
  else if (_sensor_type == SensorBosch::FORECAST) {
user2684's avatar
user2684 committed
1391
1392
1393
1394
1395
1396
1397
1398
    // pressure sensor
    setPresentation(S_BARO);
    setType(V_FORECAST);
    setValueType(TYPE_STRING);
  }
}

// setter/getter
1399
void SensorBosch::setForecastSamplesCount(int value) {