Last Updated: 2019-11-10

Sound of the Bees

This work is a collaborative project between Cardiff University and Bridgend College. We are grateful to the Wellcome Trust for their financial contributions to the project and to Llanishen Fach Primary School for allowing us to trial and test the system.

Activity Overview

Students form a circle or a line and each one is given a piece of A4 paper with a line of Python code on it.

Within the code, there will be a spelling error, a variable definition, an if statement and a loop. The tutor should discuss these various programming constructs as the students move around the circle.

print('Boom!')
jump()
prin('Test') #Note deliberate spelling error!
if <next person is wearing glasses>:
for n in range(1,3):

Key Concepts:

Downloads:

Download Coding Circle pack

Activity Overview

During this section of the class, students will assemble their Raspberry Pis and login. There are some excellent ‘Getting Started' resources provided by the Raspberry Pi foundation which can be found below:

Getting Started with Raspberry Pi

You might want to try out some physical computing activities first, to get your students familiar with the concepts of wiring a breadboard, a button, an LED and so on. You can find some very thorough tutorials below, for both Scratch (beginner) and Python (more advanced).

Physical Computing with Scratch

Physical Computing with Python

Key Concepts:

Hardware resources:

Activity Overview

Step 1:

Before getting started with the camera module, we will have to make sure it is enabled via the config page. To do this, you'll need to open a Raspberry Pi terminal. You can do that by following the instructions here.

sudo raspi-config

Step 2:

Once the camera module is connected, you may want to follow some of the recommended instructions from the Raspberry Pi foundation, available below:

Getting Started with the Camera Module

Notes:

The code generally reduces to the following two commands:

raspistill -o picture.jpg

and

raspivid -o video.h264 -t 10000

The first command takes a picture and the second takes a 10 second (10000 milisecond) video and stores them on the Pi.

Step 3:

To record audio with the Pi, you can use the following command.

arecord -D plughw:1,0 -d 3 audio.wav

This records 3 seconds of audio and stores it to the Pi. It is recommended to follow the instructions below to check each part of this command is correct for your system.

Getting Started with a USB microphone

Common Camera Error:

mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
mmal: camera component could not be enabled
mmal: main: Failed to create camera component
mmal: Failed to run camera app. Please check for firmware updates

If this happens, first check that the camera module is enabled with step 1 and reboot the Pi.

It is possible that the camera connection wire is faulty if this error persists. Unplug the camera module, ensure it is in the correct way around and try with a different camera module or Raspberry Pi.

There is further information below about troubleshooting this error:

https://raspberrypi.stackexchange.com/questions/13764/what-causes-enospc-error-when-using-the-raspberry-pi-camera-module

Key Concepts:

Hardware resources:

Activity Overview

So far, we have assembled the hardware and tested the action of the button using Scatch (or Python) to light an LED and tested our camera module with audio. This section looks to bring all of this together into one command that can be run outside at the hive.

We need to do a few things to make this possible.

  1. Write some code that says ‘if the button is pressed, start the recording' . This will embed many of the concepts from the Coding Circle activity.
  2. Activate the code to run when the Pi starts so that we don't have to do anything with it outside at the hive. This will let us just plug it into a battery pack, press the button and start the recording.
  3. Combine the video and audio streams (Optional - not tested).

Step 1 (Starting the recording when the button is pressed):

Taking a picture with Python:

Following on from the Building the Hardware section, we can use Python to trigger the camera to take a photo when a button is pressed. This is covered by the following code:

from gpiozero import Button
from picamera import PiCamera
from datetime import datetime
from signal import pause
import os #required for audio stream

button = Button(2)
camera = PiCamera()

def capture():
    timestamp = datetime.now().isoformat()
    camera.capture('/home/pi/%s.jpg' % timestamp)

button.when_pressed = capture

pause()

This code will store the output file as a jpg in the /home/pi/ directory on the Pi. Note that we are labelling the image as the date and time to avoid overwriting, as discussed in Building the Hardware.

Taking a video with Python:

Capturing a video to a file is similar and can be done in Python as follows:

from gpiozero import Button
from picamera import PiCamera
from datetime import datetime
from signal import pause
import os #required for audio stream

button = Button(2)
camera = PiCamera()
seconds = 60 #you can change this number

def capture():
    timestamp = datetime.now().isoformat()
    camera.resolution = (640, 480)
    camera.start_recording('/home/pi/%s.h264' % timestamp)
    camera.wait_recording(seconds)
    camera.stop_recording()

button.when_pressed = capture

pause()

This captures 60 seconds of footage and stores it in the /home/pi/ directory on the Pi.

Taking a video and recording audio with Python:

You can add the audio stream by adding a line as follows:

from gpiozero import Button
from picamera import PiCamera
from datetime import datetime
from signal import pause
import os #required for audio stream

button = Button(2)
camera = PiCamera()
seconds = 60 #if you change this, change the 60 in the audio command

def capture():
    timestamp = datetime.now().isoformat()
    camera.resolution = (640, 480)
    camera.start_recording('/home/pi/%s.h264' % timestamp)
    camera.wait_recording(seconds)
    os.system('arecord -D plughw:1,0 -d 60 audio.wav') #for audio
    camera.stop_recording()

button.when_pressed = capture

pause()

Step 2 (Set the code to run when the Pi starts up):

There are multiple ways to set your Python file to run when the Raspberry Pi loads up. Firstly, make sure you have the code from Step 1 saved on your Pi somewhere that you can find it. It is probably sensible to name it something like startVideo.py and store it in your /home/pi/ directory.

The following tutorial explains 5 different ways to get your program to run at boot (start up):

Five ways to run a program on a Raspberry Pi at start-up

From experience, sometimes some of them don't work, so make sure to test it and use a different method if necessary.

[Optional - Not tested] Step 3 (Combining the audio and video streams):

This may be one of the trickiest parts of this whole process, and Raspberry Pi don't really provide an easy way to do this (at least at the time of writing this tutorial). It requires quite a lengthy command that needs to be typed into a terminal.

It is up to you whether you want the students to type this, or simply copy and paste. At a basic level, it pipes the output of the command raspivid to arecord and combines them together. Here's the code:

/usr/bin/raspivid -o - -t 0 -w 1280 -h 720 -vf -fps 40 -b 8000000 -g 40 -n | /usr/bin/ffmpeg -ac 1 -f alsa -ac 1 -i hw:1,0 -f h264 -i pipe:0 -c:v copy -c:a aac -ab 128k -g 40 -f mp4 -r 30 video.mp4

Installing ffmpeg (only if not installed):

  1. Open a terminal.
  2. Change to home directory:
cd ~
  1. Download ffmpeg:
git clone git://source.ffmpeg.org/ffmpeg --depth=1
  1. Change to ffmpeg directory:
cd ffmpeg
  1. Configure installation:
./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree
  1. Make the installation: Note this step will take a long time!
make -j4
  1. Now finally run the installation:
sudo make install

The Python file for combined streams:

The following code should be saved and stored in the /home/pi/ directory.

from gpiozero import Button
from signal import pause
import os #required for audio stream

button = Button(2)

def capture():
    os.system('/usr/bin/raspivid -o - -t 0 -w 1280 -h 720 -vf -fps 40 -b 8000000 -g 40 -n | /usr/bin/ffmpeg -ac 1 -f alsa -ac 1 -i hw:1,0 -f h264 -i pipe:0 -c:v copy -c:a aac -ab 128k -g 40 -f mp4 -r 30 video.mp4') #for audio

button.when_pressed = capture

pause()

Use Step 2 to enable this script to run at boot.

Key Concepts:

Congratulations, you have reached the end of the tutorial. Good luck!

If you have any questions, please contact Scott Morgan | smorgan@bridgend.ac.uk.

Resources & Further Reading: