/// <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); }
public Task <Humiture> GetHumitureAsync() { var temperature = dht22.Temperature; var humidity = dht22.Humidity; if (dht22.IsLastReadSuccessful) { lastHumitureRead = new Humiture { Temperature = Math.Round(temperature.DegreesCelsius, 2), Humidity = Math.Round(humidity.Percent, 2) }; lastHumitureRead.HeatIndex = Math.Round(WeatherHelper.CalculateHeatIndex(temperature, humidity).DegreesCelsius, 2); lastHumitureRead.AbsoluteHumidity = Math.Round(WeatherHelper.CalculateAbsoluteHumidity(temperature, humidity).GramsPerCubicMeter, 2); } return(Task.FromResult(lastHumitureRead)); }
public void AbsoluteHumidityIsCalculatedCorrectly(double expected, double fahrenheit, double relativeHumidity) { var absoluteHumidity = WeatherHelper.CalculateAbsoluteHumidity(Temperature.FromDegreesFahrenheit(fahrenheit), Ratio.FromPercent(relativeHumidity)); Assert.Equal(expected, absoluteHumidity.GramsPerCubicMeter, 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"); }
public void AbsoluteHumidityIsCalculatedCorrectly(double expected, double fahrenheit, double relativeHumidity) { var absoluteHumidity = WeatherHelper.CalculateAbsoluteHumidity(Temperature.FromFahrenheit(fahrenheit), relativeHumidity); Assert.Equal(expected, Math.Round(absoluteHumidity, 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); }