Raspberry Pi Zero 2 W Webcam

Raspberry Pi Zero 2 W with Raspberry Pi HQ Camera

I bought a Raspberry Pi model B revision 2.0 nine years ago and I’ve been using it with various cameras as a webcam server pointed out my window in places that I’ve lived. (The window where I have the camera now gets direct sun on sunny days, causing a lot of lens flare.)

A couple of weeks ago, I found a Raspberry Pi Zero W 2 in stock at Chicago Electronic Distributors, so I decided to switch my webcam from the Pi Model B to a Pi Zero 2 W. In the last iteration of my webcam, I was using Motion with the Raspberry Pi HQ Camera and the 6mm wide angle lens. With the Pi Zero, you also need the Pi Zero Camera Cable because of the smaller the CSI-2 camera connector.

On my Pi Model B, Motion had been crashing frequently with the errors, “unexpected camera control callback event, 0x4f525245” and “motion_watchdog: Thread 1 – Watchdog timeout Trying to do a graceful restart.” I was never able to sort out the problem after spending more time on it (here and here) than I wanted.

With the new Raspberry Pi Zero W 2, I had initially tried installing the just released Raspberry Pi OS, Bullseye, but I saw that other people were having some problems with Motion and Bullseye, so I installed Buster 10.11 instead.

I used the Raspberry Pi Imager app to setup the micro SD card. One thing that I had forgotten with the imager app was that using Ctrl-Shift-x will bring up an Advanced options dialog box where you can set the hostname, enable SSH, configure WiFi and locale settings. This makes it easier to do a headless setup. After I found the Pi Zero’s IP address on my network (by looking at the client list on my Asus router) I gave it a static IP address then used these great Circuit Basics instructions to setup Motion.

I also installed Apache Web server using the command:

sudo apt install apache2 -y

With the Pi Model B, I had the webcam’s address on a subdomain of my site. I used Dynamic DNS (on Google Domains, my registrar) to redirect to the IP address that my Pi was running on. Because I wanted to enable HTTPS on the camera URL, I got a certificate from Let’s Encrypt, so the URL, pizero.lesliewong.us:8081, was able to use the HTTPS protocol. IIRC, on the Model B, I was able to edit /etc/motion/motion.conf and add the lines:

stream_tls on
webcontrol_tls on
webcontrol_key /path to…/privekey.pem
webcontrol_cert /path to…/fullchain.pem

to enable HTTPS streaming. When I did that on my new setup, it didn’t work because the version of Motion that I installed wasn’t compiled with SSL/TLS support, which I found when I ran the command:

sudo systemctl status motion

After spending a lot of time trying to get that to work and failing using stream_tls, I found a way using the Apache Module mod_proxy:

sudo a2enmod proxy
sudo a2enmod proxy_http

I also added two lines to the file /etc/apache2/sites-enabled/000-default-le-ssl.conf (where /webcam is the URL on the domain, e.g., domain.com/webcam):

ProxyPass /webcam http://xxx.xxx.xxx.xxx:8081/
ProxyPassReverse /webcam http:// xxx.xxx.xxx.xxx:8081/

I was sort of surprised when it worked.

Updating the Raspberry Pi Webcam

Update (January 25, 2021) My Raspberry Pi powered webcam is now showing the Jordan Park Neighborhood in San Francisco, California. The Raspberry Pi Model B, Rev 2 is using the Raspberry Pi High Quality Camera (with the Raspberry Pi HQ Camera Lens – 6mm Wide Angle) and is running Motion (pi_buster_motion_4.3.2-1_armhf). Details to follow.

(Original post) I built a streaming webcam showing the view from my downtown Oakland apartment in November 2012. I used a Raspberry Pi (Model B Revision 1.0) and the Raspian Wheezy (2012-10-28) image. For the webcam/web server software, I used MJPG-streamer. The problem with MJPG-streamer is that if the images/stream is available on the internet, then anyone can access the software.

Recently my Raspberry Pi began to lose the WiFi connection. In researching various solutions, I disabled WiFi power management on the Edimax EW-7811Un nano-size USB WiFi adapter. That didn’t solve the problem. I switched to Ethernet but the connection still dropped.

I found a nice script for “Rebooting the Raspberry Pi when it loses wireless connection“. It’s a script that runs in crontab and checks for a network connection by pinging a local address. If the ping fails, it restarts wlan0.

The checkwifi.sh script worked great, but then I had an issue with the video stream image oscillating. The image would continually go between light and dark and the only way to stop it was to restart MJPG-streamer.

I decided to update to the current Raspbian Jessie with Pixel and some other webcam software. The Motion-Project seemed not to difficult – I could use it as a streaming webcam. I decided to use Motion 4.0 and Kenneth Lavrsen’s Motion 4.0 Guide to help set it up. I don’t know if the image oscillation is an issue caused by using the Creative Live! Cam Chat HD (auto exposure?) but this current setup seems to have resolved the issue.

Raspberry Pi Webcam

 Raspberry Pi Webcam

I’ve had a webcam serving live images using WebCam2000 running on my Macbook. I recently bought a new webcam, a Creative Live! Cam Chat HD (it was cheaper, $25, when I bought it August 2012). The previous webcam I was using, a Vivicam 3350B (I bought it on Woot! for $8.50 in 2005), didn’t have a Windows 7 driver and I was using Windows XP Mode and Windows Virtual PC – it was a little cumbersome.

When the Raspberry Pi was released, I thought it would be a great webcam server. I ordered one from Allied Electronics last July, but they never had stock. When Adafruit started selling them, I bought one.

After I acquired a compatible USB keyboard and dug up an Intellimouse Explorer 3.0, I started out with the 2012-09-18-wheezy-raspbian image. Googling found a Romanian site, BobTech, with an excellent tutorial for setting up a Raspberry Pi streaming webcam (English Google translation). Basically, the instructions just worked.

Later, I bought an Edimax EW-7811Un USB Adapter and with the current 2012-10-28-wheezy-raspbian image, WiFi was even easier to setup. The only change I made in the network setup was to give the Raspberry Pi a static IP address so I could find it. I edited wlan0 section in /etc/network/interfaces:

iface wlan0 inet static

address 192.168.1.115

netmask 255.255.255.0

gateway 192.168.1.1

nameserver 208.67.222.222

wpa-ssid Skynet

wpa-psk xxxxxxxxxxxxxx

I made a change to have MJPG-streamer serve a single image instead of a stream. Instead of using http://raspberrypi:8080/?action=stream, I used http://raspberrypi:8080/?action=snapshot. I use a script to reload the page every 10 seconds.  In mjpg-streamer.sh, I set FRAME_RATE=”10″ You really don’t see much action unless it’s windy.

Once the Raspberry Pi was setup, I used puTTY to connect to it, so I can start mjpg-streamer and do other things without having the Raspberry Pi connected to a monitor.