bme280-driver is a java library that operates combined humidity, pressure and temperature sensor called BME280 to connect BME280 to GPIO terminal of Raspberry Pi 3B and make it for use in java. I releases this in the form of the Eclipse plug-in project. You need Java 8.
I use Pi4J for gpio communication in java and have confirmed that it works in Raspberry Pi 3B (Raspbian Buster Lite OS (2019-07-10)).
Note. To use Pi4J 1.2's I2C functionality, sun.misc.SharedSecrets.class is required, but this class can only be used up to Java 8 and cannot be used since Java 9. Therefore, Java 8 is required to use this library.
Pins
of BME280 p.38 "Figure 17: I2C connection diagram"- Vin (3.3V)
- GND
- SDA
- SCL
- SDO --> I2C address is 0x76 when connected to GND and 0x77 when connected to Vin.
- GPIO of Raspberry Pi 3B
- Vin --> (1) or (17) / 3.3V
- GND --> (6), (9), (14), (20), (25), (30), (34) or (39)
- SDA --> (3) GPIO2 / I2C bus 1, (27) GPIO0 / I2C bus 0
- SCL --> (5) GPIO3 / I2C bus 1, (28) GPIO1 / I2C bus 0
The connection terminals for SDA and SCL vary depending on the I2C bus number (1
or 0
). For reference, it seems that some BME280 products are also compatible with 5V due to the voltage regulator.
The reason for using this version is that it is the latest as of July 2019 and BlueZ 5.50 is included from the beginning, and use Bluetooth and serial communication simultaneously.
- Edit
/boot/cmdline.txt
console=serial0,115200 --> removed
- Edit
/boot/config.txt
@@ -43,9 +43,10 @@
#arm_freq=800
# Uncomment some or all of these to enable the optional hardware interfaces
-#dtparam=i2c_arm=on
+dtparam=i2c_arm=on
+dtparam=i2c_vc=on
#dtparam=i2s=on
-#dtparam=spi=on
+dtparam=spi=on
# Uncomment this to enable the lirc-rpi module
#dtoverlay=lirc-rpi
@@ -55,6 +56,10 @@
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
+enable_uart=1
+dtoverlay=pi3-miniuart-bt
+core_freq=250
+
[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
- Edit
/etc/modules
i2c-dev <-- added
When editing is complete, reboot.
Pi4J depends on the WiringPi native library by Gordon Henderson. The Pi4J native library is dynamically linked to WiringPi.
# apt-get update
# apt-get install wiringpi
When using with Raspberry Pi 4B, install the latest version as follows. Please refer to here.
# wget https://project-downloads.drogon.net/wiringpi-latest.deb
# dpkg -i wiringpi-latest.deb
Please make sure it’s version 2.52.
# gpio -v
gpio version: 2.52
Note. In October 2019, I have not confirmed the official information that can use Raspberry Pi 4B with Pi4J and WiringPi. I've simply checked that it works, but some problems may occur.
For example, the installation of OpenJDK 8 is shown below.
# apt-get update
# apt-get install openjdk-8-jdk
If git is not included, please install it.
# apt-get install git
Install i2c-tools to use i2cdetect
command.
# apt-get install i2c-tools
If BME280 is connected to I2C bus 1 and I2C address 0x76, the i2cdetect
command displays the following.
# i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --
I would like to thank the authors of these very useful codes, and all the contributors.
The following sample code will be helpful. A strange value may be returned at the first time, but I think that the normal values will be obtained after the second time.
import com.pi4j.io.i2c.I2CBus;
import io.github.s5uishida.iot.device.bme280.driver.BME280Driver;
public class MyBME280 {
private static final Logger LOG = LoggerFactory.getLogger(MyBME280.class);
public static void main(String[] args) {
BME280Driver bme280 = null;
try {
bme280 = BME280Driver.getInstance(I2CBus.BUS_1, BME280Driver.I2C_ADDRESS_76);
bme280.open();
while (true) {
float[] values = bme280.getSensorValues();
LOG.info("temperature:" + values[0]);
LOG.info("humidity:" + values[1]);
LOG.info("pressure:" + values[2]);
Thread.sleep(10000);
}
} catch (InterruptedException e) {
LOG.warn("caught - {}", e.toString());
} catch (IOException e) {
LOG.warn("caught - {}", e.toString());
} finally {
if (bme280 != null) {
bme280.close();
}
}
}
}