Skip to content
Snippets Groups Projects
Commit 702a05c7 authored by user2684's avatar user2684
Browse files

Fixed multiple BMP/BME issues #85 #86

parent d296022b
No related branches found
No related tags found
No related merge requests found
...@@ -873,16 +873,15 @@ SensorLatchingRelay::SensorLatchingRelay(int child_id, int pin): SensorRelay(chi ...@@ -873,16 +873,15 @@ SensorLatchingRelay::SensorLatchingRelay(int child_id, int pin): SensorRelay(chi
SensorDHT::SensorDHT(int child_id, int pin, DHT* dht, int sensor_type, int dht_type): Sensor(child_id, pin) { SensorDHT::SensorDHT(int child_id, int pin, DHT* dht, int sensor_type, int dht_type): Sensor(child_id, pin) {
// store the dht object // store the dht object
_dht = dht; _dht = dht;
// store the sensor type (0: temperature, 1: humidity)
_sensor_type = sensor_type; _sensor_type = sensor_type;
_dht_type = dht_type; _dht_type = dht_type;
if (_sensor_type == 0) { if (_sensor_type == SensorDHT::TEMPERATURE) {
// temperature sensor // temperature sensor
setPresentation(S_TEMP); setPresentation(S_TEMP);
setType(V_TEMP); setType(V_TEMP);
setValueType(TYPE_FLOAT); setValueType(TYPE_FLOAT);
} }
else if (_sensor_type == 1) { else if (_sensor_type == SensorDHT::HUMIDITY) {
// humidity sensor // humidity sensor
setPresentation(S_HUM); setPresentation(S_HUM);
setType(V_HUM); setType(V_HUM);
...@@ -903,7 +902,7 @@ void SensorDHT::onSetup() { ...@@ -903,7 +902,7 @@ void SensorDHT::onSetup() {
// what do to during loop // what do to during loop
void SensorDHT::onLoop() { void SensorDHT::onLoop() {
// temperature sensor // temperature sensor
if (_sensor_type == 0) { if (_sensor_type == SensorDHT::TEMPERATURE) {
// read the temperature // read the temperature
float temperature = _dht->readTemperature(); float temperature = _dht->readTemperature();
// convert it // convert it
...@@ -918,7 +917,7 @@ void SensorDHT::onLoop() { ...@@ -918,7 +917,7 @@ void SensorDHT::onLoop() {
if (! isnan(temperature)) _value_float = temperature; if (! isnan(temperature)) _value_float = temperature;
} }
// humidity sensor // humidity sensor
else if (_sensor_type == 1) { else if (_sensor_type == SensorDHT::HUMIDITY) {
// read humidity // read humidity
float humidity = _dht->readHumidity(); float humidity = _dht->readHumidity();
if (isnan(humidity)) return; if (isnan(humidity)) return;
...@@ -947,13 +946,13 @@ void SensorDHT::onReceive(const MyMessage & message) { ...@@ -947,13 +946,13 @@ void SensorDHT::onReceive(const MyMessage & message) {
SensorSHT21::SensorSHT21(int child_id, int sensor_type): Sensor(child_id,A2) { SensorSHT21::SensorSHT21(int child_id, int sensor_type): Sensor(child_id,A2) {
// store the sensor type (0: temperature, 1: humidity) // store the sensor type (0: temperature, 1: humidity)
_sensor_type = sensor_type; _sensor_type = sensor_type;
if (_sensor_type == 0) { if (_sensor_type == SensorSHT21::TEMPERATURE) {
// temperature sensor // temperature sensor
setPresentation(S_TEMP); setPresentation(S_TEMP);
setType(V_TEMP); setType(V_TEMP);
setValueType(TYPE_FLOAT); setValueType(TYPE_FLOAT);
} }
else if (_sensor_type == 1) { else if (_sensor_type == SensorSHT21::HUMIDITY) {
// humidity sensor // humidity sensor
setPresentation(S_HUM); setPresentation(S_HUM);
setType(V_HUM); setType(V_HUM);
...@@ -974,7 +973,7 @@ void SensorSHT21::onSetup() { ...@@ -974,7 +973,7 @@ void SensorSHT21::onSetup() {
// what do to during loop // what do to during loop
void SensorSHT21::onLoop() { void SensorSHT21::onLoop() {
// temperature sensor // temperature sensor
if (_sensor_type == 0) { if (_sensor_type == SensorSHT21::TEMPERATURE) {
// read the temperature // read the temperature
float temperature = SHT2x.GetTemperature(); float temperature = SHT2x.GetTemperature();
// convert it // convert it
...@@ -989,7 +988,7 @@ void SensorSHT21::onLoop() { ...@@ -989,7 +988,7 @@ void SensorSHT21::onLoop() {
if (! isnan(temperature)) _value_float = temperature; if (! isnan(temperature)) _value_float = temperature;
} }
// Humidity Sensor // Humidity Sensor
else if (_sensor_type == 1) { else if (_sensor_type == SensorSHT21::HUMIDITY) {
// read humidity // read humidity
float humidity = SHT2x.GetHumidity(); float humidity = SHT2x.GetHumidity();
if (isnan(humidity)) return; if (isnan(humidity)) return;
...@@ -1223,7 +1222,6 @@ void SensorBH1750::onReceive(const MyMessage & message) { ...@@ -1223,7 +1222,6 @@ void SensorBH1750::onReceive(const MyMessage & message) {
#if MODULE_MLX90614 == 1 #if MODULE_MLX90614 == 1
// contructor // contructor
SensorMLX90614::SensorMLX90614(int child_id, Adafruit_MLX90614* mlx, int sensor_type): Sensor(child_id,A4) { SensorMLX90614::SensorMLX90614(int child_id, Adafruit_MLX90614* mlx, int sensor_type): Sensor(child_id,A4) {
// store the sensor type (0: ambient, 1: object)
_sensor_type = sensor_type; _sensor_type = sensor_type;
_mlx = mlx; _mlx = mlx;
// set presentation and type // set presentation and type
...@@ -1244,7 +1242,7 @@ void SensorMLX90614::onSetup() { ...@@ -1244,7 +1242,7 @@ void SensorMLX90614::onSetup() {
// what do to during loop // what do to during loop
void SensorMLX90614::onLoop() { void SensorMLX90614::onLoop() {
float temperature = _sensor_type == 0 ? _mlx->readAmbientTempC() : _mlx->readObjectTempC(); float temperature = _sensor_type == SensorMLX90614::TEMPERATURE_OBJECT ? _mlx->readAmbientTempC() : _mlx->readObjectTempC();
// convert it // convert it
if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32; if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
#if DEBUG == 1 #if DEBUG == 1
...@@ -1264,32 +1262,31 @@ void SensorMLX90614::onReceive(const MyMessage & message) { ...@@ -1264,32 +1262,31 @@ void SensorMLX90614::onReceive(const MyMessage & message) {
/* /*
SensorBME280 SensorBosch
*/ */
#if MODULE_BME280 == 1 #if MODULE_BME280 == 1 || MODULE_BMP085 == 1
// contructor // contructor
SensorBME280::SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type): Sensor(child_id,A4) { SensorBosch::SensorBosch(int child_id, int sensor_type): Sensor(child_id,A4) {
// sensor type (0: temperature, 1: humidity, 2: pressure, 3: forecast)
_sensor_type = sensor_type; _sensor_type = sensor_type;
if (_sensor_type == 0) { if (_sensor_type == SensorBosch::TEMPERATURE) {
// temperature sensor // temperature sensor
setPresentation(S_TEMP); setPresentation(S_TEMP);
setType(V_TEMP); setType(V_TEMP);
setValueType(TYPE_FLOAT); setValueType(TYPE_FLOAT);
} }
else if (_sensor_type == 1) { else if (_sensor_type == SensorBosch::HUMIDITY) {
// humidity sensor // humidity sensor
setPresentation(S_HUM); setPresentation(S_HUM);
setType(V_HUM); setType(V_HUM);
setValueType(TYPE_FLOAT); setValueType(TYPE_FLOAT);
} }
else if (_sensor_type == 2) { else if (_sensor_type == SensorBosch::PRESSURE) {
// pressure sensor // pressure sensor
setPresentation(S_BARO); setPresentation(S_BARO);
setType(V_PRESSURE); setType(V_PRESSURE);
setValueType(TYPE_FLOAT); setValueType(TYPE_FLOAT);
} }
else if (_sensor_type == 3) { else if (_sensor_type == SensorBosch::FORECAST) {
// pressure sensor // pressure sensor
setPresentation(S_BARO); setPresentation(S_BARO);
setType(V_FORECAST); setType(V_FORECAST);
...@@ -1298,24 +1295,149 @@ SensorBME280::SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type): ...@@ -1298,24 +1295,149 @@ SensorBME280::SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type):
} }
// setter/getter // setter/getter
void SensorBME280::setForecastSamplesCount(int value) { void SensorBosch::setForecastSamplesCount(int value) {
_forecast_samples_count = value; _forecast_samples_count = value;
} }
// what do to during before // what do to during before
void SensorBME280::onBefore() { void SensorBosch::onBefore() {
// initialize the forecast samples array // initialize the forecast samples array
_forecast_samples = new float[_forecast_samples_count]; _forecast_samples = new float[_forecast_samples_count];
} }
// what do to during setup // what do to during setup
void SensorBME280::onSetup() { void SensorBosch::onSetup() {
} }
// what do to during loop // what do to during loop
void SensorBosch::onLoop() {
}
// what do to as the main task when receiving a message
void SensorBosch::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
void SensorBosch::_forecast(float pressure) {
if (isnan(pressure)) return;
// Calculate the average of the last n minutes.
int index = _minute_count % _forecast_samples_count;
_forecast_samples[index] = pressure;
_minute_count++;
if (_minute_count > 185) _minute_count = 6;
if (_minute_count == 5) _pressure_avg = _getLastPressureSamplesAverage();
else if (_minute_count == 35) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
// first time initial 3 hour
if (_first_round) _dP_dt = change * 2; // note this is for t = 0.5hour
else _dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value.
}
else if (_minute_count == 65) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
//first time initial 3 hour
if (_first_round) _dP_dt = change; //note this is for t = 1 hour
else _dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value
}
else if (_minute_count == 95) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
// first time initial 3 hour
if (_first_round)_dP_dt = change / 1.5; // note this is for t = 1.5 hour
else _dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value
}
else if (_minute_count == 125) {
float last_pressure_avg = _getLastPressureSamplesAverage();
// store for later use.
_pressure_avg2 = last_pressure_avg;
float change = (last_pressure_avg - _pressure_avg) * 0.1;
if (_first_round) _dP_dt = change / 2; // note this is for t = 2 hour
else _dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value
}
else if (_minute_count == 155) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
if (_first_round) _dP_dt = change / 2.5; // note this is for t = 2.5 hour
else _dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value
}
else if (_minute_count == 185) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
if (_first_round) _dP_dt = change / 3; // note this is for t = 3 hour
else _dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value
}
// Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
_pressure_avg = _pressure_avg2;
// flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
_first_round = false;
// calculate the forecast (STABLE = 0, SUNNY = 1, CLOUDY = 2, UNSTABLE = 3, THUNDERSTORM = 4, UNKNOWN = 5)
int forecast = 5;
//if time is less than 35 min on the first 3 hour interval.
if (_minute_count < 35 && _first_round) forecast = 5;
else if (_dP_dt < (-0.25)) forecast = 5;
else if (_dP_dt > 0.25) forecast = 4;
else if ((_dP_dt > (-0.25)) && (_dP_dt < (-0.05))) forecast = 2;
else if ((_dP_dt > 0.05) && (_dP_dt < 0.25)) forecast = 1;
else if ((_dP_dt >(-0.05)) && (_dP_dt < 0.05)) forecast = 0;
else forecast = 5;
_value_string = _weather[forecast];
#if DEBUG == 1
Serial.print(F("BMP I="));
Serial.print(_child_id);
Serial.print(F(" M="));
Serial.print(_minute_count);
Serial.print(F(" dP="));
Serial.print(_dP_dt);
Serial.print(F(" F="));
Serial.println(_value_string);
#endif
}
// returns the average of the latest pressure samples
float SensorBosch::_getLastPressureSamplesAverage() {
float avg = 0;
for (int i = 0; i < _forecast_samples_count; i++) avg += _forecast_samples[i];
avg /= _forecast_samples_count;
return avg;
}
// search for a given chip on i2c bus
uint8_t SensorBosch::GetI2CAddress(uint8_t chip_id) {
uint8_t addresses[] = {0x77, 0x76};
uint8_t register_address = 0xD0;
for (int i = 0; i <= sizeof(addresses); i++) {
uint8_t i2c_address = addresses[i];
uint8_t value;
Wire.beginTransmission((uint8_t)i2c_address);
Wire.write((uint8_t)register_address);
Wire.endTransmission();
Wire.requestFrom((uint8_t)i2c_address, (byte)1);
value = Wire.read();
if (value == chip_id) {
#if DEBUG == 1
Serial.print(F("I2C="));
Serial.println(i2c_address);
#endif
return i2c_address;
}
}
return addresses[0];
}
#endif
/*
* SensorBME280
*/
#if MODULE_BME280 == 1
SensorBME280::SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type): SensorBosch(child_id,sensor_type) {
_bme = bme;
}
void SensorBME280::onLoop() { void SensorBME280::onLoop() {
// temperature sensor // temperature sensor
if (_sensor_type == 0) { if (_sensor_type == SensorBME280::TEMPERATURE) {
// read the temperature // read the temperature
float temperature = _bme->readTemperature(); float temperature = _bme->readTemperature();
// convert it // convert it
...@@ -1326,25 +1448,26 @@ void SensorBME280::onLoop() { ...@@ -1326,25 +1448,26 @@ void SensorBME280::onLoop() {
Serial.print(F(" T=")); Serial.print(F(" T="));
Serial.println(temperature); Serial.println(temperature);
#endif #endif
if (isnan(temperature)) return;
// store the value // store the value
if (! isnan(temperature)) _value_float = temperature; _value_float = temperature;
} }
// Humidity Sensor // Humidity Sensor
else if (_sensor_type == 1) { else if (_sensor_type == SensorBME280::HUMIDITY) {
// read humidity // read humidity
float humidity = _bme->readHumidity(); float humidity = _bme->readHumidity();
if (isnan(humidity)) return;
#if DEBUG == 1 #if DEBUG == 1
Serial.print(F("BME I=")); Serial.print(F("BME I="));
Serial.print(_child_id); Serial.print(_child_id);
Serial.print(F(" H=")); Serial.print(F(" H="));
Serial.println(humidity); Serial.println(humidity);
#endif #endif
if (isnan(humidity)) return;
// store the value // store the value
if (! isnan(humidity)) _value_float = humidity; _value_float = humidity;
} }
// Pressure Sensor // Pressure Sensor
else if (_sensor_type == 2) { else if (_sensor_type == SensorBME280::PRESSURE) {
// read pressure // read pressure
float pressure = _bme->readPressure() / 100.0F; float pressure = _bme->readPressure() / 100.0F;
if (isnan(pressure)) return; if (isnan(pressure)) return;
...@@ -1354,100 +1477,64 @@ void SensorBME280::onLoop() { ...@@ -1354,100 +1477,64 @@ void SensorBME280::onLoop() {
Serial.print(F(" P=")); Serial.print(F(" P="));
Serial.println(pressure); Serial.println(pressure);
#endif #endif
if (isnan(pressure)) return;
// store the value // store the value
if (! isnan(pressure)) _value_float = pressure; _value_float = pressure;
} }
// Forecast Sensor // Forecast Sensor
else if (_sensor_type == 3) { else if (_sensor_type == SensorBME280::FORECAST) {
// read pressure
float pressure = _bme->readPressure() / 100.0F; float pressure = _bme->readPressure() / 100.0F;
if (isnan(pressure)) return; _forecast(pressure);
// Calculate the average of the last n minutes.
int index = _minute_count % _forecast_samples_count;
_forecast_samples[index] = pressure;
_minute_count++;
if (_minute_count > 185) _minute_count = 6;
if (_minute_count == 5) _pressure_avg = _getLastPressureSamplesAverage();
else if (_minute_count == 35) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
// first time initial 3 hour
if (_first_round) _dP_dt = change * 2; // note this is for t = 0.5hour
else _dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value.
}
else if (_minute_count == 65) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
//first time initial 3 hour
if (_first_round) _dP_dt = change; //note this is for t = 1 hour
else _dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value
}
else if (_minute_count == 95) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
// first time initial 3 hour
if (_first_round)_dP_dt = change / 1.5; // note this is for t = 1.5 hour
else _dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value
}
else if (_minute_count == 125) {
float last_pressure_avg = _getLastPressureSamplesAverage();
// store for later use.
_pressure_avg2 = last_pressure_avg;
float change = (last_pressure_avg - _pressure_avg) * 0.1;
if (_first_round) _dP_dt = change / 2; // note this is for t = 2 hour
else _dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value
}
else if (_minute_count == 155) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
if (_first_round) _dP_dt = change / 2.5; // note this is for t = 2.5 hour
else _dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value
}
else if (_minute_count == 185) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
if (_first_round) _dP_dt = change / 3; // note this is for t = 3 hour
else _dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value
}
// Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
_pressure_avg = _pressure_avg2;
// flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
_first_round = false;
// calculate the forecast (STABLE = 0, SUNNY = 1, CLOUDY = 2, UNSTABLE = 3, THUNDERSTORM = 4, UNKNOWN = 5)
int forecast = 5;
//if time is less than 35 min on the first 3 hour interval.
if (_minute_count < 35 && _first_round) forecast = 5;
else if (_dP_dt < (-0.25)) forecast = 5;
else if (_dP_dt > 0.25) forecast = 4;
else if ((_dP_dt > (-0.25)) && (_dP_dt < (-0.05))) forecast = 2;
else if ((_dP_dt > 0.05) && (_dP_dt < 0.25)) forecast = 1;
else if ((_dP_dt >(-0.05)) && (_dP_dt < 0.05)) forecast = 0;
else forecast = 5;
_value_string = _weather[forecast];
#if DEBUG == 1
Serial.print(F("BME I="));
Serial.print(_child_id);
Serial.print(F(" M="));
Serial.print(_minute_count);
Serial.print(F(" dP="));
Serial.print(_dP_dt);
Serial.print(F(" F="));
Serial.println(_value_string);
#endif
} }
} }
#endif
// what do to as the main task when receiving a message /*
void SensorBME280::onReceive(const MyMessage & message) { SensorBMP085
if (message.getCommand() == C_REQ) onLoop(); */
#if MODULE_BMP085 == 1
// contructor
SensorBMP085::SensorBMP085(int child_id, Adafruit_BMP085* bmp, int sensor_type): SensorBosch(child_id,sensor_type) {
_bmp = bmp;
} }
// returns the average of the latest pressure samples // what do to during loop
float SensorBME280::_getLastPressureSamplesAverage() { void SensorBMP085::onLoop() {
float avg = 0; // temperature sensor
for (int i = 0; i < _forecast_samples_count; i++) avg += _forecast_samples[i]; if (_sensor_type == SensorBMP085::TEMPERATURE) {
avg /= _forecast_samples_count; // read the temperature
return avg; float temperature = _bmp->readTemperature();
// convert it
if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
#if DEBUG == 1
Serial.print(F("BMP I="));
Serial.print(_child_id);
Serial.print(F(" T="));
Serial.println(temperature);
#endif
if (isnan(temperature)) return;
// store the value
_value_float = temperature;
}
// Pressure Sensor
else if (_sensor_type == SensorBMP085::PRESSURE) {
// read pressure
float pressure = _bmp->readPressure() / 100.0F;
#if DEBUG == 1
Serial.print(F("BMP I="));
Serial.print(_child_id);
Serial.print(F(" P="));
Serial.println(pressure);
#endif
if (isnan(pressure)) return;
// store the value
_value_float = pressure;
}
// Forecast Sensor
else if (_sensor_type == SensorBMP085::FORECAST) {
float pressure = _bmp->readPressure() / 100.0F;
_forecast(pressure);
}
} }
#endif #endif
...@@ -1551,174 +1638,6 @@ void SensorSonoff::_blink() { ...@@ -1551,174 +1638,6 @@ void SensorSonoff::_blink() {
} }
#endif #endif
/*
SensorBMP085
*/
#if MODULE_BMP085 == 1
// contructor
SensorBMP085::SensorBMP085(int child_id, Adafruit_BMP085* bmp, int sensor_type): Sensor(child_id,A4) {
// sensor type (0: temperature, 1: pressure, 2: forecast)
Serial.println(_sensor_type);
_sensor_type = sensor_type;
if (_sensor_type == 0) {
// temperature sensor
setPresentation(S_TEMP);
setType(V_TEMP);
setValueType(TYPE_FLOAT);
}
else if (_sensor_type == 1) {
// pressure sensor
setPresentation(S_BARO);
setType(V_PRESSURE);
setValueType(TYPE_FLOAT);
}
else if (_sensor_type == 2) {
// pressure sensor
setPresentation(S_BARO);
setType(V_FORECAST);
setValueType(TYPE_STRING);
}
}
// setter/getter
void SensorBMP085::setForecastSamplesCount(int value) {
_forecast_samples_count = value;
}
// what do to during before
void SensorBMP085::onBefore() {
// initialize the forecast samples array
_forecast_samples = new float[_forecast_samples_count];
}
// what do to during setup
void SensorBMP085::onSetup() {
}
// what do to during loop
void SensorBMP085::onLoop() {
// temperature sensor
if (_sensor_type == 0) {
// read the temperature
float temperature = _bmp->readTemperature();
// convert it
if (! getControllerConfig().isMetric) temperature = temperature * 1.8 + 32;
#if DEBUG == 1
Serial.print(F("BMP I="));
Serial.print(_child_id);
Serial.print(F(" T="));
Serial.println(temperature);
#endif
// store the value
if (! isnan(temperature)) _value_float = temperature;
}
// Pressure Sensor
else if (_sensor_type == 1) {
// read pressure
float pressure = _bmp->readPressure() / 100.0F;
if (isnan(pressure)) return;
#if DEBUG == 1
Serial.print(F("BMP I="));
Serial.print(_child_id);
Serial.print(F(" P="));
Serial.println(pressure);
#endif
// store the value
if (! isnan(pressure)) _value_float = pressure;
}
// Forecast Sensor
else if (_sensor_type == 2) {
// read pressure
float pressure = _bmp->readPressure() / 100.0F;
if (isnan(pressure)) return;
// Calculate the average of the last n minutes.
int index = _minute_count % _forecast_samples_count;
_forecast_samples[index] = pressure;
_minute_count++;
if (_minute_count > 185) _minute_count = 6;
if (_minute_count == 5) _pressure_avg = _getLastPressureSamplesAverage();
else if (_minute_count == 35) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
// first time initial 3 hour
if (_first_round) _dP_dt = change * 2; // note this is for t = 0.5hour
else _dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value.
}
else if (_minute_count == 65) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
//first time initial 3 hour
if (_first_round) _dP_dt = change; //note this is for t = 1 hour
else _dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value
}
else if (_minute_count == 95) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
// first time initial 3 hour
if (_first_round)_dP_dt = change / 1.5; // note this is for t = 1.5 hour
else _dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value
}
else if (_minute_count == 125) {
float last_pressure_avg = _getLastPressureSamplesAverage();
// store for later use.
_pressure_avg2 = last_pressure_avg;
float change = (last_pressure_avg - _pressure_avg) * 0.1;
if (_first_round) _dP_dt = change / 2; // note this is for t = 2 hour
else _dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value
}
else if (_minute_count == 155) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
if (_first_round) _dP_dt = change / 2.5; // note this is for t = 2.5 hour
else _dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value
}
else if (_minute_count == 185) {
float last_pressure_avg = _getLastPressureSamplesAverage();
float change = (last_pressure_avg - _pressure_avg) * 0.1;
if (_first_round) _dP_dt = change / 3; // note this is for t = 3 hour
else _dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value
}
// Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
_pressure_avg = _pressure_avg2;
// flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
_first_round = false;
// calculate the forecast (STABLE = 0, SUNNY = 1, CLOUDY = 2, UNSTABLE = 3, THUNDERSTORM = 4, UNKNOWN = 5)
int forecast = 5;
//if time is less than 35 min on the first 3 hour interval.
if (_minute_count < 35 && _first_round) forecast = 5;
else if (_dP_dt < (-0.25)) forecast = 5;
else if (_dP_dt > 0.25) forecast = 4;
else if ((_dP_dt > (-0.25)) && (_dP_dt < (-0.05))) forecast = 2;
else if ((_dP_dt > 0.05) && (_dP_dt < 0.25)) forecast = 1;
else if ((_dP_dt >(-0.05)) && (_dP_dt < 0.05)) forecast = 0;
else forecast = 5;
_value_string = _weather[forecast];
#if DEBUG == 1
Serial.print(F("BMP I="));
Serial.print(_child_id);
Serial.print(F(" M="));
Serial.print(_minute_count);
Serial.print(F(" dP="));
Serial.print(_dP_dt);
Serial.print(F(" F="));
Serial.println(_value_string);
#endif
}
}
// what do to as the main task when receiving a message
void SensorBMP085::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// returns the average of the latest pressure samples
float SensorBMP085::_getLastPressureSamplesAverage() {
float avg = 0;
for (int i = 0; i < _forecast_samples_count; i++) avg += _forecast_samples[i];
avg /= _forecast_samples_count;
return avg;
}
#endif
/* /*
SensorHCSR04 SensorHCSR04
...@@ -1890,26 +1809,26 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) { ...@@ -1890,26 +1809,26 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
int dht_type = sensor_type == SENSOR_DHT11 ? DHT11 : DHT22; int dht_type = sensor_type == SENSOR_DHT11 ? DHT11 : DHT22;
DHT* dht = new DHT(pin,dht_type); DHT* dht = new DHT(pin,dht_type);
// register temperature sensor // register temperature sensor
registerSensor(new SensorDHT(child_id,pin,dht,0,dht_type)); registerSensor(new SensorDHT(child_id,pin,dht,SensorDHT::TEMPERATURE,dht_type));
// register humidity sensor // register humidity sensor
child_id = _getAvailableChildId(); child_id = _getAvailableChildId();
return registerSensor(new SensorDHT(child_id,pin,dht,1,dht_type)); return registerSensor(new SensorDHT(child_id,pin,dht,SensorDHT::HUMIDITY,dht_type));
} }
#endif #endif
#if MODULE_SHT21 == 1 #if MODULE_SHT21 == 1
else if (sensor_type == SENSOR_SHT21) { else if (sensor_type == SENSOR_SHT21) {
// register temperature sensor // register temperature sensor
registerSensor(new SensorSHT21(child_id,0)); registerSensor(new SensorSHT21(child_id,SensorSHT21::TEMPERATURE));
// register humidity sensor // register humidity sensor
child_id = _getAvailableChildId(); child_id = _getAvailableChildId();
return registerSensor(new SensorSHT21(child_id,1)); return registerSensor(new SensorSHT21(child_id,SensorSHT21::HUMIDITY));
} }
else if (sensor_type == SENSOR_HTU21D) { else if (sensor_type == SENSOR_HTU21D) {
// register temperature sensor // register temperature sensor
registerSensor(new SensorHTU21D(child_id,0)); registerSensor(new SensorHTU21D(child_id,SensorHTU21D::TEMPERATURE));
// register humidity sensor // register humidity sensor
child_id = _getAvailableChildId(); child_id = _getAvailableChildId();
return registerSensor(new SensorHTU21D(child_id,1)); return registerSensor(new SensorHTU21D(child_id,SensorHTU21D::HUMIDITY));
} }
#endif #endif
#if MODULE_SWITCH == 1 #if MODULE_SWITCH == 1
...@@ -1953,32 +1872,32 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) { ...@@ -1953,32 +1872,32 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
else if (sensor_type == SENSOR_MLX90614) { else if (sensor_type == SENSOR_MLX90614) {
Adafruit_MLX90614* mlx = new Adafruit_MLX90614(); Adafruit_MLX90614* mlx = new Adafruit_MLX90614();
// register ambient temperature sensor // register ambient temperature sensor
registerSensor(new SensorMLX90614(child_id,mlx,0)); registerSensor(new SensorMLX90614(child_id,mlx,SensorMLX90614::TEMPERATURE_AMBIENT));
// register object temperature sensor // register object temperature sensor
child_id = _getAvailableChildId(); child_id = _getAvailableChildId();
return registerSensor(new SensorMLX90614(child_id,mlx,1)); return registerSensor(new SensorMLX90614(child_id,mlx,SensorMLX90614::TEMPERATURE_OBJECT));
} }
#endif #endif
#if MODULE_BME280 == 1 #if MODULE_BME280 == 1
else if (sensor_type == SENSOR_BME280) { else if (sensor_type == SENSOR_BME280) {
Adafruit_BME280* bme = new Adafruit_BME280(); Adafruit_BME280* bme = new Adafruit_BME280();
if (! bme->begin()) { if (! bme->begin(SensorBosch::GetI2CAddress(0x60))) {
#if DEBUG == 1 #if DEBUG == 1
Serial.println(F("NO BME")); Serial.println(F("NO BME"));
#endif #endif
return -1; return -1;
} }
// register temperature sensor // register temperature sensor
registerSensor(new SensorBME280(child_id,bme,0)); registerSensor(new SensorBME280(child_id,bme,SensorBME280::TEMPERATURE));
child_id = _getAvailableChildId(); child_id = _getAvailableChildId();
// register humidity sensor // register humidity sensor
registerSensor(new SensorBME280(child_id,bme,1)); registerSensor(new SensorBME280(child_id,bme,SensorBME280::HUMIDITY));
// register pressure sensor // register pressure sensor
child_id = _getAvailableChildId(); child_id = _getAvailableChildId();
return registerSensor(new SensorBME280(child_id,bme,2)); registerSensor(new SensorBME280(child_id,bme,SensorBME280::PRESSURE));
// register forecast sensor // register forecast sensor
child_id = _getAvailableChildId(); child_id = _getAvailableChildId();
return registerSensor(new SensorBME280(child_id,bme,3)); return registerSensor(new SensorBME280(child_id,bme,SensorBME280::FORECAST));
} }
#endif #endif
#if MODULE_SONOFF == 1 #if MODULE_SONOFF == 1
...@@ -1989,20 +1908,20 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) { ...@@ -1989,20 +1908,20 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
#if MODULE_BMP085 == 1 #if MODULE_BMP085 == 1
else if (sensor_type == SENSOR_BMP085) { else if (sensor_type == SENSOR_BMP085) {
Adafruit_BMP085* bmp = new Adafruit_BMP085(); Adafruit_BMP085* bmp = new Adafruit_BMP085();
if (! bmp->begin()) { if (! bmp->begin(SensorBosch::GetI2CAddress(0x55))) {
#if DEBUG == 1 #if DEBUG == 1
Serial.println(F("NO BMP")); Serial.println(F("NO BMP"));
#endif #endif
return -1; return -1;
} }
// register temperature sensor // register temperature sensor
registerSensor(new SensorBMP085(child_id,bmp,0)); registerSensor(new SensorBMP085(child_id,bmp,SensorBMP085::TEMPERATURE));
// register pressure sensor // register pressure sensor
child_id = _getAvailableChildId(); child_id = _getAvailableChildId();
return registerSensor(new SensorBMP085(child_id,bmp,1)); registerSensor(new SensorBMP085(child_id,bmp,SensorBMP085::PRESSURE));
// register forecast sensor // register forecast sensor
child_id = _getAvailableChildId(); child_id = _getAvailableChildId();
return registerSensor(new SensorBMP085(child_id,bmp,2)); return registerSensor(new SensorBMP085(child_id,bmp,SensorBMP085::FORECAST));
} }
#endif #endif
#if MODULE_HCSR04 == 1 #if MODULE_HCSR04 == 1
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include <Arduino.h> #include <Arduino.h>
// define NodeManager version // define NodeManager version
#define VERSION "1.5-dev2" #define VERSION "1.5-dev3"
/*********************************** /***********************************
Constants Constants
...@@ -623,6 +623,9 @@ class SensorDHT: public Sensor { ...@@ -623,6 +623,9 @@ class SensorDHT: public Sensor {
void onSetup(); void onSetup();
void onLoop(); void onLoop();
void onReceive(const MyMessage & message); void onReceive(const MyMessage & message);
// constants
const static int TEMPERATURE = 0;
const static int HUMIDITY = 1;
protected: protected:
DHT* _dht; DHT* _dht;
int _dht_type = DHT11; int _dht_type = DHT11;
...@@ -643,6 +646,9 @@ class SensorSHT21: public Sensor { ...@@ -643,6 +646,9 @@ class SensorSHT21: public Sensor {
void onSetup(); void onSetup();
void onLoop(); void onLoop();
void onReceive(const MyMessage & message); void onReceive(const MyMessage & message);
// constants
const static int TEMPERATURE = 0;
const static int HUMIDITY = 1;
protected: protected:
float _offset = 0; float _offset = 0;
int _sensor_type = 0; int _sensor_type = 0;
...@@ -760,19 +766,24 @@ class SensorMLX90614: public Sensor { ...@@ -760,19 +766,24 @@ class SensorMLX90614: public Sensor {
void onSetup(); void onSetup();
void onLoop(); void onLoop();
void onReceive(const MyMessage & message); void onReceive(const MyMessage & message);
// constants
const static int TEMPERATURE_AMBIENT = 0;
const static int TEMPERATURE_OBJECT = 1;
protected: protected:
Adafruit_MLX90614* _mlx; Adafruit_MLX90614* _mlx;
int _sensor_type; int _sensor_type;
}; };
#endif #endif
/* /*
SensorBME280 * SensorBosch
*/ */
#if MODULE_BME280 == 1
class SensorBME280: public Sensor { #if MODULE_BME280 == 1 || MODULE_BMP085 == 1
class SensorBosch: public Sensor {
public: public:
SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type); SensorBosch(int child_id, int sensor_type);
// define how many pressure samples to keep track of for calculating the forecast (default: 5) // define how many pressure samples to keep track of for calculating the forecast (default: 5)
void setForecastSamplesCount(int value); void setForecastSamplesCount(int value);
// define what to do at each stage of the sketch // define what to do at each stage of the sketch
...@@ -780,8 +791,13 @@ class SensorBME280: public Sensor { ...@@ -780,8 +791,13 @@ class SensorBME280: public Sensor {
void onSetup(); void onSetup();
void onLoop(); void onLoop();
void onReceive(const MyMessage & message); void onReceive(const MyMessage & message);
// constants
const static int TEMPERATURE = 0;
const static int HUMIDITY = 1;
const static int PRESSURE = 2;
const static int FORECAST = 3;
static uint8_t GetI2CAddress(uint8_t chip_id);
protected: protected:
Adafruit_BME280* _bme;
int _sensor_type; int _sensor_type;
char* _weather[6] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" }; char* _weather[6] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" };
int _forecast_samples_count = 5; int _forecast_samples_count = 5;
...@@ -792,6 +808,33 @@ class SensorBME280: public Sensor { ...@@ -792,6 +808,33 @@ class SensorBME280: public Sensor {
float _dP_dt; float _dP_dt;
bool _first_round = true; bool _first_round = true;
float _getLastPressureSamplesAverage(); float _getLastPressureSamplesAverage();
void _forecast(float pressure);
};
#endif
/*
SensorBME280
*/
#if MODULE_BME280 == 1
class SensorBME280: public SensorBosch {
public:
SensorBME280(int child_id, Adafruit_BME280* bme, int sensor_type);
void onLoop();
protected:
Adafruit_BME280* _bme;
};
#endif
/*
SensorBMP085
*/
#if MODULE_BMP085 == 1
class SensorBMP085: public SensorBosch {
public:
SensorBMP085(int child_id, Adafruit_BMP085* bmp, int sensor_type);
void onLoop();
protected:
Adafruit_BMP085* _bmp;
}; };
#endif #endif
...@@ -855,34 +898,6 @@ class SensorSonoff: public Sensor { ...@@ -855,34 +898,6 @@ class SensorSonoff: public Sensor {
}; };
#endif #endif
/*
SensorBME280
*/
#if MODULE_BMP085 == 1
class SensorBMP085: public Sensor {
public:
SensorBMP085(int child_id, Adafruit_BMP085* bmp, int sensor_type);
// define how many pressure samples to keep track of for calculating the forecast (default: 5)
void setForecastSamplesCount(int value);
// define what to do at each stage of the sketch
void onBefore();
void onSetup();
void onLoop();
void onReceive(const MyMessage & message);
protected:
Adafruit_BMP085* _bmp;
int _sensor_type;
char* _weather[6] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" };
int _forecast_samples_count = 5;
float* _forecast_samples;
int _minute_count = 0;
float _pressure_avg;
float _pressure_avg2;
float _dP_dt;
bool _first_round = true;
float _getLastPressureSamplesAverage();
};
#endif
/*************************************** /***************************************
NodeManager: manages all the aspects of the node NodeManager: manages all the aspects of the node
......
...@@ -35,7 +35,7 @@ void before() { ...@@ -35,7 +35,7 @@ void before() {
*/ */
/* /*
* Register above your sensors * Register above your sensors
*/ */
......
...@@ -22,11 +22,13 @@ ...@@ -22,11 +22,13 @@
//#define MY_RF24_ENABLE_ENCRYPTION //#define MY_RF24_ENABLE_ENCRYPTION
//#define MY_RF24_CHANNEL 76 //#define MY_RF24_CHANNEL 76
//#define MY_RF24_PA_LEVEL RF24_PA_HIGH //#define MY_RF24_PA_LEVEL RF24_PA_HIGH
//#define MY_DEBUG_VERBOSE_RF24
// RFM69 radio settings // RFM69 radio settings
//#define MY_RADIO_RFM69 //#define MY_RADIO_RFM69
//#define MY_RFM69_FREQUENCY RF69_868MHZ //#define MY_RFM69_FREQUENCY RF69_868MHZ
//#define MY_IS_RFM69HW //#define MY_IS_RFM69HW
//#define MY_DEBUG_VERBOSE_RFM69
//#define MY_RFM69_NEW_DRIVER //#define MY_RFM69_NEW_DRIVER
//#define MY_RFM69_ENABLE_ENCRYPTION //#define MY_RFM69_ENABLE_ENCRYPTION
//#define MY_RFM69_NETWORKID 100 //#define MY_RFM69_NETWORKID 100
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment