1

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

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()

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. – Peter Paul Kiefer 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. – Peter Paul Kiefer 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. – Peter Paul Kiefer 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. – Peter Paul Kiefer 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.