-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapteurs_meteo.cpp
More file actions
399 lines (333 loc) · 11.3 KB
/
capteurs_meteo.cpp
File metadata and controls
399 lines (333 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "WString.h"
/*
Capteurs_meteo
Weather sensors
*/
#include "capteurs_meteo.h"
volatile bool CAPTEURS_METEO::IsSampleRequired = false;
volatile unsigned int CAPTEURS_METEO::TimerCount = 0;
volatile unsigned long CAPTEURS_METEO::Rotations;
volatile unsigned long CAPTEURS_METEO::ContactBounceTime;
//+++++++++++ Constructor +++++++++++
CAPTEURS_METEO::CAPTEURS_METEO(uint16_t WLinstall, uint8_t pin_DS18B20)
: sht31(SHT31_ADDRESS, &Wire), //Initialization in the initialization list
oneWire_Teau(pin_DS18B20),
sensor_Teau(&oneWire_Teau),
_WLinstall(WLinstall)
{}
//++++++++++ Current values ++++++++++
float CAPTEURS_METEO::valTemp(){return TempMesure;}
float CAPTEURS_METEO::valHumid(){return HumidMesure;}
float CAPTEURS_METEO::valPatm(){return PatmMesure;}
float CAPTEURS_METEO::valRay(){return RayMesure;}
float CAPTEURS_METEO::valPyrano(){return PyranoMesure;}
float CAPTEURS_METEO::valTempWater(){return TempWaterMesure;}
float CAPTEURS_METEO::valVitesse(){return VitesseMesure;}
float CAPTEURS_METEO::valDirection(){return DirectionMesure;}
float CAPTEURS_METEO::valWaterVolt(){return WaterVoltMesure;}
float CAPTEURS_METEO::valWaterColonne(){return WaterColonneMesure;}
float CAPTEURS_METEO::valWaterHauteur(){return WaterHauteurMesure;}
//++++++++++ Averages ++++++++++
void CAPTEURS_METEO::resetSommes(){
SommeT = 0.0;
SommeHR = 0.0;
SommeL = 0.0;
SommeLW = 0.0;
SommeTW = 0.0;
SommeV = 0.0;
SommeD = 0.0;
SommeP = 0.0;
SommeWV = 0.0;
SommeWC = 0.0;
SommeWH = 0.0;
nbT = 0;
nbHR = 0;
nbL = 0;
nbLW = 0;
nbTW = 0;
nbV = 0;
nbD = 0;
nbP = 0;
nbWH = 0;
}
float CAPTEURS_METEO::meanTemp(){if(nbT > 0){return SommeT/nbT;} return 0;}
float CAPTEURS_METEO::meanHumid(){if(nbHR > 0){return SommeHR/nbHR;} return 0;}
float CAPTEURS_METEO::meanPatm(){if(nbP > 0){return SommeP/nbP;} return 0;}
float CAPTEURS_METEO::meanRay(){if(nbL > 0){return SommeL/nbL;} return 0;}
float CAPTEURS_METEO::meanTempWater(){if(nbTW > 0){return SommeTW/nbTW;} return 0;}
float CAPTEURS_METEO::meanPyrano(){if(nbLW > 0){return SommeLW/nbLW;} return 0;}
float CAPTEURS_METEO::meanVitesse(){if(nbV > 0){return SommeV/nbV;} return 0;}
float CAPTEURS_METEO::meanDirection(){if(nbD > 0){return SommeD/nbD;} return 0;}
float CAPTEURS_METEO::meanWaterVolt(){if(nbWH > 0){return SommeWV/nbWH;} return 0;}
float CAPTEURS_METEO::meanWaterColonne(){if(nbWH > 0){return SommeWC/nbWH;} return 0;}
float CAPTEURS_METEO::meanWaterHauteur(){if(nbWH > 0){return SommeWH/nbWH;} return 0;}
//++++++++++ Accumulations ++++++++++
void CAPTEURS_METEO::resetCumuls(){
dailyRain = 0.0; // clear daily-rain at midnight
dailyRain_till_LastHour = 0.0; // we do not want negative rain at 01:00
}
void CAPTEURS_METEO::setHcumulPluvio(){
hourlyRain = dailyRain - dailyRain_till_LastHour; // calculate the last hour's rain
dailyRain_till_LastHour = dailyRain; // update the rain till last hour for next calculation
}
double CAPTEURS_METEO::cumulHRain(){
return hourlyRain;
}
double CAPTEURS_METEO::cumulDRain(){
return dailyRain;
}
//++++++++++ VEML7700 ++++++++++
//VEML7700 Radiation Start-up Function
void CAPTEURS_METEO::initVEML7700(){
als.begin();
}
//acquisition VEML7700
void CAPTEURS_METEO::acqVEML7700(){
als.getALSLux(lux);//VEML7700
RayMesure = lux;
//summation
SommeL += RayMesure;
nbL++;
Serial.print(lux);Serial.print(F(" Lux\t"));
}
//++++++++++ Pyranometer Davis 6450 +++++++++
// Pyrano init function
void CAPTEURS_METEO::initPyrano(){
int rawValue = analogRead(PyranoPin);
//Filtering if value 1023 i.e. sensor not connected and therefore pull-up value
if(rawValue == 1023){Serial.print(F("## WARNING ! : Davis pyrano seems not to be connected!"));}
}
// Pyrano Davis 6450 acquisition
void CAPTEURS_METEO::acqPyrano(){
int rawValue = analogRead(PyranoPin); // Signal
// Filtering if value 1023 i.e. sensor not connected and therefore pull-up value
if(rawValue == 1023){rawValue = 0;}
float voltage = rawValue * (pyrReferenceVoltage / 1023);
// Offset correction
float correctedVoltage = voltage - pyrZeroOffset;
if (correctedVoltage < 0) correctedVoltage = 0;
// Conversion and calibration
float rawRadiation = correctedVoltage / pyrSensitivity;
PyranoMesure = rawRadiation;
// summation
SommeLW += PyranoMesure;
nbLW++;
Serial.print(PyranoMesure);Serial.print("W/m²\t");
}
//++++++++++ BME280 ++++++++++
// Init function BME280
void CAPTEURS_METEO::initBME280(){
if (!bme.begin()) {//BME280
while (1);
}
bme.setTempCal(-1);
}
// Acquisition BME280
void CAPTEURS_METEO::acqBME280(bool Ponly){
bme.readSensor();
PatmMesure = bme.getPressure_MB();
SommeP += PatmMesure;
nbP++;
if (!Ponly) {
TempMesure = bme.getTemperature_C();
HumidMesure = bme.getHumidity();
SommeT += TempMesure;
SommeHR += HumidMesure;
nbT++;
nbHR++;
Serial.print(TempMesure,1);Serial.print(F(" °C T_env bme280\t"));// T en °C
Serial.print(HumidMesure,1);Serial.print(F(" % H_env bme280\t"));// HR en %
}
Serial.print(PatmMesure, 1);Serial.print(" mbar P_env\t");// P en mbar
}
//++++++++++ SHT31 ++++++++++
// Init function SHT31
void CAPTEURS_METEO::initSHT31(){
if(sht31.begin() == false){Serial.println(F("SHT31 device address or reset pb."));}
uint16_t stat = sht31.readStatus();
Serial.print(stat, HEX);Serial.println();
}
// Acquisition SHT31
void CAPTEURS_METEO::acqSHT31(){
if(sht31.isConnected()){
sht31.read();
TempMesure = sht31.getTemperature();
HumidMesure = sht31.getHumidity();
//summation
SommeT += TempMesure;
SommeHR += HumidMesure;
nbT++;
nbHR++;
Serial.print(HumidMesure,1);Serial.print(F(" % H_env sht31\t"));// HR en %
Serial.print(TempMesure, 1); Serial.print(F(" °C T_env sht31\t")); // T en °C
}else{
Serial.println(F("Error : SHT31 Not connected! Try reset sensor."));
if(sht31.reset() == true){Serial.println(F("sht31 reset done"));}else{Serial.println(F("Failed to reset SHT31"));}
}
}
//++++++++++ SHT20/SEN0227 ++++++++++
// Init function SHT20
void CAPTEURS_METEO::initSHT20(){
sht20.initSHT20(); //SHT20/SEN0227
sht20.checkSHT20(); //SHT20/SEN0227
}
// Acquisition SHT20
void CAPTEURS_METEO::acqSHT20(){
TempMesure = sht20.readTemperature(); //SHT20/SEN0227
HumidMesure = sht20.readHumidity(); //SHT20/SEN0227
//summation
SommeT += TempMesure;
SommeHR += HumidMesure;
nbT++;
nbHR++;
Serial.print(HumidMesure, 1);Serial.print(F("% H_env sht20\t"));// HR en %
Serial.print(TempMesure, 1);Serial.print(F("*C T_env sht20\t"));// T en °C
}
//++++++++++ Tilting Pluviometer ++++++++++
// Init function pluviometer
void CAPTEURS_METEO::initPluvio(){
pinMode(RainPin, INPUT);//pluviometer read
}
void CAPTEURS_METEO::acqPluvio(){
//counting section
if ((bucketPositionA==false)&&(digitalRead(RainPin)==HIGH)){
bucketPositionA=true;
dailyRain+=bucketAmount; // update the daily rain
}
if ((bucketPositionA==true)&&(digitalRead(RainPin)==LOW)){
bucketPositionA=false;
}
}
//++++++++++ ANEMOMETER DAVIS ++++++++++
// Init function Wind speed and direction
void CAPTEURS_METEO::initVent1(){
LastValue = 0;
IsSampleRequired = false;
TimerCount = 0;
Rotations = 0;
}
void CAPTEURS_METEO::initVent2(){
pinMode(WindSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);
Timer1.initialize(500000);
Timer1.attachInterrupt(isr_timer);
}
// isr routine fr timer interrupt
void CAPTEURS_METEO::isr_timer() {
TimerCount++;
if(TimerCount == 5)
{
IsSampleRequired = true;
TimerCount = 0;
}
}
// This is the function that the interrupt calls to increment the rotation count
void CAPTEURS_METEO::isr_rotation() {
if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact.
Rotations++;
ContactBounceTime = millis();
}
}
// Get Wind Direction
void CAPTEURS_METEO::acqWindDirection() {
VaneValue = analogRead(WindVanePin);
Direction = map(VaneValue, 0, 1023, 0, 360);
CalDirection = Direction + VaneOffset;
if(CalDirection > 360)
CalDirection = CalDirection - 360;
if(CalDirection < 0)
CalDirection = CalDirection + 360;
}
// Converts compass direction to heading
void CAPTEURS_METEO::getHeading(int direction) {
if(direction < 22.5)
Serial.print(" N");
else if (direction < 45)
Serial.print(" NNE");
else if (direction < 67.5)
Serial.print(" NE");
else if (direction < 90)
Serial.print(" ENE");
else if (direction < 112.5)
Serial.print(" E");
else if (direction < 135)
Serial.print(" ESE");
else if (direction < 157.5)
Serial.print(" SE");
else if (direction < 180)
Serial.print(" SSE");
else if (direction < 180)
Serial.print(" SSE");
else if (direction < 202.5)
Serial.print(" S");
else if (direction < 225)
Serial.print(" SS0");
else if (direction < 247.5)
Serial.print(" S0");
else if (direction < 270)
Serial.print(" OSO");
else if (direction < 292.5)
Serial.print(" O");
else if (direction < 315)
Serial.print(" ONO");
else if (direction < 337.5)
Serial.print(" NO");
else if (direction < 360)
Serial.print(" NNO");
else
Serial.print(" N");
}
// Speed & Wind direction acquisition
void CAPTEURS_METEO::acqVent(){
CAPTEURS_METEO::acqWindDirection();
if(abs(CalDirection - LastValue) > 5){LastValue = CalDirection;}
DirectionMesure = CalDirection;
//summation
SommeD += DirectionMesure;
nbD++;
if(IsSampleRequired){
WindSpeed = ((Rotations * 0.1125)*1.60934); //Multiply by 1.60934 to convert to km/h. Formula for calculating wind speed: V = P(2.25/2.5) = P * 0.9
Rotations = 0;
IsSampleRequired = false;
VitesseMesure = WindSpeed;
//summation
SommeV += VitesseMesure;
nbV++;
Serial.print(WindSpeed);Serial.print(F(" km/h\t"));
getHeading(CalDirection);Serial.println(F("\t"));
}
}
//++++++++++ DS18B20 Soil temperature ++++++++++
// Init function DS18B20
void CAPTEURS_METEO::initDS18B20(){
sensor_Teau.begin();
}
// Acquisition DS18B20
void CAPTEURS_METEO::acqDS18B20Teau(){
sensor_Teau.requestTemperatures();
TempWaterMesure = sensor_Teau.getTempCByIndex(0);
//summation
SommeTW += TempWaterMesure;
nbTW++;
Serial.print(TempWaterMesure, 2);Serial.print(F("°C TW_env\t"));
}
//++++++++++ ADS1X15 + water level probe KIT0139 Franck Perret ++++++++++
void CAPTEURS_METEO::initADS(){
ads.begin();
}
// acquisition ADS with Kit0139
void CAPTEURS_METEO::acqADS_kit0139(){
float rawADC = ads.readADC_SingleEnded(0);
WaterVoltMesure = rawADC*0.0001875;
WaterColonneMesure = ((1.25*WaterVoltMesure)-1.25)*1000; //With a 250 ohm precision resistor, 5VDC power supply, be careful to connect the same ground
WaterHauteurMesure = WaterColonneMesure - _WLinstall; //3450 for piezo C5
//sommation
SommeWV += WaterVoltMesure;
SommeWC += WaterColonneMesure;
SommeWH += WaterHauteurMesure;
nbWH++;
//Serial.print(_WLinstall, 1);Serial.print(F(" WLinstall \t"));
Serial.print(WaterVoltMesure, 3);Serial.print(F(" Volt \t"));
Serial.print(WaterColonneMesure, 3);Serial.print(F(" WCol mm \t"));
Serial.print(WaterHauteurMesure, 1);Serial.println(F(" WL mm \t"));
}