示例#1
0
文件: WeatherTests.cs 项目: gLes/iot
        public void CalculateBarometricPressureWithHumidity(double measuredValue, double temperature, double altitude, double relativeHumidity,
                                                            double expected)
        {
            var result = WeatherHelper.CalculateBarometricPressure(Pressure.FromHectopascal(measuredValue),
                                                                   Temperature.FromCelsius(temperature), altitude, relativeHumidity);

            Assert.Equal(expected, result.Hectopascal, 2);
        }
示例#2
0
文件: WeatherTests.cs 项目: z-eh/iot
        public void CalculateBarometricPressureWithHumidity(double measuredValue, double temperature, double altitude, double relativeHumidity,
                                                            double expected)
        {
            var result = WeatherHelper.CalculateBarometricPressure(Pressure.FromHectopascals(measuredValue),
                                                                   Temperature.FromDegreesCelsius(temperature), Length.FromMeters(altitude), Ratio.FromPercent(relativeHumidity));

            Assert.Equal(expected, result.Hectopascals, 2);
        }
示例#3
0
        [InlineData(950, 15, 546.89, 1012.9)] // result is changed to 1012.9 from 1013.23
        public void CalculateBarometricPressure(double measuredValue, double temperature, double altitude,
                                                double expected)
        {
            Pressure result = WeatherHelper.CalculateBarometricPressure(Pressure.FromHectopascals(measuredValue),
                                                                        Temperature.FromDegreesCelsius(temperature), Length.FromMeters(altitude));

            Assert.Equal(expected, result.Hectopascals, 2);
        }
示例#4
0
        /// <inheritdoc/>
        public override ISensorSample TakeReading(WeatherObservation observation)
        {
            this.LogTakeReadingStart();
            var sensorResult = new SensorSample(this.SensorName);

            var result = this.sensor.Read();

            if (result.Humidity.HasValue && result.Pressure.HasValue && result.Temperature.HasValue)
            {
                var pressure           = result.Pressure.Value;
                var temperature        = new Temperature(result.Temperature.Value.DegreesCelsius + this.calibrationOffset, UnitsNet.Units.TemperatureUnit.DegreeCelsius);
                var rawTemperature     = result.Temperature.Value;
                var humidity           = result.Humidity.Value;
                var actualAltitude     = new Length(this.altitudeInMeters, UnitsNet.Units.LengthUnit.Meter);
                var calculatedAltitude = WeatherHelper.CalculateAltitude(pressure, WeatherHelper.MeanSeaLevel, rawTemperature);

                double absHumidity             = WeatherHelper.CalculateAbsoluteHumidity(temperature, humidity).GramsPerCubicMeter;
                double dewPoint                = WeatherHelper.CalculateDewPoint(temperature, humidity).DegreesCelsius;
                double heatIndex               = WeatherHelper.CalculateHeatIndex(temperature, humidity).DegreesCelsius;
                double vapourPressure          = WeatherHelper.CalculateActualVaporPressure(temperature, humidity).Hectopascals;
                double barometricPressure      = WeatherHelper.CalculateBarometricPressure(pressure, temperature, actualAltitude, humidity).Hectopascals;
                double vapourPressureOverIce   = WeatherHelper.CalculateSaturatedVaporPressureOverIce(temperature).Hectopascals;
                double vapourPressureOverWater = WeatherHelper.CalculateSaturatedVaporPressureOverWater(temperature).Hectopascals;
                double seaLevelPressure        = WeatherHelper.CalculateSeaLevelPressure(pressure, actualAltitude, temperature).Hectopascals;

                sensorResult.AddFinalObservation(temperature.DegreesCelsius, "TEMPERATURE", ObservationUnits.DegreesCelcius);
                sensorResult.AddFinalObservation(pressure.Hectopascals, "PRESSURE", ObservationUnits.HectoPascal);
                sensorResult.AddFinalObservation(barometricPressure, "BAROMETRIC PRESSURE", ObservationUnits.HectoPascal);
                sensorResult.AddFinalObservation(seaLevelPressure, "SEA LEVEL PRESSURE", ObservationUnits.HectoPascal);
                sensorResult.AddFinalObservation(vapourPressureOverIce, "OVER ICE PRESSURE", ObservationUnits.HectoPascal);
                sensorResult.AddFinalObservation(vapourPressureOverWater, "OVER WATER PRESSURE", ObservationUnits.HectoPascal);
                sensorResult.AddFinalObservation(humidity.Percent, "RELATIVE HUMIDITY", ObservationUnits.Percentage);
                sensorResult.AddFinalObservation(absHumidity, "ABSOLUTE HUMIDITY", ObservationUnits.GramsPerCubicMeter);
                sensorResult.AddFinalObservation(vapourPressure, "VAPOUR PRESSURE", ObservationUnits.HectoPascal);
                sensorResult.AddFinalObservation(calculatedAltitude.Meters, "CALCULATED ALTITUDE", ObservationUnits.Meters);
                sensorResult.AddFinalObservation(actualAltitude.Meters, "ACTUAL ALTITUDE", ObservationUnits.Meters);
                sensorResult.AddFinalObservation(heatIndex, "HEAT INDEX", ObservationUnits.DegreesCelcius);
                sensorResult.AddFinalObservation(dewPoint, "DEW POINT", ObservationUnits.DegreesCelcius);

                observation.Temperature1       = temperature.DegreesCelsius;
                observation.Pressure           = pressure.Hectopascals;
                observation.BarometricPressure = barometricPressure;
                observation.SealevelPressure   = seaLevelPressure;
                observation.OverIcePressure    = vapourPressureOverIce;
                observation.OverWaterPressure  = vapourPressureOverWater;
                observation.RelativeHumidity   = humidity.Percent;
                observation.AbsoluteHumidity   = absHumidity;
                observation.ActualAltitude     = actualAltitude.Meters;
                observation.CalculatedAltitude = calculatedAltitude.Meters;
                observation.HeatIndex          = heatIndex;
                observation.DewPoint           = dewPoint;
            }

            this.LogTakeReadingComplete();
            return(sensorResult);
        }
示例#5
0
        public static void DisplayModes(ArduinoBoard board)
        {
            const int Gpio2           = 2;
            const int MaxMode         = 10;
            Length    stationAltitude = Length.FromMeters(650);
            int       mode            = 0;
            var       gpioController  = board.CreateGpioController();

            gpioController.OpenPin(Gpio2);
            gpioController.SetPinMode(Gpio2, PinMode.Input);
            CharacterDisplay disp = new CharacterDisplay(board);

            Console.WriteLine("Display output test");
            Console.WriteLine("The button on GPIO 2 changes modes");
            Console.WriteLine("Press x to exit");
            disp.Output.ScrollUpDelay = TimeSpan.FromMilliseconds(500);
            AutoResetEvent buttonClicked = new AutoResetEvent(false);

            void ChangeMode(object sender, PinValueChangedEventArgs pinValueChangedEventArgs)
            {
                mode++;
                if (mode > MaxMode)
                {
                    // Don't change back to 0
                    mode = 1;
                }

                buttonClicked.Set();
            }

            gpioController.RegisterCallbackForPinValueChangedEvent(Gpio2, PinEventTypes.Falling, ChangeMode);
            var    device = board.CreateI2cDevice(new I2cConnectionSettings(0, Bmp280.DefaultI2cAddress));
            Bmp280?bmp;

            try
            {
                bmp             = new Bmp280(device);
                bmp.StandbyTime = StandbyTime.Ms250;
                bmp.SetPowerMode(Bmx280PowerMode.Normal);
            }
            catch (IOException)
            {
                bmp = null;
                Console.WriteLine("BMP280 not available");
            }

            OpenHardwareMonitor hardwareMonitor = new OpenHardwareMonitor();

            hardwareMonitor.EnableDerivedSensors();
            TimeSpan sleeptime        = TimeSpan.FromMilliseconds(500);
            string   modeName         = string.Empty;
            string   previousModeName = string.Empty;
            int      firstCharInText  = 0;

            while (true)
            {
                if (Console.KeyAvailable && Console.ReadKey(true).KeyChar == 'x')
                {
                    break;
                }

                // Default
                sleeptime = TimeSpan.FromMilliseconds(500);

                switch (mode)
                {
                case 0:
                    modeName = "Display ready";
                    disp.Output.ReplaceLine(1, "Button for mode");
                    // Just text
                    break;

                case 1:
                {
                    modeName = "Time";
                    disp.Output.ReplaceLine(1, DateTime.Now.ToLongTimeString());
                    sleeptime = TimeSpan.FromMilliseconds(200);
                    break;
                }

                case 2:
                {
                    modeName = "Date";
                    disp.Output.ReplaceLine(1, DateTime.Now.ToShortDateString());
                    break;
                }

                case 3:
                    modeName = "Temperature / Barometric Pressure";
                    if (bmp != null && bmp.TryReadTemperature(out Temperature temp) && bmp.TryReadPressure(out Pressure p2))
                    {
                        Pressure p3 = WeatherHelper.CalculateBarometricPressure(p2, temp, stationAltitude);
                        disp.Output.ReplaceLine(1, string.Format(CultureInfo.CurrentCulture, "{0:s1} {1:s1}", temp, p3));
                    }
示例#6
0
        /// <inheritdoc/>
        public override void Initialize()
        {
            this.LogStartInitialization();
            this.i2cDevice = this.Controller.GetI2CConnection(this.DeviceId);
            this.sensor    = new Bme280(this.i2cDevice)
            {
                TemperatureSampling = Sampling.LowPower,
                PressureSampling    = Sampling.UltraHighResolution,
                HumiditySampling    = Sampling.Standard,
            };

            var readResult = this.sensor.ReadStatus();

            this.LogMessage($"BME280 status {readResult}");
            var result = this.sensor.Read();

            if (result.Humidity.HasValue && result.Pressure.HasValue && result.Temperature.HasValue)
            {
                var pressure    = result.Pressure.Value;
                var temperature = new Temperature(result.Temperature.Value.DegreesCelsius - this.calibrationOffset, UnitsNet.Units.TemperatureUnit.DegreeCelsius);
                var humidity    = result.Humidity.Value;

                var actualAltitude     = new Length(this.altitudeInMeters, UnitsNet.Units.LengthUnit.Meter);
                var calculatedAltitude = WeatherHelper.CalculateAltitude(pressure, WeatherHelper.MeanSeaLevel, temperature);

                double absHumidity             = WeatherHelper.CalculateAbsoluteHumidity(temperature, humidity).GramsPerCubicMeter;
                double dewPoint                = WeatherHelper.CalculateDewPoint(temperature, humidity).DegreesCelsius;
                double heatIndex               = WeatherHelper.CalculateHeatIndex(temperature, humidity).DegreesCelsius;
                double vapourPressure          = WeatherHelper.CalculateActualVaporPressure(temperature, humidity).Hectopascals;
                double barometricPressure      = WeatherHelper.CalculateBarometricPressure(pressure, temperature, actualAltitude, humidity).Hectopascals;
                double vapourPressureOverIce   = WeatherHelper.CalculateSaturatedVaporPressureOverIce(temperature).Hectopascals;
                double vapourPressureOverWater = WeatherHelper.CalculateSaturatedVaporPressureOverWater(temperature).Hectopascals;
                double seaLevelPressure        = WeatherHelper.CalculateSeaLevelPressure(pressure, actualAltitude, temperature).Hectopascals;

                this.LogMessage($"Temperature: {temperature.DegreesCelsius:0.#}\u00B0C");
                this.LogMessage($"Pressure: {pressure.Hectopascals:0.##}hPa");
                this.LogMessage($"Barometric Pressure: {barometricPressure:0.##}hPa");
                this.LogMessage($"Sea level Pressure: {seaLevelPressure:0.##}hPa");
                this.LogMessage($"Over ice Pressure: {vapourPressureOverIce:0.##}hPa");
                this.LogMessage($"Over water Pressure: {vapourPressureOverWater:0.##}hPa");
                this.LogMessage($"Relative humidity: {humidity.Percent:0.#}%");
                this.LogMessage($"Absolute humidity: {absHumidity:0.#}g/m3");
                this.LogMessage($"Vapour pressure: {vapourPressure:0.#}hPa");
                this.LogMessage($"Calculate altitude: {calculatedAltitude.Meters:0.##}m");
                this.LogMessage($"Actual altitude: {actualAltitude.Meters:0.##}m");
                this.LogMessage($"Heat index: {heatIndex:0.#}\u00B0C");
                this.LogMessage($"Dew point: {dewPoint:0.#}\u00B0C");

                Thread.Sleep(1000);

                // set sane defaults
                this.sensor.TemperatureSampling = Sampling.UltraLowPower;
                this.sensor.PressureSampling    = Sampling.UltraLowPower;
                this.sensor.HumiditySampling    = Sampling.UltraLowPower;
                this.sensor.FilterMode          = Bmx280FilteringMode.Off;
                this.sensor.SetPowerMode(Iot.Device.Bmxx80.PowerMode.Bmx280PowerMode.Forced);

                this.IsInitialized = true;
                this.LogStartSuccess();
            }

            this.LogError("Couldn't read from BME280");
        }
示例#7
0
        /// <summary>
        /// Entry point for example program
        /// </summary>
        /// <param name="args">Command line arguments</param>
        public static void StartBmp(string[] args)
        {
            Console.WriteLine("Hello Bmp280!");

            Length stationHeight = Length.FromMeters(640); // Elevation of the sensor

            // bus id on the raspberry pi 3 and 4
            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, Bmp280.DefaultI2cAddress);
            var i2cDevice   = I2cDevice.Create(i2cSettings);
            var i2CBmp280   = new Bmp280(i2cDevice);

            using (i2CBmp280)
            {
                while (true)
                {
                    // set higher sampling
                    i2CBmp280.TemperatureSampling = Sampling.LowPower;
                    i2CBmp280.PressureSampling    = Sampling.UltraHighResolution;

                    // set mode forced so device sleeps after read
                    i2CBmp280.SetPowerMode(Bmx280PowerMode.Forced);

                    // wait for measurement to be performed
                    var measurementTime = i2CBmp280.GetMeasurementDuration();
                    Thread.Sleep(measurementTime);

                    // read values
                    i2CBmp280.TryReadTemperature(out var tempValue);
                    Console.WriteLine($"Temperature: {tempValue.DegreesCelsius} \u00B0C");
                    i2CBmp280.TryReadPressure(out var preValue);
                    Console.WriteLine($"Pressure: {preValue.Hectopascals} hPa");

                    // Note that if you already have the pressure value and the temperature, you could also calculate altitude by using
                    // double altValue = WeatherHelper.CalculateAltitude(preValue, defaultSeaLevelPressure, tempValue) which would be more performant.
                    i2CBmp280.TryReadAltitude(out var altValue);

                    Console.WriteLine($"Calculated Altitude: {altValue} m");
                    Thread.Sleep(1000);

                    // change sampling rate
                    i2CBmp280.TemperatureSampling = Sampling.UltraHighResolution;
                    i2CBmp280.PressureSampling    = Sampling.UltraLowPower;
                    i2CBmp280.FilterMode          = Bmx280FilteringMode.X4;

                    // set mode forced and read again
                    i2CBmp280.SetPowerMode(Bmx280PowerMode.Forced);

                    // wait for measurement to be performed
                    measurementTime = i2CBmp280.GetMeasurementDuration();
                    Thread.Sleep(measurementTime);

                    // read values
                    i2CBmp280.TryReadTemperature(out tempValue);
                    Console.WriteLine($"Temperature: {tempValue.DegreesCelsius} \u00B0C");
                    i2CBmp280.TryReadPressure(out preValue);
                    Console.WriteLine($"Pressure: {preValue.Hectopascals} hPa");

                    // This time use altitude calculation
                    altValue = WeatherHelper.CalculateAltitude(preValue, defaultSeaLevelPressure, tempValue);

                    Console.WriteLine($"Calculated Altitude: {altValue} m");

                    // Calculate the barometric (corrected) pressure for the local position.
                    // Change the stationHeight value above to get a correct reading, but do not be tempted to insert
                    // the value obtained from the formula above. Since that estimates the altitude based on pressure,
                    // using that altitude to correct the pressure won't work.
                    var correctedPressure = WeatherHelper.CalculateBarometricPressure(preValue, tempValue, stationHeight);

                    Console.WriteLine($"Pressure corrected for altitude {stationHeight.Meters} m (with average humidity): {correctedPressure.Hectopascals} hPa");

                    Thread.Sleep(5000);
                }
            }
        }
示例#8
0
        /// <inheritdoc/>
        public override ISensorSample TakeReading(WeatherObservation observation)
        {
            this.LogTakeReadingStart();
            var sensorResult = new SensorSample(this.SensorName);

            // Initialize for reading
            this.Device.Write(new byte[] { REGCONTROLHUM, OVERSAMPLEHUM });
            var control = GetControlCode();

            this.Device.Write(new byte[] { REGCONTROL, (byte)control });

            // Read calibaration data
            var cal1 = this.ReadBytes(0x88, 24);
            var cal2 = this.ReadBytes(0xA1, 1);
            var cal3 = this.ReadBytes(0xE1, 7);

            sensorResult.AddDiagnostic("Calibration data (1/2/3)");
            sensorResult.AddDiagnostic(ByteOperations.PrintByteArray(cal1));
            sensorResult.AddDiagnostic(ByteOperations.PrintByteArray(cal2));
            sensorResult.AddDiagnostic(ByteOperations.PrintByteArray(cal3));

            var calibrationData = ExtractcalibrationData(cal1, cal2, cal3);

            // Pause per spec
            var wait_time = GetWaitTime();

            Thread.Sleep((int)wait_time / 1000);

            // Read raw data
            var data = this.ReadBytes(REGDATA, 8);

            sensorResult.AddDiagnostic("Raw data");
            sensorResult.AddDiagnostic(ByteOperations.PrintByteArray(data));

            var    rawTemp        = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4);
            double rawTemperature = CalculateTemperature(sensorResult, calibrationData, rawTemp, out double tFine);

            // Refine pressure and adjust for temperature
            var    rawPressure   = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4);
            double pressureValue = CalculatePressure(sensorResult, calibrationData, rawPressure, tFine);

            // Refine humidity
            var    rawHumidity   = (data[6] << 8) | data[7];
            double humidityValue = CalculateHumidity(sensorResult, calibrationData, rawHumidity, tFine);

            sensorResult.AddFinalObservation(rawTemperature + this.calibrationOffset, "TEMPERATURE", ObservationUnits.DegreesCelcius);
            sensorResult.AddFinalObservation(pressureValue, "PRESSURE", ObservationUnits.HectoPascal);
            sensorResult.AddFinalObservation(humidityValue, "HUMIDITY", ObservationUnits.Percentage);

            var pressure    = new Pressure(pressureValue, UnitsNet.Units.PressureUnit.Hectopascal);
            var humidity    = new RelativeHumidity(humidityValue, UnitsNet.Units.RelativeHumidityUnit.Percent);
            var temperature = new Temperature(rawTemperature, UnitsNet.Units.TemperatureUnit.DegreeCelsius);

            var actualAltitude     = new Length(this.altitudeInMeters, UnitsNet.Units.LengthUnit.Meter);
            var altitudeCalculated = WeatherHelper.CalculateAltitude(pressure);

            double absHumidity             = WeatherHelper.CalculateAbsoluteHumidity(temperature, humidity).GramsPerCubicMeter;
            double dewPoint                = WeatherHelper.CalculateDewPoint(temperature, humidity).DegreesCelsius;
            double heatIndex               = WeatherHelper.CalculateHeatIndex(temperature, humidity).DegreesCelsius;
            double vapourPressure          = WeatherHelper.CalculateActualVaporPressure(temperature, humidity).Hectopascals;
            double barometricPressure      = WeatherHelper.CalculateBarometricPressure(pressure, temperature, actualAltitude, humidity).Hectopascals;
            double vapourPressureOverIce   = WeatherHelper.CalculateSaturatedVaporPressureOverIce(temperature).Hectopascals;
            double vapourPressureOverWater = WeatherHelper.CalculateSaturatedVaporPressureOverWater(temperature).Hectopascals;
            double seaLevelPressure        = WeatherHelper.CalculateSeaLevelPressure(pressure, actualAltitude, temperature).Hectopascals;

            sensorResult.AddFinalObservation(temperature.DegreesCelsius + this.calibrationOffset, "TEMPERATURE", ObservationUnits.DegreesCelcius);
            sensorResult.AddFinalObservation(pressure.Hectopascals, "PRESSURE", ObservationUnits.HectoPascal);
            sensorResult.AddFinalObservation(barometricPressure, "BAROMETRIC PRESSURE", ObservationUnits.HectoPascal);
            sensorResult.AddFinalObservation(seaLevelPressure, "SEA LEVEL PRESSURE", ObservationUnits.HectoPascal);
            sensorResult.AddFinalObservation(vapourPressureOverIce, "OVER ICE PRESSURE", ObservationUnits.HectoPascal);
            sensorResult.AddFinalObservation(vapourPressureOverWater, "OVER WATER PRESSURE", ObservationUnits.HectoPascal);
            sensorResult.AddFinalObservation(humidity.Percent, "RELATIVE HUMIDITY", ObservationUnits.Percentage);
            sensorResult.AddFinalObservation(absHumidity, "ABSOLUTE HUMIDITY", ObservationUnits.GramsPerCubicMeter);
            sensorResult.AddFinalObservation(vapourPressure, "VAPOUR PRESSURE", ObservationUnits.Percentage);
            sensorResult.AddFinalObservation(altitudeCalculated.Meters, "CALCULATED ALTITUDE", ObservationUnits.Meters);
            sensorResult.AddFinalObservation(actualAltitude.Meters, "ACTUAL ALTITUDE", ObservationUnits.Meters);
            sensorResult.AddFinalObservation(heatIndex, "HEAT INDEX", ObservationUnits.DegreesCelcius);
            sensorResult.AddFinalObservation(dewPoint, "DEW POINT", ObservationUnits.DegreesCelcius);

            observation.Temperature1       = temperature.DegreesCelsius + this.calibrationOffset;
            observation.Pressure           = pressure.Hectopascals;
            observation.BarometricPressure = barometricPressure;
            observation.SealevelPressure   = seaLevelPressure;
            observation.OverIcePressure    = vapourPressureOverIce;
            observation.OverWaterPressure  = vapourPressureOverWater;
            observation.RelativeHumidity   = humidity.Percent;
            observation.AbsoluteHumidity   = absHumidity;
            observation.ActualAltitude     = actualAltitude.Meters;
            observation.CalculatedAltitude = altitudeCalculated.Meters;
            observation.HeatIndex          = heatIndex;
            observation.DewPoint           = dewPoint;

            this.LogTakeReadingComplete();

            return(sensorResult);
        }