private static async Task <Weather> GetWeatherAsync() { I2cConnectionSettings settings = new I2cConnectionSettings(0, Bme280.SecondaryI2cAddress); I2cDevice device = I2cDevice.Create(settings); using Bme280 bme = new Bme280(device); bme.SetPowerMode(Bmx280PowerMode.Normal); bme.SetTemperatureSampling(Sampling.UltraHighResolution); bme.SetPressureSampling(Sampling.UltraHighResolution); bme.SetHumiditySampling(Sampling.UltraHighResolution); double t = Math.Round((await bme.ReadTemperatureAsync()).Celsius, 2); double h = Math.Round(await bme.ReadHumidityAsync(), 2); double p = Math.Round(await bme.ReadPressureAsync(), 2); bme.SetPowerMode(Bmx280PowerMode.Sleep); //Console.WriteLine($"Temperature:{t} Humidity:{h} Pressure:{p}"); return(new Weather { DateTime = DateTime.Now, WeatherName = await WeatherHelper.GetXinzhiWeatherAsync(ConfigHelper.Get("Xinzhi:Key"), ConfigHelper.Get("Xinzhi:Location")), Temperature = t, Humidity = h, Pressure = p, ImageBase64 = GetImageBase64() }); }
public static Bme280 CreateBme280() { var settings = new I2cConnectionSettings(1, Bme280.DefaultI2cAddress); var bme280 = new Bme280(I2cDevice.Create(settings)); // https://github.com/dotnet/iot/issues/753 bme280.SetPowerMode(Bmx280PowerMode.Forced); return(bme280); }
static async Task Main(string[] args) { //LED setup var pin = 17; var lightTimeInMilliseconds = 1000; var dimTimeInMilliseconds = 800; //bus id on the raspberry pi 3 const int busId = 1; var i2cSettings = new I2cConnectionSettings(busId, Bme280.DefaultI2cAddress); var i2cDevice = I2cDevice.Create(i2cSettings); var i2CBmpe80 = new Bme280(i2cDevice); using (i2CBmpe80) { while (true) { //set mode forced so device sleeps after read i2CBmpe80.SetPowerMode(Bmx280PowerMode.Forced); //set samplings i2CBmpe80.SetTemperatureSampling(Sampling.UltraLowPower); i2CBmpe80.SetHumiditySampling(Sampling.UltraLowPower); //read values Temperature tempValue = await i2CBmpe80.ReadTemperatureAsync(); Console.WriteLine($"Temperature: {tempValue.Celsius} C"); double humValue = await i2CBmpe80.ReadHumidityAsync(); Console.WriteLine($"Humidity: {humValue} %"); // Sleeping it so that we have a chance to get more measurements. Thread.Sleep(500); humValue = await i2CBmpe80.ReadHumidityAsync(); if (humValue > 50.00) { using (GpioController controller = new GpioController()) { controller.OpenPin(pin, PinMode.Output); Console.WriteLine($"GPIO pin enabled for use: {pin}"); Console.WriteLine($"Light for {lightTimeInMilliseconds}ms"); controller.Write(pin, PinValue.High); Thread.Sleep(lightTimeInMilliseconds); Console.WriteLine($"Dim for {dimTimeInMilliseconds}ms"); controller.Write(pin, PinValue.Low); Thread.Sleep(dimTimeInMilliseconds); } } } } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { //IHostedService is a singleton. It cannot consume scopped services. //Using the IServiceProvider, and a 'using' create a scope and a GetRequiredServices to create the scoped service using (var scope = Services.CreateScope()) { var _db = scope.ServiceProvider.GetRequiredService <climatepiDBContext>(); var i2cSettings = new I2cConnectionSettings(1, Bme280.SecondaryI2cAddress); using I2cDevice i2cDevice = I2cDevice.Create(i2cSettings); using var bme280 = new Bme280(i2cDevice); int measurementTime = bme280.GetMeasurementDuration(); while (true) { bme280.SetPowerMode(Bmx280PowerMode.Forced); Thread.Sleep(measurementTime); bme280.TryReadTemperature(out var tempValue); bme280.TryReadPressure(out var preValue); bme280.TryReadHumidity(out var humValue); bme280.TryReadAltitude(out var altValue); var condition = new Server.Database.Condition() { LoggedAt = DateTime.UtcNow, DegreesCelsius = tempValue.DegreesCelsius, PressureMillibars = preValue.Millibars, HumidityPercentage = humValue.Percent }; _db.Conditions.Add(condition); await _db.SaveChangesAsync(); //Thread.Sleep(1000); //This works, but it is more often than I need Thread.Sleep(60000); //New reading every 1 minute } } } catch (Exception ex) { } }
public async Task <double> GetPressure() { using (var sensor = new Bme280(_bmp280)) { sensor.SetPowerMode(PowerMode.Forced); double pressure = await sensor.ReadPressureAsync(); var temp = await sensor.ReadTemperatureAsync(); var alt = await sensor.ReadAltitudeAsync(pressure); double humid = await sensor.ReadHumidityAsync(); _logger.LogDebug( $"Pressure value: {pressure}; PowerMode: {sensor.ReadPowerMode()}; Temperature: {temp.Celsius}; Altitude: {alt}; Humidity: {humid}"); return(pressure); } }
public Task <SensorData> TakeMeasurement() { _bme280.SetPowerMode(Bmx280PowerMode.Forced); Task.Delay(_measurementDuration).Wait(); _bme280.TryReadTemperature(out var temperature); _bme280.TryReadHumidity(out var humidity); _bme280.TryReadPressure(out var pressure); var container = new SensorData(DeviceName) { Measurements = new Dictionary <string, double> { { "Temperature", temperature.DegreesCelsius }, { "Humidity", humidity }, { "Pressure", pressure.Pascals } } }; return(Task.FromResult(container)); }
public ISensorReading ReadSensor() { var result = new HumidityTemperatureAltitudePressureReading(); using (_sensor) { _sensor.SetPowerMode(Bmx280PowerMode.Forced); //set samplings _sensor.SetTemperatureSampling(Sampling.UltraLowPower); _sensor.SetPressureSampling(Sampling.UltraLowPower); _sensor.SetHumiditySampling(Sampling.UltraLowPower); result.Humidity = _sensor.ReadHumidityAsync().Result; result.Temperature = _sensor.ReadTemperatureAsync().Result; result.Altitude = _sensor.ReadAltitudeAsync(defaultSeaLevelPressure).Result; result.Pressure = _sensor.ReadPressureAsync().Result; } return(result); }
static async Task Main(string[] args) { // bus id on the raspberry pi 3 const int busId = 1; // Setup i2C device (BME280) var i2cSettings = new I2cConnectionSettings(busId, Bme280.DefaultI2cAddress); var i2cDevice = I2cDevice.Create(i2cSettings); var i2CBmpe80 = new Bme280(i2cDevice); using (i2CBmpe80) { while (true) { // set mode forced so device sleeps after read i2CBmpe80.SetPowerMode(Bmx280PowerMode.Forced); // Get sampling accuracy i2CBmpe80.SetHumiditySampling(Sampling.Standard); i2CBmpe80.SetTemperatureSampling(Sampling.Standard); i2CBmpe80.SetPressureSampling(Sampling.Standard); // Get variables Iot.Units.Temperature tempValue = await i2CBmpe80.ReadTemperatureAsync(); double humValue = await i2CBmpe80.ReadHumidityAsync(); double preValue = await i2CBmpe80.ReadPressureAsync(); // Print to screen Console.WriteLine($"Weather at time: {DateTime.Now}"); Console.WriteLine($"Temperature: {tempValue.Celsius:0.#}\u00B0C"); Console.WriteLine($"Pressure: {preValue/100:0.##}hPa"); Console.WriteLine($"Relative humidity: {humValue:0.#}%\n"); Thread.Sleep(2000); } } }
private void CreateSensor() { var i2cSettings = new I2cConnectionSettings(busId, Bmx280Base.SecondaryI2cAddress); var i2cDevice = I2cDevice.Create(i2cSettings); _bme280 = new Bme280(i2cDevice); try { // set higher sampling _bme280.TemperatureSampling = Sampling.LowPower; _bme280.PressureSampling = Sampling.UltraHighResolution; _bme280.HumiditySampling = Sampling.Standard; // set mode forced so device sleeps after read _bme280.SetPowerMode(Bmx280PowerMode.Forced); } catch (Exception ex) { _logger.Error(ex, "Error creating the sensor"); } }
/// <summary> /// Entry point for example program /// </summary> /// <param name="args">Command line arguments</param> public static void Main(string[] args) { Console.WriteLine("Hello Bme280!"); // bus id on the raspberry pi 3 const int busId = 1; // set this to the current sea level pressure in the area for correct altitude readings var defaultSeaLevelPressure = WeatherHelper.MeanSeaLevel; var i2cSettings = new I2cConnectionSettings(busId, Bme280.DefaultI2cAddress); var i2cDevice = I2cDevice.Create(i2cSettings); var i2CBmpe80 = new Bme280(i2cDevice); using (i2CBmpe80) { while (true) { // set higher sampling i2CBmpe80.TemperatureSampling = Sampling.LowPower; i2CBmpe80.PressureSampling = Sampling.UltraHighResolution; i2CBmpe80.HumiditySampling = Sampling.Standard; // set mode forced so device sleeps after read i2CBmpe80.SetPowerMode(Bmx280PowerMode.Forced); // wait for measurement to be performed var measurementTime = i2CBmpe80.GetMeasurementDuration(); Thread.Sleep(measurementTime); // read values i2CBmpe80.TryReadTemperature(out var tempValue); i2CBmpe80.TryReadPressure(out var preValue); i2CBmpe80.TryReadHumidity(out var humValue); // Note that if you already have the pressure value and the temperature, you could also calculate altitude by using // var altValue = WeatherHelper.CalculateAltitude(preValue, defaultSeaLevelPressure, tempValue) which would be more performant. i2CBmpe80.TryReadAltitude(defaultSeaLevelPressure, out var altValue); Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); Console.WriteLine($"Pressure: {preValue.Hectopascals:0.##}hPa"); Console.WriteLine($"Altitude: {altValue:0.##}m"); Console.WriteLine($"Relative humidity: {humValue:0.#}%"); // WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity. Console.WriteLine($"Heat index: {WeatherHelper.CalculateHeatIndex(tempValue, humValue).DegreesCelsius:0.#}\u00B0C"); Console.WriteLine($"Dew point: {WeatherHelper.CalculateDewPoint(tempValue, humValue).DegreesCelsius:0.#}\u00B0C"); Thread.Sleep(1000); // change sampling and filter i2CBmpe80.TemperatureSampling = Sampling.UltraHighResolution; i2CBmpe80.PressureSampling = Sampling.UltraLowPower; i2CBmpe80.HumiditySampling = Sampling.UltraLowPower; i2CBmpe80.FilterMode = Bmx280FilteringMode.X2; // set mode forced and read again i2CBmpe80.SetPowerMode(Bmx280PowerMode.Forced); // wait for measurement to be performed measurementTime = i2CBmpe80.GetMeasurementDuration(); Thread.Sleep(measurementTime); // read values i2CBmpe80.TryReadTemperature(out tempValue); i2CBmpe80.TryReadPressure(out preValue); i2CBmpe80.TryReadHumidity(out humValue); // Note that if you already have the pressure value and the temperature, you could also calculate altitude by using // var altValue = WeatherHelper.CalculateAltitude(preValue, defaultSeaLevelPressure, tempValue) which would be more performant. i2CBmpe80.TryReadAltitude(defaultSeaLevelPressure, out altValue); Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); Console.WriteLine($"Pressure: {preValue.Hectopascals:0.##}hPa"); Console.WriteLine($"Altitude: {altValue:0.##}m"); Console.WriteLine($"Relative humidity: {humValue:0.#}%"); // WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity. Console.WriteLine($"Heat index: {WeatherHelper.CalculateHeatIndex(tempValue, humValue).DegreesCelsius:0.#}\u00B0C"); Console.WriteLine($"Dew point: {WeatherHelper.CalculateDewPoint(tempValue, humValue).DegreesCelsius:0.#}\u00B0C"); Thread.Sleep(5000); } } }
static async Task Main(string[] args) { Console.WriteLine("Hello Bme280!"); //bus id on the raspberry pi 3 const int busId = 1; //set this to the current sea level pressure in the area for correct altitude readings const double defaultSeaLevelPressure = 1033.00; var i2cSettings = new I2cConnectionSettings(busId, Bme280.DefaultI2cAddress); var i2cDevice = new UnixI2cDevice(i2cSettings); var i2CBmpe80 = new Bme280(i2cDevice); using (i2CBmpe80) { while (true) { ////set mode forced so device sleeps after read i2CBmpe80.SetPowerMode(PowerMode.Forced); //set samplings i2CBmpe80.SetTemperatureSampling(Sampling.UltraLowPower); i2CBmpe80.SetPressureSampling(Sampling.UltraLowPower); //read values Temperature tempValue = await i2CBmpe80.ReadTemperatureAsync(); Console.WriteLine($"Temperature {tempValue.Celsius}"); double preValue = await i2CBmpe80.ReadPressureAsync(); Console.WriteLine($"Pressure {preValue}"); double altValue = await i2CBmpe80.ReadAltitudeAsync(defaultSeaLevelPressure); Console.WriteLine($"Altitude: {altValue}"); Thread.Sleep(1000); //set higher sampling i2CBmpe80.SetTemperatureSampling(Sampling.LowPower); Console.WriteLine(i2CBmpe80.ReadTemperatureSampling()); i2CBmpe80.SetPressureSampling(Sampling.UltraHighResolution); Console.WriteLine(i2CBmpe80.ReadPressureSampling()); //set mode forced and read again i2CBmpe80.SetPowerMode(PowerMode.Forced); //read values tempValue = await i2CBmpe80.ReadTemperatureAsync(); Console.WriteLine($"Temperature {tempValue}"); preValue = await i2CBmpe80.ReadPressureAsync(); Console.WriteLine($"Pressure {preValue}"); altValue = await i2CBmpe80.ReadAltitudeAsync(defaultSeaLevelPressure); Console.WriteLine($"Altitude: {altValue}"); Thread.Sleep(5000); //set sampling to higher i2CBmpe80.SetTemperatureSampling(Sampling.UltraHighResolution); Console.WriteLine(i2CBmpe80.ReadTemperatureSampling()); i2CBmpe80.SetPressureSampling(Sampling.UltraLowPower); Console.WriteLine(i2CBmpe80.ReadPressureSampling()); } } }
static void Main(string[] args) { Console.WriteLine("Hello Bme280!"); //bus id on the raspberry pi 3 const int busId = 1; //set this to the current sea level pressure in the area for correct altitude readings var defaultSeaLevelPressure = Pressure.MeanSeaLevel; var i2cSettings = new I2cConnectionSettings(busId, Bme280.DefaultI2cAddress); var i2cDevice = I2cDevice.Create(i2cSettings); var i2CBmpe80 = new Bme280(i2cDevice); using (i2CBmpe80) { while (true) { //set higher sampling i2CBmpe80.TemperatureSampling = Sampling.LowPower; i2CBmpe80.PressureSampling = Sampling.UltraHighResolution; i2CBmpe80.HumiditySampling = Sampling.Standard; //set mode forced so device sleeps after read i2CBmpe80.SetPowerMode(Bmx280PowerMode.Forced); // wait for measurement to be performed var measurementTime = i2CBmpe80.GetMeasurementDuration(); Thread.Sleep(measurementTime); //read values i2CBmpe80.TryReadTemperature(out var tempValue); Console.WriteLine($"Temperature: {tempValue.Celsius} \u00B0C"); i2CBmpe80.TryReadPressure(out var preValue); Console.WriteLine($"Pressure: {preValue.Hectopascal} hPa"); i2CBmpe80.TryReadAltitude(defaultSeaLevelPressure, out var altValue); Console.WriteLine($"Altitude: {altValue} meters"); i2CBmpe80.TryReadHumidity(out var humValue); Console.WriteLine($"Humidity: {humValue} %"); Thread.Sleep(1000); //change sampling and filter i2CBmpe80.TemperatureSampling = Sampling.UltraHighResolution; i2CBmpe80.PressureSampling = Sampling.UltraLowPower; i2CBmpe80.HumiditySampling = Sampling.UltraLowPower; i2CBmpe80.FilterMode = Bmx280FilteringMode.X2; //set mode forced and read again i2CBmpe80.SetPowerMode(Bmx280PowerMode.Forced); // wait for measurement to be performed measurementTime = i2CBmpe80.GetMeasurementDuration(); Thread.Sleep(measurementTime); //read values i2CBmpe80.TryReadTemperature(out tempValue); Console.WriteLine($"Temperature: {tempValue.Celsius} \u00B0C"); i2CBmpe80.TryReadPressure(out preValue); Console.WriteLine($"Pressure: {preValue.Hectopascal} hPa"); i2CBmpe80.TryReadAltitude(defaultSeaLevelPressure, out altValue); Console.WriteLine($"Altitude: {altValue} meters"); i2CBmpe80.TryReadHumidity(out humValue); Console.WriteLine($"Humidity: {humValue} %"); Thread.Sleep(5000); } } }
private void ReadSensor(object state) { if (!_debugMode && _bme280 == null) { return; } var readingOK = true; if (!_debugMode) { try { // set mode forced and read again _bme280.SetPowerMode(Bmx280PowerMode.Forced); // wait for measurement to be performed var measurementTime = _bme280.GetMeasurementDuration(); Thread.Sleep(measurementTime); // read values _bme280.TryReadTemperature(out var tempValue); _bme280.TryReadPressure(out var preValue); _bme280.TryReadHumidity(out var humValue); var itsRaining = DigitalIOConnector.Instance.ReadRainSensorDetectsRain(); var rainCorrected = false; if (itsRaining && tempValue.DegreesCelsius >= _maxTemperatureForRain) { itsRaining = false; rainCorrected = true; _logger.Information("Raining sensor corrected ({Temp}° greater than {MaxTempRain}°)", tempValue.DegreesCelsius, _maxTemperatureForRain); } _lastReadingPoint = new Measurement() { TimeStamp = DateTime.Now, Temperature = tempValue.DegreesCelsius, Pressure = preValue.Hectopascals, Humidity = humValue.Percent, DewPoint = WeatherHelper.CalculateDewPoint(tempValue, humValue).DegreesCelsius, Raining = itsRaining, RainCorrected = rainCorrected }; } catch (Exception ex) { readingOK = false; _lastReadingPoint = null; _logger.Error(ex, "Error while reading"); } } else { _lastReadingPoint = new Measurement() { TimeStamp = DateTime.Now, Temperature = (double)_random.Next(1000, 4000) / 100d, Pressure = (double)_random.Next(95000, 110000) / 100d, Humidity = (double)_random.Next(5000, 9000) / 100d, DewPoint = 12.12345d, Raining = _random.Next(0, 1) == 1 }; } OnPropertyChanged(readingOK ? "Reading done" : "Reading Error"); }
private static void SetupBme280(Bme280 bme280) { // https://github.com/dotnet/iot/issues/753 bme280.SetPowerMode(Bmx280PowerMode.Normal); Thread.Sleep(100); }
public Result <Unit> Init() { _controller = new GpioController(PinNumberingScheme.Logical, new RaspberryPi3Driver()); _controller.OpenPin(Pin.HallBottom, PinMode.InputPullUp); _controller.OpenPin(Pin.HallTop, PinMode.InputPullUp); _controller.OpenPin(Pin.PhotoelectricBarrier, PinMode.InputPullUp); _controller.OpenPin(Pin.MotorEnable, PinMode.Output); _controller.OpenPin(Pin.MotorLeft, PinMode.Output); _controller.OpenPin(Pin.MotorRight, PinMode.Output); _controller.OpenPin(Pin.EmergencyTop, PinMode.InputPullUp); _controller.OpenPin(Pin.DC12_1, PinMode.Output); _controller.OpenPin(Pin.DC12_2, PinMode.Output); //_pwmMotor = new SoftwarePwmChannel(Pin.MotorEnable, 200, 0.1); //_pwmMotor.Start(); _controller.Write(Pin.MotorEnable, PinValue.High); _controller.Write(Pin.MotorLeft, PinValue.Low); _controller.Write(Pin.MotorRight, PinValue.Low); _controller.Write(Pin.DC12_1, PinValue.Low); _controller.Write(Pin.DC12_2, PinValue.Low); Console.WriteLine($"Init sensor"); _bh1750Fvi = new Bh1750fvi(I2cDevice.Create(new I2cConnectionSettings(1, Bh1750fviExtenstion.DefaultI2cAddress))); // 23 _vl53L0X = new Vl53L0X(I2cDevice.Create(new I2cConnectionSettings(1, Vl53L0X.DefaultI2cAddress))); // 29 _bme280 = new Bme280(I2cDevice.Create(new I2cConnectionSettings(1, Bmx280Base.SecondaryI2cAddress))); // 76 _measurementTime = _bme280.GetMeasurementDuration(); _bme280.SetPowerMode(Bmx280PowerMode.Normal); //Thread.Sleep(_measurementTime); //_bme280.TryReadTemperature(out var tempValue); //_bme280.TryReadPressure(out var preValue); //_bme280.TryReadHumidity(out var humValue); //_bme280.TryReadAltitude(out var altValue); //Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); //Console.WriteLine($"Pressure: {preValue.Hectopascals:#.##} hPa"); //Console.WriteLine($"Relative humidity: {humValue.Percent:#.##}%"); //Console.WriteLine($"Estimated altitude: {altValue.Meters:#} m"); _amg88xx = new Amg88xx(I2cDevice.Create(new I2cConnectionSettings(1, Amg88xx.AlternativeI2cAddress))); // 69 try { _mpu9250 = new Mpu9250(I2cDevice.Create(new I2cConnectionSettings(1, Mpu6500.DefaultI2cAddress))); // 68 } catch (IOException e) { Console.WriteLine("AK8963 is exposed, try to connect directly."); _mpu9250 = new Mpu9250(I2cDevice.Create(new I2cConnectionSettings(1, Mpu6500.DefaultI2cAddress)), i2CDeviceAk8963: I2cDevice.Create(new I2cConnectionSettings(1, Ak8963.DefaultI2cAddress))); } _mpu9250.MagnetometerMeasurementMode = MeasurementMode.ContinuousMeasurement100Hz; Thread.Sleep(100); Console.WriteLine($"Finished Init sensor"); Run(); Thread.Sleep(100); Calibrate(); return(Unit.Instance); }