Easily monitor the indoor environment-⑤ Obtain PM2.5 concentration from PPD42NS with Java (GPIO / Pi4J)-

Introduction

Use the inexpensive PPD42NS as a sensor to measure PM2.5 concentration. This outputs the detection result as Hi / Lo of digital output, and calculates the PM2.5 concentration by the predetermined formula. In this article, I used Pi4J as a Java GPIO module for Raspberry Pi 3B, and created a Java library [ppd42ns-driver] to obtain PM2.5 concentration from PPD42NS. ](Https://github.com/s5uishida/ppd42ns-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/ppd42ns-driver/blob/master/src/io/github/s5uishida/iot/device/ppd42ns/driver/PPD42NSDriver See .java).

Connection between PPD42NS and Raspberry Pi 3B

Connect 5V, GND, PM2.5, Tx as follows. You can also refer to here.

Regarding the OS settings, the previous article "Easy monitoring of indoor environment-④ Obtain CO2 concentration from MH-Z19B with Java (serial communication / jSerialComm)- / 1754998f8f706c4c3314) ".

WiringPi installation

In order to use the Java library Pi4J that operates GPIO of Raspberry Pi 3B, a library called WiringPi is required. Install as follows.

# 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

Make sure the version is 2.52.

# gpio -v
gpio version: 2.52

** As of October 2019, we have not confirmed official information on using Raspberry Pi 4B with Pi 4J and Wiring Pi combination. I've confirmed that it works easily, but some problems may occur. ** **

Sample code

Below is a sample of how to use ppd42ns-driver. ** Note that it takes about 30 seconds for each measurement, so it takes about 30 seconds for the sensor data read ppd42ns.read () to return. ** **

import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.RaspiPin;

import io.github.s5uishida.iot.device.ppd42ns.driver.PPD42NSDriver;
import io.github.s5uishida.iot.device.ppd42ns.driver.PPD42NSObservationData;

public class MyPPD42NS {
    private static final Logger LOG = LoggerFactory.getLogger(MyPPD42NS.class);
    
    public static void main(String[] args) {
        PPD42NSDriver ppd42ns = PPD42NSDriver.getInstance(RaspiPin.GPIO_10);
        ppd42ns.open();
    
        while (true) {
            try {
                PPD42NSObservationData data = ppd42ns.read();
                LOG.info("[{}] {}", ppd42ns.getName(), data.toString());
            } catch (IOException e) {
                LOG.warn("caught - {}", e.toString());
            }
        }
    
//      if (ppd42ns != null) {
//          ppd42ns.close();
//      }
    }
}

For details of the data returned by ppd42ns.read (), see [Class](https://github.com/s5uishida/ppd42ns-driver/blob/master/src/io/github/s5uishida/iot/device/ See ppd42ns / driver / PPD42NSObservationData.java). You can get the concentration of PM2.5 in two units, pcs / 0.01cf and μg / m ^ 3.

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 with Java (serial communication / jSerialComm) The related Github is here.
  5. ** Obtain PM2.5 concentration from PPD42NS in Java (GPIO / Pi4J) (this time) ** 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 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 CO2 concentration from MH-Z19B with Java (serial communication / jSerialComm)-
Easily monitor the indoor environment-⑩ Obtain temperature / humidity / atmospheric pressure from BME280 (substitute) with Java (I2C / Pi4J)-
Easily monitor the indoor environment ~ ⑨ Get motion detection (HC-SR501 / RCWL-0516) in Java (GPIO / 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-⑦ 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
Monitor the internal state of Java programs with Kubernetes
[Note] Create a java environment from scratch with docker
Prepare the environment for java11 and javaFx with Ubuntu 18.4