How to Stream Video from Raspberry Pi Camera to Computer P2

Building on our previous journey of setting up a live video feed with a Flask App on a Raspberry Pi, Part 2 dives deeper into the world of remote accessibility. This time, we're unlocking the full potential of your Raspberry Pi security camera by enabling access from anywhere in the world. Discover the simplicity of using ngrok to generate a publicly viewable URL for your video stream, all within a few easy steps and completely free. Whether you're miles away or just on a different network, you'll learn how to seamlessly connect to your real-time security camera. Essential for anyone looking to enhance their home security or just keen on DIY tech projects. Gear up with your Raspberry Pi (4b or any compatible model), a camera, and let's break the boundaries of local networks together!
Also be sure to go through part 1 if you have not.
Before reading the remainder, be sure to subscribe and support the channel if you have not!
Subscribe:
Support:
Hire me at UpWork to build your IoT projects:
1-) Setting Up ngrok on Your Raspberry Pi
The first step towards global access to your Raspberry Pi's video stream involves installing ngrok. This powerful tool creates a secure tunnel from the internet to your locally running Flask application, transforming your private stream into a globally accessible view. Unlike our initial setup, which restricted viewing to your local network, ngrok opens up your project to any Wi-Fi-enabled device worldwide.
Getting Started with ngrok:
  • Begin by visiting ngrok's website and signing up for a free account. This account is your key to creating secure tunnels from the internet to your devices.
  • With your account ready, turn to your Raspberry Pi. Open a terminal window and follow the instructions provided on ngrok's setup page specifically for Raspberry Pi users: Setup ngrok on Raspberry Pi. This guide will walk you through downloading and installing ngrok directly onto your Pi.
  • Installation is straightforward, but crucial. Once ngrok is installed, it's time to establish the tunnel. Execute the following command in your Raspberry Pi's terminal,
ngrok http http://localhost:5000
This command initiates ngrok and directs it to create a tunnel to your Flask application's local server, typically running on port 5000.
  • Upon executing the command, ngrok responds with a publicly accessible URL. This URL is your gateway to accessing the video stream from anywhere. Note that with the free version of ngrok, this URL changes each time you initiate the command, offering a dynamic but effective solution for remote access. For a static URL, consider ngrok's paid plans.
This enhanced setup transcends the limitations of local network access, bringing your Raspberry Pi's video stream to the global stage.
2-) Run the Flask App
Run the same flask app we created in P1
import io import picamera from flask import Flask, Response app = Flask(__name__) def generate_frames(): with picamera.PiCamera() as camera: camera.resolution = (640, 480) camera.framerate = 24 stream = io.BytesIO() for _ in camera.capture_continuous(stream, 'jpeg', use_video_port=True): stream.seek(0) yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + stream.read() + b'\r\n' stream.seek(0) stream.truncate() @app.route('/') def video_feed(): return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, threaded=True)
The only difference is that I removed the video_feed extension from the @app.route('/') because we want to local host tunnel to tunnel into the base URL. You can run this program on your device in conjunction with ngrok running.
Once we have both running successfully we can visit the URL generated by ngrok in the terminal to view the stream, this is great because the URL is viewable from any device, even devices not on the local network. One thing you will notice is that there is a bit of a lag. You can overcome this by improving your internet connection or by paying for ngrok to enhance the network bandwidth of your ngrok server, which will make the stream smoother.
Conclusion
This is it for todays tutorial, we went over an extension of the first tutorial where we showed how to stream video across the local network. We took it a step further and created a publicly facing stream! Best of all, it was incredibly easy and free to get started. There are some tradeoffs when using ngrok for streaming but I think it can create a decent stream and it is suitable for cool DIY projects. Hope you learned something new and got it working, do not forget to subscribe or ask me questions on YouTube.

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Controlling a 5V Laser Diode with Raspberry Pi Pico W

Learn how to safely control high-power components like a ShillehTek 5V laser diode using the Raspberry Pi Pico W and...

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.