Git Product home page Git Product logo

mh-z19b-driver's Introduction

mh-z19b-driver

mh-z19b-driver is a java library that operates CO2 gas concentration sensor called MH-Z19B to connect MH-Z19B 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 or higher.

I use jSerialComm for serial communication in java and have confirmed that it works in Raspberry Pi 3B (Raspbian Buster Lite OS (2019-07-10)).

Connection of MH-Z19B and Raspberry Pi 3B

Connect with Vin <--> Vin,GND <--> GND, but connect Tx and Rx mutually to enable transmission / reception. Tx <--> Rx, Rx <--> Tx

  • 6. Pins of MH-Z19B
    • Vin
    • GND
    • Tx
    • Rx
  • GPIO of Raspberry Pi 3B
    • Vin --> (2) or (4)
    • GND --> (6), (9), (14), (20), (25), (30), (34) or (39)
    • Tx --> (8) GPIO14
    • Rx --> (10) GPIO15

In addition, I was able to use MH-Z19B using DSD TECH SH-U09C USB to TTL Serial Adapter with FTDI FT232RL Chip instead of GPIO of Raspberry Pi 3B. At that time, I set /dev/ttyUSB0 as the serial port name.

Install Raspbian Buster Lite OS (2019-07-10)

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.

Configuration of Raspbian Buster Lite OS

  • Edit /boot/cmdline.txt
console=serial0,115200 --> removed
  • Edit /boot/config.txt
@@ -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

When editing is complete, reboot. Now you can use /dev/ttyAMA0 for serial communication without conflicting with Bluetooth.

Install jdk8 on Raspberry Pi 3B

For example, the installation of OpenJDK 8 is shown below.

# apt-get update
# apt-get install openjdk-8-jdk

Install git

If git is not included, please install it.

# apt-get install git

Use this with the following bundles

I would like to thank the authors of these very useful codes, and all the contributors.

How to use

The following sample code will be helpful. Reading sensor data immediately after open may give a strange value, but I think that the normal values will be obtained after the second time.

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();
            mhz19b.setAutoCalibration(false);
            
            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();
            }
        }
    }
}

mh-z19b-driver's People

Contributors

s5uishida avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.