Package com.diozero.devices
Class BME280
- java.lang.Object
-
- com.diozero.devices.BME280
-
- All Implemented Interfaces:
DeviceInterface
,BarometerInterface
,HygrometerInterface
,SensorInterface
,ThermometerInterface
,AutoCloseable
public class BME280 extends Object implements BarometerInterface, ThermometerInterface, HygrometerInterface
Provides access to the Bosch BMx280 pressure and temperature sensor. The BME280 includes an additional humidity sensor. Different constructors support access via I2C or SPI. All constructors configure the device as follows:- Temperature oversampling: x1
- Pressure oversampling: x1
- Temperature oversampling: x1
- Operating mode: Normal
- Standby inactive duration: 1 second
- IIR filter coefficient: Off
- Author:
- gregflurry, mattjlewis
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BME280.FilterCoefficient
IIR Filter coefficient; can be FILTER_OFF, _2, _4, _8, _16.static class
BME280.HumidityOversampling
Humidity oversampling multiplier; value can be OVERSAMPLING_1, _2, _4, _8, _16.static class
BME280.Model
static class
BME280.OperatingMode
Operating mode; value can be MODE_SLEEP, _FORCED, or _NORMAL.static class
BME280.PressureOversampling
Pressure oversampling multiplier; value can be OVERSAMPLING_1, _2, _4, _8, _16.static class
BME280.StandbyDuration
Inactive duration in standby mode; can be STANDBY_ 500_US (0.5 ms) 62_5_MS (62.5 ms) 125_MS (125 ms) 250_MS (250 ms) 500_MS (500 ms) 1_S (1 second) 10_MS (10 ms) 20_MS (20 ms)static class
BME280.TemperatureOversampling
Temperature oversampling multiplier; value can be OVERSAMPLING_1, _2, _4, _8, _16.
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_I2C_ADDRESS
-
Constructor Summary
Constructors Constructor Description BME280()
Creates an instance that uses I2C.BME280(int chipSelect)
Creates an instance that uses SPI.BME280(int bus, int address)
Creates an instance that uses I2C.BME280(int controller, int chipSelect, int frequency, SpiClockMode mode)
Creates an instance that uses SPI.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the devicefloat
getPressure()
Reads the temperature, pressure, and humidity registers; compensates the raw values to provide meaningful results.float
getRelativeHumidity()
Reads the temperature, pressure, and humidity registers; compensates the raw values to provide meaningful results.float
getTemperature()
Reads the temperature, pressure, and humidity registers; compensates the raw values to provide meaningful results.float[]
getValues()
Reads the temperature, pressure, and humidity registers; compensates the raw values to provide meaningful results.boolean
isDataAvailable()
Indicates if data is available.void
reset()
Resets the device.void
setOperatingModes(BME280.TemperatureOversampling tempOversampling, BME280.PressureOversampling pressOversampling, BME280.HumidityOversampling humOversampling, BME280.OperatingMode operatingMode)
Sets the oversampling multipliers and operating mode.void
setStandbyAndFilterModes(BME280.StandbyDuration standbyDuration, BME280.FilterCoefficient filterCoefficient)
Sets the standby duration for normal mode and the IIR filter coefficient.boolean
waitDataAvailable(int interval, int maxIntervals)
Waits for data to become available.
-
-
-
Field Detail
-
DEFAULT_I2C_ADDRESS
public static final int DEFAULT_I2C_ADDRESS
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BME280
public BME280() throws RuntimeIOException
Creates an instance that uses I2C. Assumes I2C bus 1 and the default I2C address (0x76); all other I2C instantiation parameters are set to the default.- Throws:
RuntimeIOException
- if instance cannot be created
-
BME280
public BME280(int bus, int address) throws RuntimeIOException
Creates an instance that uses I2C. The caller must provide the I2C bus number and the I2C address; all other I2C instantiation parameters are set to the default.- Parameters:
bus
- I2C bus numberaddress
- device address- Throws:
RuntimeIOException
- if instance cannot be created
-
BME280
public BME280(int chipSelect) throws RuntimeIOException
Creates an instance that uses SPI. The caller must provide the chip select line; all other SPI instantiation parameters are set to the default.- Parameters:
chipSelect
- the chip select line used- Throws:
RuntimeIOException
- if instance cannot be created
-
BME280
public BME280(int controller, int chipSelect, int frequency, SpiClockMode mode) throws RuntimeIOException
Creates an instance that uses SPI. The caller must provide all SPI instantiation parameters.- Parameters:
controller
- the SPI controller usedchipSelect
- the chip select line usedfrequency
- the frequency usedmode
- the clock mode used- Throws:
RuntimeIOException
- if an error occurs
-
-
Method Detail
-
setOperatingModes
public void setOperatingModes(BME280.TemperatureOversampling tempOversampling, BME280.PressureOversampling pressOversampling, BME280.HumidityOversampling humOversampling, BME280.OperatingMode operatingMode)
Sets the oversampling multipliers and operating mode.- Parameters:
tempOversampling
- oversampling multiplier for temperaturepressOversampling
- oversampling multiplier for pressurehumOversampling
- oversampling multiplier for humidityoperatingMode
- operating mode
-
setStandbyAndFilterModes
public void setStandbyAndFilterModes(BME280.StandbyDuration standbyDuration, BME280.FilterCoefficient filterCoefficient)
Sets the standby duration for normal mode and the IIR filter coefficient.- Parameters:
standbyDuration
- standby durationfilterCoefficient
- IIR filter coefficient
-
waitDataAvailable
public boolean waitDataAvailable(int interval, int maxIntervals)
Waits for data to become available.- Parameters:
interval
- sleep intervalmaxIntervals
- maximum number of intervals to wait- Returns:
- true if data available, false if not
-
isDataAvailable
public boolean isDataAvailable()
Indicates if data is available.- Returns:
- rue if data available, false if not
-
getValues
public float[] getValues()
Reads the temperature, pressure, and humidity registers; compensates the raw values to provide meaningful results.- Returns:
- array in order of: temperature, pressure, humidity
-
getTemperature
public float getTemperature()
Reads the temperature, pressure, and humidity registers; compensates the raw values to provide meaningful results.- Specified by:
getTemperature
in interfaceThermometerInterface
- Returns:
- temperature
-
getPressure
public float getPressure()
Reads the temperature, pressure, and humidity registers; compensates the raw values to provide meaningful results.- Specified by:
getPressure
in interfaceBarometerInterface
- Returns:
- pressure in hectoPascals (hPa)
-
getRelativeHumidity
public float getRelativeHumidity()
Reads the temperature, pressure, and humidity registers; compensates the raw values to provide meaningful results.- Specified by:
getRelativeHumidity
in interfaceHygrometerInterface
- Returns:
- humidity
-
close
public void close() throws RuntimeIOException
Closes the device- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceDeviceInterface
- Throws:
RuntimeIOException
- if close fails
-
reset
public void reset()
Resets the device.
-
-