sensoresp/include/sensor_anemometer.cpp

69 lines
1.9 KiB
C++
Raw Normal View History

2021-11-08 18:49:25 +00:00
#include "sensor_anemometer.h"
//uses ATS177 Latched hall sensor for rotation sensing
Sensor_Anemometer::Sensor_Anemometer(int p)
{
pin=p;
}
void Sensor_Anemometer::init() //Things to be done during setup()
{
pinMode(pin,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pin),interrupt_anemometer,CHANGE); //anemometer interrupt
init_ok=true;
}
//Also called during setup()
void Sensor_Anemometer::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data.minchange=minchange;
data.senddelaymax=senddelaymax;
data.readdelay=readdelay;
}
//Called during setup
void Sensor_Anemometer::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
sensorNode->advertise("windspeed");
}
void Sensor_Anemometer::sensorloop()
{
if (init_ok) {
sensordata &d=data;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
uint16_t _anepulsesPerMinute=anemometer_pulsecounter/((millis()-anemometer_lasttimereset)/60000.0);
d.value = _anepulsesPerMinute*SENSOR_ANEMOMETER_PPMtoMPS;
if (abs((int)d.lastsentvalue-d.value)>=d.minchange){ //int abs
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending windspeed. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
Homie.getLogger() << "windspeed tcs " << ": " << d.value << endl;
sensorNode->setProperty("windspeed").send(String(d.value));
//reset when sent. makes it more accurate but keeps fast response
anemometer_pulsecounter=0; //reset counter
anemometer_lasttimereset=millis();
d.lastreadtime=millis(); //also set lastread time to avoid having 1 count with a low time = high windspeed
d.lastsentvalue=d.value;
d.lastsent=millis();
}
}
}