Easily monitor the indoor environment-④ Obtain CO2 concentration from MH-Z19B with Java (serial communication / jSerialComm)-

Introduction

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).

Connection of MH-Z19B and Raspberry Pi 3B

Connect 5V, GND, Tx, Rx as follows. ** Please connect to each other with Tx⇔Rx. ** **

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.

OS settings

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

Sample code

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();
            }
        }
    }
}

A series of articles

This series consists of the following articles:

  1. Motivation and Concept
  2. Catch Bluetooth LE advertisement signal with Java (Bluetooth LE / bluez-dbus) The related Github is here.
  3. Get temperature / humidity / illuminance etc. from TI SensorTag CC2650 with Java (Bluetooth LE / bluez-dbus) The related Github is here.
  4. ** Obtain CO2 concentration from MH-Z19B in Java (serial communication / jSerialComm) (this time) ** The related Github is here.
  5. Get PM2.5 concentration from PPD42NS in Java (GPIO / Pi4J) The related Github is here.
  6. Getting operational information of industrial automation equipment in Java (OPC-UA / Eclipse Milo) The related Github is here.
  7. Collect in a simple tool The related Github is here.
  8. Postscript

Postscript

[2019.11.16] For the latest information on simple tools, please refer to here.

Recommended Posts

Easily monitor the indoor environment-④ Obtain CO2 concentration from MH-Z19B with Java (serial communication / jSerialComm)-
Easily monitor the indoor environment-⑤ Obtain PM2.5 concentration from PPD42NS with Java (GPIO / Pi4J)-
Easily monitor the indoor environment-⑪ Obtain the illuminance with Java from BH1750FVI (substitute)-(I2C / Pi4J)-
Easily monitor the indoor environment-⑩ Obtain temperature / humidity / atmospheric pressure from BME280 (substitute) with Java (I2C / Pi4J)-
Easily monitor the indoor environment-② Catch the Bluetooth LE advertisement signal with Java (Bluetooth LE / bluez-dbus)-
Easily monitor the indoor environment ~ ⑧ Postscript ~
Easily monitor the indoor environment ~ ③ Get temperature / humidity / illuminance etc. from TI SensorTag CC2650 with Java (Bluetooth LE / bluez-dbus) ~
Easily monitor the indoor environment- (1) Motivation and concept-
Easily monitor the indoor environment ~ ⑨ Get motion detection (HC-SR501 / RCWL-0516) in Java (GPIO / Pi4J) ~
Easily monitor the indoor environment-⑦ Summarize in a simple tool-
Easily monitor the indoor environment-⑥ Acquire operation information of industrial automation equipment in Java (OPC-UA / Eclipse Milo)-
[Java] Set the time from the browser with jsoup