Skip to content

Commit ad44b91

Browse files
authored
Merge pull request letscontrolit#3607 from TD-er/bugfix/bme680_altitude
[BME680] Fix elevation compensation + add 2 conversion commands
2 parents a842e30 + 09d6fe6 commit ad44b91

15 files changed

+65
-59
lines changed

docs/source/Reference/SystemVariable.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ The conversion always outputs a string, but not all of these can be converted ba
172172
* - Dew point(T,H): ``%c_dew_th%(18.6,67)``
173173
- Dew point(T,H): ``12.31``
174174
- Compute dew point given 2 values, temperature and relative humidity
175+
* - Altitude(air,sea): ``%c_alt_pres_sea%(850,1000)``
176+
- Altitude(air,sea): ``1350.03``
177+
- Compute Altitude (m) given 2 values, atmospheric pressure and pressure at sea level (hPa). (Added: 2021/04/27)
178+
* - PressureElevation(air,alt): ``%c_sea_pres_alt%(850,1350.03)``
179+
- PressureElevation(air,alt): ``1000.00``
180+
- Compensate air pressure for measured atmospheric pressure (hPa) and given altitude (m). (Added: 2021/04/27)
175181
* - cm to imperial: ``%c_cm2imp%(190)``
176182
- cm to imperial: ``6'2.8"``
177183
- Centimeter to imperial units

src/_P006_BMP085.ino

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ boolean Plugin_006(byte function, struct EventStruct *event, String& string)
9090

9191
if (elev != 0)
9292
{
93-
pressure = P006_data->pressureElevation(
94-
pressure,
95-
elev);
93+
pressure = pressureElevation(pressure, elev);
9694
}
9795
UserVar[event->BaseVarIndex + 1] = pressure;
9896

src/_P028_BME280.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ boolean Plugin_028(byte function, struct EventStruct *event, String& string)
151151
const int elev = PCONFIG(1);
152152

153153
if (elev != 0) {
154-
UserVar[event->BaseVarIndex + 2] = P028_data->pressureElevation(elev);
154+
UserVar[event->BaseVarIndex + 2] = pressureElevation(P028_data->last_press_val, elev);
155155
} else {
156156
UserVar[event->BaseVarIndex + 2] = P028_data->last_press_val;
157157
}

src/_P032_MS5611.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ boolean Plugin_032(byte function, struct EventStruct *event, String& string)
101101
P032_data->readout();
102102

103103
UserVar[event->BaseVarIndex] = P032_data->ms5611_temperature / 100;
104-
int elev = PCONFIG(1);
105-
106-
if (elev)
104+
105+
const int elev = PCONFIG(1);
106+
if (elev != 0)
107107
{
108-
UserVar[event->BaseVarIndex + 1] = P032_data->pressureElevation(P032_data->ms5611_pressure, elev);
108+
UserVar[event->BaseVarIndex + 1] = pressureElevation(P032_data->ms5611_pressure, elev);
109109
} else {
110110
UserVar[event->BaseVarIndex + 1] = P032_data->ms5611_pressure;
111111
}

src/_P106_BME680.ino

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,15 @@ boolean Plugin_106(byte function, struct EventStruct *event, String& string)
132132

133133
UserVar[event->BaseVarIndex + 0] = P106_data->bme.temperature;
134134
UserVar[event->BaseVarIndex + 1] = P106_data->bme.humidity;
135-
UserVar[event->BaseVarIndex + 2] = P106_data->bme.pressure / 100.0f;
136135
UserVar[event->BaseVarIndex + 3] = P106_data->bme.gas_resistance / 1000.0f;
136+
137+
const int elev = PCONFIG(1);
138+
if (elev != 0)
139+
{
140+
UserVar[event->BaseVarIndex + 2] = pressureElevation(P106_data->bme.pressure / 100.0f, elev);
141+
} else {
142+
UserVar[event->BaseVarIndex + 2] = P106_data->bme.pressure / 100.0f;
143+
}
137144
}
138145

139146
success = true;

src/src/Helpers/Convert.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,34 @@ float compute_humidity_from_dewpoint(float temperature, float dew_temperature) {
181181
}
182182

183183

184+
185+
/********************************************************************************************\
186+
Compensate air pressure for given altitude (in meters)
187+
\*********************************************************************************************/
188+
float pressureElevation(float atmospheric, float altitude) {
189+
// Equation taken from BMP180 datasheet (page 16):
190+
// http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
191+
192+
// Note that using the equation from wikipedia can give bad results
193+
// at high altitude. See this thread for more information:
194+
// http://forums.adafruit.com/viewtopic.php?f=22&t=58064
195+
return atmospheric / pow(1.0f - (altitude / 44330.0f), 5.255f);
196+
}
197+
198+
float altitudeFromPressure(float atmospheric, float seaLevel)
199+
{
200+
// Equation taken from BMP180 datasheet (page 16):
201+
// http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
202+
203+
// Note that using the equation from wikipedia can give bad results
204+
// at high altitude. See this thread for more information:
205+
// http://forums.adafruit.com/viewtopic.php?f=22&t=58064
206+
return 44330.0f * (1.0f - pow(atmospheric / seaLevel, 0.1903f));
207+
}
208+
209+
210+
211+
184212
/********************************************************************************************\
185213
In memory convert float to long
186214
\*********************************************************************************************/

src/src/Helpers/Convert.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ float compute_dew_point_temp(float temperature, float humidity_percentage);
3838
// f = 100 * ((112 - 0.1*T + Td) / (112 + 0.9 * T))^8
3939
float compute_humidity_from_dewpoint(float temperature, float dew_temperature);
4040

41+
/********************************************************************************************\
42+
Compensate air pressure for measured atmospheric
43+
pressure (in hPa) and given altitude (in meters)
44+
\*********************************************************************************************/
45+
float pressureElevation(float atmospheric, float altitude);
46+
47+
/********************************************************************************************\
48+
Calculates the altitude (in meters) from the specified atmospheric
49+
pressure (in hPa), and sea-level pressure (in hPa).
50+
@param seaLevel Sea-level pressure in hPa
51+
@param atmospheric Atmospheric pressure in hPa
52+
\*********************************************************************************************/
53+
float altitudeFromPressure(float atmospheric, float seaLevel);
4154

4255
/********************************************************************************************\
4356
In memory convert float to long

src/src/Helpers/StringConverter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,8 @@ void parseStandardConversions(String& s, bool useURLencode) {
904904
float arg2 = 0.0f;
905905
SMART_CONV(F("%c_dew_th%"), toString(compute_dew_point_temp(arg1, arg2), 2))
906906
SMART_CONV(F("%c_u2ip%"), formatUnitToIPAddress(arg1, arg2))
907+
SMART_CONV(F("%c_alt_pres_sea%"), toString(altitudeFromPressure(arg1, arg2), 2))
908+
SMART_CONV(F("%c_sea_pres_alt%"), toString(pressureElevation(arg1, arg2), 2))
907909
#undef SMART_CONV
908910
}
909911

src/src/PluginStructs/P006_data_struct.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,4 @@ float P006_data_struct::readTemperature(void)
130130
return temp;
131131
}
132132

133-
float P006_data_struct::pressureElevation(float atmospheric, int altitude) {
134-
return atmospheric / pow(1.0f - (altitude / 44330.0f), 5.255f);
135-
}
136-
137133
#endif // ifdef USES_P006

src/src/PluginStructs/P006_data_struct.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ struct P006_data_struct : public PluginTaskData_base {
1818

1919
float readTemperature(void);
2020

21-
float pressureElevation(float atmospheric,
22-
int altitude);
23-
2421
uint8_t oversampling = BMP085_ULTRAHIGHRES;
2522
int16_t ac1, ac2, ac3, b1, b2, mb, mc, md = 0;
2623
uint16_t ac4, ac5, ac6 = 0;

src/src/PluginStructs/P028_data_struct.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -397,22 +397,4 @@ float P028_data_struct::readHumidity()
397397
return h / 1024.0f;
398398
}
399399

400-
float P028_data_struct::Plugin_028_readAltitude(float seaLevel)
401-
{
402-
// Equation taken from BMP180 datasheet (page 16):
403-
// http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
404-
405-
// Note that using the equation from wikipedia can give bad results
406-
// at high altitude. See this thread for more information:
407-
// http://forums.adafruit.com/viewtopic.php?f=22&t=58064
408-
409-
float atmospheric = readPressure() / 100.0f;
410-
411-
return 44330.0f * (1.0f - pow(atmospheric / seaLevel, 0.1903f));
412-
}
413-
414-
float P028_data_struct::pressureElevation(int altitude) {
415-
return last_press_val / pow(1.0f - (altitude / 44330.0f), 5.255f);
416-
}
417-
418400
#endif // ifdef USES_P028

src/src/PluginStructs/P028_data_struct.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,6 @@ struct P028_data_struct : public PluginTaskData_base {
156156
// **************************************************************************/
157157
float readHumidity();
158158

159-
// **************************************************************************/
160-
// Calculates the altitude (in meters) from the specified atmospheric
161-
// pressure (in hPa), and sea-level pressure (in hPa).
162-
// @param seaLevel Sea-level pressure in hPa
163-
// @param atmospheric Atmospheric pressure in hPa
164-
// **************************************************************************/
165-
float Plugin_028_readAltitude(float seaLevel);
166-
167-
// **************************************************************************/
168-
// MSL pressure formula
169-
// **************************************************************************/
170-
float pressureElevation(int altitude);
171-
172159
bme280_uncomp_data uncompensated;
173160
bme280_calib_data calib;
174161
float last_hum_val = 0.0f;

src/src/PluginStructs/P032_data_struct.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,5 @@ void P032_data_struct::readout() {
117117
ms5611_pressure = (((D1 * SENS) / (1 << 21) - Offset) / (1 << 15)); // FIXME TD-er: This is computed twice, is that correct?
118118
}
119119

120-
// **************************************************************************/
121-
// MSL pressure formula
122-
// **************************************************************************/
123-
double P032_data_struct::pressureElevation(double atmospheric, int altitude) {
124-
return atmospheric / pow(1.0f - (altitude / 44330.0f), 5.255f);
125-
}
126120

127121
#endif // ifdef USES_P032

src/src/PluginStructs/P032_data_struct.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ struct P032_data_struct : public PluginTaskData_base {
3535
// **************************************************************************/
3636
void readout();
3737

38-
// **************************************************************************/
39-
// MSL pressure formula
40-
// **************************************************************************/
41-
double pressureElevation(double atmospheric,
42-
int altitude);
43-
4438
uint8_t i2cAddress;
4539
unsigned int ms5611_prom[8] = { 0 };
4640
double ms5611_pressure = 0;

src/src/WebServer/SysVarPage.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ void handle_sysvars() {
203203
addSysVar_html(F("{D}C to {D}F: %c_c2f%(20.4)"));
204204
addSysVar_html(F("m/s to Bft: %c_ms2Bft%(5.1)"));
205205
addSysVar_html(F("Dew point(T,H): %c_dew_th%(18.6,67)"));
206+
addSysVar_html(F("Altitude(air,sea): %c_alt_pres_sea%(850,1000)"));
207+
addSysVar_html(F("PressureElevation(air,alt): %c_sea_pres_alt%(850,1350.03)"));
206208
addFormSeparator(3);
207209
addSysVar_html(F("cm to imperial: %c_cm2imp%(190)"));
208210
addSysVar_html(F("mm to imperial: %c_mm2imp%(1900)"));

0 commit comments

Comments
 (0)