NodeManager.cpp 118 KB
Newer Older
DV's avatar
DV committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * NodeManager
 */

#include "NodeManager.h"

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

// set the vcc and ground pin the sensor is connected to
user2684's avatar
user2684 committed
12
void PowerManager::setPowerPins(int ground_pin, int vcc_pin, int wait_time) {
13
14
  _ground_pin = ground_pin;
  _vcc_pin = vcc_pin;
DV's avatar
DV committed
15
  #if DEBUG == 1
16
    Serial.print(F("PWR G="));
17
    Serial.print(_ground_pin);
18
    Serial.print(F(" V="));
19
    Serial.println(_vcc_pin);
DV's avatar
DV committed
20
  #endif
21
22
23
24
25
26
27
28
29
30
31
  if (_ground_pin > 0) {
    // configure the ground pin as output and initialize to low
    pinMode(_ground_pin, OUTPUT);
    digitalWrite(_ground_pin, LOW);
  }
  if (_vcc_pin > 0) {
    // configure the vcc pin as output and initialize to high (power on)
    pinMode(_vcc_pin, OUTPUT);
    digitalWrite(_vcc_pin, HIGH);
  }
  // save wait time
user2684's avatar
user2684 committed
32
  _wait = wait_time;
DV's avatar
DV committed
33
34
35
36
37
}


// turn on the sensor by activating its power pins
void PowerManager::powerOn() {
38
  if (_vcc_pin == -1) return;
DV's avatar
DV committed
39
  #if DEBUG == 1
40
    Serial.print(F("ON P="));
DV's avatar
DV committed
41
42
43
44
45
    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
46
  if (_wait > 0) wait(_wait);
DV's avatar
DV committed
47
48
49
50
}

// turn off the sensor
void PowerManager::powerOff() {
51
  if (_vcc_pin == -1) return;
DV's avatar
DV committed
52
  #if DEBUG == 1
53
    Serial.print(F("OFF P="));
DV's avatar
DV committed
54
55
56
57
58
59
    Serial.println(_vcc_pin);
  #endif
  // power off the sensor by turning low the vcc pin
  digitalWrite(_vcc_pin, LOW);
}

60
61
62
63
64
65
66
67
68
/******************************************
    Timer
*/

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

// start the timer
69
void Timer::start(int target, int unit) {
70
71
72
73
74
75
76
77
78
79
80
81
  set(target,unit);
  start();
}
void Timer::start() {
  if (_is_configured) _is_running = true;
}

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

82
83
// reset the timer
void Timer::reset() {
84
85
86
  // reset the timer
  _elapsed = 0;
  _last_millis = 0;
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
}

// restart the timer
void Timer::restart() {
  if (! isRunning()) return;
  stop();
  reset();
  // if using millis(), keep track of the current timestamp for calculating the difference
  if (! _node_manager->isSleepingNode()) _last_millis = millis();
  start();
}

// setup the timer
void Timer::set(int target, int unit) {
  reset();
102
103
  // save the settings
  _target = target;
104
105
106
107
108
109
110
111
112
113
  if (unit == MINUTES) _target = _target * 60;
  else if (unit == HOURS) _target = _target * 60 *60;
  else if (unit == DAYS) _target = _target * 60 * 60 *24;
  _is_running = false;
  _is_configured = true;
}

// unset the timer
void Timer::unset() {
  stop();
114
115
116
117
118
119
  _is_configured = true;
}

// update the timer at every cycle
void Timer::update() {
  if (! isRunning()) return;
120
121
122
123
124
125
  if (_node_manager->isSleepingNode()) {
    // millis() is not reliable while sleeping so calculate how long a sleep cycle would last in seconds and update the elapsed time
    _elapsed += _node_manager->getSleepSeconds();
  } else {
    // use millis() to calculate the elapsed time in seconds
    _elapsed = (long)((millis() - _last_millis)/1000);
126
  }
127
  _first_run = false;
128
129
130
131
132
133
134
135
136
137
138
139
140
141
}

// 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() {
142
  if (! isConfigured()) return false;
143
144
145
146
147
148
149
150
  return _is_running;
}

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

151
152
153
// return true if this is the first time the timer runs
bool Timer::isFirstRun() {
  return _first_run;
154
155
}

156
// return elapsed seconds so far
157
158
159
160
float Timer::getElapsed() {
  return _elapsed;
}

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
197
198
199
200
201
202
203
204
205
206
207

/******************************************
    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
208
209
210
211
212
213
214
215
/******************************************
    Sensors
*/

/*
   Sensor class
*/
// constructor
216
217
Sensor::Sensor(NodeManager* node_manager, int child_id, int pin) {
  _node_manager = node_manager;
DV's avatar
DV committed
218
219
220
  _child_id = child_id;
  _pin = pin;
  _msg = MyMessage(_child_id, _type);
221
  _msg_service = MyMessage(_child_id, V_CUSTOM);
222
223
  _report_timer = new Timer(_node_manager);
  _force_update_timer = new Timer(_node_manager);
DV's avatar
DV committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
}

// 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
252
253
254
void Sensor::setDescription(char* value) {
  _description = value;
}
DV's avatar
DV committed
255
256
257
258
259
260
void Sensor::setSamples(int value) {
  _samples = value;
}
void Sensor::setSamplesInterval(int value) {
  _samples_interval = value;
}
261
void Sensor::setTrackLastValue(bool value) {
DV's avatar
DV committed
262
263
  _track_last_value = value;
}
264
265
void Sensor::setForceUpdateMinutes(int value) {
  _force_update_timer->start(value,MINUTES);
DV's avatar
DV committed
266
}
267
268
269
void Sensor::setForceUpdateHours(int value) {
  _force_update_timer->start(value,HOURS);
}
DV's avatar
DV committed
270
271
272
void Sensor::setValueType(int value) {
  _value_type = value;
}
273
274
275
int Sensor::getValueType() {
  return _value_type;
}
DV's avatar
DV committed
276
277
278
void Sensor::setFloatPrecision(int value) {
  _float_precision = value;
}
279
280
281
void Sensor::setDoublePrecision(int value) {
  _double_precision = value;
}
DV's avatar
DV committed
282
#if POWER_MANAGER == 1
user2684's avatar
user2684 committed
283
284
    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
285
    }
DV's avatar
DV committed
286
287
288
    void Sensor::setAutoPowerPins(bool value) {
      _auto_power_pins = value;
    }
DV's avatar
DV committed
289
290
291
292
293
294
295
    void Sensor::powerOn() {
      _powerManager.powerOn();
    }
    void Sensor::powerOff() {
      _powerManager.powerOff();
    }
#endif
296
297
298
int Sensor::getInterruptPin() {
  return _interrupt_pin;
}
299
300
301
302
303
304
305
306
307
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
308

user2684's avatar
user2684 committed
309
310
311
312
313
// After how many seconds the sensor will report back its measure
void Sensor::setReportIntervalSeconds(int value) {
  _report_timer->start(value,SECONDS);
}

314
// After how many minutes the sensor will report back its measure 
315
316
317
318
void Sensor::setReportIntervalMinutes(int value) {
  _report_timer->start(value,MINUTES);
}

user2684's avatar
user2684 committed
319
320
321
322
323
324
325
326
// After how many minutes the sensor will report back its measure 
void Sensor::setReportIntervalHours(int value) {
  _report_timer->start(value,HOURS);
}

// After how many minutes the sensor will report back its measure 
void Sensor::setReportIntervalDays(int value) {
  _report_timer->start(value,DAYS);
327
328
}

user2684's avatar
user2684 committed
329

330
331
332
333
334
// return true if the report interval has been already configured
bool Sensor::isReportIntervalConfigured() {
  return _report_timer->isConfigured();
}

335
336
337
338
339
340
// listen for interrupts on the given pin so interrupt() will be called when occurring
void Sensor::setInterrupt(int pin, int mode, int initial) {
  _interrupt_pin = pin;
  _node_manager->setInterrupt(pin,mode,initial);
}

DV's avatar
DV committed
341
342
343
// present the sensor to the gateway and controller
void Sensor::presentation() {
  #if DEBUG == 1
344
    Serial.print(F("PRES I="));
DV's avatar
DV committed
345
    Serial.print(_child_id);
346
    Serial.print(F(" T="));
DV's avatar
DV committed
347
348
    Serial.println(_presentation);
  #endif
349
  present(_child_id, _presentation,_description,_node_manager->getAck());
DV's avatar
DV committed
350
351
352
353
354
355
356
357
}

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

user2684's avatar
user2684 committed
358
359
360
361
362
363
// call the sensor-specific implementation of setup
void Sensor::setup() {
  if (_pin == -1) return;
  onSetup();
}

DV's avatar
DV committed
364
365
366
// call the sensor-specific implementation of loop
void Sensor::loop(const MyMessage & message) {
  if (_pin == -1) return;
367
368
369
  // update the timers if within a loop cycle
  if (! _isReceive(message)) {
    if (_report_timer->isRunning()) {
370
371
      // store the elapsed time before updating it
      bool first_run = _report_timer->isFirstRun();
372
373
      // update the timer
      _report_timer->update();
374
375
      // if it is not the time yet to report a new measure, just return (unless the first time)
      if (! _report_timer->isOver() && ! first_run) return;
376
377
378
    }
    if (_force_update_timer->isRunning()) _force_update_timer->update();
  }
DV's avatar
DV committed
379
380
  #if POWER_MANAGER == 1
    // turn the sensor on
DV's avatar
DV committed
381
    if (_auto_power_pins) powerOn();
DV's avatar
DV committed
382
383
  #endif
  // for numeric sensor requiring multiple samples, keep track of the total
384
  double total = 0;
DV's avatar
DV committed
385
386
387
  // 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
388
    if (_isReceive(message)) {
DV's avatar
DV committed
389
390
391
      // we've been called from receive(), pass the message along
      onReceive(message);
    }
392
393
394
395
    else {
      // we'be been called from loop()
      onLoop();
    }
396
    // for integers, floats and doubles, keep track of the total
DV's avatar
DV committed
397
398
    if (_value_type == TYPE_INTEGER) total += (float)_value_int;
    else if (_value_type == TYPE_FLOAT) total += _value_float;
399
    else if (_value_type == TYPE_DOUBLE) total += _value_double;
DV's avatar
DV committed
400
    // wait between samples
401
    if (_samples_interval > 0) _node_manager->sleepOrWait(_samples_interval);
DV's avatar
DV committed
402
  }
403
  // process the result and send a response back
DV's avatar
DV committed
404
405
406
407
  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
408
    if (_isReceive(message) || _isWorthSending(avg != _last_value_int))  {
DV's avatar
DV committed
409
410
      _last_value_int = avg;
      _send(_msg.set(avg));
411
      _value_int = -1;
DV's avatar
DV committed
412
413
414
415
416
417
    }
  }
  // process a float value
  else if (_value_type == TYPE_FLOAT && total > -1) {
    // calculate the average value of the samples
    float avg = total / _samples;
418
419
    // report the value back
    if (_isReceive(message) || _isWorthSending(avg != _last_value_float))  {
DV's avatar
DV committed
420
421
      _last_value_float = avg;
      _send(_msg.set(avg, _float_precision));
422
      _value_float = -1;
DV's avatar
DV committed
423
424
    }
  }
425
426
427
428
429
430
431
432
433
434
435
  // process a double value
  else if (_value_type == TYPE_DOUBLE && total > -1) {
    // calculate the average value of the samples
    double avg = total / _samples;
    // report the value back
    if (_isReceive(message) || _isWorthSending(avg != _last_value_double))  {
      _last_value_double = avg;
      _send(_msg.set(avg, _double_precision));
      _value_double = -1;
    }
  }
DV's avatar
DV committed
436
437
438
  // 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
      _last_value_string = _value_string;
      _send(_msg.set(_value_string));
442
      _value_string = "";
DV's avatar
DV committed
443
444
445
446
    }
  }
  // turn the sensor off
  #if POWER_MANAGER == 1
DV's avatar
DV committed
447
    if (_auto_power_pins) powerOff();
DV's avatar
DV committed
448
  #endif
449
450
  // restart the report timer if over
  if (! _isReceive(message) && _report_timer->isRunning() && _report_timer->isOver()) _report_timer->restart();
DV's avatar
DV committed
451
452
}

453
454
455
456
457
458
// receive and handle an interrupt
void Sensor::interrupt() {
  // call the implementation of onInterrupt()
  onInterrupt();
}

DV's avatar
DV committed
459
460
461
// receive a message from the radio network
void Sensor::receive(const MyMessage &message) {
  // return if not for this sensor
462
463
464
465
466
467
468
469
470
471
472
473
474
  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;
475
  // a request would make the sensor executing its main task passing along the message
DV's avatar
DV committed
476
477
478
  loop(message);
}

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 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 16: setReportIntervalMinutes(request.getValueInt()); break;
499
    case 17: setReportIntervalSeconds(request.getValueInt()); break;
user2684's avatar
user2684 committed
500
501
    case 19: setReportIntervalHours(request.getValueInt()); break;
    case 20: setReportIntervalDays(request.getValueInt()); break;
502
    case 18: setForceUpdateHours(request.getValueInt()); break;
503
    case 21: setDoublePrecision(request.getValueInt()); break;
504
505
506
507
508
    default: return;
  }
  _send(_msg_service.set(function));
}

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

535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// 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;
}

556
#if MODULE_ANALOG_INPUT == 1
DV's avatar
DV committed
557
558
559
560
561
/*
   SensorAnalogInput
*/

// contructor
562
SensorAnalogInput::SensorAnalogInput(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager, child_id, pin) {
DV's avatar
DV committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
}

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

582
// what to do during before
DV's avatar
DV committed
583
584
585
586
587
void SensorAnalogInput::onBefore() {
  // prepare the pin for input
  pinMode(_pin, INPUT);
}

588
// what to do during setup
user2684's avatar
user2684 committed
589
590
591
void SensorAnalogInput::onSetup() {
}

592
// what to do during loop
DV's avatar
DV committed
593
594
595
596
597
598
599
void SensorAnalogInput::onLoop() {
  // read the input
  int adc = _getAnalogRead();
  // calculate the percentage
  int percentage = 0;
  if (_output_percentage) percentage = _getPercentage(adc);
  #if DEBUG == 1
600
    Serial.print(F("A-IN I="));
DV's avatar
DV committed
601
    Serial.print(_child_id);
602
    Serial.print(F(" V="));
DV's avatar
DV committed
603
    Serial.print(adc);
604
    Serial.print(F(" %="));
DV's avatar
DV committed
605
606
607
608
609
610
    Serial.println(percentage);
  #endif
  // store the result
  _value_int = _output_percentage ? percentage : adc;
}

611
// what to do during loop
DV's avatar
DV committed
612
void SensorAnalogInput::onReceive(const MyMessage & message) {
613
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
614
615
}

616
617
618
619
620
621
622
623
624
625
626
627
628
629
// 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));
}

630
631
632
633
// what to do when receiving an interrupt
void SensorAnalogInput::onInterrupt() {
}

DV's avatar
DV committed
634
635
// read the analog input
int SensorAnalogInput::_getAnalogRead() {
636
637
638
639
640
641
642
  #ifndef MY_GATEWAY_ESP8266
    // set the reference
    if (_reference != -1) {
      analogReference(_reference);
      wait(100);
    }
  #endif
DV's avatar
DV committed
643
644
645
646
647
648
649
650
651
  // 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
652
653
  // restore the original value
  if (_reverse) value = 1024 - value;
DV's avatar
DV committed
654
655
  // scale the percentage based on the range provided
  float percentage = ((value - _range_min) / (_range_max - _range_min)) * 100;
DV's avatar
DV committed
656
  if (_reverse) percentage = 100 - percentage;
DV's avatar
DV committed
657
658
659
660
661
662
663
664
665
666
  if (percentage > 100) percentage = 100;
  if (percentage < 0) percentage = 0;
  return (int)percentage;
}

/*
   SensorLDR
*/

// contructor
667
SensorLDR::SensorLDR(NodeManager* node_manager, int child_id, int pin): SensorAnalogInput(node_manager, child_id, pin) {
DV's avatar
DV committed
668
669
670
671
672
673
674
675
676
677
678
  // set presentation and type and reverse (0: no light, 100: max light)
  setPresentation(S_LIGHT_LEVEL);
  setType(V_LIGHT_LEVEL);
  setReverse(true);
}

/*
   SensorThermistor
*/

// contructor
679
SensorThermistor::SensorThermistor(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager, child_id, pin) {
DV's avatar
DV committed
680
681
682
683
684
685
686
  // set presentation, type and value type
  setPresentation(S_TEMP);
  setType(V_TEMP);
  setValueType(TYPE_FLOAT);
}

// setter/getter
687
void SensorThermistor::setNominalResistor(long value) {
DV's avatar
DV committed
688
689
690
691
692
693
694
695
  _nominal_resistor = value;
}
void SensorThermistor::setNominalTemperature(int value) {
  _nominal_temperature = value;
}
void SensorThermistor::setBCoefficient(int value) {
  _b_coefficient = value;
}
696
void SensorThermistor::setSeriesResistor(long value) {
DV's avatar
DV committed
697
698
699
700
701
702
  _series_resistor = value;
}
void SensorThermistor::setOffset(float value) {
  _offset = value;
}

703
// what to do during before
DV's avatar
DV committed
704
705
706
707
708
void SensorThermistor::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

709
// what to do during setup
user2684's avatar
user2684 committed
710
711
712
void SensorThermistor::onSetup() {
}

713
// what to do during loop
DV's avatar
DV committed
714
715
716
717
718
719
720
721
722
723
724
725
726
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
727
  temperature = _node_manager->celsiusToFahrenheit(temperature);
DV's avatar
DV committed
728
  #if DEBUG == 1
729
    Serial.print(F("THER I="));
DV's avatar
DV committed
730
    Serial.print(_child_id);
731
    Serial.print(F(" V="));
DV's avatar
DV committed
732
    Serial.print(adc);
733
    Serial.print(F(" T="));
734
    Serial.println(temperature);
DV's avatar
DV committed
735
736
737
738
739
  #endif
  // store the value
  _value_float = temperature;
}

740
// what to do as the main task when receiving a message
DV's avatar
DV committed
741
void SensorThermistor::onReceive(const MyMessage & message) {
742
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
743
744
}

745
746
747
748
749
750
751
752
753
754
755
756
757
// 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));
}
758

759
760
761
762
// what to do when receiving an interrupt
void SensorThermistor::onInterrupt() {
}

763
764
765
766
767
/*
   SensorML8511
*/

// contructor
768
SensorML8511::SensorML8511(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager, child_id, pin) {
769
770
771
772
773
774
  // set presentation, type and value type
  setPresentation(S_UV);
  setType(V_UV);
  setValueType(TYPE_FLOAT);
}

775
// what to do during before
776
777
778
779
780
void SensorML8511::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

781
// what to do during setup
782
783
784
void SensorML8511::onSetup() {
}

785
// what to do during loop
786
787
788
void SensorML8511::onLoop() {
  // read the voltage 
  int uvLevel = analogRead(_pin);
789
  int refLevel = _node_manager->getVcc()*1024/3.3;
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
  //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;
}

806
// what to do as the main task when receiving a message
807
void SensorML8511::onReceive(const MyMessage & message) {
808
  if (message.getCommand() == C_REQ) onLoop();
809
810
}

811
812
813
814
// what to do when receiving a remote message
void SensorML8511::onProcess(Request & request) {
}

815
816
817
818
// what to do when receiving an interrupt
void SensorML8511::onInterrupt() {
}

819
820
821
822
823
// 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;
}

824
825
826
827
828
/*
   SensorACS712
*/

// contructor
829
SensorACS712::SensorACS712(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager, child_id, pin) {
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  // 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;
}

844
// what to do during before
845
846
847
848
849
void SensorACS712::onBefore() {
  // set the pin as input
  pinMode(_pin, INPUT);
}

850
// what to do during setup
851
852
853
void SensorACS712::onSetup() {
}

854
// what to do during loop
855
856
857
858
859
860
861
862
863
864
865
866
867
868
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
}

869
// what to do as the main task when receiving a message
870
871
872
873
void SensorACS712::onReceive(const MyMessage & message) {
  if (message.getCommand() == C_REQ) onLoop();
}

874
875
876
877
878
879
880
881
882
883
884
// 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));
}

885
886
887
888
// what to do when receiving an interrupt
void SensorACS712::onInterrupt() {
}

889
890
891
892
893
/*
   SensorRain
*/

// contructor
894
SensorRain::SensorRain(NodeManager* node_manager, int child_id, int pin): SensorAnalogInput(node_manager,child_id, pin) {
895
896
897
898
899
900
901
902
903
904
905
906
907
908
  // set presentation and type and reverse
  setPresentation(S_RAIN);
  setType(V_RAINRATE);
  setReference(DEFAULT);
  setOutputPercentage(true);
  setReverse(true);
  setRangeMin(100);
}

/*
   SensorSoilMoisture
*/

// contructor
909
SensorSoilMoisture::SensorSoilMoisture(NodeManager* node_manager, int child_id, int pin): SensorAnalogInput(node_manager, child_id, pin) {
910
911
912
913
914
915
916
917
918
  // set presentation and type and reverse
  setPresentation(S_MOISTURE);
  setType(V_LEVEL);
  setReference(DEFAULT);
  setOutputPercentage(true);
  setReverse(true);
  setRangeMin(100);
}

919
#endif
920

921
#if MODULE_DIGITAL_INPUT == 1
DV's avatar
DV committed
922
923
924
925
926
/*
   SensorDigitalInput
*/

// contructor
927
SensorDigitalInput::SensorDigitalInput(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager,child_id, pin) {
DV's avatar
DV committed
928
929
}

930
// what to do during before
DV's avatar
DV committed
931
932
933
934
935
void SensorDigitalInput::onBefore() {
  // set the pin for input
  pinMode(_pin, INPUT);
}

936
// what to do during setup
user2684's avatar
user2684 committed
937
938
939
void SensorDigitalInput::onSetup() {
}

940
// what to do during loop
DV's avatar
DV committed
941
942
943
944
void SensorDigitalInput::onLoop() {
  // read the value
  int value = digitalRead(_pin);
  #if DEBUG == 1
945
    Serial.print(F("D-IN I="));
DV's avatar
DV committed
946
    Serial.print(_child_id);
947
    Serial.print(F(" P="));
DV's avatar
DV committed
948
    Serial.print(_pin);
949
    Serial.print(F(" V="));
DV's avatar
DV committed
950
951
952
953
954
955
    Serial.println(value);
  #endif
  // store the value
  _value_int = value;
}

956
// what to do as the main task when receiving a message
DV's avatar
DV committed
957
void SensorDigitalInput::onReceive(const MyMessage & message) {
958
  if (message.getCommand() == C_REQ) onLoop();
DV's avatar
DV committed
959
960
}

961
962
963
// what to do when receiving a remote message
void SensorDigitalInput::onProcess(Request & request) {
}
964
965
966
967

// what to do when receiving an interrupt
void SensorDigitalInput::onInterrupt() {
}
968
#endif
DV's avatar
DV committed
969

970

971
#if MODULE_DIGITAL_OUTPUT == 1
DV's avatar
DV committed
972
973
974
975
/*
   SensorDigitalOutput
*/

976
SensorDigitalOutput::SensorDigitalOutput(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager,child_id, pin) {
977
  _safeguard_timer = new Timer(node_manager);
DV's avatar
DV committed
978
979
}

980
// what to do during before
DV's avatar
DV committed
981
void SensorDigitalOutput::onBefore() {
982
  _setupPin(_pin);
983

DV's avatar
DV committed
984
985
}

986
// what to do during setup
user2684's avatar
user2684 committed
987
988
989
void SensorDigitalOutput::onSetup() {
}

DV's avatar
DV committed
990
// setter/getter
991
992
993
void SensorDigitalOutput::setOnValue(int value) {
  _on_value = value;
}
994
995
996
void SensorDigitalOutput::setLegacyMode(bool value) {
  _legacy_mode = value;
}
997
998
999
void SensorDigitalOutput::setSafeguard(int value) {
  _safeguard_timer->set(value,MINUTES);
}
1000
int SensorDigitalOutput::getStatus() {