You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BME280 does not measure pressure/altitude correctly when SetPowerMode is not called #753
Priority:2Work that is important, but not critical for the releasearea-device-bindingsDevice Bindings for audio, sensor, motor, and display hardware that can used with System.Device.GpioenhancementNew feature or request
Yeah you will always need to perform a measurement by calling SetPowerMode (this is by design), I just tested this with a reference python library and basically got the same value for pressure:
My phones sensor is currently giving me a value of 981.0 so not far off. I'll take a look at the Altitude formula since I didn't tocuh it in my last PR, maybe something is wrong with that.
@RobinTTY python's bug doesn't mean we have to repeat the same bug in here.
If pressure cannot be read for whatever reason it should throw an exception or return false.
Constructing BME280 should also provide sane defaults which means it should be usable.
We cannot return bogus results unless hardware has gone bad.
Yeah currently we are providing the default configuration at construction (Things like sampling modes, etc.). The problem is that currently TryReadX has no check whether a measurement was already made since it only reads the register with the values of the last measurement (after device startup default register value). So we could just perform a single measurement on construction to avoid that.
Or probably a check whether a read has been performed is better and just return NaN if non was performed. Since if there is a long timespan between construction and first call of TryRead, the values would be out of date.
Edit: The altitude formula seems to be correct but it's not taking temperature into account. But that shouldn't make a big difference. Also keep in mind you need to reference the current atmospheric pressure at sea level, a difference of 10hPa can make a difference of 100m.
I will open a pull request to take temperature into account and to return NaN if no measurement was taken before reading the value.
Cool, thank you @RobinTTY! Yes we should either return false+NaN or give correct answer.
Also, I've actually tried sleeping for 2s before reading the value and it read the wrong value as well, do you also need to call something to initiate reading process?
@krwq That would require a change in the current behavior of the driver. The way the sensor works is that you specify settings like sampling rate and filter (you can for instance skip the pressure meassurement and only measure temperature/humidity/gas). Then you trigger the actual measurement by setting the PowerMode.
The Bmx280 has besides the forced power mode a normal power mode, where measurements are continously being made after setting the sensor to that mode. That is why you always have to set the power mode currently, before reading the results. We could instead change the behavior to something like this:
That would mean that you would still need the methods to read the registers for the case of the continous measurements but wouldn't need them for a single one. Is that what you would want?
@RobinTTY changing the behavior is fine if it is well justified and in this case I think it is.
I think by default users will expect constructing a class and reading temperature will just work without any extra knowledge and I think this should be the default option (if I understand correctly: PowerMode.Normal). For any advanced settings we should have a way of doing it but it should not be required to read the spec or all docs.
Does the power mode need to be set before every measurement and cannot be set once globally? The current API shape suggests this is a one-time operation. If this is something you need to do every time the general rule of thumb suggest we should automate it.
Perhaps something along the lines of (pseudo C#):
structReadResult{double?Temperature{get;}double?Pressure{get;}double?Humidity{get;}DateTimeOffsetTimestamp{get;}// optional: when the measurement was made}structReadMode{Continuous,Manual,}classBme280{publicBme280(...,ReadModereadMode=ReadMode.Continuous);publicReadResultRead();publicTask<ReadResult>ReadAsync();}
basically for continuous mode Read() will always return immediately and is suggested method of use. For manual mode ReadAsync is suggested which will start measurement and wait for measurement to be completed.
Thank you @RobinTTY! To avoid extra work/refactoring in the PR it might be worth to write something resembling API proposal (no implementation) - just what you think everything should look like (i.e. just sample code from user perspective)
changed the title [-]BME280 does not measure pressure/altitude correctly[/-][+]BME280 does not measure pressure/altitude correctly when SetPowerMode is not called[/+]on Oct 1, 2019
The altitude calculation can be improved. I'm working on a WeatherHelper (#783) and I'll be adding an altitude calculation there. However I'm trying to gather information on how to improve this calculation due to the fact that there is a missing input in the current formula: temperature. The current calculation is assuming 15C, but you can see that temperature affects the altitude calculation. Take a look at https://www.mide.com/pages/air-pressure-at-altitude-calculator for example.
still an issue in 1.4 (it keeps reading same value if you don't set it). I also need to wait around 100ms after setting power mode, otherwise I'm getting random errors ocasionally
[Triage] It would need convenience method but since sample is doing the right thing we don't want to do extra operations. We don't track requests for convenience methods, PR is needed to further discuss this.
Priority:2Work that is important, but not critical for the releasearea-device-bindingsDevice Bindings for audio, sensor, motor, and display hardware that can used with System.Device.GpioenhancementNew feature or request
Activity
krwq commentedon Sep 30, 2019
Seems this is working around the problem with pressure:
The pressure/altitude after this shows us:
I'm definitely not under water right now so presumably sea level pressure or some other calculation is wrong as well
RobinTTY commentedon Sep 30, 2019
Yeah you will always need to perform a measurement by calling SetPowerMode (this is by design), I just tested this with a reference python library and basically got the same value for pressure:
My phones sensor is currently giving me a value of 981.0 so not far off. I'll take a look at the Altitude formula since I didn't tocuh it in my last PR, maybe something is wrong with that.
krwq commentedon Sep 30, 2019
@RobinTTY python's bug doesn't mean we have to repeat the same bug in here.
If pressure cannot be read for whatever reason it should throw an exception or return false.
Constructing BME280 should also provide sane defaults which means it should be usable.
We cannot return bogus results unless hardware has gone bad.
RobinTTY commentedon Sep 30, 2019
Yeah currently we are providing the default configuration at construction (Things like sampling modes, etc.). The problem is that currently TryReadX has no check whether a measurement was already made since it only reads the register with the values of the last measurement (after device startup default register value). So we could just perform a single measurement on construction to avoid that.
RobinTTY commentedon Sep 30, 2019
Or probably a check whether a read has been performed is better and just return NaN if non was performed. Since if there is a long timespan between construction and first call of TryRead, the values would be out of date.
Edit: The altitude formula seems to be correct but it's not taking temperature into account. But that shouldn't make a big difference. Also keep in mind you need to reference the current atmospheric pressure at sea level, a difference of 10hPa can make a difference of 100m.
I will open a pull request to take temperature into account and to return NaN if no measurement was taken before reading the value.
krwq commentedon Sep 30, 2019
Cool, thank you @RobinTTY! Yes we should either return false+NaN or give correct answer.
Also, I've actually tried sleeping for 2s before reading the value and it read the wrong value as well, do you also need to call something to initiate reading process?
RobinTTY commentedon Sep 30, 2019
@krwq: So I tried this piece of code:
And I get correct values on the first read. What value was wrong for you?
krwq commentedon Oct 1, 2019
@RobinTTY I'm expecting everything to work without
sensor.SetPowerMode(Bmx280PowerMode.Forced);RobinTTY commentedon Oct 1, 2019
@krwq That would require a change in the current behavior of the driver. The way the sensor works is that you specify settings like sampling rate and filter (you can for instance skip the pressure meassurement and only measure temperature/humidity/gas). Then you trigger the actual measurement by setting the PowerMode.
The Bmx280 has besides the forced power mode a normal power mode, where measurements are continously being made after setting the sensor to that mode. That is why you always have to set the power mode currently, before reading the results. We could instead change the behavior to something like this:
That would mean that you would still need the methods to read the registers for the case of the continous measurements but wouldn't need them for a single one. Is that what you would want?
krwq commentedon Oct 1, 2019
@RobinTTY changing the behavior is fine if it is well justified and in this case I think it is.
I think by default users will expect constructing a class and reading temperature will just work without any extra knowledge and I think this should be the default option (if I understand correctly: PowerMode.Normal). For any advanced settings we should have a way of doing it but it should not be required to read the spec or all docs.
Does the power mode need to be set before every measurement and cannot be set once globally? The current API shape suggests this is a one-time operation. If this is something you need to do every time the general rule of thumb suggest we should automate it.
Perhaps something along the lines of (pseudo C#):
basically for continuous mode
Read()will always return immediately and is suggested method of use. For manual mode ReadAsync is suggested which will start measurement and wait for measurement to be completed.RobinTTY commentedon Oct 1, 2019
@krwq Sounds good to me, I'll submit a PR along with the other changes in the next few days.
krwq commentedon Oct 1, 2019
Thank you @RobinTTY! To avoid extra work/refactoring in the PR it might be worth to write something resembling API proposal (no implementation) - just what you think everything should look like (i.e. just sample code from user perspective)
[-]BME280 does not measure pressure/altitude correctly[/-][+]BME280 does not measure pressure/altitude correctly when SetPowerMode is not called[/+]RobinTTY commentedon Oct 1, 2019
Sure, I'll post the proposal here.
16 remaining items
MarkCiliaVincenti commentedon Oct 11, 2019
The altitude calculation can be improved. I'm working on a WeatherHelper (#783) and I'll be adding an altitude calculation there. However I'm trying to gather information on how to improve this calculation due to the fact that there is a missing input in the current formula: temperature. The current calculation is assuming 15C, but you can see that temperature affects the altitude calculation. Take a look at https://www.mide.com/pages/air-pressure-at-altitude-calculator for example.
MarkCiliaVincenti commentedon Oct 11, 2019
@RobinTTY please take the changes in #783 into consideration when making these changes, because they will conflict otherwise.
Ellerbach commentedon Dec 22, 2020
I feel this has been fixed over time with the various PR. So closing it. Feel free to reopen if it's still not the case.
krwq commentedon Jun 14, 2021
still an issue in 1.4 (it keeps reading same value if you don't set it). I also need to wait around 100ms after setting power mode, otherwise I'm getting random errors ocasionally
pgrawehr commentedon Dec 15, 2022
Removing vNext, since not a blocker (there's an existing and well-known workaround)
krwq commentedon Apr 24, 2025
[Triage] It would need convenience method but since sample is doing the right thing we don't want to do extra operations. We don't track requests for convenience methods, PR is needed to further discuss this.