Raspberry Zero Cam With Webserver
by joejackson2003 in Circuits > Raspberry Pi
81 Views, 0 Favorites, 0 Comments
Raspberry Zero Cam With Webserver
This is a simple project to turn a Raspberry Zero 2 W with an attached camera (either the basic Pi Zero Cam or an infrared camera) into an internet accessible live web camera. In essence, it combines a Python script and Flask server to provide a webserver, streaming the images from the attached to camera, for access either within a local network or access from anywhere else through the internet.
Supplies
For the UK, I've included links for ordering from ThePiHut or Pimoroni.
- Raspberry Zero 2 W (alternatively, the Zero W works ok but has less CPU power and is slower in booting up). Get it here or here. You will also need a micro SD card and good 5V power supply with micro USB plug.
- Camera: either original Zero Cam, also comes as noIR version (without IR filter, for night vision), or the really good combined noIR module combined with built-in IR illuminators (get it here).
- Enclosure is optional. Still working on a 3D model.
Prepare Rasperry Zero 2 W
I would recommend either the official documentation or multiple other sources on the web and YouTube.
There are tons of variants of Raspbian. The current working installation I have is the 32bit version of Raspbian Bookworm (Kernel Linux raspberrypi 6.6.51+rpt-rpi-v7). This was after I wasted half a day with the 64bit version, which essentially made it impossible to detect the camera and use the required Libcamera libraries.
Avoid the 64bit Raspbian version.
Install Dependencies
Your Raspbian distribution will likely already have Python3, so one of these may be superfluous.
1. You will need to install various libraries, especially libcamera.
2. Installing Flask server in a virtual environment to avoid library conflicts.
Set Up Files and Directories, and Run Script.
Only two files are required.
- Put cam3.py in your home directory (if not already in /home/pi, change with cd ~)
- Create templates subdirectory within your home folder
- Put index.html into the templates directory.
- To run script: python3 cam3.py
View Stream. Optional External Web Access Setup
Find your Raspberry Zero W's IP address:
The stream can be access from your local network through a browser, by entering the Raspberry's IP and port 8082, i.e. 192.168.1.158:8082
The HTML currently limits the stream to 30 seconds, after which you can manually resume for another 30 seconds. I added this as I found that I can unintentionally access the stream for a long time, and if on a 4G network, you might use up your data allowance very quickly! For some reason, the Android Chrome browser seems to continue streaming even if it is in the background. Change the JavaScript in the HTML accordingly if you don't need this limit.
External access
Your Raspberry is likely connected to the internet through a router.
- You will need to set up a rule in router's configuration to direct any requests through the internet to your Raspberry. An example for the BT SmartHub is shown in the image at the top of this section. Other routers will likely have the appropriate settings under Firewall or Network Allocation Table settings. Decide on an external port number that you will need later to access the server. In this example I'm using 8084.
- Find out your router's IP address (you can visit whatsmyip.org, for example). If your broadband connection changes occasionally, you might want to consider setting up a dynamic DNS server, so you can access your home network externally, even if the IP has changed (noIP for example).
- You can access the video stream externally through your router's IP address plus the external port number (see step 1) that you have set up in your router, e.g. open your browser at 109.159.2.111:8084
External access issues and considerations
- If you have a domain set up that points to your home network, for example another Apache server that you are running behind your router, check that your Domain provider does not filter out ports. If you try to access your video stream by going through your domain, e.g. mydomain.org:8084 and you cannot reach your video stream, it's possible that your Domain provider has removed the port and your request arrives on the default port 80.
- If your Domain provider enforces encryption, you might be unable to access your video stream, as setting up SSL for Flask is quite complicated (but can be done). In this case, you must have your router's IP address and go directly via IP + port number.
A custom 3D case can be found here.