Simple setup Raspberry Pi as Webcam
I’m owner of 256mb version Raspberry PI. Recently I’ve bought a camera module. Here is my tutorial how to make a web camera.
The idea is simple. We will capture images every 5 seconds and will share them via a web interface. For capturing we will use the raspistill
console utility which is provided with RASPBIAN as out-of-box.
Setup RAM drive for storing captured images
Since any SD card has limit resources for writing cycles we will use ram drive as temporary storage drive. Let’s create a ram drive. First make a folder for it:
Add following string to fstab to make sure a ram drive is created on an every system boot
Now mount it to a system:
Check if it’s mounted:
Create bash script to capture images
We will create a script which will be launched by the cron daemon every minute. 1 minute is not enough we need refresh image every 5 secs. The solution is run raspistill
12 times as one cron task (60/12 = 5 sec). Furthermore this script will work with 2 image files. One image for publishing it via web daemon, another is kind of “double buffer” file. Also timestamp will be added to images.
Install imagemagick
which is responsable for this work.
Open nano editor and copy/past the script:
Make it executable:
chmod +x cam.sh
Install web daemon
We will use web to have access to our camera. Since rasp pi is small device with lack of resources we need to make sure we use best software which give us best performance. According to this comparison the best web daemon is Monkey. Let’s install it.
Add source repo to /etc/apt/sources.list
file:
Now install and config Monkey:
After installation the monkey daemon should be up. Open browser and type <your pi address>:2001
you should see default monkey welcome page. The default place for web site is in /usr/share/monkey
. Go there and remove everything. Now make sym link to ram drive image.
By default monkey doesn’t support sym links you have to enable it. Open /etc/monkey/monkey.conf
and enable sym links:
Restart daemon:
Web page
Let’s create web page which will refresh image every 5 secs.
index.html
main.css
script.js
Check <your pi address>:2001
you should see updatable pictures from pi.
Conclusion
This is not a best solution for “streaming” pi’s pictures online. I came across better solutions on internet. However, this article has helpful tricks I may need in future.