private static void Dht(DhtBase dht) { while (!Console.KeyAvailable) { var temp = dht.Temperature; var hum = dht.Humidity; // You can only display temperature and humidity if the read is successful otherwise, this will raise an exception as // both temperature and humidity are NAN if (dht.IsLastReadSuccessful) { Console.WriteLine($"Temperature: {temp.DegreesCelsius}\u00B0C, Relative humidity: {hum.Percent}%"); // WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity. Console.WriteLine( $"Heat index: {WeatherHelper.CalculateHeatIndex(temp, hum).DegreesCelsius:0.#}\u00B0C"); Console.WriteLine( $"Dew point: {WeatherHelper.CalculateDewPoint(temp, hum).DegreesCelsius:0.#}\u00B0C"); } else { Console.WriteLine("Error reading DHT sensor"); } // You must wait some time before trying to read the next value Thread.Sleep(2000); } }
public static void Main(string[] args) { Console.WriteLine("Hello DHT!"); // Init DHT10 through I2C I2cConnectionSettings settings = new I2cConnectionSettings(1, Dht10.DefaultI2cAddress); I2cDevice device = I2cDevice.Create(settings); using (Dht10 dht = new Dht10(device)) { while (true) { var tempValue = dht.Temperature; var humValue = dht.Humidity; Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); 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(2000); } } }
private static void ConsoleWriteInfo(Temperature temperature, Ratio relativeHumidity) { Console.WriteLine($"Temperature: {temperature.DegreesCelsius:0.#}\u00B0C"); Console.WriteLine($"Humidity: {relativeHumidity.Percent:0.#}%"); // WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity. Console.WriteLine($"Heat index: {WeatherHelper.CalculateHeatIndex(temperature, relativeHumidity).DegreesCelsius:0.#}\u00B0C"); Console.WriteLine($"Dew point: {WeatherHelper.CalculateDewPoint(temperature, relativeHumidity).DegreesCelsius:0.#}\u00B0C"); }
/// <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 static void Run() { using SenseHatTemperatureAndHumidity th = new (); while (true) { var tempValue = th.Temperature; var humValue = th.Humidity; Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); 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); } }
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 static void Main(string[] args) { using (var th = new Hts221(CreateI2cDevice())) { while (true) { var tempValue = th.Temperature; var humValue = th.Humidity; Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); 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); } } }
private void PiStatusCheck(object sender, ElapsedEventArgs args) { try { using (var sensor = GetSensor()) { var temp = sensor.Temperature; var hum = sensor.Humidity; if (sensor.IsLastReadSuccessful && CheckDelta(temp, hum)) { Status.Temp = temp.DegreesFahrenheit; Status.Humidity = hum.Percent; Status.HeatIndex = WeatherHelper.CalculateHeatIndex(temp, hum).DegreesFahrenheit; Status.DewPoint = WeatherHelper.CalculateDewPoint(temp, hum).DegreesFahrenheit; Status.ErrorsSinceLastUpdate = 0; Status.TimeSinceLastUpdate = 0; _logger?.LogInformation( $"Temperature: {temp.DegreesFahrenheit}\u00B0F, Relative humidity: {hum.Percent}%," + $" Heat index: {Status.HeatIndex:0.#}\u00B0F, Dew point: {Status.DewPoint:0.#}\u00B0F"); } else { Status.ErrorsSinceLastUpdate++; Status.TimeSinceLastUpdate = Status.ErrorsSinceLastUpdate * 2; if (Status.ErrorsSinceLastUpdate % 30 == 0) { _logger.LogError($"{Status.ErrorsSinceLastUpdate} Errors have occured"); } } } } catch (Exception e) { _logger?.LogError(e, "Exception Occurred While Reading Sensor"); _lifetime?.StopApplication(); } }
public static void Main(string[] args) { I2cConnectionSettings settings = new I2cConnectionSettings(1, Si7021.DefaultI2cAddress); I2cDevice device = I2cDevice.Create(settings); using (Si7021 sensor = new Si7021(device, Resolution.Resolution1)) { while (true) { var tempValue = sensor.Temperature; var humValue = sensor.Humidity; Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); Console.WriteLine($"Relative humidity: {humValue.Percent: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"); Console.WriteLine(); Thread.Sleep(1000); } } }
/// <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); } } }
/// <summary> /// Main entry point for the program. /// </summary> public static void Main() { Console.WriteLine("Hello BME680!"); // The I2C 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, Bme680.DefaultI2cAddress); var i2cDevice = I2cDevice.Create(i2cSettings); using (var bme680 = new Bme680(i2cDevice)) { while (true) { // get the time a measurement will take with the current settings var measurementDuration = bme680.GetMeasurementDuration(bme680.HeaterProfile); // 10 consecutive measurement with default settings for (var i = 0; i < 10; i++) { // This instructs the sensor to take a measurement. bme680.SetPowerMode(Bme680PowerMode.Forced); // wait while measurement is being taken Thread.Sleep(measurementDuration); // Print out the measured data bme680.TryReadTemperature(out var tempValue); bme680.TryReadPressure(out var preValue); bme680.TryReadHumidity(out var humValue); bme680.TryReadGasResistance(out var gasResistance); var altValue = WeatherHelper.CalculateAltitude(preValue, defaultSeaLevelPressure, tempValue); Console.WriteLine($"Gas resistance: {gasResistance:0.##}Ohm"); 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"); // when measuring the gas resistance on each cycle it is important to wait a certain interval // because a heating plate is activated which will heat up the sensor without sleep, this can // falsify all readings coming from the sensor Thread.Sleep(1000); } // change the settings bme680.TemperatureSampling = Sampling.HighResolution; bme680.HumiditySampling = Sampling.UltraHighResolution; bme680.PressureSampling = Sampling.Skipped; bme680.ConfigureHeatingProfile(Bme680HeaterProfile.Profile2, 280, 80, 24); bme680.HeaterProfile = Bme680HeaterProfile.Profile2; measurementDuration = bme680.GetMeasurementDuration(bme680.HeaterProfile); // 10 consecutive measurements with custom settings for (int i = 0; i < 10; i++) { // perform the measurement bme680.SetPowerMode(Bme680PowerMode.Forced); Thread.Sleep(measurementDuration); // Print out the measured data bme680.TryReadTemperature(out var tempValue); bme680.TryReadPressure(out var preValue); bme680.TryReadHumidity(out var humValue); bme680.TryReadGasResistance(out var gasResistance); var altValue = WeatherHelper.CalculateAltitude(preValue, defaultSeaLevelPressure, tempValue); Console.WriteLine($"Gas resistance: {gasResistance:0.##}Ohm"); 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); } // reset will change settings back to default bme680.Reset(); } } }
public void HeatIndexIsCalculatedCorrectly(double expected, double celsius, double relativeHumidity) { var heatIndex = WeatherHelper.CalculateHeatIndex(Temperature.FromDegreesCelsius(celsius), Ratio.FromPercent(relativeHumidity)); Assert.Equal(expected, Math.Round(heatIndex.DegreesCelsius)); }
/// <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"); }
/// <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); }
public static void Main(string[] args) { // Sample for each device separately // LedMatrix.Run(); // Joystick.Run(); // AccelerometerAndGyroscope.Run(); // Magnetometer.Run(); // TemperatureAndHumidity.Run(); // PressureAndTemperature.Run(); // set this to the current sea level pressure in the area for correct altitude readings var defaultSeaLevelPressure = Pressure.MeanSeaLevel; using (var sh = new SenseHat()) { int n = 0; int x = 3, y = 3; while (true) { Console.Clear(); (int dx, int dy, bool holding) = JoystickState(sh); if (holding) { n++; } x = (x + 8 + dx) % 8; y = (y + 8 + dy) % 8; sh.Fill(n % 2 == 0 ? Color.DarkBlue : Color.DarkRed); sh.SetPixel(x, y, Color.Yellow); var tempValue = sh.Temperature; var temp2Value = sh.Temperature2; var preValue = sh.Pressure; var humValue = sh.Humidity; var accValue = sh.Acceleration; var angValue = sh.AngularRate; var magValue = sh.MagneticInduction; var altValue = WeatherHelper.CalculateAltitude(preValue, defaultSeaLevelPressure, tempValue); Console.WriteLine($"Temperature Sensor 1: {tempValue.Celsius:0.#}\u00B0C"); Console.WriteLine($"Temperature Sensor 2: {temp2Value.Celsius:0.#}\u00B0C"); Console.WriteLine($"Pressure: {preValue:0.##}hPa"); Console.WriteLine($"Altitude: {altValue:0.##}m"); Console.WriteLine($"Acceleration: {sh.Acceleration}g"); Console.WriteLine($"Angular rate: {sh.AngularRate}DPS"); Console.WriteLine($"Magnetic induction: {sh.MagneticInduction}gauss"); 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).Celsius:0.#}\u00B0C"); Console.WriteLine($"Dew point: {WeatherHelper.CalculateDewPoint(tempValue, humValue).Celsius:0.#}\u00B0C"); Thread.Sleep(1000); } } }