In this tutorial, we are going to see how to make raspberry pi home security system with Pi Cam. It will stream live video to your local IP address, so that you can monitor it via any browser with any device but on same network as your pi connected. Once you learned this how to stream live video from raspberry pi, you can also make projects like

  1. Home security camera system,
  2. Surveillance camera system,
  3. CCTV Camera,
  4. Visitor monitoring system,
  5. Border security,
  6. Monitoring Pet etc.

If you want to monitor a part of your house, you don’t need to leave your sofa, just type IP address to any browser, you can see live video stream from raspberry pi. So let’s get started.

Components Required:
  1. Raspberry pi 4,
  2. Pi Cam V2.1,
Enable Camera of Raspberry Pi:

Go to Menu-> Preferences -> Raspberry pi configuration -> interfaces -> Enable Camera

Raspberry pi configuration

Enable Camera for Raspberry pi 4

 

Find out your Raspberry Pi IP address:

Open terminal and type this command

hostname -I
Finding Raspberry pi IP Address
Circuit for Raspberry Pi Home Security System:

There is no circuit you just need to attach Pi Cam to Raspberry pi 4.

Code for Streaming live video from Raspberry Pi to Local IP address:
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server

PAGE="""\
<html>
<head>
<title>Raspberry Pi - Home Security Camera</title>
</head>
<body>
<center><h1>Raspberry Pi - Home Security Camera</h1></center>
<center><img src="stream.mjpg" width="1280" height="720"></center>
</body>
</html>
"""

class StreamingOutput(object):
    def __init__(self):
        self.frame = None
        self.buffer = io.BytesIO()
        self.condition = Condition()

    def write(self, buf):
        if buf.startswith(b'\xff\xd8'):
            #new frame, copy the existing buffer's content and
            #notify all clients it's available
            self.buffer.truncate()
            with self.condition:
                self.frame = self.buffer.getvalue()
                self.condition.notify_all()
            self.buffer.seek(0)
        return self.buffer.write(buf)

class StreamingHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.send_response(301)
            self.send_header('Location', '/index.html')
            self.end_headers()
        elif self.path == '/index.html':
            content = PAGE.encode('utf-8')
            self.send_response(200)
            self.send_header('Content-Type', 'text/html')
            self.send_header('Content-Length', len(content))
            self.end_headers()
            self.wfile.write(content)
        elif self.path == '/stream.mjpg':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type','multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    with output.condition:
                        output.condition.wait()
                        frame = output.frame
                    self.wfile.write(b'--FRAME\r\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\r\n')
            except Exception as e:
                logging.warning(
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()

class StreamingServer(socketserver.ThreadingMixIn,server.HTTPServer):
    allow_reuse_address = True
    daemon_threads = True

with picamera.PiCamera(resolution='1280x720', framerate=24) as camera:
    camera.rotation = 180
    output = StreamingOutput()
    camera.start_recording(output, format='mjpeg')
    try:
        address = ('', 8000)
        server = StreamingServer(address, StreamingHandler)
        server.serve_forever()
    finally:
        camera.stop_recording()
Run the Code:

Open Thonny Python IDE, Copy paste this code, Save the file. Finally upload the code. Type URL in your web browser for example 192.168.0.102:8000, http://<RPi-IP-address>:8000, Now you will be able to see live video stream.

Raspberry pi Live video streaming

Code Explanation:

We are defining web page content using HTML, You can change title and heading of your web page. If you want you can change width and height also.

PAGE="""\
<html>
<head>
<title>Raspberry Pi - Home Security Camera</title>
</head>
<body>
<center><h1>Raspberry Pi - Home Security Camera</h1></center>
<center><img src="stream.mjpg" width="1280" height="720"></center>
</body>
</html>
"""

Here you can change screen resolution and frame rate. Maximum resolution you can set is 1920×1080 & maximum frame rate is 90 but it is also depends on the resolution you have set. If you set 1920×1080 resolution then you have to select maximum frame rate 30.

with picamera.PiCamera(resolution='1280x720', framerate=24) as camera:

here is a table you can understand it more clearly. For more information click here.

raspberry pi resolution and frame rate

Set camera rotation angle according to your need.

camera.rotation = 180

Web server available at the Raspberry Pi IP address port 8000, this is the address where we will see live video streaming.

address = ('', 8000)

web server will run forever, unless you stop it.

server.serve_forever()
Video:

If you like this content please subscribe to my YouTube Channel for latest update. Till Then Keep Learning Keep Making.