imlement raingauge

This commit is contained in:
interfisch 2020-11-01 13:44:42 +01:00
parent ed77d41b7a
commit 5e00a7974b
2 changed files with 106 additions and 25 deletions

View File

@ -48,9 +48,16 @@ build_flags =
-D SENSOR_ANEMOMETER
-D ANEMOMETERPIN=D6 #Light Blue thicker cable (in distribution box)
-D dataAnemometer_minchange=5.0
-D dataAnemometer_minchange=0.25
-D dataAnemometer_readdelay=1000*30
-D dataAnemometer_senddelaymax=1000*60*5
-D ANEMOMETER_PPMtoMPS=0.0208640462
# 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
# Cable from Anemometer: Black=GND, Blue=3v3, Brown=Signal (needs pullup (internal))
lib_deps =

View File

@ -186,21 +186,11 @@ struct sensordata
#endif
#ifdef SENSOR_ANEMOMETER
// ## ANEMOMETER ##
sensordata dataAnemometer;
//unsigned long anemometer_timeLastReset=0;
unsigned long anemometer_lasttimereset=0;
uint16_t anemometer_pulsecounter=0; //counted pulses since last reset
//boolean anemometer_update_flag=false;
//unsigned long anemometer_lastPulse=0; //time for max calculation
//uint16_t anemometer_mintime=65535; //minimum time for this minute
//#define ANEMOMETER_MINTIMES_SIZE 4
//uint8_t anemometer_mintimepos=0;
//uint16_t anemometer_mintimes[ANEMOMETER_MINTIMES_SIZE];
#define ANEMOMETER_PPMtoMPS 0.0208640462;
#define ANEMOMETER_DEBOUNCETIME 0.015 //0.015ms between pulses is approx 85m/s windspeed
#define ANEMOMETER_DEBOUNCETIME 15 //15ms between pulses is approx 85m/s windspeed
unsigned long anemometer_lastpulse_fordebounce=0;
float value_anemometer=0; // [m/s]
@ -209,6 +199,29 @@ struct sensordata
void updateAnemometer();
#endif
#ifdef SENSOR_RAINGAUGE
sensordata dataRaingauge;
unsigned long raingauge_lasttimereset=0;
uint16_t raingauge_pulsecounter=0; //counted pulses since last reset
#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();
#endif
// data/homie/config.json hochladen mit platformio run --target uploadfs
// config contains homie device name, mqtt ip and wifi credentials
@ -378,8 +391,17 @@ void setup() {
#ifdef dataAnemometer_senddelaymax
dataAnemometer.senddelaymax=dataAnemometer_senddelaymax;
#endif
#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
#endif
@ -453,6 +475,10 @@ void setup() {
#ifdef SENSOR_ANEMOMETER
sensorNode.advertise("windspeed");
#endif
#ifdef SENSOR_RAINGAUGE
sensorNode.advertise("rain");
#endif
Serial.println("connecting..");
@ -934,17 +960,6 @@ void loop_TCS34725_colortemp()
#ifdef SENSOR_ANEMOMETER
void loop_anemometer()
{
/*
if (anemometer_update_flag) {
anemometer_update_flag=false;
//anemometer_mintime=0;
for (int i=0;i<ANEMOMETER_MINTIMES_SIZE;i++){
if (anemometer_mintime<anemometer_mintimes[i] && anemometer_mintimes[i]!=0){ //use longest time (=slowest speed) in array as max speed, reduces false readings. =0 -> initial value, ignore (not enough roations counted)
anemometer_mintime=anemometer_mintimes[i];
}
}
}*/
sensordata &d=dataAnemometer;
bool _changed=false;
@ -979,6 +994,51 @@ void loop_anemometer()
}
#endif
#ifdef SENSOR_RAINGAUGE
void loop_raingauge()
{
sensordata &d=dataRaingauge;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (raingauge_pulsecounter>0){ //if rg flipped
if (millis()-raingauge_lasttimereset > d.senddelaymax) { //last flip is before reset time
value_raingauge=raingauge_pulsecounter*RAINGAUGE_FLIPAMOUNT; //set to fixed amount if flip was exactly at that time
}else{
value_raingauge=3600000/(millis()-raingauge_lasttimereset)/raingauge_pulsecounter*RAINGAUGE_FLIPAMOUNT;
}
}
if (abs((int)d.lastsentvalue-value_raingauge)>=d.minchange){ //int abs //for raingauge minchange should be 0 (send every change)
_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() {
@ -1036,6 +1096,10 @@ void loopHandler() {
loop_anemometer();
#endif
#ifdef SENSOR_RAINGAUGE
loop_anemometer();
#endif
}
@ -1210,6 +1274,16 @@ void ICACHE_RAM_ATTR interrupt_anemometer()
}
#endif
#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
/*##################################