public async Task <IHttpActionResult> PostBmp180(Bmp180 bmp180) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Bmp180.Add(bmp180); try { await db.SaveChangesAsync(); } catch (DbUpdateException ex) { if (Bmp180Exists(bmp180.Id)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = bmp180.Id }, bmp180)); }
public async Task <IHttpActionResult> PutBmp180(Guid id, Bmp180 bmp180) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != bmp180.Id) { return(BadRequest()); } db.Entry(bmp180).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!Bmp180Exists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
void Initialize() { Console.WriteLine("Initialize hardware..."); onboardLed = new RgbPwmLed(device: Device, redPwmPin: Device.Pins.OnboardLedRed, greenPwmPin: Device.Pins.OnboardLedGreen, bluePwmPin: Device.Pins.OnboardLedBlue, 3.3f, 3.3f, 3.3f, Meadow.Peripherals.Leds.IRgbLed.CommonType.CommonAnode); var config = new SpiClockConfiguration(3000, SpiClockConfiguration.Mode.Mode3); var spiBus = Device.CreateSpiBus(Device.Pins.SCK, Device.Pins.MOSI, Device.Pins.MISO, config); button = Device.CreateDigitalInputPort(Device.Pins.D12, InterruptMode.EdgeRising, ResistorMode.Disabled); button.Changed += Button_Changed; //display display = new St7789( device: Device, spiBus: spiBus, chipSelectPin: Device.Pins.D02, dcPin: Device.Pins.D01, resetPin: Device.Pins.D00, width: 240, height: 240); graphics = new GraphicsLibrary(display); graphics.CurrentFont = new Font12x20(); sensor = new Bmp180(Device.CreateI2cBus()); sensor.Updated += Sensor_Updated; sensor.StartUpdating(); }
static void Main(string[] args) { const int busId = 1; I2cConnectionSettings i2cSettings = new(busId, Bmp180.DefaultI2cAddress); I2cDevice i2cDevice = I2cDevice.Create(i2cSettings); using (var i2CBmp180 = new Bmp180(i2cDevice)) { var temperature = i2CBmp180.ReadTemperature().DegreesCelsius; var pressure = i2CBmp180.ReadPressure().Hectopascals; var readings = new List <JsonReading>() { new JsonReading() { kind = "temperature", value = temperature, unit = "celsius", accessory_type = "Temperature" }, new JsonReading() { kind = "pressure", value = pressure, unit = "hpa", accessory_type = "Pressure" } }; Console.WriteLine(JsonConvert.SerializeObject(readings)); } }
public async Task <IHttpActionResult> GetBmp180(string id) { Bmp180 bmp180 = await db.Bmp180.FindAsync(id); if (bmp180 == null) { return(NotFound()); } return(Ok(bmp180)); }
public override bool Configure(string jsonDeviceConfiguration) { var config = DeserializeDeviceConfig <Bmp180Configuration>(jsonDeviceConfiguration); var i2CSettings = new I2cConnectionSettings(1, config.I2CAddress); var i2CDevice = I2cDevice.Create(i2CSettings); // TODO: probably requires try catch?! Check device availability _bmp180 = new Bmp180(i2CDevice); _bmp180.SetSampling(config.Sampling); return(true); }
/// <summary> /// Entry point for example program /// </summary> /// <param name="args">Command line arguments</param> public static void Run() { Console.WriteLine("Using BMP180!"); try { // bus id on the raspberry pi 3 const int busId = 1; var i2cSettings = new I2cConnectionSettings(busId, Bmp180.DefaultI2cAddress); var i2cDevice = I2cDevice.Create(i2cSettings); var i2cBmp280 = new Bmp180(i2cDevice); if (i2cBmp280 != null) { using (i2cBmp280) { // set samplings i2cBmp280.SetSampling(Sampling.Standard); // read values Temperature tempValue = i2cBmp280.ReadTemperature(); Console.WriteLine($"Temperature {tempValue.Celsius} \u00B0C"); var preValue = i2cBmp280.ReadPressure(); Console.WriteLine($"Pressure {preValue.Hectopascal} hPa"); double altValue = i2cBmp280.ReadAltitude(); Console.WriteLine($"Altitude {altValue:0.##} m"); Thread.Sleep(1000); // set higher sampling i2cBmp280.SetSampling(Sampling.UltraLowPower); // read values tempValue = i2cBmp280.ReadTemperature(); Console.WriteLine($"Temperature {tempValue.Celsius} \u00B0C"); preValue = i2cBmp280.ReadPressure(); Console.WriteLine($"Pressure {preValue.Hectopascal} hPa"); altValue = i2cBmp280.ReadAltitude(); Console.WriteLine($"Altitude {altValue:0.##} m"); } } else { Console.WriteLine($"Failed: No DMP180"); } } catch (Exception) { Console.WriteLine("Failed: Probably no hw."); } }
public async Task <IHttpActionResult> DeleteBmp180(string id) { Bmp180 bmp180 = await db.Bmp180.FindAsync(id); if (bmp180 == null) { return(NotFound()); } db.Bmp180.Remove(bmp180); await db.SaveChangesAsync(); return(Ok(bmp180)); }
public PressureTempAltitudeUpdater(I2CBus bus, int sigFigs = 4, int delay = 30000) { _bmpSensor = new Bmp180(bus); _dataArray = new byte[_dataCount + _metaDataCount + _timeDataCount]; _dataArray[0] = (byte)PacketType.StartByte; // start bit = 0xff _dataArray[1] = (byte)PacketType.BmpDump; _delay = delay; _precision = (int)Math.Pow(10, sigFigs - 1); _workItem = new WorkItem(BmpUpdater, ref _dataArray, true, true, true); _bmpSensor.Init(); }
internal void ReadFromDevice(Bmp180 bmp180) { AC1 = (short)bmp180.Read16BitsFromRegisterBE((byte)Register.AC1); AC2 = (short)bmp180.Read16BitsFromRegisterBE((byte)Register.AC2); AC3 = (short)bmp180.Read16BitsFromRegisterBE((byte)Register.AC3); AC4 = bmp180.Read16BitsFromRegisterBE((byte)Register.AC4); AC5 = bmp180.Read16BitsFromRegisterBE((byte)Register.AC5); AC6 = bmp180.Read16BitsFromRegisterBE((byte)Register.AC6); B1 = (short)bmp180.Read16BitsFromRegisterBE((byte)Register.B1); B2 = (short)bmp180.Read16BitsFromRegisterBE((byte)Register.B2); MB = (short)bmp180.Read16BitsFromRegisterBE((byte)Register.MB); MC = (short)bmp180.Read16BitsFromRegisterBE((byte)Register.MC); MD = (short)bmp180.Read16BitsFromRegisterBE((byte)Register.MD); }
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { base.OnNavigatingFrom(e); _timer.Stop(); _tempHum?.Dispose(); _tempHum = null; _barometer?.Dispose(); _barometer = null; _luxMeter?.Dispose(); _luxMeter = null; _magnetometer?.Dispose(); _magnetometer = null; }
/// <summary> /// Entry point for example program /// </summary> /// <param name="args">Command line arguments</param> public static void Main(string[] args) { Console.WriteLine("Hello Bmp180!"); // bus id on the raspberry pi 3 const int busId = 1; var i2cSettings = new I2cConnectionSettings(busId, Bmp180.DefaultI2cAddress); var i2cDevice = I2cDevice.Create(i2cSettings); var i2cBmp280 = new Bmp180(i2cDevice); using (i2cBmp280) { // set samplings i2cBmp280.SetSampling(Sampling.Standard); // read values Temperature tempValue = i2cBmp280.ReadTemperature(); Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); var preValue = i2cBmp280.ReadPressure(); Console.WriteLine($"Pressure: {preValue.Hectopascals:0.##}hPa"); // Note that if you already have the pressure value and the temperature, you could also calculate altitude by // calling WeatherHelper.CalculateAltitude(preValue, Pressure.MeanSeaLevel, tempValue) which would be more performant. var altValue = i2cBmp280.ReadAltitude(WeatherHelper.MeanSeaLevel); Console.WriteLine($"Altitude: {altValue:0.##}m"); Thread.Sleep(1000); // set higher sampling i2cBmp280.SetSampling(Sampling.UltraLowPower); // read values tempValue = i2cBmp280.ReadTemperature(); Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C"); preValue = i2cBmp280.ReadPressure(); Console.WriteLine($"Pressure: {preValue.Hectopascals:0.##}hPa"); // Note that if you already have the pressure value and the temperature, you could also calculate altitude by // calling WeatherHelper.CalculateAltitude(preValue, Pressure.MeanSeaLevel, tempValue) which would be more performant. altValue = i2cBmp280.ReadAltitude(WeatherHelper.MeanSeaLevel); Console.WriteLine($"Altitude: {altValue:0.##}m"); } }
void Initialize() { Console.WriteLine("Initialize hardware..."); onboardLed = new RgbPwmLed(device: Device, redPwmPin: Device.Pins.OnboardLedRed, greenPwmPin: Device.Pins.OnboardLedGreen, bluePwmPin: Device.Pins.OnboardLedBlue); onboardLed.SetColor(Color.Red); var config = new SpiClockConfiguration( speed: new Frequency(48000, Frequency.UnitType.Kilohertz), mode: SpiClockConfiguration.Mode.Mode3); var spiBus = Device.CreateSpiBus( clock: Device.Pins.SCK, copi: Device.Pins.MOSI, cipo: Device.Pins.MISO, config: config); var display = new St7789( device: Device, spiBus: spiBus, chipSelectPin: Device.Pins.D02, dcPin: Device.Pins.D01, resetPin: Device.Pins.D00, width: 240, height: 240); graphics = new MicroGraphics(display); graphics.CurrentFont = new Font12x20(); button = Device.CreateDigitalInputPort(Device.Pins.D12, InterruptMode.EdgeRising, ResistorMode.Disabled); button.Changed += Button_Changed; sensor = new Bmp180(Device.CreateI2cBus()); sensor.Updated += SensorUpdated; sensor.StartUpdating(); onboardLed.SetColor(Color.Green); }
static void Main(string[] args) { Console.WriteLine("Hello Bmp180!"); //bus id on the raspberry pi 3 const int busId = 1; var i2cSettings = new I2cConnectionSettings(busId, Bmp180.DefaultI2cAddress); var i2cDevice = new UnixI2cDevice(i2cSettings); var i2cBmp280 = new Bmp180(i2cDevice); using (i2cBmp280) { //set samplings i2cBmp280.SetSampling(Sampling.Standard); //read values Temperature tempValue = i2cBmp280.ReadTemperature(); Console.WriteLine($"Temperature {tempValue.Celsius} °C"); double preValue = i2cBmp280.ReadPressure(); Console.WriteLine($"Pressure {preValue} Pa"); double altValue = i2cBmp280.ReadAltitude(); Console.WriteLine($"Altitude {altValue:0.##} m"); Thread.Sleep(1000); //set higher sampling i2cBmp280.SetSampling(Sampling.UltraLowPower); //read values tempValue = i2cBmp280.ReadTemperature(); Console.WriteLine($"Temperature {tempValue.Celsius} °C"); preValue = i2cBmp280.ReadPressure(); Console.WriteLine($"Pressure {preValue} Pa"); altValue = i2cBmp280.ReadAltitude(); Console.WriteLine($"Altitude {altValue:0.##} m"); } }
private void BmpUpdater() { var dataIndex = _metaDataCount; var time = BitConverter.GetBytes(Clock.Instance.ElapsedMilliseconds); _dataArray[dataIndex++] = time[0]; _dataArray[dataIndex++] = time[1]; _dataArray[dataIndex++] = time[2]; var pressure = _bmpSensor.GetPressure(); var temp = _bmpSensor.GetTemperature() * _precision; //precision because 4 sig figs go into decimals. var altitude = Bmp180.PressureToAltitude(Bmp180.SensorsPressureSealevelhpa, pressure, temp); //add pressure to data array (8 bytes) var pressureBytes = BitConverter.GetBytes(pressure); for (int i = 0; i < 8; i++) { _dataArray[dataIndex++] = pressureBytes[i]; } //add temp data (needs sign) to data (3 bytes) _dataArray[dataIndex++] = (temp < 0 ? (byte)1 : (byte)0); temp = (float)Math.Abs(temp); _dataArray[dataIndex++] = (byte)(((short)temp >> 8) & 0xFF); _dataArray[dataIndex++] = (byte)((short)temp & 0xFF); //add altitude data (can be unsigned, less than 65536) (2 bytes) altitude = (ushort)altitude; _dataArray[dataIndex++] = (byte)(((short)altitude >> 8) & 0xFF); _dataArray[dataIndex] = (byte)((short)altitude & 0xFF); Array.Copy(_dataArray, _workItem.PacketData, _dataArray.Length); Thread.Sleep(_delay); }
public MeadowApp() { Console.WriteLine("Initializing..."); // configure our BME280 on the I2C Bus var i2c = Device.CreateI2cBus(); bmp180 = new Bmp180(i2c); // Example that uses an IObersvable subscription to only be notified // when the temperature changes by at least a degree, and humidty by 5%. // (blowing hot breath on the sensor should trigger) bmp180.Subscribe(new FilterableObserver <AtmosphericConditionChangeResult, AtmosphericConditions>( h => { Console.WriteLine($"Temp and pressure changed by threshold; new temp: {h.New.Temperature}, old: {h.Old.Temperature}"); }, e => { return( (Math.Abs(e.Delta.Temperature) > 1) && (Math.Abs(e.Delta.Pressure) > 5) ); } )); // classical .NET events can also be used: bmp180.Updated += (object sender, AtmosphericConditionChangeResult e) => { Console.WriteLine($"Temperature: {e.New.Temperature}°C, Pressure: {e.New.Pressure}hPa"); }; // get an initial reading ReadConditions().Wait(); // start updating continuously bmp180.StartUpdating(); }
private async void InitializeAsync() { try { _tempHum = new Dsth01(27); // GPIO27 connected to Dsth01 CS-pin _barometer = new Bmp180(); _magnetometer = new Hmc5883L(22); // GPIO22 connected to Hmc5883L DRDY-pin _luxMeter = new Bh1750Fvi(); } catch { Debug.WriteLine("Sensor initialization failed."); return; } await Task.WhenAll(_magnetometer.ConnectAsync(), _barometer.ConnectAsync(), _tempHum.ConnectAsync(), _luxMeter.ConnectAsync()); // Set magnetometer gain an averaging if (_magnetometer.Connected) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Magnetometer.Visibility = Visibility.Visible; }); _magnetometer.WriteRegister(Hmc5883L.Register.ConfA, (byte) Hmc5883L.ConfigA.Average8); _magnetometer.WriteRegister(Hmc5883L.Register.ConfB, (byte) Hmc5883L.ConfigB.Gain1370); } // The BH175FVI sensor supports a continuous measurement mode if (_luxMeter.Connected) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Ambient.Visibility = Visibility.Visible; }); _luxMeter.Mode = Bh1750Fvi.Resolution.VeryHigh; _luxMeter.ReadingChanged += _AmbientLuxChanged; _luxMeter.ContinuousPeriod = 2000; // every 2 seconds _luxMeter.ContinuousMeasurement = true; } await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { if (_tempHum.Connected) Humidity.Visibility = Visibility.Visible; if (_barometer.Connected) Barometer.Visibility = Visibility.Visible; }); // The rest of the sensors are polled periodically _timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(1000)}; _timer.Tick += _timer_Tick; _timer.Start(); }
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { base.OnNavigatingFrom(e); _timer.Stop(); _tempHum?.Dispose(); _tempHum = null; _barometer?.Dispose(); _barometer = null; _luxMeter?.Dispose(); _luxMeter = null; _magnetometer?.Dispose(); _magnetometer = null; }
private async void InitializeAsync() { try { _tempHum = new Dsth01(27); // GPIO27 connected to Dsth01 CS-pin _barometer = new Bmp180(); _magnetometer = new Hmc5883L(22); // GPIO22 connected to Hmc5883L DRDY-pin _luxMeter = new Bh1750Fvi(); } catch { Debug.WriteLine("Sensor initialization failed."); return; } await Task.WhenAll(_magnetometer.ConnectAsync(), _barometer.ConnectAsync(), _tempHum.ConnectAsync(), _luxMeter.ConnectAsync()); // Set magnetometer gain an averaging if (_magnetometer.Connected) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Magnetometer.Visibility = Visibility.Visible; }); _magnetometer.WriteRegister(Hmc5883L.Register.ConfA, (byte)Hmc5883L.ConfigA.Average8); _magnetometer.WriteRegister(Hmc5883L.Register.ConfB, (byte)Hmc5883L.ConfigB.Gain1370); } // The BH175FVI sensor supports a continuous measurement mode if (_luxMeter.Connected) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Ambient.Visibility = Visibility.Visible; }); _luxMeter.Mode = Bh1750Fvi.Resolution.VeryHigh; _luxMeter.ReadingChanged += _AmbientLuxChanged; _luxMeter.ContinuousPeriod = 2000; // every 2 seconds _luxMeter.ContinuousMeasurement = true; } await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { if (_tempHum.Connected) { Humidity.Visibility = Visibility.Visible; } if (_barometer.Connected) { Barometer.Visibility = Visibility.Visible; } }); // The rest of the sensors are polled periodically _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1000) }; _timer.Tick += _timer_Tick; _timer.Start(); }
public void Dispose() { _bmp180?.Dispose(); _bmp180 = null; }