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

Build a Real-Time Azure IoT Sensor Pipeline with Raspberry Pi Pico W + BME280

Build a Real-Time Azure IoT Sensor Pipeline with Raspberry Pi Pico W + BME280

How to send live environmental sensor data to Azure IoT Hub and visualize it in Azure Data Explorer.

How to Connect an Arduino Sensor to ROS 2 on a Raspberry Pi

How to Connect an Arduino Sensor to ROS 2 on a Raspberry Pi

Learn how to connect a BME280 sensor to an Arduino and publish real-time sensor data into ROS...

How to Install ROS 2 Humble on Raspberry Pi (Ubuntu 22.04.5 LTS)

How to Install ROS 2 Humble on Raspberry Pi (Ubuntu 22.04.5 LTS)

Learn how to install ROS 2 Humble on a Raspberry Pi 4B running Ubuntu 22.04.5 LTS. This step-by-step guide walks...

Part 5: Building a Wi-Fi Control Interface for Remote Control Robot

Part 5: Building a Wi-Fi Control Interface for Remote Control Robot

Part 4: Adding Obstacle Detection - Raspberry Pi Pico W Robotics Course

Part 4: Adding Obstacle Detection - Raspberry Pi Pico W Robotics Course

Part 3: Motor Control Programming - Raspberry Pi Pico W Robotics Course

Part 3: Motor Control Programming - Raspberry Pi Pico W Robotics Course

Part 2: Robot Assembly - Raspberry Pi Pico W Robotics Course

Part 2: Robot Assembly - Raspberry Pi Pico W Robotics Course

Build a Wi-Fi-Controlled Obstacle-Avoidance Robot with Raspberry Pi Pico W

Build a Wi-Fi-Controlled Obstacle-Avoidance Robot with Raspberry Pi Pico W

Learn how to build a remote-control robot using the Raspberry Pi Pico W and a few simple hardware components. This...

Simple Guide: Build a Reverse Geolocator with Raspberry Pi Pico W and GPS Module

Simple Guide: Build a Reverse Geolocator with Raspberry Pi Pico W and GPS Module

Reverse Geolocator Tutorial with Raspberry Pi Pico W and GPS Module

Learn how to create a reverse...

LoRa and Raspberry Pi Pico W: Building a Sender-Receiver Communication System

LoRa and Raspberry Pi Pico W: Building a Sender-Receiver Communication System

function toggleAuthorBio() { var bio = document.getElementById('authorBio'); var button = document.querySelector('.toggle-button'); if (bio.style.display === 'none') { bio.style.display = 'block'; button.textContent = '▼ About this Author'; // Using Unicode character directly button.classList.add('expanded'); } else { bio.style.display = 'none'; button.textContent = '▶ About this Author'; // Using Unicode character directly button.classList.remove('expanded'); } }