Control the camera on Raspberry Pi

In this post we will look at a few different use cases in which we want to control the Raspberry Pi camera. We also look at how to transfer photos from the Pi to your computer or a server.

Helping us is, which has all the modules we need for making this work.

You will see in later tutorials that we will work with containers to deploy functionality onto our Pi, but for more simpler tasks we will only use modules. Modules are far more lightweight and work on any Pi remotely often without any prior installation or additional dependencies.

We will do everything over SSH, assuming it is a headless Pi. You could however install and use directly on your Pi, if you happen to be working on the device itself


curl | sudo sh

1. Install camera packages

If you have Raspbian or HypriotOS on your Pi you likely already have the camera programs raspistill and raspivideo. Those programs are required in order to run the camera operations listed below

2. Enable the camera

Assuming you have already attached the camera to the Pi, we now need to enable it in the OS.

This step only applies to Raspbian since the camera is enabled by default in HypriotOS.

Run the following command, navigate to enable camera and reboot on exit.

space -m ssh /wrap/ -e SSHHOST=IP -e SSHUSER=pi -m /config/ -s sudo

3. Taking a picture

Using and stacking two modules onto each other, we will:
1. Connect to the Pi over SSH either using the local network or Space Agent
2. Take a photo with the camera
3. Store the jpeg to disk

space -m ssh /wrap/ -e SSHHOST= -e SSHUSER=pi \
        -m /still/ -e file=- > img.jpg

Using -e file=- means that raspistill will stream the photo to stdout. That is why we capture the output using the redirection to the file img.jpg.

If we had set -e file=img.jpg then the photo file would have been saved onto the Pi storage instead of getting streamed over SSH.

4. Streaming video from the Pi to your computer

Now we’ll do something really cool: we will stream live video from the Pi’s camera through SSH and watch it live.
In this example we’ll use mplayer to load and watch the video stream.

space -m ssh /wrap/ -e SSHHOST= -e SSHUSER=pi \
        -m /video_mplayer/ -e file=-

This will automatically start mplayer on your computer. To use a different video player, run:

space -m ssh /wrap/ -e SSHHOST= -e SSHUSER=pi \
        -m /video/ -e file=- | myvideoplayer

If we had set -e file=video.h264 then the video stream would have been saved onto the Pi instead of getting streamed over SSH.

Tip: you could pipe through ‘tee’ to save to file and watch it live at the same time.

5. Troubleshooting

Pi freezing or rebooting

We have noticed on a few occasions that when activating the camera, before taking a still or video, the Pi could hang or even reboot.
This seems to happen when the power source for the Pi is insufficient, such as when powering it from an USB port or from a battery.
Plugging it into the wall or using a larger battery (1A+ output) seems to solve that problem.