Esempio n. 1
0
        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);
                        }
                    }
                }
            }
        }
        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()
            });
        }
Esempio n. 3
0
        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 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);
        }
Esempio n. 5
0
        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);
                }
            }
        }
Esempio n. 6
0
        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   = I2cDevice.Create(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);
                    i2CBmpe80.SetHumiditySampling(Sampling.UltraLowPower);

                    //read values
                    Temperature tempValue = await i2CBmpe80.ReadTemperatureAsync();

                    Console.WriteLine($"Temperature: {tempValue.Celsius} C");
                    double preValue = await i2CBmpe80.ReadPressureAsync();

                    Console.WriteLine($"Pressure: {preValue} Pa");
                    double altValue = await i2CBmpe80.ReadAltitudeAsync(defaultSeaLevelPressure);

                    Console.WriteLine($"Altitude: {altValue} meters");
                    double humValue = await i2CBmpe80.ReadHumidityAsync();

                    Console.WriteLine($"Humidity: {humValue} %");
                    Thread.Sleep(1000);

                    //set higher sampling
                    i2CBmpe80.SetTemperatureSampling(Sampling.LowPower);
                    Console.WriteLine(i2CBmpe80.ReadTemperatureSampling());
                    i2CBmpe80.SetPressureSampling(Sampling.UltraHighResolution);
                    Console.WriteLine(i2CBmpe80.ReadPressureSampling());
                    i2CBmpe80.SetHumiditySampling(Sampling.Standard);
                    Console.WriteLine(i2CBmpe80.ReadHumiditySampling());

                    i2CBmpe80.SetFilterMode(FilteringMode.Off);
                    Console.WriteLine(i2CBmpe80.ReadFilterMode());

                    //set mode forced and read again
                    i2CBmpe80.SetPowerMode(PowerMode.Forced);

                    //read values
                    tempValue = await i2CBmpe80.ReadTemperatureAsync();

                    Console.WriteLine($"Temperature: {tempValue.Celsius} C");
                    preValue = await i2CBmpe80.ReadPressureAsync();

                    Console.WriteLine($"Pressure: {preValue} Pa");
                    altValue = await i2CBmpe80.ReadAltitudeAsync(defaultSeaLevelPressure);

                    Console.WriteLine($"Altitude: {altValue} meters");
                    humValue = await i2CBmpe80.ReadHumidityAsync();

                    Console.WriteLine($"Humidity: {humValue} %");
                    Thread.Sleep(5000);

                    //set sampling to higher
                    i2CBmpe80.SetTemperatureSampling(Sampling.UltraHighResolution);
                    Console.WriteLine(i2CBmpe80.ReadTemperatureSampling());
                    i2CBmpe80.SetPressureSampling(Sampling.UltraLowPower);
                    Console.WriteLine(i2CBmpe80.ReadPressureSampling());
                    i2CBmpe80.SetHumiditySampling(Sampling.UltraLowPower);
                    Console.WriteLine(i2CBmpe80.ReadHumiditySampling());

                    i2CBmpe80.SetFilterMode(FilteringMode.X2);
                    Console.WriteLine(i2CBmpe80.ReadFilterMode());
                }
            }
        }