From eLinux.org
Revision as of 17:58, 12 November 2014 by P4trykx (talk | contribs)
Jump to: navigation, search

This is the wiki for user contributed ideas and code for the excellent RPi Cam Web Interface found here: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=43&t=63276

Source code also available on GitHub:

Remember, anyone can create an account on here and add to this wiki.

Updating to the latest version

First, update your firmware because RaspiMJPEG will utilise the latest camera firmware:

sudo apt-get update
sudo apt-get dist-upgrade
sudo rpi-update
sudo reboot

Then update the code (git pull and stop raspimjpeg are now run when using the install option):

cd /home/pi/RPi_Cam_Web_Interface
git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git
git pull origin master
./RPi_Cam_Web_Interface_Installer.sh install
./RPi_Cam_Web_Interface_Installer.sh start

The Config File

Thanks to the amazing hard work by Silvan Melchior, RaspiMJPEG now uses a config file to set various parameters on starting the RPi Can Web Interface.

The release update which includes an example of the config file can be found here: http://www.raspberrypi.org/phpBB3/viewtopic.php?p=524707#p524707

A few examples on how to set variables can be found below.

Changing the Field of View

Follow this tutorial to start a different view automatically during boot:

There are three different preview modes available which are set by changing the resolution of the video_width and video_height options in the # Video Options section.

The view along with resolution is shown below:

<view name> - <video_width> x <video_height>
Std FOV - 1920 x 1080
16:9 wide FOV - 1296 x 730
4:3 full FOV - 1296 x 976

e.g. to set 4:3 full FOV:

# Video Options
video_width 1296
video_height 976

Remote access to website with User/Pass and changing port


If using motion you need to edit motion.conf adding the directive:

netcam_userpass username:password

(source http://www.raspberrypi.org/forums/viewtopic.php?p=530057#p530057)

Central Motion Detection from Multiple Cams using iSpy Connect


Configure Timelapse

Timelapse is now built into RaspiMJPEG

Use nginx web server instead of Apache

This could cause difficulties when silvanmelchior releases an update because it would try to install Apache again


View video stream on an iDevice / Smartphone

Credit goes to Oke for the original post. A few more tweaks added for iPhone app. http://www.raspberrypi.org/phpBB3/viewtopic.php?p=507756#p507756

  • On the Pi, run:
sudo ln -s /run/shm/mjpeg/cam.jpg /var/www/cam.jpg
  • Download "IP Cam View Pro" (I used IP Cam View Lite on the iPhone - can upgrade to Pro)
  • Select the menu icon
  • Press Manage Cameras
  • Select Add Camera then Generic URL
  • Give your cam a name
  • For Type choose Generic Video URL
  • Enter your URL, e.g.
  • Press Test
  • If it works, press Save

Embed live-preview in own homepage

There is an example in /var/www called "clean.php" with "script_min.js" as an example for the minimum code to implement the live-preview. The size can be changed either as parameter in /etc/raspimjpeg or with CSS. To add further features (change settings, record images/videos), study the existing homepage.

Save an image upon motion detection


Move saved images and videos to another folder

Detailed instructions on how to map a network NFS share to /var/www/media - http://www.raspberrypi.org/forums/viewtopic.php?p=531344#p531344

When to do the move - http://www.raspberrypi.org/forum/viewtopic.php?p=515967#p515967

Mounting a share - http://www.raspberrypi.org/phpBB3/viewtopic.php?p=513781#p513781

Mounting a Windows Share - http://www.stuffaboutcode.com/2012/05/raspberry-pi-connect-nas-windows-share.html

If you've mounted a network share to something other than /var/www/media, such as /mnt/myshare, you can bind the two together using this command:

sudo mount --bind /mnt/myshare /var/www/media

Scale camera window correctly in Chrome on Android


Button on web interface to reboot the Pi


Rotation not working?

Try a different browser - http://www.raspberrypi.org/forum/viewtopic.php?p=513705#p513705

Internet Explorer isn't supported, Opera and Firefox <21 can't show the preview of recorded videos.

Slow video stream?


Pi-Pan and Pi-Light

Information about Pi-Pan and Pi-Light: http://www.openelectrons.com/Pi-Pan‎

Needed Hardware for this addition:

  • Pi-Pan (OpenElectrons)
  • Pi-Light (OpenElectrons)
  • Pi-Case to mount Pi-Pan recommended (e.g. OpenElectrons)

With these code-changes and additions, it's possible to control Pi-Pan and Pi-Light from the RPi Cam Web Interface. Just follow these steps:

#!/usr/bin/env python

import time
import os, sys
import pipan
import pilight

p_servo = pipan.PiPan()
p_led = pilight.PILIGHT()

while True:
  pipein = open("/var/www/FIFO_pipan", 'r')
  line = pipein.readline()
  line_array = line.split(' ')
  if line_array[0] == "servo":
  elif line_array[0] == "led":
  • Navigate to "/var/www" and add a named pipe with the following commands:
sudo mknod FIFO_pipan p
sudo chmod 666 FIFO_pipan
  • Edit "/etc/rc.local": add the following line below the raspimjpeg-command (change the path to the directory where you extracted the pipan-files:
python /home/pi/pipan_pipe.py &
  • Add the file "pipan.php" to "/var/www":

  // settings
  $min_pan = 60;
  $max_pan = 190;
  $min_tilt = 120;
  $max_tilt = 220;

  // code
  if(isset($_GET["pan"])) {
    if(is_numeric($_GET["pan"])) {
      if(is_numeric($_GET["tilt"])) {
        $pan = round($min_pan + (($max_pan - $min_pan)/200*$_GET["pan"]));
        $tilt = round($min_tilt + (($max_tilt - $min_tilt)/200*$_GET["tilt"]));
        $pipe = fopen("FIFO_pipan","w");
        fwrite($pipe, "servo $pan $tilt ");

  if(isset($_GET["red"])) {
    if(is_numeric($_GET["red"])) {
      if(is_numeric($_GET["green"])) {
        if(is_numeric($_GET["blue"])) {
          $pipe = fopen("FIFO_pipan","w");
          fwrite($pipe, "led " . $_GET["red"] . " " . $_GET["green"] . " " . $_GET["blue"] . " ");

  • Add the file "pipan.js" to "/var/www":
var pan = 100;
var tilt = 100;
var pan_bak = 100;
var tilt_bak = 100;
var pan_start;
var tilt_start;
var touch = false;
var led_stat = false;
var ajax_pipan;
var pipan_mouse_x;
var pipan_mouse_y;

document.onkeypress = pipan_onkeypress;

if(window.XMLHttpRequest) {
  ajax_pipan = new XMLHttpRequest();
else {
  ajax_pipan = new ActiveXObject("Microsoft.XMLHTTP");
ajax_pipan.onreadystatechange = ajax_pipan_done;

function ajax_pipan_done() {
  if(ajax_pipan.readyState == 4) {
    if(touch) {
      if((pan_bak != pan) || (tilt_bak != tilt)) {
      else {
        setTimeout("ajax_pipan_done()", 100);

function ajax_pipan_start () {
  ajax_pipan.open("GET","pipan.php?pan=" + pan + "&tilt=" + tilt, true);
  pan_bak = pan;
  tilt_bak = tilt;

function servo_left () {
  if(pan <= 190) pan += 10;

function servo_right () {
  if(pan >= 10) pan -= 10;

function servo_up () {
  if(tilt >= 10) tilt -= 10;

function servo_down () {
  if(tilt <= 190) tilt += 10;

function led_switch () {

  if(!led_stat) {
    led_stat = true;
    ajax_pipan.open("GET","pipan.php?red=" + document.getElementById("pilight_r").value + "&green=" + document.getElementById("pilight_g").value + "&blue=" + document.getElementById("pilight_b").value, true);
  else {
    led_stat = false;
    ajax_pipan.open("GET","pipan.php?red=0&green=0&blue=0", true);


function pipan_onkeypress (e) {

  if(e.keyCode == 97) servo_left();
  else if(e.keyCode == 119) servo_up();
  else if(e.keyCode == 100) servo_right();
  else if(e.keyCode == 115) servo_down();
  else if(e.keyCode == 102) led_switch();


function pipan_start () {

  pipan_mouse_x = null;
  pipan_mouse_y = null;
  pan_start = pan;
  tilt_start = tilt;
  document.body.addEventListener('touchmove', pipan_move, false)
  document.body.addEventListener('touchend', pipan_stop, false)
  touch = true;


function pipan_move (e) {

  var ev = e || window.event;

  if(pipan_mouse_x == null) {
    pipan_mouse_x = e.changedTouches[0].pageX;
    pipan_mouse_y = e.changedTouches[0].pageY;
  mouse_x = e.changedTouches[0].pageX;
  mouse_y = e.changedTouches[0].pageY;

  var pan_temp = pan_start + Math.round((mouse_x-pipan_mouse_x)/5);
  var tilt_temp = tilt_start + Math.round((pipan_mouse_y-mouse_y)/5);
  if(pan_temp > 200) pan_temp = 200;
  if(pan_temp < 0) pan_temp = 0;
  if(tilt_temp > 200) tilt_temp = 200;
  if(tilt_temp < 0) tilt_temp = 0;
  pan = pan_temp;
  tilt = tilt_temp;

function pipan_stop () {

  document.body.removeEventListener('touchmove', pipan_move, false)
  document.body.removeEventListener('touchend', pipan_stop, false)
  touch = false;

  if(pipan_mouse_x == null) led_switch();
  • Add the following line to "index.html" below "<script src="script.js"></script>":
<script src="pipan.js"></script>
  • Add the following lines to "index.html" below "<input id="halt_button" type="button">":
<input type="button" value="up" onclick="servo_up();"><br>
<input type="button" value="left" onclick="servo_left();">
<input type="button" value="down" onclick="servo_down();">
<input type="button" value="right" onclick="servo_right();">
  • Add the following lines to "index.html" below "<input id="halt_button" type="button">":
    R: <input type="text" size=4 id="pilight_r" value="255">
    G: <input type="text" size=4 id="pilight_g" value="255">
    B: <input type="text" size=4 id="pilight_b" value="255"><br>
    <input type="button" value="ON/OFF" onclick="led_switch();">

That's it. After rebooting your Pi, you should be able to control Pi-Pan with the new Buttons "Up", "Down", "Left" and "Right" or on the keyboard with "W", "S", "A" and "D". The Pi-Light can be controled in the settings-table or on the keyboard with "F". If you have a touch-device (Android or iOS), you can pan/tilt by dragging the preview-image around and change the Pi-Light by clicking on the preview. To change the minimum/maximum pan/tilt angles, edit the settings in /var/www/pipan.php.

Overlay/Composite two images together

One way to add an image over the top of another is with ImageMagick. I've constructed a crude example below, and it works! But it bogs down the refresh rate, drives the load level to 1.5 and is barely acceptable. I'd like to toggle it on/off with a new button, and improve it's performance, so please edit this article better.

/// derived from http://www.php.net/manual/en/imagick.compositeimage.php
/// requires 'sudo apt-get install php5-imagick' on RPi
/// backup /var/www/cam_pic.php, then replace it with this
/// goal is to put backup lines on reverse truck/trailer camera(s)

    $tlines = new Imagick('/var/www/lines/TrailerLines0deg.png');
    /// TrailerLines0deg.png is a image of the guidelines with a transparent background
    /// TODO: use steering wheel to select correct line graphic from library

    $camimg = new Imagick('/dev/shm/mjpeg/cam.jpg');
    $camimg->compositeImage($tlines, Imagick::COMPOSITE_DEFAULT, 70, 20);
    header("Content-Type: image/png");
    echo $camimg;