Introduction: How to Live Stream Using Raspberry Pi

In this tutorial, we will learn how to make our own surveillance camera using Raspberry Pi.

You can access this stream using any device as long as it's connected to the same network the RPi is.

Supplies

Raspberry Pi completed with Screen (you can use a TV or and monitor you have), Keyboard and Mouse

Raspberry Pi Camera

Wifi

Step 1: Attach the Camera

  1. Pull the black plastic edges of the camera port
  2. Make sure the blue end of the ribbon is facing the USB ports
  3. Make sure the silver connectors are all in

*Refer to pictures attached

Step 2: Enable Camera

1. On your Raspberry Pi, go to [Preferences] > [Raspberry Pi Configuration]

2. On Raspberry Pi Configuration window, click on [interfaces] tap, then click on [Enable] next to [Camera]

3. Reboot

*You need to reboot your Raspberry Pi, for the camera to work

4. Test the camera

sudo raspistill -o tester.jpg

This command will take a shot in 5 seconds to test if the camera is working

Step 3: Script

1. Launch Thonny or Python and paste the script below

import io<br>import picamera
import logging
import socketserver
from threading import Condition
from http import server
PAGE="""\
<html>
<head>
<title>PiCamera MJPEG streaming demo</title>
</head>
<body>
<h1>PiCamera MJPEG Streaming Demo</h1>
<img src="stream.mjpg" width="640" height="480" /"
</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='640x480', framerate=24) as camera:
    output = StreamingOutput()
    camera.start_recording(output, format='mjpeg')
    try:
        address = ('', 8000)
        server = StreamingServer(address, StreamingHandler)
        server.serve_forever()
    finally:
        camera.stop_recording()

* code resourced from https://picamera.readthedocs.io/en/latest/recipes2...

2. Save the file and hit [Run]

Step 4: Find Your IP Address

1. On Terminal, type:

ifconfig

2. Refer to the image attach to where to find your IP address

Step 5: View Your Stream

To view your stream:

  • Launch your web browser
  • Type your IP address followed by:
:8000/index.html

*Refer to the image attached

To learn how to live stream on YouTube, you can check this tutorial