Previously introduced in this article, the simple tool for monitoring environmental information provides temperature, humidity, and barometric pressure information Texas Instruments. Obtained from SensorTag CC2650. However, CC2650, which has been known worldwide for a long time as a BLE environment sensor tag, is discontinued, so it will be difficult to obtain it in the future. Therefore, I decided to support another sensor.
There are many temperature and humidity sensors in the world, and I honestly wonder which one to choose. Just then, I found helpful information, and as a result, Bosch's BME280 You have selected .com / products / environmental-sensors / humidity-sensors-bme280 /). It's affordable and can be obtained for hundreds of yen.
This sensor can measure temperature, humidity and barometric pressure. In addition, temperature seems to be mainly used to correct the measured value of atmospheric pressure, but since it is a good idea, the value itself is also used. The interface supports I2C and SPI, but I2C is used here. The I2C address can be either 0x76ʻor
0x77. Also, since there are two I2C buses for Raspberry Pi 3B or 4B,
0 and
1`, you can use up to four BME280s at the same time with Raspberry Pi 3B or 4B.
The created BME280 Java library (bme280-driver) is available on Github. I also wrote the OS and sensor setting procedure. In addition, a simple tool for monitoring environmental information that incorporates this library is available on Github here.
Here is a simple usage of bme280-driver. In the sample code below, I2C bus = 1
and address = 0x76
are specified.
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();
}
}
}
}
It's simple to use like this. It was created using Pi4J, a Java library for using GPIO of Raspberry Pi.
A simple tool that incorporates this Java library has the ability to monitor each sensor value on the dashboard and the ability to send it to the MQTT broker in JSON format.
Finally, the simple tool is available on Github here.
This series consists of the following articles:
Recommended Posts