BayEOSArduino Library
RainGauge.h
1 #include <QuickStats.h>
2 float bat_voltage;
3 float t2[TMEAN2];
4 uint8_t current_t1i;
5 uint8_t current_t2i;
6 float t_mean_t2, t_sum_t1, temp0;
7 uint8_t t_mode = 0;
8 float y_cur[MITTELUNG];
9 uint8_t current_i = 0;
10 uint8_t calc_mode = 0;
11 QuickStats stats; // initialize an instance of this class
12 long adc[1];
13 
14 float rainsum = 0;
15 float current = 0;
16 float current_mean = 0;
17 
18 
19 #ifndef WITH_RAINSUM
20 #define WITH_RAINSUM 0
21 #endif
22 
23 
24 #ifdef BayEOSLOGGER_H
25 #if WITH_RAINSUM
26 #define NUMBER_OF_CHANNELS 9
27 #else
28 #define NUMBER_OF_CHANNELS 5
29 #endif
30 
31 float values[NUMBER_OF_CHANNELS];
32 uint8_t counted;
33 
34 void delayBoard(unsigned long d) {
35  if (client.connected) {
36  unsigned long s = millis();
37  while ((millis() - s) < d) {
38  myLogger.handleCommand();
39  myLogger.sendBinaryDump();
40  }
41  } else {
42  delayLCB(d);
43  }
44 }
45 #else
46 void delayBoard(unsigned long d){
47  delayLCB(d);
48 }
49 #endif
50 
51 #ifndef BayRF24_h
52 #define WITH_CHECKSUM 0
53 #endif
54 
55 void handleAction0(void)
56 {
57  UNSET_ACTION(0); // clear the flag
58 #ifdef BayEOSLOGGER_H
59  if (myLogger._logged_flag)
60  {
61  myLogger._logged_flag = 0;
62  counted = 0;
63  for (uint8_t i = 0; i < NUMBER_OF_CHANNELS; i++)
64  {
65  values[i] = 0;
66  }
67  }
68 #endif
69 
70  digitalWrite(POWER_PIN, HIGH); // power up modem AND power divider (for battery voltage read)
71  analogReference(DEFAULT);
72  delayBoard(100);
73  bat_voltage = 3.3 * BAT_DIVIDER / 1023 * analogRead(A7); // Read battery voltage
74  digitalWrite(POWER_PIN, LOW); // power down modem AND power divider
75 
76  ntc.readResistance();
77  temp0 = ntc.getTemp(0);
78  t_sum_t1 += temp0;
79  current_t1i++;
80  if (current_t1i == TMEAN1)
81  {
82  t2[current_t2i] = t_sum_t1 / TMEAN1;
83  t_mean_t2 = stats.average(t2, TMEAN2);
84  current_t2i++;
85  if (current_t2i == TMEAN2)
86  {
87  current_t2i = 0;
88  t_mode = 1;
89  }
90  current_t1i = 0;
91  t_sum_t1 = 0;
92  }
93  scale.power_up();
94  scale.read_average(adc, 2);
95  // scale.read_average(adc);
96  uint8_t counts[1];
97  while (scale.read_average_with_filter(adc, 3000, counts) == 0xf0000000)
98  {
99  delayBoard(30);
100  }
101  scale.power_down();
102  client.startDataFrame(BayEOS_Float32le,WITH_CHECKSUM); // Start the data frame with float values
103  client.addChannelValue(millis()); // add uptime (ms) als 4 byte float
104 #ifdef BayEOSLOGGER_H
105  counted++;
106  myLogger._bat = bat_voltage * 1000;
107  values[0] += bat_voltage;
108  values[1] += temp0;
109  values[2] += adc[0];
110  values[3] += cal0.getWeight(adc[0], 20, 0);
111 #else
112  client.addChannelValue(bat_voltage); // add battery voltage
113  client.addChannelValue(temp0);
114 #ifdef BayRF24_h
115 #if WITH_CHECKSUM
116  client.addChecksum();
117 #endif
118  sendOrBufferLCB();
119  client.startDataFrame(BayEOS_Float32le,WITH_CHECKSUM); // Start the data frame with float values
120 #endif
121  client.addChannelValue(adc[0], 4);
122  client.addChannelValue(cal0.getWeight(adc[0], 20, 0));
123 #endif
124 
125  if (t_mode == 1)
126  {
127  uint8_t newest_t2i = current_t2i - 1;
128  if (newest_t2i > TMEAN2)
129  newest_t2i = TMEAN2 - 1;
130  float t_mean = (t_mean_t2 * TMEAN2 * TMEAN1 - t2[current_t2i] * current_t1i + t_sum_t1) / TMEAN1 / TMEAN2;
131  float dt = t_mean - ((t_mean_t2 * TMEAN2 * TMEAN1 - t2[current_t2i] * current_t1i - t2[newest_t2i] * (TMEAN1 - current_t1i)) / TMEAN1 / (TMEAN2 - 1));
132  y_cur[current_i] = cal0.getWeight(adc[0], t_mean, dt);
133 #ifdef BayEOSLOGGER_H
134  values[4] += y_cur[current_i];
135 #else
136  client.addChannelValue(y_cur[current_i]);
137 #ifdef BayRF24_h
138 #if WITH_CHECKSUM
139  client.addChecksum();
140 #endif
141  client.sendOrBuffer();
142  client.startDataFrame(BayEOS_Float32le,WITH_CHECKSUM); // Start the data frame with float values
143 #endif
144 
145 #endif
146  if (calc_mode >= 1)
147  current_mean = stats.average(y_cur, MITTELUNG);
148 
149  if (calc_mode == 3 && abs(current_mean - y_cur[current_i]) < MAX_DEVIATION / 5)
150  {
151  calc_mode = 2; // Back in the limit
152  current = current_mean;
153  }
154 
155  if (calc_mode == 2 && abs(current_mean - y_cur[current_i]) > MAX_DEVIATION)
156  calc_mode = 3; // To much deviation!
157 
158  current_i++;
159  if (current_i == MITTELUNG)
160  {
161  if (calc_mode == 0)
162  {
163  current = stats.average(y_cur, MITTELUNG);
164  calc_mode = 1;
165  }
166  current_i = 0;
167  }
168 #if WITH_RAINSUM
169  if (calc_mode == 1 && current_i == 1)
170  calc_mode = 2; // mean of MITTELUNG values available + next value
171  if (calc_mode == 2)
172  {
173  float diff = current_mean - current;
174 #ifdef BayEOSLOGGER_H
175  values[5] += current;
176  values[6] += diff * 3600 / SAMPLING_INT;
177 #else
178  client.addChannelValue(current, 7);
179  client.addChannelValue(diff * 3600 / SAMPLING_INT);
180 #ifdef BayRF24_h
181 #if WITH_CHECKSUM
182  client.addChecksum();
183 #endif
184  client.sendOrBuffer();
185  client.startDataFrame(BayEOS_Float32le,WITH_CHECKSUM); // Start the data frame with float values
186 #endif
187 #endif
188  float max_evap = -1 * MAX_EVAP * exp(17.62 * temp0 / (243.12 + temp0)) / exp(17.62 * 20 / (243.12 + 20)) * SAMPLING_INT / 3600;
189  if (diff < max_evap)
190  diff = max_evap; //
191  current += diff; // Mittelungswert anpassen
192  if (diff > 0 && diff < (MIN_SLOPE * SAMPLING_INT / 3600))
193  diff = 0;
194  if (diff > 0)
195  rainsum += diff;
196  if (diff < 0)
197  diff = 0;
198 #ifdef BayEOSLOGGER_H
199  values[7] += diff * 3600 / SAMPLING_INT;
200  values[8] += rainsum;
201 #else
202  client.addChannelValue(diff * 3600 / SAMPLING_INT, 9);
203  client.addChannelValue(rainsum);
204 #endif
205  }
206 #endif
207  }
208 #ifdef BayEOSLOGGER_H
209  for (uint8_t i = 0; i < NUMBER_OF_CHANNELS; i++)
210  {
211  client.addChannelValue(values[i] / counted);
212  }
213 #else
214 #ifdef BayRF24_h
215 #if WITH_CHECKSUM
216  client.addChecksum();
217 #endif
218  client.sendOrBuffer();
219 #endif
220 
221 #endif
222 }
uint8_t addChannelValue(float v, uint8_t channel_number=0)
Definition: BayEOS.cpp:61
void startDataFrame(uint8_t subtype=BayEOS_Float32le, uint8_t checksum=0)
Definition: BayEOS.cpp:12
uint8_t sendOrBuffer(void)
Definition: BayEOS.cpp:369