项目作者: knobtviker

项目描述 :
高级语言: Java
项目地址: git://github.com/knobtviker/bme280.git
创建时间: 2017-12-30T20:45:06Z
项目社区:https://github.com/knobtviker/bme280

开源协议:MIT License

下载


Download

Bosch BME280 driver for Android Things

This driver supports BME280 environmental sensor.

It’s based on Adafruit BME280 C library and BMX280 contrib-driver for Android Things.

After observing contrib-drivers early developer preview version providing inconsistent data and not supporting forced mode,
I decided to implement my own take for this driver.

  • Force mode support.
  • Config once and run, with sampling presets.
  • Consistent data without sudden humidity spikes or pressure dropouts.

This driver is not compatible with BMP280 sensor.
Compensation formulas are different, resulting in pressure and humidity data with a slight offset.

How to use the driver

Gradle dependency

To use the bme280 driver, simply add the line below to your project’s build.gradle,
where <version> matches the last version of the driver available on jcenter .

  1. dependencies {
  2. implementation 'com.knobtviker.android.things.contrib.community.driver:bme280:<version>'
  3. }

Sample usage

  1. import com.knobtviker.android.things.contrib.community.driver.bme280.BME280;
  2. // Access the environmental sensor:
  3. BME280 bme280;
  4. try {
  5. bme280 = new BME280(i2cBusName);
  6. // Configure driver power mode and oversampling for temperature, humidity or pressure,
  7. bme280.setSamplingNormal(); // Various other presets are exposed
  8. } catch (IOException e) {
  9. // couldn't configure the device...
  10. }
  11. // Read the current data:
  12. try {
  13. float temperature = bme280.readTemperature();
  14. float humidty = bme280.readHumidity();
  15. float pressure = bme280.readPressure();
  16. } catch (IOException e) {
  17. // error reading temperature
  18. }
  19. // Close the environmental sensor when finished:
  20. try {
  21. bme280.close();
  22. } catch (IOException e) {
  23. // error closing sensor
  24. }

If you need to read sensor values continuously, you can register the BME280 with the system and
listen for sensor values using the Sensor APIs:

  1. SensorManager mSensorManager = getSystemService(Context.SENSOR_SERVICE);
  2. SensorEventListener mListener = ...;
  3. BME280SensorDriver mSensorDriver;
  4. mSensorManager.registerDynamicSensorCallback(new SensorManager.DynamicSensorCallback() {
  5. @Override
  6. public void onDynamicSensorConnected(Sensor sensor) {
  7. if (sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
  8. mSensorManager.registerListener(mListener, sensor,
  9. SensorManager.SENSOR_DELAY_NORMAL);
  10. }
  11. }
  12. });
  13. try {
  14. mSensorDriver = new BME280SensorDriver(i2cBusName);
  15. mSensorDriver.registerTemperatureSensor();
  16. mSensorDriver.registerHumiditySensor();
  17. mSensorDriver.registerPressureSensor();
  18. } catch (IOException e) {
  19. // Error configuring sensor
  20. }
  21. // Unregister and close the driver when finished:
  22. mSensorManager.unregisterListener(mListener);
  23. mSensorDriver.unregisterTemperatureSensor();
  24. mSensorDriver.unregisterPressureSensor();
  25. mSensorDriver.unregisterHumiditySensor();
  26. try {
  27. mSensorDriver.close();
  28. } catch (IOException e) {
  29. // error closing sensor
  30. }