Watch your fish with a Raspberry Pi camera
Remote video monitoring is easy with Raspberry Pi. On this #MagPiMonday, PJ Evans shows you how to use a waterproof camera case to get a unique view of some aquatic friends.
This tutorial was inspired by the Entaniya waterproof case for the Raspberry Pi Camera Module. This case protects the Camera Module without distorting the image. With it, we can submerge a camera into the depths of an aquarium and keep an eye on our fishy pals by streaming video. If that doesn’t appeal, you can adapt this tutorial to be able to monitor a video stream of anything you like, whether it be indoors or outdoors. Combined with Home Assistant, you can soon be monitoring your cameras from anywhere in the world.
01. Prepare you Raspberry Pi
For the best picture quality, a Raspberry Pi 4 is the perfect choice. That said, you’ll get good results from a Raspberry Pi Zero 2 W as well, and it’ll be easier to install. Whichever you choose, we recommend using the Raspberry Pi Lite (Legacy) OS. The project uses the raspivid tool, support for which is limited in the latest version of the OS. The Legacy ‘Buster’ image still has full compatibility. You can find it in Raspberry Pi Imager under ‘Raspberry Pi OS (Other)’. Set up Wi-Fi, and make sure everything is up-to-date with
sudo apt -y update && sudo apt -y upgrade.
02. Install and configure your camera
With your Raspberry Pi disconnected from power, install the ribbon cable and Camera Module (we’ll get to the case later). The cable will work either way around; however, the blue side (the one without the exposed contacts) needs to be against the black clip at each end. Find the connector on your Raspberry Pi marked ‘CAMERA’, raise the clip, gently insert the cable, then press the clip down again. Repeat for the Camera Module. Power up your Raspberry Pi and, at the command line, run
sudo raspi-config. Go to ‘Interfaces’, then ‘Camera’, and choose ‘Enable’. You’ll then need
03. Test your setup
We’ve set up the camera first to check everything is working before we put it in a waterproof case. After the reboot, run the following command:
All being well, you should see video from the camera on-screen for five seconds. If you don’t, check if the cable is inserted correctly, and that you’ve enabled the camera, as in Step 2. Now check the orientation. With the cable pointing up from the module, is the image the right way up? If not, you can switch it using this command:
v4l2-ctl --set-ctrl horizontal_flip=1
Finally, check everything is in focus and adjust the camera accordingly.
04. Install dependencies
We’re going to be using a real-time video streaming server to watch our fish (or whatever you’ve decided). This requires some supporting software so we can build and run it, so now is the time to get everything ready. Make sure you have run
sudo apt -y update && sudo apt -y upgrade, as in Step 1. Enter the following command:
sudo apt install cmake liblog4cpp5-dev libv4l-dev git
This will install everything you need.
05. Build the video streaming server
Unfortunately, the streaming server is not available on APT, so we have to build it ourselves. This is straightforward, providing you enter the commands below carefully and in order. From the command line, enter the following:
cd git clone https://github.com/mpromonet/v4l2rtspserver.gitcd v4l2rtspserver cmake . make sudo make install
This will download the source code, prepare a configuration for Raspberry Pi, and then compile the software and install it. Once installed, you can then delete the v4l2rtspserver directory if you wish.
06. Test your video stream
Before we start filming the fish, let’s test if the video stream itself is working. On another computer, install VLC (videolan.org). This is a multi-purpose video playback application that supports the server we are using. From your Raspberry Pi, run this command:
v4l2rtspserver -W 640 -H 480 -F 15 -P 8554 /dev/video0
You should see some text output on the screen. On the other computer, open VLC, click on ‘File’ then ‘Open network’. In the URL box, enter:
<ip-address> with the IP address of your Raspberry Pi (use
ip addr to find it). Click ‘Open’ and wait a few seconds. You will see a stream from the camera.
07. Start at boot
Now we have a working video stream, we need to make sure it always starts at boot time. A service file already exists for the server; we just need to change one line. Open the file with:
sudo nano /lib/systemd/system/ v4l2rtspserver.service
Find the line starting with ‘ExecStart’ and change it so it reads as follows:
ExecStart=/usr/local/bin/v4l2rtspserver -W 640 -H 480 -F 15 -P 8554 /dev/video0
Check it’s exactly as written here, then use CTRL+X followed by Y and ENTER to save the file and close the editor. Now enable the service:
sudo systemctl enable v4l2rtspserver
To test it, reboot, and then use VLC again to open the stream.
08. Mount the camera in the case
Let’s turn our attention to the waterproof case. Following the included instructions, separate the base from the outer cover. Now open the supplied desiccant, and place both leaves in the centre of the base in the space provided. If any moisture gets in, these will absorb it, protecting your camera. Screw in the adapter mount for the camera, then carefully thread the connected ribbon cable through the slot. Finally, mount the camera to the adapter, being careful not to over-tighten the screws. Make sure the cable is fitted correctly.
09. Install the camera cover
Locate the rubber O-ring and place it on the base so it sits in the ridge provided. Take the clear cover and place it over the base, and secure it with the M3 nuts and bolts. We recommend going across each one rather than around the base; it will ensure a more even fit. Again, be careful not to over-tighten the screws. Snap the outer cover over the clear cover. Take the small rubber grommet and, very carefully, feed the ribbon cable through, threading it through the grommet until it meets the base. Insert in the slot and secure with the metal plate. You now have a waterproof case.
10. Mount your waterproof camera
What happens next will require some initiative. Depending on what it is you want to monitor, this determines how you will set things up. For our aquarium project, you can either attach some rubber ‘suckers’ to keep the camera in place, or find a suitable place to rest it on the substrate or sand. For outdoor use, Entaniya sells a wall-mount adapter to make things easier. Either way, you still need to consider how to get the ribbon cable inside or out of the tank and to a safely installed Raspberry Pi. Your biggest restriction is the length of the cable itself.
11. Add to Home Assistant
One of the best ways of utilising this monitoring solution is Home Assistant, the popular home automation operating system. Luckily, support for a remote camera like this is baked right into the service. To add the stream in, you’ll need to be able to access and edit the configuration.yaml file. Add the following text:
camera: - platform: ffmpeg name: FishCam input: rtsp://<ip-address>:8554/unicast
As before, change
<ip-address> to your Raspberry Pi’s address. Now restart Home Assistant. In your dashboard you can now add a ‘Picture Glance’ card using the newly created ‘FishCam’ entity. Now your fish can be viewed wherever you can access Home Assistant.
12. Next steps
There are many ways you can build on this project. If you’ve installed a Fish Cam in your aquarium, there’s lots more to do. Monitor the aquarium temperature with a 1-Wire temperature sensor and send alarms when things go awry. Or, use a flow sensor to keep an eye on the pump. If your interest lies more with remote monitoring, try adding presence detection or facial recognition to Home Assistant alongside the video feeds. Motion detection can be used to trigger recording or alerts. Outdoor cameras can be used to keep an eye on your backyard chickens or who’s at the front door. As ever, it’s over to you.
The author would like to acknowledge siytek.com, whose excellent video streaming tutorial informed this piece.
Jump to the comment form
This is a great project. I have a suggestion for optimization. Streaming server setup can be made more effortless and sophisticated. Look for the open-source solution, datarhei Restreamer, designed to work seamlessly with Raspberry Pi devices. Enjoy a streamlined, user-friendly interface while having a complete streaming server. Link to GitHub Repo https://github.com/datarhei/restreamer
Surprised by the use of Buster OS , and raspivid now its been replaced by libcamera.
Raspberry Pi Staff Liz Upton
IDK – I’ve got a very large number of Pis at home, and a lot of those are running stuff I put on them some years ago and haven’t felt the need to upgrade or update. And seriously; if someone’s happy with a library, and doesn’t need any new features, I don’t see that they’d feel the need to upgrade or update there either.
Agreed. Where are all the successful projects using the new framework? This was my biggest disappointment as a first time pi user – latest OS (for all intents and purposes) broke the camera. I wasted so many hours trying to get new or legacy working.
I’ve been impressed with https://github.com/ayufan/camera-streamer if you want to do this with bullseye and libcamera (for instance to use the new V3 camera).
One way to overcome the positive buoyancy would be to fill the housing with baby oil like they they have done with mother boards. I use this method to submerge 12v led lights in jars and the results are good.
Needs a courageous tester to see what happens with a camera!
Raspberry Pi Staff Ashley Whittaker — post author
If in doubt, add baby oil. Life lessons.
Raspberry Pi Staff Liz Upton
We must have been listening to a different podcast.
Raspberry Pi Staff Ashley Whittaker — post author
Took me a while, but I get it now.
Filling the housing with oil may improve the clarity of the image due to the refractive index. Try looking through a bottle of baby oil in the open and then doing the same when the bottle is in a glass jug.
Any water that gets into the housing goes straight to the bottom, away from the components. Another advantage!
I’ll post some photos….led lights/oil/fish tank…sometime soon.