Use inexpensive MH-Z19B as a sensor to measure CO2 concentration. To do. It can retrieve data via serial communication. In this article, the Java library [mh-z19b] created to obtain the CO2 concentration from MH-Z19B using the Java serial communication module jSerialComm. -driver](https://github.com/s5uishida/mh-z19b-driver) is an overview. However, the contents of Github are the same. For implementation details and sample usage, see [Github Code](https://github.com/s5uishida/mh-z19b-driver/blob/master/src/io/github/s5uishida/iot/device/mhz19b/driver Please refer to /MHZ19BDriver.java).
Connect 5V, GND, Tx, Rx as follows. ** Please connect to each other with Tx⇔Rx. ** **
6. Pins
of MH-Z19B
In addition, instead of the GPIO terminal of Raspberry Pi 3B, insert a USB-connected serial communication adapter (SH-U09C) into Raspberry Pi 3B. You can also connect this to the MH-Z19B for use. In that case, the serial communication port name will be / dev / ttyUSB0
.
To enable simultaneous use of Bluetooth and serial communication (UART), configure the Raspbian Buster Lite OS as follows.
console=serial0,115200 -->Delete.
@@ -45,7 +45,7 @@
# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
-#dtparam=spi=on
+dtparam=spi=on
# Uncomment this to enable the lirc-rpi module
#dtoverlay=lirc-rpi
@@ -55,6 +55,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
Below is a sample of how to use mh-z19b-driver. ** Note that the sensor data read for the first time returns a strange value, but the appropriate value is returned for the second and subsequent times. ** **
import io.github.s5uishida.iot.device.mhz19b.driver.MHZ19BDriver;
public class MyMHZ19B {
private static final Logger LOG = LoggerFactory.getLogger(MyMHZ19B.class);
public static void main(String[] args) throws IOException {
MHZ19BDriver mhz19b = null;
try {
mhz19b = MHZ19BDriver.getInstance("/dev/ttyAMA0");
mhz19b.open();
mhz19b.setDetectionRange5000();
while (true) {
int value = mhz19b.getGasConcentration();
LOG.info("co2:" + value);
Thread.sleep(10000);
}
} catch (InterruptedException e) {
LOG.warn("caught - {}", e.toString());
} catch (IOException e) {
LOG.warn("caught - {}", e.toString());
} finally {
if (mhz19b != null) {
mhz19b.close();
}
}
}
}
This series consists of the following articles:
[2019.11.16] For the latest information on simple tools, please refer to here.
Recommended Posts