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:
- 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
- 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?
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<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