BresserWeatherSensorReceiver
Bresser 5-in-1/6-in-1/7-in-1 868 MHz Weather Sensor Radio Receiver for Arduino based on CC1101 or SX1276/RFM95W
Loading...
Searching...
No Matches
RainGauge Class Reference

Calculation of hourly (past 60 minutes), daily, weekly and monthly rainfall. More...

#include <RainGauge.h>

Public Member Functions

 RainGauge (const float raingauge_max=RAINGAUGE_MAX_VALUE, const float quality_threshold=DEFAULT_QUALITY_THRESHOLD)
 
void set_max (float raingauge_max)
 
void setUpdateRate (uint8_t rate=RAINGAUGE_UPD_RATE)
 Set expected update rate for pastHour() calculation.
 
void reset (uint8_t flags=0xF)
 
void hist_init (int16_t rain=-1)
 
void update (time_t ts, float rain, bool startup=false)
 Update rain gauge statistics.
 
float pastHour (bool *valid=nullptr, int *nbins=nullptr, float *quality=nullptr)
 
float currentDay (void)
 
float currentWeek (void)
 
float currentMonth (void)
 

Detailed Description

Calculation of hourly (past 60 minutes), daily, weekly and monthly rainfall.

Additionally overflow of the rain gauge is handled when reaching RAINGAUGE_MAX_VALUE.

Constructor & Destructor Documentation

◆ RainGauge()

RainGauge::RainGauge ( const float  raingauge_max = RAINGAUGE_MAX_VALUE,
const float  quality_threshold = DEFAULT_QUALITY_THRESHOLD 
)
inline

Constructor

Parameters
raingauge_maxraingauge value which causes a counter overflow
quality_thresholdfraction of valid rain_hist entries required for valid pastHour() result

Member Function Documentation

◆ currentDay()

float RainGauge::currentDay ( void  )

Rainfall of current calendar day

Returns
amount of rain

◆ currentMonth()

float RainGauge::currentMonth ( void  )

Rainfall of current calendar month

Returns
amount of rain

◆ currentWeek()

float RainGauge::currentWeek ( void  )

Rainfall of current calendar week

Returns
amount of rain

◆ hist_init()

void RainGauge::hist_init ( int16_t  rain = -1)

Initialize history buffer for hourly (past 60 minutes) rainfall

◆ pastHour()

float RainGauge::pastHour ( bool *  valid = nullptr,
int *  nbins = nullptr,
float *  quality = nullptr 
)

Rainfall during past 60 minutes

Parameters
validnumber of valid entries in rain_hist >= qualityThreshold * 60 / updateRate
nbinsnumber of valid entries in rain_hist
qualityfraction of valid entries in rain_hist (0..1); quality = nbins / (60 / updateRate)
Returns
amount of rain during past 60 minutes

◆ reset()

void RainGauge::reset ( uint8_t  flags = 0xF)

Reset non-volatile data and current rain counter value

Parameters
flagsFlags defining what to reset:

◆ set_max()

void RainGauge::set_max ( float  raingauge_max)
inline

Set maximum rain counter value

Parameters
raingauge_maxraingauge value which causes a counter overflow

◆ setUpdateRate()

void RainGauge::setUpdateRate ( uint8_t  rate = RAINGAUGE_UPD_RATE)
inline

Set expected update rate for pastHour() calculation.

RAIN_HIST_SIZE: number of entries in rain_hist[] updateRate: update rate in minutes

60 minutes / updateRate = no_of_hist_bins The resulting number of history bins must be an integer value which does not exceed RAIN_HIST_SIZE.

Examples:

  1. updateRate = 6 -> 60 / 6 = 10 entries
  2. updateRate = 12 -> 60 / 12 = 5 entries

Changing the update rate will reset the history buffer, therefore the caller should avoid frequent changes.

Actual update intervals shorter than updateRate will lead to a reduced resolution of the pastHour() result and a higher risk of an invalid result if a bin in the history buffer was missed.

Actual update intervals longer than updateRate will lead to an invalid result, because bins in the history buffer will be missed.

Parameters
rateupdate rate in minutes (default: 6)

◆ update()

RainGauge::update ( time_t  ts,
float  rain,
bool  startup = false 
)

Update rain gauge statistics.

Parameters
tstimestamp
rainrain gauge raw value (in mm/m²)
startupsensor startup flag
* Total rainfall during past 60 minutes
* --------------------------------------
*
* In each update():
* - timestamp (time_t) ->                  t (localtime, struct tm)
* - calculate index into hist[]:           idx = t.tm_min / updateRate
* - expired time since last update:        t_delta = timestamp - nvData.lastUpdate
* - amount of rain since last update:      rainDelta = rainCurr - nvData.rainPrev
* - t_delta
*      < 0:                                something is wrong, e.g. RTC was not set correctly -> ignore, return
*      t_delta < expected update rate:
*          idx same as in previous cycle:  hist[idx] += rainDelta
*          idx changed by 1:               hist[idx] = rainDelta
*      t_delta >= history size:            mark all history entries as invalid
*      else (index changed > 1):           mark all history entries in interval [expected_index, current_index) as invalid
*                                          hist[idx] = rainDelta
*
*   ---------------     -----------
*  |   |   |   |   |...|   |   |   |   hist[RAIN_HIST_SIZE]
*   ---------------     -----------
*        ^
*        |
*       idx = t.tm_min / updateRate
*
* - Calculate hourly rate:
*   pastHour = sum of all valid hist[] entries
*
* Notes:
* - rainDelta values (floating point with resolution of 0.1) are stored as integers to reduce memory consumption.
*   To avoid rounding errors, the rainDelta values are multiplied by 100 for conversion to integer.
* 

The documentation for this class was generated from the following files: