-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherSensors.cpp
664 lines (601 loc) · 18.8 KB
/
WeatherSensors.cpp
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//
// Weather sensors class.
//
// MIT License
//
// Copyright(c) 2016 Mark Stevens
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "WeatherSensors.h"
//******************************************************************************
//
// Constructors etc.
//
//
// Default constructor.
//
WeatherSensors::WeatherSensors()
{
//
// Populate the wind direction lookup table. Each entry has the following form:
//
// midPoint, reading, angle, direction, direction as text
//
// Note that this table is order not in angles/wind direction but in terms of the
// mid points of the ADC readings found empirically. This allows for easier
// searching of the table.
//
// (char *) casts have been used in the wind direction names to stop a compiler
// warning.
//
_windDirectionLookupTable[0] = { 0, 68, 112.5, EastSouthEast, (char *) "East-South-East" };
_windDirectionLookupTable[1] = { 77, 87, 67.5, EastNorthEast, (char *) "East-North-East" };
_windDirectionLookupTable[2] = { 91, 96, 90, East, (char *) "East" };
_windDirectionLookupTable[3] = { 115, 135, 157.5, SouthSouthEast, (char *) "South-South-East" };
_windDirectionLookupTable[4] = { 165, 196, 135, SouthEast, (char *) "South-East" };
_windDirectionLookupTable[5] = { 227, 259, 202.5, SouthSouthWest, (char *) "South-South-West" };
_windDirectionLookupTable[6] = { 281, 304, 180, South, (char *) "South" };
_windDirectionLookupTable[7] = { 366, 429, 22.5, NorthNorthEast, (char *) "North-North-East" };
_windDirectionLookupTable[8] = { 456, 484, 45, NorthEast, (char *) "North-East" };
_windDirectionLookupTable[9] = { 553, 623, 247.5, WestSouthWest, (char *) "West-South-West" };
_windDirectionLookupTable[10] = { 640, 657, 225, SouthWest, (char *) "South-West" };
_windDirectionLookupTable[11] = { 693, 730, 337.5, NorthNorthWest, (char *) "North-North-West" };
_windDirectionLookupTable[12] = { 771, 812, 0, North, (char *) "North" };
_windDirectionLookupTable[13] = { 832, 852, 292.5, WestNorthWest, (char *) "West-North-West" };
_windDirectionLookupTable[14] = { 883, 914, 315, NorthWest, (char *) "North-West" };
_windDirectionLookupTable[15] = { 942, 970, 270, West, (char *) "West" };
//
// Make A0 an input pin so that we can read the wind direction.
//
pinMode(A0, INPUT);
//
// Clear the sesnosr readings to 0.
//
_ultraviolet = 0;
_windSpeedPulseCount = 0;
_pluviometerPulseCount = 0;
_pluviometerPulseCountToday = 0;
}
//
// Destructor - clean up any memory / objects in use.
//
WeatherSensors::~WeatherSensors()
{
delete(_groundSensor);
}
//******************************************************************************
//
// General methods (initialisation etc.)
//
//
// Initialise all of the weather sensors.
//
void WeatherSensors::InitialiseSensors()
{
SetupGroundTemperatureSensor();
SetupLuminositySensor();
SetupTemperatureHumidityPressureSensor();
//SetupRainfallSensor();
//SeupWindSpeedSensor();
}
//
// Read all of the sensors.
//
// This method forces all of the sensor readings to be taken in one go.
//
void WeatherSensors::ReadAllSensors()
{
//ReadSTM8SSensors();
ReadGroundTemperatureSensor();
yield();
ReadLuminositySensor();
yield();
ReadTemperatureHumidityPressureSensor();
yield();
//ReadUltravioletLightSensor();
//ReadRainfallSensor();
//ReadWindSpeedSensor();
ReadWindDirection();
yield();
}
//******************************************************************************
//
// DS18B20 temperature sensor.
//
// Setup the DS18B20 sensor (ground temperature).
//
void WeatherSensors::SetupGroundTemperatureSensor()
{
int index;
String message;
char number[20];
_groundSensor = new OneWire(7);
if (!_groundSensor->search(_groundTemperatureSensorAddress))
{
Debugger::DebugMessage("No more addresses.");
_groundSensor->reset_search();
delay(250);
return;
}
message = "ROM =";
for (index = 0; index < 8; index++)
{
message += " ";
message += itoa(_groundTemperatureSensorAddress[index], number, 16);
}
Debugger::DebugMessage(message);
if (OneWire::crc8(_groundTemperatureSensorAddress, 7) != _groundTemperatureSensorAddress[7])
{
Debugger::DebugMessage("CRC is not valid!");
return;
}
switch (_groundTemperatureSensorAddress[0])
{
case 0x10:
Debugger::DebugMessage("Chip = DS18S20");
_groundTemperatureSensorType = 1;
break;
case 0x28:
Debugger::DebugMessage("Chip = DS18B20");
_groundTemperatureSensorType = 0;
break;
case 0x22:
Debugger::DebugMessage("Chip = DS1822");
_groundTemperatureSensorType = 0;
break;
default:
Debugger::DebugMessage("Device is not a DS18x20 family device.");
return;
}
}
//
// Read the ground temperature from the DS18B20 sensor.
//
float WeatherSensors::ReadGroundTemperatureSensor()
{
int index;
byte data[12];
byte present = 0;
String message;
char number[20];
_groundSensor->reset();
_groundSensor->select(_groundTemperatureSensorAddress);
_groundSensor->write(0x44, 1); // Start conversion, with parasite power on at the end
delay(1000); // Maybe 750ms is enough, maybe not
present = _groundSensor->reset();
_groundSensor->select(_groundTemperatureSensorAddress);
_groundSensor->write(0xBE); // Read Scratchpad
message = "DS18B20 Data = ";
message += itoa(present, number, 16);
message += " ";
for (index = 0; index < 9; index++)
{
data[index] = _groundSensor->read();
message += itoa(data[index], number, 16);
message += " ";
}
message += " CRC=";
message += itoa(OneWire::crc8(data, 8), number, 16);
Debugger::DebugMessage(message);
if (OneWire::crc8(data, 8) == data[8])
{
int16_t raw = (data[1] << 8) | data[0];
if (_groundTemperatureSensorType)
{
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10)
{
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
}
else
{
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
switch (cfg)
{
case 0x00:
raw = raw & ~7; // 9 bit resolution, 93.75 ms
break;
case 0x20:
raw = raw & ~3; // 10 bit res, 187.5 ms
break;
case 0x40:
raw = raw & ~1; // 11 bit res, 375 ms
break;
}
//// default is 12 bit resolution, 750 ms conversion time
}
_groundTemperature = (double) (raw / 16.0);
}
else
{
Debugger::DebugMessage("DS18B20 CRC failure");
}
return(_groundTemperature);
}
//
// Get the last ground temperature reading.
//
float WeatherSensors::GetGroundTemperatureReading()
{
return(_groundTemperature);
}
//******************************************************************************
//
// Ultraviolet light sensor.
//
//
// Read the ultraviolet light sensor.
//
float WeatherSensors::ReadUltravioletLightSensor()
{
return(_ultraviolet);
}
//
// Get the last value read from the UV sensor.
//
float WeatherSensors::GetUltravioletLightReading()
{
return(_ultraviolet);
}
//
// Convert the ultraviolet light sensor reading into mW/cm2.
//
float WeatherSensors::GetUltravioletLightStrength()
{
float uvStrength = _ultraviolet * _voltsPerDivision;
if (uvStrength < _uvOffset)
{
uvStrength = 0;
}
else
{
uvStrength = (uvStrength - _uvOffset) / _uvGradient;
}
return(uvStrength);
}
//******************************************************************************
//
// Get data from the STM8S.
//
// Sensors connected to the STM8S:
//
// 1 - Wind Speed
// 2 - Wind Direction
// 3 - Ultraviolet Light
// 4 - Rain Guage (Pluviometer)
//
void WeatherSensors::ReadSTM8SSensors()
{
uint8_t buffer[I2CBufferSize];
Wire.beginTransmission(STM8SAddress);
Wire.write(I2CGetSensorData);
Wire.endTransmission();
Wire.requestFrom(STM8SAddress, I2CBufferSize);
bool allFF = true;
bool allAA = true;
for (int index = 0; index < I2CBufferSize; index++)
{
buffer[index] = Wire.read();
allFF = (allFF && (0xff == buffer[index]));
allAA = (allAA && (0xaa == buffer[index]));
}
Debugger::DebugMessage("STM8S Sensor data", buffer, I2CBufferSize);
if (allFF)
{
Debugger::DebugMessage("There is a problem with the I2C readings from the STM8S.");
}
else if (allAA)
{
Debugger::DebugMessage("STM8S data is not ready.");
}
else
{
_windSpeedPulseCount = (buffer[I2CWindSpeedMSB] * 256) + buffer[I2CWindSpeedLSB];
_pluviometerPulseCount = (buffer[I2CRainfallCounterMSB] * 256) + buffer[I2CRainfallCounterLSB];
_pluviometerPulseCountToday = _pluviometerPulseCount;
_ultraviolet = (buffer[I2CUVReadingMSB] * 256) + buffer[I2CUVReadingLSB];
}
}
//******************************************************************************
//
// Sparkfun Luminosity sensor.
//
//
// Translate the error code into a meaningful message.
//
String WeatherSensors::LuminositySensorErrorMessage(byte error)
{
switch (error)
{
case 0:
return("TSL2561 Error: Success");
case 1:
return("TSL2561 Error: Data too long for transmit buffer");
case 2:
return("TSL2561 Error: Received NACK on address (disconnected?)");
case 3:
return("TSL2561 Error: Received NACK on data");
case 4:
return("TSL2561 Error: Other error");
default:
return("TSL2561 Error: Unknown error code");
}
}
//
// Set up the luminsoity sensor.
//
void WeatherSensors::SetupLuminositySensor()
{
char buffer[256];
_light.begin();
// Get factory ID from sensor:
// (Just for fun, you don't need to do this to operate the sensor)
unsigned char id;
if (_light.getID(id))
{
sprintf(buffer, "Retrieved TSL2561 device ID: 0x%x", id);
Debugger::DebugMessage(buffer);
}
else
{
byte error = _light.getError();
Debugger::DebugMessage(LuminositySensorErrorMessage(error));
}
// The light sensor has a default integration time of 402ms,
// and a default gain of low (1X).
// If you would like to change either of these, you can
// do so using the setTiming() command.
// If gain = false (0), device is set to low gain (1X)
// If gain = high (1), device is set to high gain (16X)
_gain = 0;
// If time = 0, integration will be 13.7ms
// If time = 1, integration will be 101ms
// If time = 2, integration will be 402ms
// If time = 3, use manual start / stop to perform your own integration
unsigned char time = 2;
// setTiming() will set the third parameter (ms) to the
// requested integration time in ms (this will be useful later):
_light.setTiming(_gain, time, _ms);
// To start taking measurements, power up the sensor:
Debugger::DebugMessage((char *) "Powering up the luminosity sensor.");
_light.setPowerUp();
}
//
// Read the luminosity frolm the TSL2561 luminosity sensor.
//
double WeatherSensors::ReadLuminositySensor()
{
unsigned int data0, data1;
String message;
char number[20];
if (_light.getData(data0, data1))
{
//
// To calculate lux, pass all your settings and readings to the getLux() function.
//
// The getLux() function will return 1 if the calculation was successful, or 0 if one or both of the sensors was
// saturated (too much light). If this happens, you can reduce the integration time and/or gain.
// For more information see the hookup guide at:
// https://learn.sparkfun.com/tutorials/getting-started-with-the-tsl2561-luminosity-sensor
//
// Perform lux calculation.
//
double localLux;
_good = _light.getLux(_gain, _ms, data0, data1, localLux);
if (_good)
{
_lux = localLux;
}
}
else
{
byte error = _light.getError();
Debugger::DebugMessage(LuminositySensorErrorMessage(error));
}
return(_lux);
}
//
// Get the last luminosity reading.
//
double WeatherSensors::GetLuminosityReading()
{
return(_lux);
}
//******************************************************************************
//
// BME280 temperature, air pressure and humidity sensor.
//
//
// Setup the Adafruit BME280 Temperature, pressure and humidity sensor.
//
void WeatherSensors::SetupTemperatureHumidityPressureSensor()
{
if (!_bme.begin())
{
Debugger::DebugMessage("Could not find a valid BME280 sensor, check wiring!");
}
else
{
Debugger::DebugMessage("BME280 sensor located on I2C bus.");
}
_temperature = 0;
_pressure = 0;
_humidity = 0;
}
//
// Read the data from the Temperature, pressure and humidity sensor.
//
void WeatherSensors::ReadTemperatureHumidityPressureSensor()
{
_temperature = _bme.readTemperature();
_pressure = _bme.readPressure();
_humidity = _bme.readHumidity();
}
//
// Get the last air temperature reading.
//
float WeatherSensors::GetAirTemperature()
{
return(_temperature);
}
//
// Get the last humidity reading.
//
float WeatherSensors::GetHumidity()
{
return(_humidity);
}
//
// Get thje last air pressure reading.
//
float WeatherSensors::GetAirPressure()
{
return(_pressure);
}
//******************************************************************************
//
// Pluvometer (Rainfall) sensor.
//
// Set up the pluiometer, zero the counts and then attached and initialise the
// counter to the output expander.
//
void WeatherSensors::SetupRainfallSensor()
{
}
//
// Read the counter from the Rainfall sensor then reset the count.
//
// It is theoretically possible for the counter to change while it is being read.
// This can give an incorrect reading so the counter is read twice in succession, if
// the two readings are the same then we can assume that the reading is correct.
//
void WeatherSensors::ReadRainfallSensor()
{
}
//
// Reset the pluviometer pulse counter.
//
void WeatherSensors::ResetPluviometerPulseCounter()
{
_pluviometerPulseCount = 0;
_pluviometerPulseCountToday = 0;
}
//
// Convert the number of pulses into mm of rain.
//
float WeatherSensors::CalculateRainfall(int pulseCount)
{
return(pulseCount * RAINFALL_TIPPER_MM_PER_PULSE);
}
//
// Get the rainfal in mm between the last sensor reading and this one.
//
float WeatherSensors::GetRainfall()
{
return(CalculateRainfall(_pluviometerPulseCount));
}
//
// Get the total rainfall in mm today.
//
float WeatherSensors::GetTotalRainfallToday()
{
return(CalculateRainfall(_pluviometerPulseCountToday));
}
//******************************************************************************
//
// Windspeed sensor.
//
//
// Set up the anemometer.
//
void WeatherSensors::SeupWindSpeedSensor()
{
pinMode(PIN_ANEMOMETER, INPUT);
_windSpeedPulseCount = 0;
WindSpeedISR = NULL;
}
//
// Read the wind speed.
//
float WeatherSensors::ReadWindSpeedSensor()
{
_windSpeedPulseCount = 0;
if (WindSpeedISR != NULL)
{
attachInterrupt(PIN_ANEMOMETER, WindSpeedISR, RISING);
delay(WINDSPEED_DURATION * 1000);
detachInterrupt(PIN_ANEMOMETER);
}
}
//
// Get the last wind speed reading.
//
float WeatherSensors::GetWindSpeed()
{
return(_windSpeedPulseCount * WINDSPEED_PER_PULSE);
}
//
// Set the Wind speed ISR
//
void WeatherSensors::SetWindSpeedISR(void (*isr)())
{
WindSpeedISR = isr;
}
//
// Called by the Wind speed ISR.
//
void WeatherSensors::HandleWindSpeedInterrupt()
{
_windSpeedPulseCount++;
}
//******************************************************************************
//
// Wind direction sensor.
//
//
// Read the wind direction sensor and calculate the direction the vane is pointing.
//
WeatherSensors::WindDirection WeatherSensors::ReadWindDirection()
{
int windDirection = analogRead(A0);
Debugger::DebugMessage("Wind direction ADC reading: ", windDirection, 10, "");
Debugger::DebugMessage("Wind direction (volts): ", windDirection * _voltsPerDivision, 4u, "V");
_windDirectionLookupEntry = 15;
for (int index = 0; index < 15; index++)
{
if ((windDirection > _windDirectionLookupTable[index].midPoint) && (windDirection <= _windDirectionLookupTable[index + 1].midPoint))
{
_windDirectionLookupEntry = index;
break;
}
}
Debugger::DebugMessage("Wind is blowing from " + String(_windDirectionLookupTable[_windDirectionLookupEntry].directionAsText));
return(_windDirectionLookupTable[_windDirectionLookupEntry].direction);
}
//
// Get the last wind direction reading as a textural description.
//
char *WeatherSensors::GetWindDirectionAsString()
{
return(_windDirectionLookupTable[_windDirectionLookupEntry].directionAsText);
}