Skip to content

iMartzen/raspberrypi-camera-mjpgstreamer-homebridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

Raspberrypi-camera-mjpegstreamer-homebridge

This a tutorial about how to setup a Raspberry Pi Zero W to display in Homekit. This tutorial is not complete. It doesn't show how to set up homebridge in example.

What do I need?

Let's get started!

Flash Raspbian on SD card

Enable ssh

  • Create an empty ssh file on the root of the sd card
$ touch ssh

Give direct WIFI access

  • Create an file called: wpa_supplicant.conf with content as below, change SSID name + PASSWORD
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="<<SSID>>"
psk="<<PASSWORD>>"
}

Boot the RPI with the SD card inserted

  • Detect it's ip address (default assigned by dhcp)
$ nmap -sn 10.0.100.1-100

Login with ssh (default password: raspberry)

ssh pi@<pi_ip_address>

Changing your username

  • Set passed for root
$ sudo passwd root 
  • Make it possible to login via ssh for root
$ sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
  • Restart ssh
$ sudo /etc/init.d/ssh reload 
  • Login as root
$ ssh root@<ipadres>
  • Change username and make home directory
$ usermod -l <username> -d /home/<username> -m pi 
  • Change name in the group to username
$ groupmod -n <username> pi 

SSH configuration

  • Generate new SSH Key (anser defaults with enter)
$ ssh-keygen
  • Add your public keys to .ssh/authorized_keys file
$ echo '<public key>' > ~/.ssh/authorized_keys 
  • Test your key, exit the connection and log back in. There should be no prompt for password!

  • Disable root login and password based login

$ sudo sed -i 's/PermitRootLogin yes/#PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
$ sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
$ sudo sed -i 's/UsePAM yes/usePAM no/' /etc/ssh/sshd_config
$ sudo sed -i 's/PrintMotd no/PrintMotd yes/' /etc/ssh/sshd_config 
$ sudo /etc/init.d/ssh reload

Update and upgrade the lastest packages & update Raspberry PI firmware

$ sudo apt-get update && sudo apt-get -y upgrade && sudo apt auto-clean && sudo rpi-update

Reboot is needed

sudo reboot now 

Change Raspbian config

$ sudo raspi-config
    $ Network Configuration
        - change hostname (i.e camera-1)
        - WIFI SSID + WW 
    $ Localisation options:
        - change locale: en_GB.UTF-8 UTF-8 and en_US.UTF-8 UTF-8
        - locale default: none
        - change timezone: Europe/Amsterdam
    $ Interfacing options: 
        - enable camera: yes

Reboot is needed

sudo reboot now 

Check if configuration works, making a photo

$ cd ~ 
$ raspistill -o cam.jpg
  • On your local computer:
	$ scp pi@raspberry.local:~/cam.jpg 
  • Check if the image contains the image you expect, where the camera is pointed at.

Prepare the system for the software mjpg-streamer

$ sudo apt-get install cmake libjpeg8-dev git gcc g++ 

Clone repository & compile the software

$ cd ~
$ git clone https://github.com/jacksonliam/mjpg-streamer.git
$ cd mjpg-streamer/mjpg-streamer-experimental
$ make
$ sudo make install 

Test the installed software

$ export LD_LIBRARY_PATH=.
$ ./mjpg_streamer -o "output_http.so -w ./www" -i "input_raspicam.so"

Browse to

  • http://<>:8080
  • If you see the MJPG Streamer welcome page, it works!

Enable it on reboot

$ nano /home/<username>/mjpg-streamer.sh
  • Copy this text in the mjpg-streamer.sh file
#!/bin/bash
# chmod +x mjpg-streamer.sh
# Crontab: @reboot /home/pi/mjpg-streamer/mjpg-streamer.sh start
# Crontab: @reboot /home/pi/mjpg-streamer/mjpg-streamer-experimental/mjpg-streamer.sh start

MJPG_STREAMER_BIN="/usr/local/bin/mjpg_streamer"  # "$(dirname $0)/mjpg_streamer"
MJPG_STREAMER_WWW="/usr/local/share/mjpg-streamer/www"
MJPG_STREAMER_LOG_FILE="${0%.*}.log"  # "$(dirname $0)/mjpg-streamer.log"
RUNNING_CHECK_INTERVAL="2" # how often to check to make sure the server is running (in seconds)
HANGING_CHECK_INTERVAL="3" # how often to check to make sure the server is not hanging (in seconds)

VIDEO_DEV="/dev/video0"
FRAME_RATE="30"
QUALITY="80"
RESOLUTION="1280x720"  # 160x120 176x144 320x240 352x288 424x240 432x240 640x360 640x480 800x448 800x600 960x544 1280x720 1920x1080 (QVGA, VGA, SVGA, WXGA)   #  lsusb -s 001:006 -v | egrep "Width|Height" # https://www.textfixer.com/tools/alphabetical-order.php  # v4l2-ctl --list-formats-ext  # Show Supported Video Formates
PORT="8080"
YUV="yes"

################INPUT_OPTIONS="-r ${RESOLUTION} -d ${VIDEO_DEV} -f ${FRAME_RATE} -q ${QUALITY} -pl 60hz"
INPUT_OPTIONS="-r ${RESOLUTION} -d ${VIDEO_DEV} -q ${QUALITY} -pl 60hz --every_frame 2"  # Limit Framerate with  "--every_frame ", ( mjpg_streamer --input "input_uvc.so --help" )


if [ "${YUV}" == "true" ]; then
	INPUT_OPTIONS+=" -y"
fi

OUTPUT_OPTIONS="-p ${PORT} -w ${MJPG_STREAMER_WWW}"

# ==========================================================
function running() {
    if ps aux | grep ${MJPG_STREAMER_BIN} | grep ${VIDEO_DEV} >/dev/null 2>&1; then
        return 0

    else
        return 1

    fi
}

function start() {
    if running; then
        echo "[$VIDEO_DEV] already started"
        return 1
    fi

    export LD_LIBRARY_PATH="$(dirname $MJPG_STREAMER_BIN):."

	echo "Starting: [$VIDEO_DEV] ${MJPG_STREAMER_BIN} -i \"input_uvc.so ${INPUT_OPTIONS}\" -o \"output_http.so ${OUTPUT_OPTIONS}\""
    ${MJPG_STREAMER_BIN} -i "input_uvc.so ${INPUT_OPTIONS}" -o "output_http.so ${OUTPUT_OPTIONS}" >> ${MJPG_STREAMER_LOG_FILE} 2>&1 &

    sleep 1

    if running; then
        if [ "$1" != "nocheck" ]; then
            check_running & > /dev/null 2>&1 # start the running checking task
            check_hanging & > /dev/null 2>&1 # start the hanging checking task
        fi

        echo "[$VIDEO_DEV] started"
        return 0

    else
        echo "[$VIDEO_DEV] failed to start"
        return 1

    fi
}

function stop() {
    if ! running; then
        echo "[$VIDEO_DEV] not running"
        return 1
    fi

    own_pid=$$

    if [ "$1" != "nocheck" ]; then
        # stop the script running check task
        ps aux | grep $0 | grep start | tr -s ' ' | cut -d ' ' -f 2 | grep -v ${own_pid} | xargs -r kill
        sleep 0.5
    fi

    # stop the server
    ps aux | grep ${MJPG_STREAMER_BIN} | grep ${VIDEO_DEV} | tr -s ' ' | cut -d ' ' -f 2 | grep -v ${own_pid} | xargs -r kill

    echo "[$VIDEO_DEV] stopped"
    return 0
}

function check_running() {
    echo "[$VIDEO_DEV] starting running check task" >> ${MJPG_STREAMER_LOG_FILE}

    while true; do
        sleep ${RUNNING_CHECK_INTERVAL}

        if ! running; then
            echo "[$VIDEO_DEV] server stopped, starting" >> ${MJPG_STREAMER_LOG_FILE}
            start nocheck
        fi
    done
}

function check_hanging() {
    echo "[$VIDEO_DEV] starting hanging check task" >> ${MJPG_STREAMER_LOG_FILE}

    while true; do
        sleep ${HANGING_CHECK_INTERVAL}

        # treat the "error grabbing frames" case
        if tail -n2 ${MJPG_STREAMER_LOG_FILE} | grep -i "error grabbing frames" > /dev/null; then
            echo "[$VIDEO_DEV] server is hanging, killing" >> ${MJPG_STREAMER_LOG_FILE}
            stop nocheck
        fi
    done
}

function help() {
    echo "Usage: $0 [start|stop|restart|status]"
    return 0
}

if [ "$1" == "start" ]; then
    start && exit 0 || exit -1

elif [ "$1" == "stop" ]; then
    stop && exit 0 || exit -1

elif [ "$1" == "restart" ]; then
    stop && sleep 1
    start && exit 0 || exit -1

elif [ "$1" == "status" ]; then
    if running; then
        echo "[$VIDEO_DEV] running"
        exit 0

    else
        echo "[$VIDEO_DEV] stopped"
        exit 1

    fi

else
    help

fi
  • Make the file executable
$ chmod +x mjpg-streamer.sh 
  • Edit conjob configuration
$ crontab -e 
  • Add line for reboot
$ @reboot /home/pi/mjpg-streamer.sh start

Add camera to homebridge:

  • Install plugin homebridge-camera-ffmpeg
    • Click on 'plugin'
    • Search for homebridge-camera-ffmpeg
    • Click on ‘install’
  • Add to homebridge config:
"platforms": [
    {
        "name": "Camera ffmpeg",
        "cameras": [
            {
                "name": "Default Camera or <other name>" ,
                "videoConfig": {
                    "source": "-re -f mjpeg -i http://<ip-or-dns-of-your-camera>:8080/?action=stream",
                    "stillImageSource": "-f mjpeg -i http://<ip-or-dns-of-your-camera>:8080/?action=snapshot",
                    "maxStreams": 2,
                    "maxWidth": 1280,
                    "maxHeight": 720,
                    "maxFPS": 30,
                    "debug": false,
                    "audio": false,
                    "motion": false
                }
            }
        ]
    }
],

Add to Homekit:

  • Click on ‘+’
  • Add accessory
  • Select your device in your the overview
  • Enter pin manually
  • Pin is known in your homebridge instance

Sources used for this:

About

Tutorial about how to setup a Raspberry Pi Zero W to display in Homekit

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published