NucuCar/NucuCar.Sensors/EnvironmentSensor/Bme680Sensor.cs

140 lines
4.8 KiB
C#
Raw Normal View History

2019-11-09 14:54:06 +00:00
using System;
2019-11-14 14:45:32 +00:00
using System.Collections.Generic;
2019-11-09 14:54:06 +00:00
using System.Device.I2c;
using System.Threading.Tasks;
using Iot.Device.Bmxx80;
using Iot.Device.Bmxx80.PowerMode;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2019-12-06 22:54:32 +00:00
using NucuCar.Domain.Sensors;
using NucuCarSensorsProto;
2019-11-09 14:54:06 +00:00
namespace NucuCar.Sensors.EnvironmentSensor
2019-11-09 14:54:06 +00:00
{
2019-11-24 13:12:12 +00:00
/// <summary>
/// Abstraction for the BME680 sensor.
/// See: https://www.bosch-sensortec.com/bst/products/all_products/bme680
/// </summary>
public class Bme680Sensor : GenericTelemeterSensor, IDisposable, ISensor<Bme680Sensor>
2019-11-09 14:54:06 +00:00
{
private I2cConnectionSettings _i2CSettings;
private I2cDevice _i2CDevice;
private Bme680 _bme680;
private Bme680MeasurementData _lastMeasurement;
2019-11-09 14:54:06 +00:00
public Bme680Sensor()
{
}
public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options)
{
2019-12-17 19:40:20 +00:00
SensorStateEnum = SensorStateEnum.Uninitialized;
Logger = logger;
if (!options.Value.Enabled)
{
2019-12-17 19:40:20 +00:00
Logger?.LogInformation("BME680 Sensor is disabled!");
SensorStateEnum = SensorStateEnum.Disabled;
}
2019-11-30 16:55:55 +00:00
2019-12-17 19:40:20 +00:00
TelemetryEnabled = options.Value.Telemetry;
2019-11-30 16:55:55 +00:00
Object = this;
}
2019-12-17 19:40:20 +00:00
public override Bme680MeasurementData GetMeasurement()
{
return _lastMeasurement;
}
2019-12-17 19:40:20 +00:00
public override SensorStateEnum GetState()
{
2019-12-17 19:40:20 +00:00
return SensorStateEnum;
}
public void Dispose()
{
_bme680?.Dispose();
}
public override void Initialize()
2019-11-09 14:54:06 +00:00
{
2019-12-17 19:40:20 +00:00
if (SensorStateEnum == SensorStateEnum.Initialized || SensorStateEnum == SensorStateEnum.Disabled)
{
return;
}
_lastMeasurement = new Bme680MeasurementData();
2019-11-09 14:54:06 +00:00
try
{
/* Connect to default i2c address 0x76 */
_i2CSettings = new I2cConnectionSettings(1, Bme680.DefaultI2cAddress);
_i2CDevice = I2cDevice.Create(_i2CSettings);
_bme680 = new Bme680(_i2CDevice);
2019-11-09 14:54:06 +00:00
/* Initialize measurement */
_bme680.Reset();
_bme680.SetHumiditySampling(Sampling.UltraLowPower);
_bme680.SetTemperatureSampling(Sampling.UltraHighResolution);
_bme680.SetPressureSampling(Sampling.UltraLowPower);
2019-12-17 19:40:20 +00:00
SensorStateEnum = SensorStateEnum.Initialized;
2019-12-17 19:40:20 +00:00
Logger?.LogInformation($"{DateTimeOffset.Now}:BME680 Sensor initialization OK.");
2019-11-09 14:54:06 +00:00
}
catch (System.IO.IOException e)
{
2019-12-17 19:40:20 +00:00
Logger?.LogError($"{DateTimeOffset.Now}:BME680 Sensor initialization FAIL.");
Logger?.LogTrace(e.Message);
SensorStateEnum = SensorStateEnum.Error;
2019-11-09 14:54:06 +00:00
}
}
2019-12-17 19:40:20 +00:00
public override async Task TakeMeasurementAsync()
2019-11-09 14:54:06 +00:00
{
2019-12-17 19:40:20 +00:00
if (SensorStateEnum != SensorStateEnum.Initialized)
2019-11-09 14:54:06 +00:00
{
throw new InvalidOperationException("Can't take measurement on uninitialized sensor!");
2019-11-09 14:54:06 +00:00
}
2019-11-09 14:54:06 +00:00
/* Force the sensor to take a measurement. */
_bme680.SetPowerMode(Bme680PowerMode.Forced);
_lastMeasurement.Temperature = (await _bme680.ReadTemperatureAsync()).Celsius;
_lastMeasurement.Pressure = await _bme680.ReadPressureAsync();
_lastMeasurement.Humidity = await _bme680.ReadHumidityAsync();
_lastMeasurement.VolatileOrganicCompounds = 0.0; // Not implemented.
2019-12-17 19:40:20 +00:00
Logger?.LogDebug($"{DateTimeOffset.Now}:BME680: reading");
Logger?.LogInformation(
$"temperature:{_lastMeasurement.Temperature:N2} \u00B0C|" +
$"pressure:{_lastMeasurement.Pressure:N2} hPa|" +
$"humidity:{_lastMeasurement.Humidity:N2} %rH|" +
$"voc:{_lastMeasurement.VolatileOrganicCompounds}");
2019-11-09 14:54:06 +00:00
}
2019-11-14 14:45:32 +00:00
public override string GetIdentifier()
{
return nameof(EnvironmentSensor);
}
public override Dictionary<string, object> GetTelemetryData()
2019-11-14 14:45:32 +00:00
{
Dictionary<string, object> returnValue = null;
2019-12-17 19:40:20 +00:00
if (_lastMeasurement != null && TelemetryEnabled)
2019-11-14 14:45:32 +00:00
{
returnValue = new Dictionary<string, object>
{
2019-12-17 19:40:20 +00:00
["sensor_state"] = SensorStateEnum,
["temperature"] = _lastMeasurement.Temperature,
["humidity"] = _lastMeasurement.Humidity,
["pressure"] = _lastMeasurement.Pressure,
["voc"] = _lastMeasurement.VolatileOrganicCompounds
};
}
2019-11-24 13:12:12 +00:00
return returnValue;
2019-11-14 14:45:32 +00:00
}
2019-11-30 16:55:55 +00:00
public Bme680Sensor Object { get; }
2019-11-09 14:54:06 +00:00
}
}