예제 #1
0
        protected override async Task RunContinuouslyAsync(IMqttClient client, TimeSpan delay, CancellationToken stoppingToken)
        {
            var bme280   = new BME280("/dev/i2c-1", 0x76);
            var settings = new bme280_settings
            {
                osr_h  = BME280_OVERSAMPLING_1X,
                osr_p  = BME280_OVERSAMPLING_1X,
                osr_t  = BME280_OVERSAMPLING_1X,
                filter = BME280_FILTER_COEFF_OFF
            };

            bme280.SetSensorSettings(ref settings, BME280_OSR_HUM_SEL | BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL);

            var oldData        = new bme280_data();
            var nextReportTime = DateTimeOffset.MinValue;

            while (!stoppingToken.IsCancellationRequested)
            {
                bme280.SetSensorMode(BME280_FORCED_MODE);
                await Task.Delay(100, stoppingToken).ConfigureAwait(false);

                var data = bme280.GetSensorData(BME280_ALL);
                var now  = DateTimeOffset.UtcNow;
                if ((now > nextReportTime) || ShouldReport(ref oldData, ref data))
                {
                    await SendReading(client, data, stoppingToken);

                    oldData        = data;
                    nextReportTime = now.AddSeconds(60);
                }
                await Task.Delay(delay, stoppingToken);
            }
        }
예제 #2
0
        public static async Task Main()
        {
            Console.WriteLine("Bme280 test");
            try
            {
                CancellationToken ct = default;

                var bme280   = new BME280("/dev/i2c-1", 0x76);
                var settings = new bme280_settings
                {
                    osr_h  = BME280_OVERSAMPLING_1X,
                    osr_p  = BME280_OVERSAMPLING_1X,
                    osr_t  = BME280_OVERSAMPLING_1X,
                    filter = BME280_FILTER_COEFF_OFF,
                };

                bme280.SetSensorSettings(ref settings, BME280_OSR_HUM_SEL | BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL);

                while (!ct.IsCancellationRequested)
                {
                    bme280.SetSensorMode(BME280_FORCED_MODE);
                    await Task.Delay(100, ct).ConfigureAwait(false);

                    var data = bme280.GetSensorData(BME280_ALL);
                    Console.WriteLine(FormattableString.Invariant($"T: {data.temperature / 100.0} degC, P: {data.pressure / 100.0} Pascal, H: {data.humidity / 1024.0} %rH"));
                    await Task.Delay(1000, ct).ConfigureAwait(false);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                Console.WriteLine(FormattableString.Invariant($"Error: {ex}"));
            }
        }