move raingauge to class

This commit is contained in:
interfisch 2021-11-08 20:09:59 +01:00
parent d45eddd707
commit 310fb73fba
6 changed files with 168 additions and 108 deletions

View File

@ -3,18 +3,6 @@
//uses ATS177 Latched hall sensor for rotation sensing //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) Sensor_Anemometer::Sensor_Anemometer(int p)
{ {
pin=p; pin=p;

View File

@ -4,7 +4,7 @@
#include "sensordata.h" #include "sensordata.h"
#include <Homie.h> #include <Homie.h>
void ICACHE_RAM_ATTR interrupt_anemometer();
class Sensor_Anemometer class Sensor_Anemometer
{ {

View File

@ -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();
}
}
}

View File

@ -0,0 +1,54 @@
#ifndef SENSOR_Raingauge_H
#define SENSOR_Raingauge_H
#include "sensordata.h"
#include <Homie.h>
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

View File

@ -59,9 +59,10 @@ build_flags =
# Cable from Anemometer: Black=GND, Blue=3v3, Brown=Signal (needs pullup (internal)) # Cable from Anemometer: Black=GND, Blue=3v3, Brown=Signal (needs pullup (internal))
-D SENSOR_RAINGAUGE -D SENSOR_RAINGAUGE
-D RAINGAUGEPIN=D7 -D SENSOR_Raingauge_PIN=D7
-D dataRaingauge_readdelay=1000 -D RAINGAUGE_FLIPAMOUNT=0.38888
-D dataRaingauge_senddelaymax=1000*60*60 #also used for rain waiting timeout -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 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 #Cable colors from anemometers sensor (before longer able): blue=gnd, brown=vcc, white=signal

View File

@ -265,10 +265,12 @@
#endif #endif
#ifdef SENSOR_ANEMOMETER #ifdef SENSOR_ANEMOMETER
void ICACHE_RAM_ATTR interrupt_anemometer();
#include "sensor_anemometer.cpp" #include "sensor_anemometer.cpp"
//Sensor_Anemometer_Instance = new Sensor_Anemometer(SENSOR_Anemometer_PIN); //Sensor_Anemometer_Instance = new Sensor_Anemometer(SENSOR_Anemometer_PIN);
Sensor_Anemometer sensor_anemometer(SENSOR_Anemometer_PIN); Sensor_Anemometer sensor_anemometer(SENSOR_Anemometer_PIN);
void ICACHE_RAM_ATTR interrupt_anemometer() void ICACHE_RAM_ATTR interrupt_anemometer()
{ {
if (millis() - sensor_anemometer.anemometer_lastpulse_fordebounce >= ANEMOMETER_DEBOUNCETIME) { //ignore if pulse came too fast if (millis() - sensor_anemometer.anemometer_lastpulse_fordebounce >= ANEMOMETER_DEBOUNCETIME) { //ignore if pulse came too fast
@ -290,26 +292,29 @@
#ifdef SENSOR_RAINGAUGE #ifdef SENSOR_RAINGAUGE
//uses ATS177 Latched Hall Sensor for rauge flip sensing //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 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 #endif
// data/homie/config.json hochladen mit platformio run --target uploadfs // data/homie/config.json hochladen mit platformio run --target uploadfs
@ -415,14 +420,8 @@ void setup() {
#endif #endif
#ifdef SENSOR_RAINGAUGE #ifdef SENSOR_RAINGAUGE
pinMode(RAINGAUGEPIN,INPUT_PULLUP); sensor_raingauge.init();
attachInterrupt(digitalPinToInterrupt(RAINGAUGEPIN),interrupt_raingauge,CHANGE); //anemometer interrupt sensor_raingauge.setSettings(SENSOR_Raingauge_minchange,SENSOR_Raingauge_senddelaymax,SENSOR_Raingauge_readdelay);
#ifdef dataRaingauge_senddelaymax
dataRaingauge.senddelaymax=dataRaingauge_senddelaymax;
#endif
#ifdef dataRaingauge_readdelay
dataRaingauge.readdelay=dataRaingauge_readdelay;
#endif
#endif #endif
@ -497,7 +496,7 @@ void setup() {
#endif #endif
#ifdef SENSOR_RAINGAUGE #ifdef SENSOR_RAINGAUGE
sensorNode.advertise("rain"); sensor_raingauge.advertise(sensorNode);
#endif #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() { void loopHandler() {
checkESPStatus(); checkESPStatus();
@ -621,7 +572,7 @@ void loopHandler() {
#endif #endif
#ifdef SENSOR_RAINGAUGE #ifdef SENSOR_RAINGAUGE
loop_raingauge(); sensor_raingauge.sensorloop();
#endif #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 ######## * ######## HELPER FUNCTIONS ########
*/ */