Commit 126812a9 authored by user2684's avatar user2684
Browse files

Add support for rain gauge sensor #90

parent 38fd51c9
...@@ -33,6 +33,7 @@ float getVcc() { ...@@ -33,6 +33,7 @@ float getVcc() {
#endif #endif
} }
/*************************************** /***************************************
PowerManager PowerManager
*/ */
...@@ -325,17 +326,17 @@ void SensorAnalogInput::setRangeMax(int value) { ...@@ -325,17 +326,17 @@ void SensorAnalogInput::setRangeMax(int value) {
_range_max = value; _range_max = value;
} }
// what do to during before // what to do during before
void SensorAnalogInput::onBefore() { void SensorAnalogInput::onBefore() {
// prepare the pin for input // prepare the pin for input
pinMode(_pin, INPUT); pinMode(_pin, INPUT);
} }
// what do to during setup // what to do during setup
void SensorAnalogInput::onSetup() { void SensorAnalogInput::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorAnalogInput::onLoop() { void SensorAnalogInput::onLoop() {
// read the input // read the input
int adc = _getAnalogRead(); int adc = _getAnalogRead();
...@@ -354,7 +355,7 @@ void SensorAnalogInput::onLoop() { ...@@ -354,7 +355,7 @@ void SensorAnalogInput::onLoop() {
_value_int = _output_percentage ? percentage : adc; _value_int = _output_percentage ? percentage : adc;
} }
// what do to during loop // what to do during loop
void SensorAnalogInput::onReceive(const MyMessage & message) { void SensorAnalogInput::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -428,17 +429,17 @@ void SensorThermistor::setOffset(float value) { ...@@ -428,17 +429,17 @@ void SensorThermistor::setOffset(float value) {
_offset = value; _offset = value;
} }
// what do to during before // what to do during before
void SensorThermistor::onBefore() { void SensorThermistor::onBefore() {
// set the pin as input // set the pin as input
pinMode(_pin, INPUT); pinMode(_pin, INPUT);
} }
// what do to during setup // what to do during setup
void SensorThermistor::onSetup() { void SensorThermistor::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorThermistor::onLoop() { void SensorThermistor::onLoop() {
// read the voltage across the thermistor // read the voltage across the thermistor
float adc = analogRead(_pin); float adc = analogRead(_pin);
...@@ -467,7 +468,7 @@ void SensorThermistor::onLoop() { ...@@ -467,7 +468,7 @@ void SensorThermistor::onLoop() {
_value_float = temperature; _value_float = temperature;
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorThermistor::onReceive(const MyMessage & message) { void SensorThermistor::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -485,17 +486,17 @@ SensorML8511::SensorML8511(int child_id, int pin): Sensor(child_id, pin) { ...@@ -485,17 +486,17 @@ SensorML8511::SensorML8511(int child_id, int pin): Sensor(child_id, pin) {
setValueType(TYPE_FLOAT); setValueType(TYPE_FLOAT);
} }
// what do to during before // what to do during before
void SensorML8511::onBefore() { void SensorML8511::onBefore() {
// set the pin as input // set the pin as input
pinMode(_pin, INPUT); pinMode(_pin, INPUT);
} }
// what do to during setup // what to do during setup
void SensorML8511::onSetup() { void SensorML8511::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorML8511::onLoop() { void SensorML8511::onLoop() {
// read the voltage // read the voltage
int uvLevel = analogRead(_pin); int uvLevel = analogRead(_pin);
...@@ -516,7 +517,7 @@ void SensorML8511::onLoop() { ...@@ -516,7 +517,7 @@ void SensorML8511::onLoop() {
_value_float = uvIntensity; _value_float = uvIntensity;
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorML8511::onReceive(const MyMessage & message) { void SensorML8511::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -546,17 +547,17 @@ void SensorACS712::setOffset(int value) { ...@@ -546,17 +547,17 @@ void SensorACS712::setOffset(int value) {
_ACS_offset = value; _ACS_offset = value;
} }
// what do to during before // what to do during before
void SensorACS712::onBefore() { void SensorACS712::onBefore() {
// set the pin as input // set the pin as input
pinMode(_pin, INPUT); pinMode(_pin, INPUT);
} }
// what do to during setup // what to do during setup
void SensorACS712::onSetup() { void SensorACS712::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorACS712::onLoop() { void SensorACS712::onLoop() {
int value = analogRead(_pin); int value = analogRead(_pin);
// convert the analog read in mV // convert the analog read in mV
...@@ -571,11 +572,93 @@ void SensorACS712::onLoop() { ...@@ -571,11 +572,93 @@ void SensorACS712::onLoop() {
#endif #endif
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorACS712::onReceive(const MyMessage & message) { void SensorACS712::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
/*
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() {
// on tipping, two consecutive interrupts are received, ignore the second one
if (millis() - _last_tip > 100){
// increase the counter
_count++;
#if DEBUG == 1
Serial.println(F("RAIN+"));
#endif
}
_last_tip = millis();
}
// what to do during loop
void SensorRainGauge::onLoop() {
// avoid reporting the same value multiple times
_value_float = -1;
// time elapsed since the last report
long elapsed = millis() - _last_report;
// minimum time interval between reports
long min_interval = ((long)_report_interval*1000)*60;
// time to report
if (elapsed > min_interval) {
// 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;
_last_report = millis();
}
}
// 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;
}
}
/* /*
* SensorMQ * SensorMQ
...@@ -623,18 +706,18 @@ void SensorMQ::setSmokeCurve(float *value) { ...@@ -623,18 +706,18 @@ void SensorMQ::setSmokeCurve(float *value) {
_SmokeCurve[2] = value[2]; _SmokeCurve[2] = value[2];
} }
// what do to during before // what to do during before
void SensorMQ::onBefore() { void SensorMQ::onBefore() {
// prepare the pin for input // prepare the pin for input
pinMode(_pin, INPUT); pinMode(_pin, INPUT);
} }
// what do to during setup // what to do during setup
void SensorMQ::onSetup() { void SensorMQ::onSetup() {
_ro = _MQCalibration(); _ro = _MQCalibration();
} }
// what do to during loop // what to do during loop
void SensorMQ::onLoop() { void SensorMQ::onLoop() {
if (_pin == -1) return; if (_pin == -1) return;
// calculate rs/ro // calculate rs/ro
...@@ -664,7 +747,7 @@ void SensorMQ::onLoop() { ...@@ -664,7 +747,7 @@ void SensorMQ::onLoop() {
_value_int = (int16_t)ceil(value); _value_int = (int16_t)ceil(value);
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorMQ::onReceive(const MyMessage & message) { void SensorMQ::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -729,17 +812,17 @@ int SensorMQ::_MQGetPercentage(float rs_ro_ratio, float *pcurve) { ...@@ -729,17 +812,17 @@ int SensorMQ::_MQGetPercentage(float rs_ro_ratio, float *pcurve) {
SensorDigitalInput::SensorDigitalInput(int child_id, int pin): Sensor(child_id, pin) { SensorDigitalInput::SensorDigitalInput(int child_id, int pin): Sensor(child_id, pin) {
} }
// what do to during before // what to do during before
void SensorDigitalInput::onBefore() { void SensorDigitalInput::onBefore() {
// set the pin for input // set the pin for input
pinMode(_pin, INPUT); pinMode(_pin, INPUT);
} }
// what do to during setup // what to do during setup
void SensorDigitalInput::onSetup() { void SensorDigitalInput::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorDigitalInput::onLoop() { void SensorDigitalInput::onLoop() {
// read the value // read the value
int value = digitalRead(_pin); int value = digitalRead(_pin);
...@@ -755,7 +838,7 @@ void SensorDigitalInput::onLoop() { ...@@ -755,7 +838,7 @@ void SensorDigitalInput::onLoop() {
_value_int = value; _value_int = value;
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorDigitalInput::onReceive(const MyMessage & message) { void SensorDigitalInput::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -769,7 +852,7 @@ void SensorDigitalInput::onReceive(const MyMessage & message) { ...@@ -769,7 +852,7 @@ void SensorDigitalInput::onReceive(const MyMessage & message) {
SensorDigitalOutput::SensorDigitalOutput(int child_id, int pin): Sensor(child_id, pin) { SensorDigitalOutput::SensorDigitalOutput(int child_id, int pin): Sensor(child_id, pin) {
} }
// what do to during before // what to do during before
void SensorDigitalOutput::onBefore() { void SensorDigitalOutput::onBefore() {
// set the pin as output and initialize it accordingly // set the pin as output and initialize it accordingly
pinMode(_pin, OUTPUT); pinMode(_pin, OUTPUT);
...@@ -779,7 +862,7 @@ void SensorDigitalOutput::onBefore() { ...@@ -779,7 +862,7 @@ void SensorDigitalOutput::onBefore() {
_value_int = _initial_value; _value_int = _initial_value;
} }
// what do to during setup // what to do during setup
void SensorDigitalOutput::onSetup() { void SensorDigitalOutput::onSetup() {
} }
...@@ -799,7 +882,7 @@ void SensorDigitalOutput::onLoop() { ...@@ -799,7 +882,7 @@ void SensorDigitalOutput::onLoop() {
// do nothing on loop // do nothing on loop
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorDigitalOutput::onReceive(const MyMessage & message) { void SensorDigitalOutput::onReceive(const MyMessage & message) {
if (message.getCommand() == C_SET) { if (message.getCommand() == C_SET) {
// retrieve from the message the value to set // retrieve from the message the value to set
...@@ -889,17 +972,17 @@ SensorDHT::SensorDHT(int child_id, int pin, DHT* dht, int sensor_type, int dht_t ...@@ -889,17 +972,17 @@ SensorDHT::SensorDHT(int child_id, int pin, DHT* dht, int sensor_type, int dht_t
} }
} }
// what do to during before // what to do during before
void SensorDHT::onBefore() { void SensorDHT::onBefore() {
// initialize the dht library // initialize the dht library
_dht->begin(); _dht->begin();
} }
// what do to during setup // what to do during setup
void SensorDHT::onSetup() { void SensorDHT::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorDHT::onLoop() { void SensorDHT::onLoop() {
// temperature sensor // temperature sensor
if (_sensor_type == SensorDHT::TEMPERATURE) { if (_sensor_type == SensorDHT::TEMPERATURE) {
...@@ -932,7 +1015,7 @@ void SensorDHT::onLoop() { ...@@ -932,7 +1015,7 @@ void SensorDHT::onLoop() {
} }
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorDHT::onReceive(const MyMessage & message) { void SensorDHT::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -960,17 +1043,17 @@ SensorSHT21::SensorSHT21(int child_id, int sensor_type): Sensor(child_id,A2) { ...@@ -960,17 +1043,17 @@ SensorSHT21::SensorSHT21(int child_id, int sensor_type): Sensor(child_id,A2) {
} }
} }
// what do to during before // what to do during before
void SensorSHT21::onBefore() { void SensorSHT21::onBefore() {
// initialize the library // initialize the library
Wire.begin(); Wire.begin();
} }
// what do to during setup // what to do during setup
void SensorSHT21::onSetup() { void SensorSHT21::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorSHT21::onLoop() { void SensorSHT21::onLoop() {
// temperature sensor // temperature sensor
if (_sensor_type == SensorSHT21::TEMPERATURE) { if (_sensor_type == SensorSHT21::TEMPERATURE) {
...@@ -1003,7 +1086,7 @@ void SensorSHT21::onLoop() { ...@@ -1003,7 +1086,7 @@ void SensorSHT21::onLoop() {
} }
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorSHT21::onReceive(const MyMessage & message) { void SensorSHT21::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -1045,18 +1128,18 @@ int SensorSwitch::getInitial() { ...@@ -1045,18 +1128,18 @@ int SensorSwitch::getInitial() {
return _initial; return _initial;
} }
// what do to during before // what to do during before
void SensorSwitch::onBefore() { void SensorSwitch::onBefore() {
// initialize the value // initialize the value
if (_mode == RISING) _value_int = LOW; if (_mode == RISING) _value_int = LOW;
else if (_mode == FALLING) _value_int = HIGH; else if (_mode == FALLING) _value_int = HIGH;
} }
// what do to during setup // what to do during setup
void SensorSwitch::onSetup() { void SensorSwitch::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorSwitch::onLoop() { void SensorSwitch::onLoop() {
// wait to ensure the the input is not floating // wait to ensure the the input is not floating
if (_debounce > 0) wait(_debounce); if (_debounce > 0) wait(_debounce);
...@@ -1080,7 +1163,7 @@ void SensorSwitch::onLoop() { ...@@ -1080,7 +1163,7 @@ void SensorSwitch::onLoop() {
_value_int = -1; _value_int = -1;
} }
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorSwitch::onReceive(const MyMessage & message) { void SensorSwitch::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -1118,15 +1201,15 @@ SensorDs18b20::SensorDs18b20(int child_id, int pin, DallasTemperature* sensors, ...@@ -1118,15 +1201,15 @@ SensorDs18b20::SensorDs18b20(int child_id, int pin, DallasTemperature* sensors,
_sensors->getAddress(_device_address, index); _sensors->getAddress(_device_address, index);
} }
// what do to during before // what to do during before
void SensorDs18b20::onBefore() { void SensorDs18b20::onBefore() {
} }
// what do to during setup // what to do during setup
void SensorDs18b20::onSetup() { void SensorDs18b20::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorDs18b20::onLoop() { void SensorDs18b20::onLoop() {
// do not wait for conversion, will sleep manually during it // do not wait for conversion, will sleep manually during it
if (_sleep_during_conversion) _sensors->setWaitForConversion(false); if (_sleep_during_conversion) _sensors->setWaitForConversion(false);
...@@ -1151,7 +1234,7 @@ void SensorDs18b20::onLoop() { ...@@ -1151,7 +1234,7 @@ void SensorDs18b20::onLoop() {
_value_float = temperature; _value_float = temperature;
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorDs18b20::onReceive(const MyMessage & message) { void SensorDs18b20::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -1189,16 +1272,16 @@ SensorBH1750::SensorBH1750(int child_id): Sensor(child_id,A4) { ...@@ -1189,16 +1272,16 @@ SensorBH1750::SensorBH1750(int child_id): Sensor(child_id,A4) {
_lightSensor = new BH1750(); _lightSensor = new BH1750();
} }
// what do to during before // what to do during before
void SensorBH1750::onBefore() { void SensorBH1750::onBefore() {
_lightSensor->begin(); _lightSensor->begin();
} }
// what do to during setup // what to do during setup
void SensorBH1750::onSetup() { void SensorBH1750::onSetup() {
} }
// what do to during loop // what to do during loop
void SensorBH1750::onLoop() { void SensorBH1750::onLoop() {
// request the light level // request the light level
_value_int = _lightSensor->readLightLevel(); _value_int = _lightSensor->readLightLevel();
...@@ -1210,7 +1293,7 @@ void SensorBH1750::onLoop() { ...@@ -1210,7 +1293,7 @@ void SensorBH1750::onLoop() {
#endif #endif
} }
// what do to as the main task when receiving a message // what to do as the main task when receiving a message
void SensorBH1750::onReceive(const MyMessage & message) { void SensorBH1750::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop(); if (message.getCommand() == C_REQ) onLoop();
} }
...@@ -1230,17 +1313,17 @@ SensorMLX90614::SensorMLX90614(int child_id, Adafruit_MLX90614* mlx, int sensor_ ...@@ -1230,17 +1313,17 @@ SensorMLX90614::SensorMLX90614(int child_id, Adafruit_MLX90614* mlx, int sensor_
setValueType(TYPE_FLOAT); setValueType(TYPE_FLOAT);
} }
// what do to during before // what to do during before
void SensorMLX90614::onBefore() { void SensorMLX90614::onBefore() {
// initialize the library // initialize the library
_mlx->begin(); _mlx->begin();
} }