Creating a Fire and Theft Prevention System with Raspberry Pi

Creating a Fire and Theft Prevention System with Raspberry Pi

The graduation season is approaching, and this time I joined the army of graduates and rented a house outside.

Creating a Fire and Theft Prevention System with Raspberry Pi

But for some reason, I worry every day at work whether my home will be burglarized? Is there any plug that hasn’t been pulled that might cause a fire? I heard this is a condition, but unfortunately, I can’t afford treatment. Luckily, I have a Raspberry Pi.

Well, our goal is to have the Raspberry Pi determine whether someone has broken in or whether there is a flame using sensors, and send the data to the server so that the phone can retrieve the data from the server. The process is roughly as follows:

Creating a Fire and Theft Prevention System with Raspberry Pi

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 via GPIO ports, using pins 11 and 12. The GPIO of the Raspberry Pi has various numbering methods, so to avoid confusion, we will only mention one. Look at the picture; the mysterious protrusion below represents pin 1, and others have kindly labeled P1.

Creating a Fire and Theft Prevention System with Raspberry Pi

We connect pin 12 to the human infrared sensing module and pin 11 to the flame sensor.

The hardware connection is very 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 infrared radiation from the human body has a central wavelength of 9-10 um. If the probe receives the infrared radiation from the human body, it outputs a high level. In simple terms, it outputs a high level when there is someone and a low level when there is no one. Of course, this has obvious weaknesses; first, it is easily affected by other heat sources, and second, the infrared radiation from the human body can be blocked. If a thief wraps themselves up in a strange way, it becomes ineffective. I remember watching a science program when I was young, where someone tricked an infrared alarm with a piece of glass.

The flame sensor detects whether there is a flame; its resistance value increases with the increase in flame size. So how do we collect the signal? We connect it in series with a 10K resistor; as we learned in middle school, the voltage obtained increases with the resistance value in a series voltage divider. However, it outputs an analog signal, and the Raspberry Pi does not have a built-in ADC. What to do? Of course, we can buy a flame sensor module with a digital signal output, or we can 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 high level. Of course, this way, only a sufficiently large flame can trigger an alarm, but it is better than not knowing that a fire has occurred. The connection method for the flame sensor is shown in the picture, but note 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.

Creating a Fire and Theft Prevention System with Raspberry Pi

Then attach the pin numbering diagram, and everyone can connect the VCC and GND to 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.

Creating a Fire and Theft Prevention System with Raspberry Pi

After connecting, it looks like this:

Creating a Fire and Theft Prevention System with Raspberry Pi

Then it’s programming time. We will use Python on the Raspberry Pi, as Python can easily operate the GPIO. We read the GPIO level and send it to the server.

# -*- coding:utf-8 -*-

import urllib

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)#Set GPIO numbering method

GPIO.setwarnings(False)#Cancel warnings

GPIO.setup(11,GPIO.IN)#Set pin 11 as input

GPIO.setup(12,GPIO.IN)#Set pin 12 as input

if __name__ == ‘__main__’:

while True:

i = GPIO.input(12)#Read the level of pin 12, low level is 0, high level is 1

f = GPIO.input(11)#Read the level of pin 11

url = ‘http://xxx.xxx.xxx.xxx:5000/record/?i=’+str(i)+’&f=’+str(f)

print url

web = urllib.urlopen(url)

print web.read()

time.sleep(5)#Send data every 5 seconds

On the server side, let’s write a simple API using Flask.

First, we need to provide an API for the Raspberry Pi to send information. For convenience, we use the GET request method to send data, where parameter i indicates whether someone has broken in, and 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. Again, we use the GET request method for convenience. 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.

# -*- coding : utf-8 -*-

from flask import Flask,request,jsonify

app = Flask(__name__)

isFire = 0

isInvaded = 0

@app.route(‘/record/’,methods=[‘GET’])

def record():

if request.method == ‘GET’:

global isInvaded

global isFire

try:

isInvaded = int(request.args.get(‘i’))

isFire = int(request.args.get(‘f’))

except:

return ‘error’

return ‘ok’

@app.route(‘/state/’,methods=[‘GET’])

def state():

global isInvaded

global isFire

return jsonify({‘isInvaded’:isInvaded, ‘isFire’:isFire})

if __name__ == ‘__main__’:

app.run(host=’0.0.0.0′)

Next is the Android side. The functionality is simple to the point that I don’t even know how to design the interface. It just shows whether there is someone breaking in and whether there is a flame. Of course, we also need to have alarm notifications when there is an intrusion or a flame.

Creating a Fire and Theft Prevention System with Raspberry Pi

Creating a Fire and Theft Prevention System with Raspberry Pi

The Android side is also quite simple. First, send a GET request to retrieve data from the server. It seems there is a new method to make GET requests now; I haven’t written an Android app in a long time, so my method is a bit outdated; please guide me.

Creating a Fire and Theft Prevention System with Raspberry Pi

Of course, you cannot directly access the network in the main thread; we start another thread to request data every 5 seconds and communicate with the main thread through a handler. Additionally, when an intrusion or a flame is detected, a notification should pop up in the notification bar.

public class MainActivity extends Activity { private Handler mHandler; private TextView fire, people; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler = new Handler(); mHandler.post(new TimerProcess()); fire = (TextView) findViewById(R.id.fire); people = (TextView) findViewById(R.id.people); } @SuppressLint(“HandlerLeak”) final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { String ret = msg.obj.toString(); switch (msg.what) { try { case 1: StringBuilder builder = new StringBuilder(); JSONObject jsonObject = new JSONObject(ret); Log.i(“json”, “” + jsonObject); if (jsonObject.getInt(“isFire”) == 0) { fire.setText(“No flame detected”); fire.setTextColor(android.graphics.Color.GREEN); } else { fire.setText(“Flame detected”); fire.setTextColor(android.graphics.Color.RED); mes(“Flame sensor detected a flame!”); } if (jsonObject.getInt(“isInvaded”) == 0) { people.setText(“Safe”); people.setTextColor(android.graphics.Color.GREEN); } else { people.setText(“Intruder detected”); people.setTextColor(android.graphics.Color.RED); mes(“An intruder has entered the room!”); } } catch (JSONException e) { e.printStackTrace(); } break; default: break; } } }; private void getDevInformation() { new Thread(new Runnable() { @Override public void run() { String ret = Webconn.getDev(); System.out.println(“get return:”); System.out.println(ret); handler.sendMessage(handler.obtainMessage(1, ret)); } }).start(); } private class TimerProcess implements Runnable { public void run() { mHandler.postDelayed(this, 5000); getDevInformation(); } } public void mes(String m) { //Define NotificationManager String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); //Define the content information displayed in the notification bar int icon = R.drawable.ic_launcher; CharSequence tickerText = “My notification bar title”; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); //Define the content information to be displayed when pulling down the notification bar Context context = getApplicationContext(); CharSequence contentTitle = “Raspberry Pi Security”; CharSequence contentText = m; Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); //Use mNotificationManager’s notify method to notify the user to generate title bar message notifications mNotificationManager.notify(1, notification); }}

This is mainly for my psychological comfort, but now the place I work is only a 10-minute walk from where I live, so even if something happens, I can run back. Thinking about it this way, I don’t even need to treat my illness (fog).

All the things are here:

tp://pan.baidu.com/s/1jHKfnb4

Creating a Fire and Theft Prevention System with Raspberry Pi

Long press the public account to “top

———————————-

Important news, dry goods, original, professional attention to “Black and White Road” WeChat: i77169 Huaxia Hacker Alliance we insist on freedom, free, sharing!

Creating a Fire and Theft Prevention System with Raspberry Pi

Leave a Comment