Meadow.Foundation.Sensors.Atmospheric.Bme688
Bme688 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Bme688? sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initializing...");
//CreateSpiSensor();
CreateI2CSensor();
//uncomment to enable on sensor heater for gas readings
//EnableGasHeater();
var consumer = Bme688.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
},
filter: result =>
{
if (result.Old?.Temperature is { } oldTemp &&
result.Old?.Humidity is { } oldHumidity &&
result.New.Temperature is { } newTemp &&
result.New.Humidity is { } newHumidity)
{
return ((newTemp - oldTemp).Abs().Celsius > 0.5 &&
(newHumidity - oldHumidity).Percent > 0.05);
}
return false;
}
);
sensor?.Subscribe(consumer);
if (sensor != null)
{
sensor.Updated += (sender, result) =>
{
Resolver.Log.Info($" Temperature: {result.New.Temperature?.Celsius:N2}C");
Resolver.Log.Info($" Relative Humidity: {result.New.Humidity:N2}%");
Resolver.Log.Info($" Pressure: {result.New.Pressure?.Millibar:N2}mbar ({result.New.Pressure?.Pascal:N2}Pa)");
if (sensor.GasConversionIsEnabled)
{
Resolver.Log.Info($" Gas Resistance: {result.New.GasResistance:N0}Ohms");
}
};
}
sensor?.StartUpdating(TimeSpan.FromSeconds(2));
ReadConditions().Wait();
return base.Initialize();
}
void EnableGasHeater()
{
if (sensor != null)
{
sensor.GasConversionIsEnabled = true;
sensor.HeaterIsEnabled = true;
sensor.ConfigureHeatingProfile(Bme688.HeaterProfileType.Profile1, new Temperature(300), TimeSpan.FromMilliseconds(100), new Temperature(22));
sensor.HeaterProfile = Bme688.HeaterProfileType.Profile1;
}
}
void CreateSpiSensor()
{
Resolver.Log.Info("Create BME688 sensor with SPI...");
var spiBus = Device.CreateSpiBus();
sensor = new Bme688(spiBus, Device.CreateDigitalOutputPort(Device.Pins.D01));
}
void CreateI2CSensor()
{
Resolver.Log.Info("Create BME688 sensor with I2C...");
var i2c = Device.CreateI2cBus();
sensor = new Bme688(i2c, (byte)Bme688.Addresses.Address_0x76);
}
async Task ReadConditions()
{
if (sensor == null) { return; }
var (Temperature, Humidity, Pressure, Resistance) = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info($" Temperature: {Temperature?.Celsius:N2}C");
Resolver.Log.Info($" Pressure: {Pressure?.Hectopascal:N2}hPa");
Resolver.Log.Info($" Relative Humidity: {Humidity?.Percent:N2}%");
Resolver.Log.Info($" Gas Resistance: {Resistance?.Ohms:N0}Ohms");
}
Sample project(s) available on GitHub
Wiring Example
To wire a BME688 to your Meadow board, connect the following:
Class Bme688
Represents a BME688 Temperature, Pressure, Humidity and VOS sensor
Assembly: Bme68x.dll
View Source
public class Bme688 : Bme68x, IObservable<IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity, Pressure? Pressure, Resistance? GasResistance)>>, ISamplingSensor<(Temperature? Temperature, RelativeHumidity? Humidity, Pressure? Pressure, Resistance? GasResistance)>, ISensor<(Temperature? Temperature, RelativeHumidity? Humidity, Pressure? Pressure, Resistance? GasResistance)>, ITemperatureSensor, ISamplingSensor<Temperature>, ISensor<Temperature>, IHumiditySensor, ISamplingSensor<RelativeHumidity>, ISensor<RelativeHumidity>, IBarometricPressureSensor, ISamplingSensor<Pressure>, ISensor<Pressure>, IGasResistanceSensor, ISamplingSensor<Resistance>, ISensor<Resistance>, ISensor, ISamplingSensor, ISpiPeripheral, II2cPeripheral, ISleepAwarePeripheral, IDisposable
Inheritance: System.Object
-> Meadow.Foundation.ObservableBase<UNIT>
Implements:
Expand
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>,System.Nullable<Meadow.Units.Pressure>,System.Nullable<Meadow.Units.Resistance>>>>
, Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>,System.Nullable<Meadow.Units.Pressure>,System.Nullable<Meadow.Units.Resistance>>>
, Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>,System.Nullable<Meadow.Units.Pressure>,System.Nullable<Meadow.Units.Resistance>>>
, Meadow.Peripherals.Sensors.ITemperatureSensor
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Temperature>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Temperature>
, Meadow.Peripherals.Sensors.Atmospheric.IHumiditySensor
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.RelativeHumidity>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.RelativeHumidity>
, Meadow.Peripherals.Sensors.Atmospheric.IBarometricPressureSensor
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Pressure>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Pressure>
, Meadow.Peripherals.Sensors.Environmental.IGasResistanceSensor
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Resistance>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Resistance>
, Meadow.Peripherals.Sensors.ISensor
, Meadow.Peripherals.Sensors.ISamplingSensor
, Meadow.Hardware.ISpiPeripheral
, Meadow.Hardware.II2cPeripheral
, Meadow.ISleepAwarePeripheral
, System.IDisposable
Implements
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>,System.Nullable<Meadow.Units.Pressure>,System.Nullable<Meadow.Units.Resistance>>>>
Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>,System.Nullable<Meadow.Units.Pressure>,System.Nullable<Meadow.Units.Resistance>>>
Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Temperature>,System.Nullable<Meadow.Units.RelativeHumidity>,System.Nullable<Meadow.Units.Pressure>,System.Nullable<Meadow.Units.Resistance>>>
Meadow.Peripherals.Sensors.ITemperatureSensor
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Temperature>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Temperature>
Meadow.Peripherals.Sensors.Atmospheric.IHumiditySensor
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.RelativeHumidity>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.RelativeHumidity>
Meadow.Peripherals.Sensors.Atmospheric.IBarometricPressureSensor
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Pressure>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Pressure>
Meadow.Peripherals.Sensors.Environmental.IGasResistanceSensor
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Resistance>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Resistance>
Meadow.Peripherals.Sensors.ISensor
Meadow.Peripherals.Sensors.ISamplingSensor
Meadow.Hardware.ISpiPeripheral
Meadow.Hardware.II2cPeripheral
Meadow.ISleepAwarePeripheral
System.IDisposable