By Russell Barnes. Posted

Explore the underwater world with a Raspberry Pi camera

There are plenty of underwater sports cameras available, but they can be quite expensive, especially if you want to control them remotely. In this tutorial we’re going to use readily available Pi add-ons to make a cheaper, customisable camera unit. There are lots of options and alternative sources of components for a project like this. For example, the Pimoroni Enviro pHAT is a really useful option that can report back information about the environment in which the camera is operating, especially how much light is available. There’s a fair bit of software configuration involved, but example config files are in the GitHub repo for this article.

You'll need

STEP-01 Find a suitable container

This needs to be watertight and have at least a see-through lid. You can find Tupperware boxes with a very tight seal, but these tend to be translucent rather than transparent. The size of box will probably determine your choice of Pi and power source. Zeros are great as they’re so small, but then you’ll need a WiFi dongle and shim. You can also save space by using a LiPo battery instead of a power bank, although you’ll need a boost regulator too, such as the Pimoroni Zero LiPo.

STEP-02 Configure your Pi to be a WiFi access point

Start from a fresh Raspbian Jessie Lite SD card and install the following:

sudo apt-get update
sudo apt-get install -y dnsmasq hostapd python3 python3-dev python3-flask python3-picamera

First, configure your wireless interface to have a static IP address by editing /etc/network/interfaces. Then set it to not use DHCP by adding this line:

denyinterfaces wlan0

…to the end of your /etc/dhcpcd.conf file. Next, create the /etc/hostapd/hostapd.conf file, using the example in this tutorial’s GitHub repository as a template. Change the interface, ssid, and passphrase parameters as needed. Finally, edit /etc/dnsmasq.conf, ensuring that the IP addresses are consistent with your settings in /etc/network/interfaces. Then reboot!

STEP-03 Add the Enviro pHAT

You have the option of soldering this board directly onto the Pi’s GPIO pins, or you can use the supplied female header if you want to reuse it in other projects. After that, install the Python library and dependencies using the following command:

curl -sS | bash

The library comes with some example programs and you should run these to test that everything is working correctly.

STEP-04 Fit everything into your container

To cut down on reflections and get the best possible images, the camera should be as close to the transparent side of your container as possible. The ZeroView from the Pi Hut is a clever mounting plate that uses suction cups and will also hold your Pi securely. Alternatively, you could make a mount out of cardboard and glue this to the inside of the container. Velcro tape can be a good solution for power sources (which normally need to be removable for recharging).

 Find bits from around the house to finish the box

STEP-05 Add some code, HTML and CSS

Clone the entire Flask folder from the project repository onto your Pi. Flask is a small web framework written in Python which allows you to create simple web services; in this case, it’s a webpage that allows us to see data from the Enviro pHAT and the latest captured images. We can also switch between recording modes (movie or continuous still frames) or take photos on demand. This control of the camera is achieved via the excellent Python picamera library. You could enhance the project by adding additional exposure and shutter speed controls to your interface if you want.

STEP-06 Set the code to run at boot

To set the AquaPiCam program to run when the Pi boots up, add this line to your /etc/rc.local file, immediately above the exit 0 line:

python3 /home/pi/Flask/ &

It’s also a good idea to configure the Pi to only boot to the command line, using:

sudo raspi-config

…and selecting ‘console’ under option 3.

Now go and find somewhere wet! You might want to run a few tests in the bath before venturing further afield!

 Find some crab friends of your own

From The MagPi store