Raspi BabyCam v2

On my first babycam I used a usb-webcam, LXDE, chrome and Jitsi to get the video signal to the receiver. I needed to replace the camera and decided to buy a used raspberry pi cam (v1.3). I also printed a case for the cam but this turned the camera upside down and the jitsi picture too.

While you could turn the picture with:

v4l2-ctl --set-ctrl vertical_flip=1
v4l2-ctl --set-ctrl horizontal_flip=1

I also was a little disturbed with the high resource consumption of the desktop and especially the jitsi session inside chrome. The pi got hot (ca. 80 degrees C) so i needed to install a fan to keep the pi cool. That makes noise and is just not the sleak solution I wanted to build.

Thats why I searched solution that provides a simple video stream via http. And finally i found a nice python script that grabs the video stream from the camera, resizes it, rotates it and shares it on a website:

# Web streaming example
# Source code from the official PiCamera package
# http://picamera.readthedocs.io/en/latest/recipes2.html#web-streaming

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

<title>Raspberry Pi - Surveillance Camera</title>
<center><h1>Raspberry Pi - Surveillance Camera</h1></center>
<center><img src="stream.mjpg" width="640" height="480"></center>

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()
    #Uncomment the next line to change your Pi's Camera rotation (in degrees)
    #camera.rotation = 90
    camera.start_recording(output, format='mjpeg')
        address = ('', 8000)
        server = StreamingServer(address, StreamingHandler)

So, the next step was to start the script and start of the pi and keep it running until the pi is turned of. Here i found another solution.

Running the Script at startup

On your Pi, edit the file /etc/rc.local using the editor of your choice. You must edit it with root permissions:

sudo nano /etc/rc.local

Add commands to execute the python program, preferably using absolute referencing of the file location (complete file path are preferred). Be sure to leave the line exit 0 at the end, then save the file and exit. In nano, to exit, type Ctrl-x, and then Y.

If your program runs continuously (runs an infinite loop) or is likely not to exit, you must be sure to fork the process by adding an ampersand (“&”) to the end of the command, like:

sudo python /home/pi/babycam.py &

The Pi will run this program at bootup, and before other services are started.  If you don’t include the ampersand and if your program runs continuously, the Pi will not complete its boot process. The ampersand allows the command to run in a separate process and continue booting with the main process running.

Now reboot the Pi to test it:

sudo reboot


Also, be sure to reference absolute file names rather than relative to your home folder. For example use `/home/pi/babypi.py` instead of `babypi.py`.

If you add a script into /etc/rc.local, it is added to the boot sequence. If your code gets stuck then the boot sequence cannot proceed. So be careful as to which code you are trying to run at boot and test the code a couple of times. You can also get the script’s output and error written to a text file (say log.txt) and use it to debug.

sudo python /home/pi/sample.py & > /home/pi/Desktop/log.txt 2>&1

Once the script is running, you can access your video streaming web server at: http://<Your_Pi_IP_Address>:8000.

Different than the Jitsi Session this stream is not directly available from outside your network.