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

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

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
            with self.condition:
                self.frame = self.buffer.getvalue()
        return self.buffer.write(buf)

class StreamingHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.send_header('Location', '/index.html')
        elif self.path == '/index.html':
            content = PAGE.encode('utf-8')
            self.send_header('Content-Type', 'text/html')
            self.send_header('Content-Length', len(content))
        elif self.path == '/stream.mjpg':
            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')
                while True:
                    with output.condition:
                        frame = output.frame
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
            except Exception as e:
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))

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')
        address = ('', 8000)
        server = StreamingServer(address, StreamingHandler)
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, 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.

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

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.


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