I have set up my new Raspberry Pi Zero WH with the new Raspbian Buster (recommmended software), avaiable on raspberrypi.org.

I have tried the following to real-time stream video over WiFi using SSH connection:

  1. Motion: Below is the link to the article using Motion I have followed: https://www.instructables.com/id/How-to-Make-Raspberry-Pi-Webcam-Server-and-Stream-/

Below is the configurations that have been set in /etc/motion/motion.conf file:

daemon ON
framerate 1000
Stream_port 8081
Stream_quality 100
Stream_localhost OFF
webcontrol_localhost OFF
quality 100
width 640
height 480
post_capture 5
  1. Picamera Python: Below is the link to the page using Picamera Phyton, section 4.10. Web streaming, I have followed: https://picamera.readthedocs.io/en/release-1.13/recipes2.html#web-streaming

Below is the sample Python code:

import picamera
import logging
import socketserver
from threading import Condition
from http import server

        <title>picamera MJPEG streaming demo</title>
        <h1>PiCamera MJPEG Streaming Demo</h1>
        <img src="stream.mjpg" width="640" height="480" />
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='640x480', framerate=24) as camera:
    output = StreamingOutput()
    camera.start_recording(output, format='mjpeg')
        address = ('', 8000)
        server = StreamingServer(address, StreamingHandler)

With both Motion and Picamera, I can stream real-time video in the browser on my mobile phone or on my laptop using the url raspberry.local:port_number.

But I can stream video for just a few minutes only. Then the browser stops getting video streaming from the Pi. When I reload the page in the browser, it said: "It could not connect to the server" and I cannot ssh to the Pi, When I tried to ping to the Pi via the terminal, it said: "ping: cannot resolve raspberrypi.local: Unknown host". When I tried to connect to the Pi via VNC client, it said: "cannot connect to the host". But the green led on the Pi and the red led on the camera module remain on.

How can I fix that?

  • What browsers do you use? At least the python code, creates a http connection and streams image after image on that connection. multipart/x-mixed-replace. For the browser it is, as it is loading a file and do not come to an end. Browsers are configured with timeouts, that close such infinite downloads after a while.. I don't know if your browser switches timeouts off, if the ContentType is "multipart/x-mixed-replace;". But it's possible. Timeouts may be configurable. (Browser?) BTW: There is no SSH connection just an unclosed HTTP request. Nov 15 '19 at 16:54
  • Search for "<your browser name> request timeout configuration". Perhaps you find docu how to configure the timeout. Then you can try another value to see if your issues depend on that timeout. Nov 15 '19 at 16:58
  • I mean I keep losing SSH connection via cmd or terminal and via VNC client on the moment the browser stops getting video streaming from the Pi after real-time streaming for a few minutes.
    – O Connor
    Nov 16 '19 at 16:26
  • Sorry, my fault. I've not been reading carefully. What do you mean with "When I tried to connect to the Pi via SSH on the terminal or via VNC client, I can't because the connection has already been closed" ? You can only establish a new SSH or VNC connection if the old one has been closed i.e if there is no connection active. Or do you mean the WIFI connection was closed, and you can't connect to the raspi over the WLAN router? Then probably you router has timeouts or can not handle the load (that would not be very probable) and loose the WIFI connection. Nov 17 '19 at 11:24
  • In any way, if all connections (SSH, VNC, HTTP) get closed at the same time, I would blame the WLAN router. Have you access to the log of the WLAN router. Perhaps it contains anything useful. A timeout notice or a "connection dropped for reason X" message. Nov 17 '19 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.