Background
Graduation season is coming again, and this time I joined the ranks of graduates, renting a house outside. But I don’t know why, every day when I go to work, I worry about whether my home will be robbed? Is there any plug left unplugged that could cause a fire? I heard this is a problem, but unfortunately, I don’t have the money to treat it. Fortunately, I have a Raspberry Pi.
So, our goal is to have the Raspberry Pi use sensors to determine whether someone has broken in or if there is a flame, and send the data to a server, which the phone will retrieve from the server. So the process is roughly like this:
Hardware Preparation:
Raspberry Pi
Human Infrared Sensing Module
Flame Sensor
10K Resistor
Breadboard and Dupont Wires
The human infrared sensing module and flame sensor are connected to the Raspberry Pi through the GPIO pins, using pins 11 and 12. The GPIO of the Raspberry Pi has multiple numbering methods; to avoid confusion, we will only mention one here. In the picture, the mysterious bulge at the bottom represents pin 1, and it is friendly marked as P1.
We connect the human infrared sensing module to pin 12 and the flame sensor to pin 11.
The hardware connection is simple. Although the human infrared sensing module requires a 5V power supply, the signal output is 3.3V, and the current is very small, so the signal output OUT can be directly connected to pin 12. The human infrared sensing module is essentially a pyroelectric module. The center wavelength of the infrared radiation emitted by the human body is 9~10–um. If the probe receives the infrared radiation from the human body, it outputs a high level. Simply put, it outputs a high level when someone is present and a low level when no one is present. Of course, this also has its weaknesses. First, it is easily affected by other heat sources. Second, the infrared radiation from the human body can be blocked. If a thief wraps himself up in a strange way, it will be powerless. I remember watching a science program when I was a child, where someone fooled an infrared sensing alarm with a piece of glass.
The flame sensor detects whether there is a flame. Its own resistance value increases as the flame gets larger. So how do we collect the signal? We connect it in series with a 10K resistor. We learned in middle school that in series, the larger the resistance value, the larger the voltage obtained. However, it outputs an analog signal, and the Raspberry Pi does not have a built-in ADC. What to do? Of course, we can directly buy a flame sensor module with digital signal output or buy an ADC chip ourselves. I tried it, and when the flame is close to the sensor, the output voltage can reach 3V, which meets the Raspberry Pi’s definition of a high level. Of course, this means that only a sufficiently large flame can trigger an alarm, but it’s better than not knowing if a fire occurs. The connection method of the flame sensor is shown in the picture, but it should be noted that the GPIO level of the Raspberry Pi is 3.3V, so the VCC here should also be 3.3V, and the signal output can be directly connected to pin 11.
Then, here is the pin numbering diagram. Everyone can connect the VCC and GND of the human infrared sensor and flame sensor according to the diagram.
For example, I use pin 1 to power the flame sensor, pin 9 as the GND for the flame sensor, and pin 11 as the signal line for the flame; pin 2 to power the human infrared sensing module, pin 6 as the GND for the human infrared sensing module, and pin 12 as the signal line.
After the connection, it looks like this:
Next is Python programming. We will of course use Python on the Raspberry Pi, which can easily operate the GPIO. We read the GPIO level and send it to the server.
On the server side, we will simply write an API using Flask.
First, we need to provide an API for the Raspberry Pi to send information. To make it convenient, we use a GET request to send data. The parameter i indicates whether someone has broken in, and the parameter f indicates whether there is a flame. Then we need to leave an API for the phone to retrieve information, returning data in JSON format. Of course, for convenience, we also use a GET request for data. In fact, for projects that require real-time performance, using sockets is more reliable. The code looks something like this; I admit I took a shortcut.
Then we move on to the Android phone side. The functionality is so simple that I don’t even know how to design the interface; it just displays whether someone has broken in and whether there is a flame. Of course, we also need an alarm prompt when someone breaks in or there is a flame.
The Android side is also quite simple. First, send a GET request to retrieve data from the server. It seems that there is a new way to make GET requests now, but I haven’t written an Android app for a long time, so my method is a bit outdated, and I hope everyone can guide me.
Of course, you cannot directly do network access in the main thread. We start a new thread, request data every five seconds, and communicate with the main thread through a handler. Additionally, when detecting an intrusion or a flame, a notification message should pop up in the notification bar.
This is mainly for my peace of mind, but now my workplace is only a 10-minute walk from home, so even if something happens, I can run back. Thinking about it this way, I don’t even need to treat my illness (fog).
Log in to NetEase Cloud Classroom
This month is the first exam for Python level 2, and the course is on sale 🙂