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. 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.
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.
--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.
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.
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.
This series consists of the following articles:
Recommended Posts