Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BMxx80 library BME680 sample code throws exceptions when GasResistance and Pressure values accessed #1157

Closed
KiwiBryn opened this issue Sep 28, 2022 · 0 comments · Fixed by nanoframework/nanoFramework.IoT.Device#612

Comments

@KiwiBryn
Copy link

Library/API/IoT binding

nanoFramework.IoT.Device.Bmxx80

Visual Studio version

17.3.5

.NET nanoFramework extension version

2022.2.0.41

Target name(s)

ESP32 wrover

Firmware version

NA

Device capabilities

System Information
HAL build info: nanoCLR running @ ESP32 built with ESP-IDF 1b16ef6
Target: ESP32
Platform: ESP32

Firmware build Info:
Date: Sep 27 2022
Type: MinSizeRel build, chip rev. 3, without support for PSRAM
CLR Version: 1.8.0.611
Compiler: GNU ARM GCC v8.4.0

OEM Product codes (vendor, model, SKU): 0, 0, 0

Serial Numbers (module, system):
00000000000000000000000000000000
0000000000000000

Target capabilities:
Has nanoBooter: NO
IFU capable: NO
Has proprietary bootloader: YES

AppDomains:

Assemblies:
AzureIoTHubRAK11200PowerConservation, 1.0.0.0
System.IO.Streams, 1.1.15.36091
System.Device.I2c, 1.0.3.3
UnitsNet.Temperature, 4.145.0.0
nanoFramework.Runtime.Events, 1.10.0.3
System.Net.Http, 1.5.24.8533
System.Buffers.Binary.BinaryPrimitives, 1.2.0.0
System.Net, 1.10.21.18203
mscorlib, 1.12.0.4
nanoFramework.System.Collections, 1.4.0.3
System.Math, 1.4.4.3
System.Diagnostics.Stopwatch, 1.2.0.0
nanoFramework.Hardware.Esp32, 1.4.1.63006
System.Threading, 1.1.8.6695
nanoFramework.Runtime.Native, 1.5.4.3
Iot.Device.Shtc3, 1.2.0.0
nanoFramework.System.Text, 1.2.7.60109
System.Device.Wifi, 1.5.22.4970
System.Device.Adc, 1.0.2.4
UnitsNet.RelativeHumidity, 4.145.0.0

Native Assemblies:
mscorlib v100.5.0.17, checksum 0x004CF1CE
nanoFramework.Runtime.Native v100.0.9.0, checksum 0x109F6F22
nanoFramework.Hardware.Esp32 v100.0.7.3, checksum 0xBE7FF253
nanoFramework.Hardware.Esp32.Rmt v100.0.3.0, checksum 0x0A915860
nanoFramework.Device.OneWire v100.0.4.0, checksum 0xB95C43B4
nanoFramework.Networking.Sntp v100.0.4.4, checksum 0xE2D9BDED
nanoFramework.ResourceManager v100.0.0.1, checksum 0xDCD7DF4D
nanoFramework.System.Collections v100.0.1.0, checksum 0x2DC2B090
nanoFramework.System.Text v100.0.0.1, checksum 0x8E6EB73D
nanoFramework.Runtime.Events v100.0.8.0, checksum 0x0EAB00C9
EventSink v1.0.0.0, checksum 0xF32F4C3E
System.IO.FileSystem v1.0.0.0, checksum 0x3AB74021
System.Math v100.0.5.4, checksum 0x46092CB1
System.Net v100.1.5.0, checksum 0x5BAB8CB3
System.Device.Adc v100.0.0.0, checksum 0xE5B80F0B
System.Device.Dac v100.0.0.6, checksum 0x02B3E860
System.Device.Gpio v100.1.0.6, checksum 0x097E7BC5
System.Device.I2c v100.0.0.1, checksum 0xFA806D33
System.Device.Pwm v100.1.0.4, checksum 0xABF532C3
System.IO.Ports v100.1.6.1, checksum 0xB798CE30
System.Device.Spi v100.1.2.0, checksum 0x3F6E2A7E
System.Device.Wifi v100.0.6.4, checksum 0x1C1D3214
Windows.Storage v100.0.2.0, checksum 0x954A4192

++++++++++++++++++++++++++++++++
++ Memory Map ++
++++++++++++++++++++++++++++++++
Type Start Size
++++++++++++++++++++++++++++++++
RAM 0x3ffe49ac 0x0001b000
FLASH 0x00000000 0x00400000

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++ Flash Sector Map ++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Region Start Blocks Bytes/Block Usage
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0 0x00010000 1 0x1A0000 nanoCLR
1 0x001B0000 1 0x1F0000 Deployment
2 0x003C0000 1 0x040000 Configuration

+++++++++++++++++++++++++++++++++++++++++++++++++++
++ Storage Usage Map ++
+++++++++++++++++++++++++++++++++++++++++++++++++++
Start Size (kB) Usage
+++++++++++++++++++++++++++++++++++++++++++++++++++
0x003C0000 0x040000 (256kB) Configuration
0x00010000 0x1A0000 (1664kB) nanoCLR
0x001B0000 0x1F0000 (1984kB) Deployment

Deployment Map
Empty

Description

Every so often BME680 sample GasResistance value access would throw an exception (see screen grab below). Less often Pressure (see screen grab below) would do the same

The values in the UnitsNet resistance and pressure were values "invalid". I think this could also occur for Temperature and Humidity but have been unable to repro.

The gas resistance tended to fail on start-up, and may have been more likely to fail if app hadn't been run for a while....

Have ordered BME280 and BMP280 breakouts to see if I can repro with them.

How to reproduce

  1. Download IoT.Device zip file
  2. Extract Bmxx80 Device code with sample app
  3. Setup ESP32 (I used wrover) with BME680 breakout as per device library instructions
  4. Modify Bmxx80 sample in program.cs to call BME680 RunSample()
  5. Run repeatedly and after a while when gas resistance or pressure values accessed exception will be thrown.

Note .equals will cause problems if #1143 not in release

Expected behaviour

If an exception thrown the sample code should catch it or there should be a way of checking whether temperature\humidity\pressure\gas resistance value is valid before use (This would be my preferred option)

I think the return values for the TryRead...Core calls should be checked

Screenshots

Bmxx80GasResitanceException
Bmxx80PressureException

Sample project or code

I used the sample code from

https://github.com/nanoframework/nanoFramework.IoT.Device/tree/develop/devices/Bmxx80/samples

Aditional information

I think the issue might be a "hangover" from .Net Core code.

The .Net core version of the Bmxx80 library used nullable reference types which are not supported by the nanoFramework.

https://github.com/dotnet/iot/blob/main/src/devices/Bmxx80/ReadResult/Bme680ReadResult.cs

 /// <summary>
/// Collected gas resistance measurement. NaN if no measurement was performed.
/// </summary>
public ElectricResistance? GasResistance { get; }

A "null" is used for readings that "fail"

/// <summary>
/// Performs an asynchronous reading.
/// </summary>
/// <returns><see cref="Bme680ReadResult"/></returns>
public async Task<Bme680ReadResult> ReadAsync()
{
    SetPowerMode(Bme680PowerMode.Forced);
    await Task.Delay((int)GetMeasurementDuration(HeaterProfile).Milliseconds);

    var tempSuccess = TryReadTemperatureCore(out var temperature);
    var pressSuccess = TryReadPressureCore(out var pressure, skipTempFineRead: true);
    var humiditySuccess = TryReadHumidityCore(out var humidity, skipTempFineRead: true);
    var gasSuccess = TryReadGasResistanceCore(out var gasResistance);

    return new Bme680ReadResult(tempSuccess ? temperature : null, pressSuccess ? pressure : null, humiditySuccess ? humidity : null, gasSuccess ? gasResistance : null);
}

Which were then checked in sample code

Console.WriteLine($"Gas resistance: {readResult.GasResistance?.Ohms:0.##}Ohm");

The nanoFramework code ignores TryRead... returns

/// <summary>
/// Performs a synchronous reading.
/// </summary>
/// <returns><see cref="Bme680ReadResult"/></returns>
public Bme680ReadResult Read()
{
    SetPowerMode(Bme680PowerMode.Forced);
    Thread.Sleep((int)GetMeasurementDuration(HeaterProfile).Milliseconds);

    TryReadTemperatureCore(out Temperature temperature);
    TryReadPressureCore(out Pressure pressure, skipTempFineRead: true);
    TryReadHumidityCore(out RelativeHumidity humidity, skipTempFineRead: true);
    TryReadGasResistanceCore(out ElectricResistance gasResistance);

    return new Bme680ReadResult(temperature, pressure, humidity, gasResistance);
}

Have PR just about finished where I have added TemperatureIsValid, HumidityIsValid etc. just waiting on BME280 & BMP280 breakout boards I ordered to test fixes with.

I will check other device libraries to see if there is the same pattern used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants