Buffer overflow windspeed fixed

This commit is contained in:
marcel
2024-01-21 16:05:37 +01:00
parent 74175ab241
commit e1331d6266
5 changed files with 51 additions and 32 deletions

View File

@@ -27,6 +27,11 @@
* You should have received a copy of the GNU General Public License
* along with weather_station. If not, see <https://www.gnu.org/licenses/>.
*
* 2023-01-21: - Buffer overflow when calculating average wind speed in AverageOfArray()
* Fix: use 32 bit register for average_value.
* - Changed some variables to the propper standard (uint8_t, uint16_t, etc.)
* - SparkFun wind interrupt now calculates over 3 seconds in stead of 1 second (KNMI standard)
*
**********************************************************************************/
#include <ModbusSerial.h>
#include "SparkFun_Weather_Meter_Kit_Arduino_Library.h"
@@ -114,28 +119,28 @@ ModbusSerial mb (MySerial, SlaveId, TxenPin);
unsigned long ts;
unsigned long HourTimer;
int WindGustData1[30];
unsigned char WindGustData1Counter=0;
int WindGustData2[10];
unsigned char WindGustData2Counter=0;
int WindAverageData1[30];
unsigned char WindAverageData1Counter=0;
int WindAverageData2[10];
unsigned char WindAverageData2Counter=0;
int RainPerHour[24];
unsigned char RainPerHourCounter=0;
uint16_t WindGustData1[30];
uint8_t WindGustData1Counter=0;
uint16_t WindGustData2[10];
uint8_t WindGustData2Counter=0;
uint16_t WindAverageData1[30];
uint8_t WindAverageData1Counter=0;
uint16_t WindAverageData2[10];
uint8_t WindAverageData2Counter=0;
uint16_t RainPerHour[24];
uint8_t RainPerHourCounter=0;
struct MeasuredData {
int WindDirection;
int WindSpeed;
int WindGust;
int Rain;
int RainLast24;
int SensorRainSinceMidnight;
int Pressure;
int Luminosity;
int StatusBits = 0;
unsigned int RainfallCounter = 0;
uint16_t WindDirection;
uint16_t WindSpeed;
uint16_t WindGust;
uint16_t Rain;
uint16_t RainLast24;
uint16_t SensorRainSinceMidnight;
uint16_t Pressure;
uint16_t Luminosity;
uint16_t StatusBits = 0;
uint16_t RainfallCounter = 0;
float Temperature;
float Humidity;
@@ -287,7 +292,7 @@ void ReadBMP280 (void)
}
int MaxOfArray (int array[], unsigned int length)
int MaxOfArray (int array[], uint16_t length)
{
int maximum_value = 0;
@@ -299,11 +304,11 @@ int MaxOfArray (int array[], unsigned int length)
return maximum_value;
}
int AverageOfArray (int array[], unsigned int length)
uint16_t AverageOfArray (uint16_t array[], uint16_t length)
{
int tmp_value = 0;
unsigned char tmp_length = length;
int average_value = 0;
uint32_t tmp_value = 0;
uint8_t tmp_length = length;
uint16_t average_value = 0;
while (length--)
{
@@ -329,7 +334,7 @@ void ReadSparkfunWeatherStation (void)
MeasuredData.Rain = tmpRegister;
// FIFO for calculating wind gust of last 10 minutes
// to preserve valuable RAM we caanot store all measurements of the last 10 minutes.
// to preserve valuable RAM we cannot store all measurements of the last 10 minutes.
// So we use a hack: store the last 30 values in a FIFO and every minute we store the maximum value from this FIFO in another FIFO.
// This second FIFO is 10 deep: it stores the maximum values of the last 10 minutes.
// The maximum value from this FIFO is the maximum wind gust of the last 10 minutes.
@@ -446,7 +451,7 @@ void setup() {
mb.Ireg (SensorRainSinceMidnightIreg, 0);
mb.Ireg (SensorSnowFallIreg, 0);
Serial.println(F("Weather station v0.2.1"));
Serial.println(F("Weather station v0.2.2"));
Serial.println(F("(C)2024 M.T. Konstapel"));
Serial.println(F("This project is free and open source"));
Serial.println(F("More details: https://meezenest.nl/mees/"));
@@ -553,7 +558,9 @@ void setup() {
// is implemented in the library that averages the wind speed over a certain
// time period, which defaults to 1 second. Longer intervals result in more
// accurate measurements, but cause delay in the measurement
calibrationParams.windSpeedMeasurementPeriodMillis = 1000;
// Dutch metrology institute (KNMI) defines that the windspeed and gust should
// be calculated from 3 seconds measurements.
calibrationParams.windSpeedMeasurementPeriodMillis = 3000;
// Now we can set all the calibration parameters at once
weatherMeterKit.setCalibrationParams(calibrationParams);