Easily monitor the indoor environment ~ ⑨ Get motion detection (HC-SR501 / RCWL-0516) in Java (GPIO / Pi4J) ~

I once wrote in this article about a simple tool for monitoring general environmental information in an indoor environment (home / office / factory). .. I couldn't find any open source software that was put together in a tool for monitoring such environmental information, so I created the following concept image in private. rainy_concept_jp.png Information obtained from each sensor and industrial automation equipment can be used as follows.

--Monitor with a time series graph from a web browser. (Dashboard) --Convert to JSON and output with MQTT so that it can be used for other purposes. (Storing in the cloud or storage for offline analysis, or using it to trigger some event)

This simple tool was created to be complete with a sensor and a Raspberry Pi 3B or 4B at a minimum. This time, I tried to make this tool compatible with motion detection sensors.

About motion detection sensor

The following two types of motion detection sensors have been adopted this time.

-HC-SR501 --Pyroelectric infrared method -RCWL-0516 --Microwave Doppler radar method

Java libraries (hc-sr501-driver, rcwl-0516-driver, respectively. -0516-driver)) has been created and published on Github. I also wrote the OS and sensor setting procedure. In addition, a simple tool for monitoring environmental information that incorporates these libraries is available on Github here.

An example of the usage image of these motion detection sensors is as follows. rainy_motion_detector_overview.png

--HC-SR501 is a so-called "human sensor" type that uses infrared rays radiated from the heat source of an object. The maximum detection distance is 7m. Since it uses infrared rays, it cannot be detected if there is something blocking it. --RCWL-0516 is a type that irradiates 3.2GHz microwaves and detects the movement of the object from the Doppler transition of the reflected wave so that the movement of the object can be detected regardless of the presence or absence of a heat source. The maximum detection distance is 9m. For example, it reacts to the movement of objects on the other side of the wall of the house. I think this feature can be both an advantage and a disadvantage depending on the application.

Considering these characteristics, we have supported two types so that they can be used properly according to the application.

Basic usage of Java library

Since it is Qiita, I will post a few samples of how to use the code.

-How to use hc-sr501-driver

The sample code below uses GPIO12 to receive the motion detection signal. It implements handle () which is called when motion is detected.

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

import io.github.s5uishida.iot.device.hcsr501.driver.HCSR501Driver;
import io.github.s5uishida.iot.device.hcsr501.driver.IHCSR501Handler;

public class MyHCSR501 {
    private static final Logger LOG = LoggerFactory.getLogger(MyHCSR501.class);
    
    public static void main(String[] args) {
        HCSR501Driver hcsr501 = HCSR501Driver.getInstance(RaspiPin.GPIO_12, new MyHCSR501Handler());
        hcsr501.open();

//      if (hcsr501 != null) {
//          hcsr501.close();
//      }
    }
}

class MyHCSR501Handler implements IHCSR501Handler {
    private static final Logger LOG = LoggerFactory.getLogger(MyHCSR501Handler.class);

    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
    private static final SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

    @Override
    public void handle(String pinName, boolean detect, Date date) {
        LOG.info("[{}] {} {}", pinName, detect, sdf.format(date));
    }
}

-How to use rcwl-0516-driver

The sample code below uses GPIO18 to receive the motion detection signal. It implements handle () which is called when motion is detected.

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

import io.github.s5uishida.iot.device.rcwl0516.driver.RCWL0516Driver;
import io.github.s5uishida.iot.device.rcwl0516.driver.IRCWL0516Handler;

public class MyRCWL0516 {
    private static final Logger LOG = LoggerFactory.getLogger(MyRCWL0516.class);
    
    public static void main(String[] args) {
        RCWL0516Driver rcwl0516 = RCWL0516Driver.getInstance(RaspiPin.GPIO_18, new MyRCWL0516Handler());
        rcwl0516.open();

//      if (rcwl0516 != null) {
//          rcwl0516.close();
//      }
    }
}

class MyRCWL0516Handler implements IRCWL0516Handler {
    private static final Logger LOG = LoggerFactory.getLogger(MyRCWL0516Handler.class);

    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
    private static final SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

    @Override
    public void handle(String pinName, boolean detect, Date date) {
        LOG.info("[{}] {} {}", pinName, detect, sdf.format(date));
    }
}

Both codes are simple, aren't they? It was created using Pi4J, a Java library for using GPIO of Raspberry Pi.

Finally

In the simple tool, these motion detections are sent as events in JSON format by MQTT for ease of use. This can be used as a trigger to connect to some subsequent processing (such as turning on the camera or microphone). It's good that these motion detection sensors are available for a few hundred yen.

Finally, the simple tool is available on Github here.

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. 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
  9. ** Get motion detection (HC-SR501 / RCWL-0516) in Java (GPIO / Pi4J) (this time) ** Related Github is here (HC-SR501) and here (RCWL-0516) -0516-driver).
  10. Get temperature / humidity / barometric pressure from BME280 (substitute) in Java (I2C / Pi4J) The related Github is here.
  11. Get illuminance from BH1750FVI (substitute) in Java (I2C / Pi4J) The related Github is here.

Recommended Posts

Easily monitor the indoor environment ~ ⑨ Get motion detection (HC-SR501 / RCWL-0516) in Java (GPIO / Pi4J) ~
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-⑦ Summarize in a simple tool-
Easily monitor the indoor environment ~ ⑧ Postscript ~
Easily monitor the indoor environment- (1) Motivation and concept-
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)-
[Java] Get the file in the jar regardless of the environment
Easily monitor the indoor environment-④ Obtain CO2 concentration from MH-Z19B with Java (serial communication / jSerialComm)-
Get the result of POST in Java
How to get the date in java
Get the URL of the HTTP redirect destination in Java
I get Mysql2 :: Error :: ConnectionError in the production environment
Source used to get the redirect source URL in Java
[Java] Get the file path in the folder with List
Easily monitor the indoor environment-② Catch the Bluetooth LE advertisement signal with Java (Bluetooth LE / bluez-dbus)-
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-⑤ Obtain PM2.5 concentration from PPD42NS with Java (GPIO / Pi4J)-
Easily monitor the indoor environment-④ Obtain CO2 concentration from MH-Z19B with Java (serial communication / jSerialComm)-
Easily monitor the indoor environment ~ ⑨ Get motion detection (HC-SR501 / RCWL-0516) in Java (GPIO / Pi4J) ~
Easily monitor the indoor environment ~ ⑧ Postscript ~
Easily monitor the indoor environment- (1) Motivation and concept-
Read temperature / humidity with Java from Raspberry Pi 3 & DHT11
Easily monitor the indoor environment-⑦ Summarize in a simple tool-