sensoresp/include/sensor_raingauge.cpp

82 lines
2.2 KiB
C++

#include "sensor_raingauge.h"
//uses ATS177 Latched hall sensor for rotation sensing
Sensor_Raingauge::Sensor_Raingauge(int p)
{
pin=p;
}
void Sensor_Raingauge::init() //Things to be done during setup()
{
init_ok=true;
pinMode(pin,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pin),interrupt_raingauge,CHANGE); //anemometer interrupt
}
//Also called during setup()
void Sensor_Raingauge::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data.minchange=minchange;
data.senddelaymax=senddelaymax;
data.readdelay=readdelay;
}
//Called during setup
void Sensor_Raingauge::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
sensorNode->advertise("rain");
}
void Sensor_Raingauge::sensorloop()
{
if (init_ok) {
sensordata &d=data;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis()-raingauge_lasttimereset > d.senddelaymax) {
raingauge_idleflag=true; //raingauge didn't flip for a long time
}
if (raingauge_pulsecounter>0){ //if rg flipped
if (raingauge_idleflag) { //last flip is before reset time
d.value=raingauge_pulsecounter*RAINGAUGE_FLIPAMOUNT; //set to fixed amount if flip was exactly at that time
raingauge_idleflag=false;
}else{
d.value=3600000/(millis()-raingauge_lasttimereset)/raingauge_pulsecounter*RAINGAUGE_FLIPAMOUNT;
raingauge_idleflag=false;
}
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending rain. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
if (!_changed) { //no flip since a long time
d.value=0; //set to no rain
}
Homie.getLogger() << "rain " << ": " << d.value << endl;
sensorNode->setProperty("rain").send(String(d.value));
//reset when sent. makes it more accurate but keeps fast response
raingauge_pulsecounter=0; //reset counter
raingauge_lasttimereset=millis();
d.lastsentvalue=d.value;
d.lastsent=millis();
}
}
}