top of page

Raspberry Pi Webcam directions

Raspberry Pi Webcam, With RPi_Camera_Web_Interface & pi-pan

	THIS IS NOT A GOOD BEGINNERS PROJECT! There are a lot of steps to setting up the hardware and software. It is a lot easier with a working knowledge from simpler projects. You should be familiar with installing, configuring and updating the operating system. You should be familiar with using a terminal to input commands, also called “Command Line Interface”. You should be familiar with connecting your Pi to your local network, turning on SSH, and accessing it from a terminal on another computer. You can then copy commands from this document and paste them into the terminal, thereby running them on the PI without having to retype everything! There are 7 files put out previously to the club detailing setting up a Pi.

	Currently working on Pi 2B+, 3B+ and ZeroW. Not tested on 4.
		Pi-Pan and servos are optional.

Two cameras are available: Normal and NoIR. The normal camera has an IR filter.

	All Camera sensors are sensitive to IR. Typically an IR filter is installed internally to improve color rendition and reduce fogging from IR interference. The NoIR camera does not have this filter. Images in lower light levels will appear “Redish”, but it will typically perform better in low light and respond to heat sources. IR leds can be used as an “Invisible light source”. In sunlighted scenes, the NoIR camera will easily “Washout”.ON TO INSTALLING SOFTWARE!*********************************************************************************

	Install Raspbian Buster. Not Lite, Not Full!  You may want to add an external HDD, move the root filesystem to it, and create a data partition for the video. That can be done before installing the camera software, or after the camera is up and running.

	On booting, Buster will automatically expand the file system and walk you through setting up password, locality, wifi, and software update. Will reboot.

Run config program to set hostname, turn on camera and ssh interfaces, expand graphics mem to 128. Will reboot.

  
	Configure fixed IP (Preferred to using DHCP).
Right click the network icon in the upper right and click “Wired and Wireless Network Settings” You can select eth0 and / or wlan0 and set the IP address and router IP. Select ssid and your wifi signal and set the IP’s there too. 
Reboot to apply changes.


    Make sure software is updated. If it didn’t update on the initialization… 	Click on the terminal icon, upper left.

	sudo apt-get update
	sudo apt-get upgrade 
	sudo apt-get autoremove
	sudo reboot 

	You should now have your pi up and running, software up to date, and a known fixed IP address.

	To make life easier, you can open a terminal on the computer you are using to read this. Enter ssh pi@(the IP of the camera) You should now be remotely logged into the pi. You can copy commands from here and paste them to the terminal to execute them on the pi. (be sure SSH is turned on in the Pi Config.

*******************************************************************************
set up config file

cd /boot     
sudo nano config.txt
  add start_file=start_x.elf and fixup_file=fixup_x.dat below gpu_mem=128  
      (2 lines)  save and exit 
  sudo reboot


********************************************************************************

Install Camera and test camera software 

  
  python
  >>>import picamera      >>> is the prompt, just add import picamera
  >>>camera = picamera.PiCamera()
  >>>camera.start_preview()   test camera, image appears on pi display
  
  Ctrl D - terminate python program
  raspivid -t 0    #also outputs raw video to screen Ctrl c to stop
  
You now have a camera working on the pi.


Install RPi-Cam_Web-Interface

	  (More info at   http://elinux.org/RPi-Cam-Web-Interface)
    cd /home/pi	  
    sudo apt-get update
    sudo apt-get dist-upgrade
    sudo git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git
    cd RPi_Cam_Web_Interface
    run installer program  *****For preBuster OS you will need a different version of      raspimjpeg in the bin folder. Rename raspimjpeg to raspimjpeg-buster and raspimjpeg-stretch to raspimjpeg before installing.******


    sudo ./install.sh    on menu, select html yes apache 80 ok 
            do not run RPi_Cam_Web_Interface_installer.sh -DOESN'T WORK

		****** just tab to OK ******

    Open browser, go to 192.168.x.x,If you set manual ip, you know .x.x,              
      click on html link. Webpage with camera should come up.


      change owner of Rpi_Cam_Web_Interface. 
 
	cd ..  
	sudo chown -R pi:pi RPi_Cam_Web_Interface
      sudo reboot
      
You should now have a webcam producing video @ 192.168.x.x/html   .x.x being the address of your pi      
        If webpage comes up with small icon instead of video, go to: 
sudo raspi-config      use TAB to navigate to interfaces: and turn camera back on!

*************************************************************************

    Config at /etc/raspimjpeg  (Configured automatically by web page menus)
    Pictures stored at  /var/www/html/media


    For time lapse video.....
        Open up Camera Settings.
        Select Option - Full HD Timelapse (X30) 1080p 16:9, set video split (300
        seconds records 5 minute file segments) OK to save each change
        Open Edit Schedule Settings
        Change Management_Interval to 300 (5 minutes)
        Set PurgeSpace_ModeEX and PurgeSpace_Level  (Min space GB, 1)
        Save settings, then Stop and Start Scheduler to force update.
        Will now save 5 minute files with a 1 second framerate as a .mp4 and a 
        thumbnail as a .jpg
        
        
    
5 separate scripts are provided to do separate installation and maintenance functions.

The scripts are

    install.sh main installation as used above
    update.sh check for updates and then run main installation
    start.sh starts the software
    stop.sh stops the software
    remove.sh removes the software

    To run these scripts make sure you are in the RPi_Cam_Web_Interface folder then precede the script with a ./
    E.g. To update an existing installation ./update.sh
    E.g. To start the camera software ./start.sh
    E.g. To stop the camera software ./stop.sh
  
You have just created a “LAMP” server and installed a webpage to control and display your picam images remotely!

********************************************************************************

	If you want to use 2 radio control servos to pan and tilt the camera  
  		Connect servos to 40 pin header, pinouts below


Install pi-pan      

go to http://www.mindsensors.com/content/33-pi-pan-readme

download the latest version of pi-pan and unzip it. It will create a pi-pan    directory
  copy pi-pan directory to RpiB, /pi

  If using Linux, open a file manager and select file – connect to a remote server
	copy pi-pan directory to the webcam pi
  You can also turn off the pi and move the sd card or hdd to this computer to copy it over.

With the pi-pan directory copied to the Pi, from a terminal type:

  cd pi-pan
  sudo ./install-pi-pan.bash 
  sudo reboot
  ** test servos  Ctrl c to stop
  cd pi-pan
  python ./check-pan.py                  
  python ./check-tilt.py                 
  python ./neutral_servo.py              
  cd ..

  (pan tests port 23, tilt, port 24 and neutral centers the servos)

     ***Fix boot file that was overwritten

  cd /boot     
sudo nano config.txt
  add start_x=1  gpu_mem=128  start_file=start_x.elf  fixup_file=fixup_x.dat at bottom  (4 lines)  
  save and exit 

 
sudo reboot			


  Add the file "pipan_pipe.py" to /pi/pi-pan if not already there. To create it,

cd /home/pi/pi-pan
sudo nano pipan_pipe.py  copy and paste text below


#!/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/html/FIFO_pipan", 'r')
  line = pipein.readline()
  line_array = line.split(' ')
  if line_array[0] == "servo":
    p_servo.do_pan(int(line_array[1]))
    p_servo.do_tilt(int(line_array[2]))
  elif line_array[0] == "led":
    p_led.createPiLight(int(line_array[1]),int(line_array[2]),int(line_array[3]))
  pipein.close()


Save and close to create  pipan_pipe.py


  cd /
  cd var/www/html
  sudo mknod FIFO_pipan p
  sudo chmod 666 FIFO_pipan
  sudo chown 33:33 FIFO_pipan
  sudo nano /etc/rc.local     Edit "/etc/rc.local": add the following line above
                                                    the exit-command 

  python /home/pi/pi-pan/pipan_pipe.py &

      
  Go to /var/www/html and rename the file "pipan_off" to "pipan_on" and
                                        "pilight_off" to "pilight_on"

  cd /var/www/html
  sudo mv pipan_off pipan_on
  sudo mv pilight_off pilight_on

	The camera web page will now have pan and tilt arrows under the image

  modify pipan.php to change limits and step, etc.


  sudo reboot

******************************************************************************

** You may want to move the OS to an external USB drive if not done already **

******************************************************************************

  
So, now pointing your browser to the webcam page at 192.168.n.n (whatever you set) will put up your live video, configuration buttons, and 4 arrows to pan and tilt.

  **** when up and running, click arrows to move camera even if no servos are present. This generates pipan_bak.txt. Apache2 generates an error if this file is not present ******

  **** Camera Settings, Watchdog, defaults to 3 sec interval, 3 errors. Change to 0 sec to disable. *********

  **** Time Lapse needs to be scheduled to stop and start. Creates new series, tl_000n_..... Purge deletes whole series... tl_0001_... then tl_0002_... etc. *******
  
    
  R/C servos, Blk or Brn, Gnd  Red, +5v   White or Orange, control signal GPIO23 for pan, GPIO24 for tilt


	Your webcam is useable only through the same router. To access it from the outside world, you need to access the control page of your router. Router software differs, so you will have to play with it. Basically, you want to assign your webcam a static lease and register the host name. Then, forward some obscure port to port 80 of the webcam.
  You can now see your webcam at(your router’s IP):(The port number you picked)

 
			
			That's it. 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. To change the minimum/maximum pan/tilt angles, edit the settings in /var/www/html/pipan.php.

			Pi-Pan uses servoblaster to generate the signal for pwm. However, it might be that servoblaster will interfere with 3.5mm jack audio output. If so, edit /etc/init.d/servoblaster.sh and add the option --pcm to the servod-command on line 17.

			
			Check that you have connected servo pins correctly. cd pi-pan
			To check Pan servo, run following command:
			python ./check-pan.py

			To check Tilt servo, run following command:
			python ./check-tilt.py

			Try the demo program, run following command:
			python ./demo-pi-pan.py
			
			Servo Connections:
			
					    O O Red
					    O O Red
					    O O Blk
					    O O
					Blk O O
					    O O
					    O O
					    O O White
					    O O White
					    O O
					    O O
					    O O 
 
Time Lapse capture. *************

    Go to camera website. Click "Edit schedule settings"
    Go to PurgeSpace_ModeEX and select Min Space %, and 10. Save settings. Stop and Start Scheduler.
 
               
    Setting camera to Full HD Time Lapse, Video Split determines length of recording before starting new file. 
    Edit schedule settings to set Management Interval (In seconds, how often to evaluate purge), and purge parameters.  

****************************************************************************** 

If you did not move the OS to an external drive, saving video files will crash your SD card in several months. You can set up a separate partition on a usb drive for pictures. 

Setup usb drive for media
 
 Add drive for media:  format as ext4 and insert  will become sda1, unless you moved OS from SD card to usb drive.  Then it will probably be sda3.

Edit fstab to tell the OS where to mount the media drive.

Sudo nano etc/fstab 

	add the following line, correcting the location (sda1 or sda3)

    /dev/sda1 /var/www/html/media ext4 defaults 0 0 

    sudo reboot                                            
	 		#mount it
    sudo chown -R www-data:www-data /var/www/html/media     
			#make owner	/group www-  data:www-data
    sudo usermod -a -G www-data pi 
                      #add user pi to group www-data
    sudo chmod -R 771 /var/www/html/media 
                      #give pi permission to r w x
     
 	Your pictures are now saved on the separate partition you created.

************************************************************************
 
  Frequently Asked Questions Webcam interface (From  http://elinux.org/RPi-Cam-Web-Interface)
  
  
How do I change the path for the video images and pictures?   (/var/www/html/media/im  or /tl  or /vi plus template info)
There are separate path settings for video, images and time lapse in the /etc/raspimjpeg config file.

These allow changing the naming scheme of the files and separating out into sub-folders and even including date parameters for these sub-folders.

If you change the 'root' from the media folder in the web install then you also have to change that setting in the web side config.php.

Instead use standard linux link/bind (fstab) facilities to 'relocate these to onto other locations like USB sticks, disk drives and network storage locations. When you do that bear in mind the linked location must have read/write permissions for user www-data

I recommend using doing it that way and leaving the logical location root location as media under the web install as this avoids having to change the web side config and also means that if the software is upgraded then nothing else needs to change each time.

Is there any way to have the "UPTIME" displayed on the Web Cam Interface?
You can use the user annotation variable %a to put any other info in the annotation. The info is picked up from /dev/shm/mjpeg/user_annotate.txt each time the annotation is generated. So use a CRON job or something similar to update this file with a string representing UPTIME.

have you considered adding some additions to this program to monitor the temperature?
Adding a temperature sensor (e.g. DS18B20) should be reasonably straightforward. There are lots of articles on how to connect and read theses on the Pi. It is more a question of how to display it. One way that could be used even with the current software would be to use a program to regularly read the temperature and write it to /dev/shm/mjpeg/user_annotate.txt. The %a annotation variable can then be used to incorporate this in the video annotation. See wiki for the annotation variables.

How to I enable motion edition to start automatically after the Pi powers up?
Motion Detection can be controlled at start up in 2 basic ways.

1) You can edit the /etc/raspimjpeg main config file and change motion_detection from false to true
2) You can configure the scheduler so that it has a period start command of md 1. The Period Start command will be executed when the raspberry starts up.

Method 2 is better in my view as it allows control if you have set up the Scheduler to have different periods. E.g. in Sun based mode you could choose to have motion detection only active during the Day (md 1 in Day Period Start and md 0 in other Period Starts). When the Raspberry boots then it will turn on motion detection if it starts in a Day period otherwise it would wait for the next Day period to start. If you don't want to split the Day up then you use All Day mode and put a md 1 in its one Period Start entry so the Raspberry always goes into motion detection no matter when it boots up.



Adding a temperature sensor (e.g. DS18B20) should be reasonably straightforward. There are lots of articles on how to connect and read theses on the Pi. It is more a question of how to display it. One way that could be used even with the current software would be to use a program to regularly read the temperature and write it to /dev/shm/mjpeg/user_annotate.txt. The %a annotation variable can then be used to incorporate this in the video annotation. See wiki for the annotation variables.


**************************** UPDATE **********************************************

It appears that DHCP has been updated and improved (Broken!!!!)
If you use the networking dialog box to set up a static IP address, it will do it, and dhcp will ignore it and ask for an address approval 8 times a second. 

sudo nano /etc/dhcpcd.conf

modify the entry to:   (change for your IP)

interface wlan0
static ip_address=192.168.0.n/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

SSID (Your network ssid)
static ip_address=192.168.0.n/24
static routers=192.168.0.1
noipv6
static domain_name_servers=192.168.0.1


The website uses a smaller line and some commands were continued on the next line. Here is the file I wrote and posted above.

RaspiBusterWebcamV2
.pdf
Download PDF • 55KB

Jim K2BHM


2 views0 comments

Recent Posts

See All

The Raspberry Pi is a small handy computer. Lots of programs are available to do all kinds of things. It's easy to use the mouse and keyboard to bring up the program you need and you're off and runnin

bottom of page