From 310fb73fba88355bb8d6ceabf2b061ada240732a Mon Sep 17 00:00:00 2001 From: Fisch Date: Mon, 8 Nov 2021 20:09:59 +0100 Subject: [PATCH] move raingauge to class --- include/sensor_anemometer.cpp | 12 ---- include/sensor_anemometer.h | 2 +- include/sensor_raingauge.cpp | 81 +++++++++++++++++++++++ include/sensor_raingauge.h | 54 +++++++++++++++ platformio.ini | 7 +- src/main.cpp | 120 ++++++++-------------------------- 6 files changed, 168 insertions(+), 108 deletions(-) create mode 100644 include/sensor_raingauge.cpp create mode 100644 include/sensor_raingauge.h diff --git a/include/sensor_anemometer.cpp b/include/sensor_anemometer.cpp index 3d3aeef..3826108 100644 --- a/include/sensor_anemometer.cpp +++ b/include/sensor_anemometer.cpp @@ -3,18 +3,6 @@ //uses ATS177 Latched hall sensor for rotation sensing -/* -Sensor_Anemometer* Sensor_Anemometer_Instance; - - -void assignAnemometerObject(Sensor_Anemometer* p) { - Sensor_Anemometer_Instance = p; -}*/ -/* -void sensor_anemometer_interrupt() { - Sensor_Anemometer_Instance->interrupt_anemometer(); -}*/ - Sensor_Anemometer::Sensor_Anemometer(int p) { pin=p; diff --git a/include/sensor_anemometer.h b/include/sensor_anemometer.h index ba9cd77..bb93cd7 100644 --- a/include/sensor_anemometer.h +++ b/include/sensor_anemometer.h @@ -4,7 +4,7 @@ #include "sensordata.h" #include -void ICACHE_RAM_ATTR interrupt_anemometer(); + class Sensor_Anemometer { diff --git a/include/sensor_raingauge.cpp b/include/sensor_raingauge.cpp new file mode 100644 index 0000000..9243e36 --- /dev/null +++ b/include/sensor_raingauge.cpp @@ -0,0 +1,81 @@ + +#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(); + } + } +} + + diff --git a/include/sensor_raingauge.h b/include/sensor_raingauge.h new file mode 100644 index 0000000..293a205 --- /dev/null +++ b/include/sensor_raingauge.h @@ -0,0 +1,54 @@ +#ifndef SENSOR_Raingauge_H +#define SENSOR_Raingauge_H + +#include "sensordata.h" +#include + + +class Sensor_Raingauge +{ + +private: + HomieNode *sensorNode; //reference to HomieNode + + int pin; + + struct sensordata data; //struct values are changed in setup() + + unsigned long raingauge_lasttimereset=0; + + bool raingauge_idleflag=true; + + #define RAINGAUGE_DEBOUNCETIME 1000 + + + bool init_ok; + + //value in [mm] or [L/m^2] + + //#define RAINGAUGE_FLIPAMOUNT 0.38888 //how much mm rain (L/m^2) per gauge flip. mL (rain to flip) / A (opening area) + //was 0.69292 until 201702 + /* Calibration: + * Test1: 1000mL -> 259 Flips + * Test2: 1000mL -> 256 in ca 10min + * -> 3,9mL per Flip, opening diameter =113mm -> A=0,010028749 + */ + + void updateRaingauge(); + + +public: + Sensor_Raingauge(int p); + + + void init(); + void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay); + void advertise(HomieNode& p_sensorNode); + void sensorloop(); + + uint16_t raingauge_pulsecounter=0; //counted pulses since last reset + unsigned long raingauge_lastpulse_fordebounce=0; +}; + +#endif + diff --git a/platformio.ini b/platformio.ini index 9d5dc50..f8d785f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -59,9 +59,10 @@ build_flags = # Cable from Anemometer: Black=GND, Blue=3v3, Brown=Signal (needs pullup (internal)) -D SENSOR_RAINGAUGE - -D RAINGAUGEPIN=D7 - -D dataRaingauge_readdelay=1000 - -D dataRaingauge_senddelaymax=1000*60*60 #also used for rain waiting timeout + -D SENSOR_Raingauge_PIN=D7 + -D RAINGAUGE_FLIPAMOUNT=0.38888 + -D SENSOR_Raingauge_readdelay=1000 + -D SENSOR_Raingauge_senddelaymax=1000*60*60 #also used for rain waiting timeout # Cable from Anemometer: Black=GND, Blue=3v3, Brown=Signal (needs pullup (internal)) #Cable colors from anemometers sensor (before longer able): blue=gnd, brown=vcc, white=signal diff --git a/src/main.cpp b/src/main.cpp index d0f40e3..4927bb1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -265,10 +265,12 @@ #endif #ifdef SENSOR_ANEMOMETER + void ICACHE_RAM_ATTR interrupt_anemometer(); #include "sensor_anemometer.cpp" //Sensor_Anemometer_Instance = new Sensor_Anemometer(SENSOR_Anemometer_PIN); Sensor_Anemometer sensor_anemometer(SENSOR_Anemometer_PIN); + void ICACHE_RAM_ATTR interrupt_anemometer() { if (millis() - sensor_anemometer.anemometer_lastpulse_fordebounce >= ANEMOMETER_DEBOUNCETIME) { //ignore if pulse came too fast @@ -290,26 +292,29 @@ #ifdef SENSOR_RAINGAUGE //uses ATS177 Latched Hall Sensor for rauge flip sensing - sensordata dataRaingauge; - unsigned long raingauge_lasttimereset=0; - uint16_t raingauge_pulsecounter=0; //counted pulses since last reset - bool raingauge_idleflag=true; - - #define RAINGAUGE_DEBOUNCETIME 1000 - unsigned long raingauge_lastpulse_fordebounce=0; - - float value_raingauge=0; // [mm] or [L/m^2] - - #define RAINGAUGE_FLIPAMOUNT 0.38888 //how much mm rain (L/m^2) per gauge flip. mL (rain to flip) / A (opening area) - //was 0.69292 until 201702 - /* Calibration: - * Test1: 1000mL -> 259 Flips - * Test2: 1000mL -> 256 in ca 10min - * -> 3,9mL per Flip, opening diameter =113mm -> A=0,010028749 - */ - void ICACHE_RAM_ATTR interrupt_raingauge(); - void updateRaingauge(); + #include "sensor_raingauge.cpp" + + Sensor_Raingauge sensor_raingauge(SENSOR_Raingauge_PIN); + + void ICACHE_RAM_ATTR interrupt_raingauge() + { + if (millis() - sensor_raingauge.raingauge_lastpulse_fordebounce >= RAINGAUGE_DEBOUNCETIME) { //ignore if pulse came too fast + sensor_raingauge.raingauge_pulsecounter++; + sensor_raingauge.raingauge_lastpulse_fordebounce=millis(); + } + } + + #ifndef SENSOR_Raingauge_minchange + #define SENSOR_Raingauge_minchange 0 + #endif + #ifndef SENSOR_Raingauge_senddelaymax + #define SENSOR_Raingauge_senddelaymax 1000*60*60 + #endif + #ifndef SENSOR_Raingauge_readdelay + #define SENSOR_Raingauge_readdelay 1000 + #endif + #endif // data/homie/config.json hochladen mit platformio run --target uploadfs @@ -415,14 +420,8 @@ void setup() { #endif #ifdef SENSOR_RAINGAUGE - pinMode(RAINGAUGEPIN,INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(RAINGAUGEPIN),interrupt_raingauge,CHANGE); //anemometer interrupt - #ifdef dataRaingauge_senddelaymax - dataRaingauge.senddelaymax=dataRaingauge_senddelaymax; - #endif - #ifdef dataRaingauge_readdelay - dataRaingauge.readdelay=dataRaingauge_readdelay; - #endif + sensor_raingauge.init(); + sensor_raingauge.setSettings(SENSOR_Raingauge_minchange,SENSOR_Raingauge_senddelaymax,SENSOR_Raingauge_readdelay); #endif @@ -497,7 +496,7 @@ void setup() { #endif #ifdef SENSOR_RAINGAUGE - sensorNode.advertise("rain"); + sensor_raingauge.advertise(sensorNode); #endif @@ -512,54 +511,6 @@ void loop() { } -#ifdef SENSOR_RAINGAUGE -void loop_raingauge() -{ - sensordata &d=dataRaingauge; - - 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 - value_raingauge=raingauge_pulsecounter*RAINGAUGE_FLIPAMOUNT; //set to fixed amount if flip was exactly at that time - raingauge_idleflag=false; - }else{ - value_raingauge=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"); - checkESPStatus(); - - if (!_changed) { //no flip since a long time - value_raingauge=0; //set to no rain - } - - Homie.getLogger() << "rain " << ": " << value_raingauge << endl; - sensorNode.setProperty("rain").send(String(value_raingauge)); - - //reset when sent. makes it more accurate but keeps fast response - raingauge_pulsecounter=0; //reset counter - raingauge_lasttimereset=millis(); - - d.lastsentvalue=value_raingauge; - - d.lastsent=millis(); - } - -} -#endif void loopHandler() { checkESPStatus(); @@ -621,7 +572,7 @@ void loopHandler() { #endif #ifdef SENSOR_RAINGAUGE - loop_raingauge(); + sensor_raingauge.sensorloop(); #endif } @@ -636,21 +587,6 @@ void checkESPStatus() } - - -#ifdef SENSOR_RAINGAUGE -void ICACHE_RAM_ATTR interrupt_raingauge() -{ - if (millis() - raingauge_lastpulse_fordebounce >= RAINGAUGE_DEBOUNCETIME) { //ignore if pulse came too fast - raingauge_pulsecounter++; - raingauge_lastpulse_fordebounce=millis(); - } -} -#endif - - - - /*################################## * ######## HELPER FUNCTIONS ######## */