/// <summary> /// Entry point for example program /// </summary> /// <param name="args">Command line arguments</param> public static void Main(string[] args) { Console.WriteLine("Hello Bmp280!"); // 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 = Pressure.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.Celsius} \u00B0C"); i2CBmp280.TryReadPressure(out var preValue); Console.WriteLine($"Pressure: {preValue.Hectopascal} hPa"); i2CBmp280.TryReadAltitude(defaultSeaLevelPressure, out var altValue); Console.WriteLine($"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.Celsius} \u00B0C"); i2CBmp280.TryReadPressure(out preValue); Console.WriteLine($"Pressure {preValue.Hectopascal} hPa"); i2CBmp280.TryReadAltitude(defaultSeaLevelPressure, out altValue); Console.WriteLine($"Altitude: {altValue} m"); Thread.Sleep(5000); } } }
static void Main(string[] args) { Console.WriteLine("You are modified"); using Lcd2004 lcdDisplay = new Lcd2004(22, 17, new int[] { 25, 24, 23, 18 }); using Bmp280 tempSensor = new Bmp280(I2cDevice.Create(new I2cConnectionSettings(1, 0x77))); tempSensor.SetTemperatureSampling(Sampling.UltraHighResolution); tempSensor.SetPowerMode(Bmx280PowerMode.Normal); using CancellationTokenSource cts = new CancellationTokenSource(); Task workTask = Task.Run(() => DoWorkAsync(lcdDisplay, tempSensor, cts.Token), cts.Token); Console.WriteLine("Press Enter to stop the program..."); // Run until user adds input Console.ReadLine(); cts.Cancel(); workTask.Wait(5000); lcdDisplay.Clear(); }
private static void TestI2c(ArduinoBoard board) { var device = board.CreateI2cDevice(new I2cConnectionSettings(0, Bmp280.DefaultI2cAddress)); var bmp = new Bmp280(device); bmp.StandbyTime = StandbyTime.Ms250; bmp.SetPowerMode(Bmx280PowerMode.Normal); Console.WriteLine("Device open"); while (!Console.KeyAvailable) { bmp.TryReadTemperature(out var temperature); bmp.TryReadPressure(out var pressure); Console.Write($"\rTemperature: {temperature.DegreesCelsius:F2}°C. Pressure {pressure.Hectopascals:F1} hPa "); Thread.Sleep(100); } bmp.Dispose(); device.Dispose(); Console.ReadKey(); Console.WriteLine(); }
static async Task Main(string[] args) { Console.WriteLine("Hello Bmp280!"); //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, Bmp280.DefaultI2cAddress); var i2cDevice = new UnixI2cDevice(i2cSettings); var i2CBmp280 = new Bmp280(i2cDevice); using (i2CBmp280) { while (true) { ////set mode forced so device sleeps after read i2CBmp280.SetPowerMode(PowerMode.Forced); //set samplings i2CBmp280.SetTemperatureSampling(Sampling.UltraLowPower); i2CBmp280.SetPressureSampling(Sampling.UltraLowPower); //read values Temperature tempValue = await i2CBmp280.ReadTemperatureAsync(); Console.WriteLine($"Temperature {tempValue.Celsius}"); double preValue = await i2CBmp280.ReadPressureAsync(); Console.WriteLine($"Pressure {preValue}"); double altValue = await i2CBmp280.ReadAltitudeAsync(defaultSeaLevelPressure); Console.WriteLine($"Altitude: {altValue}"); Thread.Sleep(1000); //set higher sampling i2CBmp280.SetTemperatureSampling(Sampling.LowPower); Console.WriteLine(i2CBmp280.ReadTemperatureSampling()); i2CBmp280.SetPressureSampling(Sampling.UltraHighResolution); Console.WriteLine(i2CBmp280.ReadPressureSampling()); //set mode forced and read again i2CBmp280.SetPowerMode(PowerMode.Forced); //read values tempValue = await i2CBmp280.ReadTemperatureAsync(); Console.WriteLine($"Temperature {tempValue.Celsius}"); preValue = await i2CBmp280.ReadPressureAsync(); Console.WriteLine($"Pressure {preValue}"); altValue = await i2CBmp280.ReadAltitudeAsync(defaultSeaLevelPressure); Console.WriteLine($"Altitude: {altValue}"); Thread.Sleep(5000); //set sampling to higher i2CBmp280.SetTemperatureSampling(Sampling.UltraHighResolution); Console.WriteLine(i2CBmp280.ReadTemperatureSampling()); i2CBmp280.SetPressureSampling(Sampling.UltraLowPower); Console.WriteLine(i2CBmp280.ReadPressureSampling()); } } }
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)); }
/// <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); } } }