Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Will McGugan: Using a Raspberry Pi + Camera to Monitor the Nocturnal Activities of Tropical Insects

$
0
0

Raspberry Pis are useful little computers. I own several, since I work with them in my day job, and I thought it was about time I put one to use.

I also happen to keep tropical insects. Specifically, beetles. These are not your garden variety beetles, unless you happen to live in a rain forest. The ones I have at the moment are elephant beetles which come from Central and South America. Here's a photo:

Male elephant beetle. Banana for scale.

These insects are mostly nocturnal. During the day they tend to burrow under their bedding material (moss), or hang out on a branch. But during the night, they can be quite active. I know this because in the morning they have re-arranged the branches in their tank.

For my Raspberry Pi project, I wanted to make use of the Raspberry Pi's camera to create a webcam. I also wanted to be able to create timelapse movies, because I didn't really want to watch 12 hours of video to see what they get up to at night. The result was Beetlecam, which may or not be live here. Beetlecam takes a picture every 30 seconds (by default), and can generate timelapse movies for a given time period. Here's 24 hours worth of footage condensed in to around 2 minutes:

In this post I'll describe how you can install and run a Beetlecam on your own Raspberry Pi.

Beetlecam Hardware

To run Beetlecam, you will need the following 3 things:

  • A Raspberry Pi (any model)
  • A Raspberry Pi Camera
  • A tank of large tropical Insects

If you don't happen to have the last one, don't worry, it will work with just about anything you would like to make a timelapse of.

The regular RPi camera will work fine, but I used an infra-red camera, which can film in complete darkness. Essential if your subjects are active at night. Here's a photo of my RPi and camera (note the infra-red LEDs either side of the camera itself):

A Raspberry Pi with Infra red camera attached

Mounting the RP is simple enough. I used velcro tabs which easily support its weight, and lets me move it to different positions. Here it us mounted:

Raspberry Pi and infra-red camera mounted

Beetlecam Software

There are two parts to Beetlecam; there is a web application which shows the latest frame, and a script which periodically takes a photo and uploads it. Both are in the Beetlecam repository.

There are a few additional libraries you will need to run Beetlecam. You can install these from a terminal in the Raspberry Pi Desktop, or you could use SSH/Dataplicity to connect to your Pi via another computer on your network. Whatever method you use, run the following commands:

sudo apt-get update
sudo apt-get install python-dev python-pip python-lxml libjpeg-dev
pip install moya picamera

This may take a while. Plenty of time for you to grab coffee.

Once that has finished, you should be able to run the command moya -v.

The next step is to get the Beetlecam code from Github. Run the following commands to do that:

sudo apt-get install git
git clone https://github.com/moyaproject/beetlecam.git

This should create a folder, beetlecam in your current directory. Change to that directory, with cd beetlecam.

In that directory, you should find a Python file called beetlecam.py. This is the script which periodically takes photos and uploads them. All the Python code does is take a picture then make a POST request to the web application. A POST request is what your browser does when you fill in a form on the web. In the case of Beetlecam, it is posting to this form in the same way a browser would.

Run beetlecam.py with the following:

python beetlecam.py run -r 30

You will see some errors in the console, because we are not yet running the web application part of Beetlecam.

To run the web application, first create a new terminal window, or SSH instance. That way we can leave beetlecam.py running on the Pi. Change to the beetlecam directory then run the following:

cd site
moya init

This will initialize the web application and create a database. Next, run the web application with the following command:

moya runserver -t --host 0.0.0.0

You should now be able to point your browser at http://127.0.0.1:8000 and see the photos that beetlecam.py is taking. Or if you are using another machine on your network, you can use the hostname of your Raspberry Pi in the URL, by default that would be http://raspberrypi:8000.

In Part II?

If there is enough interest, I'll post about how to create the timelapse videos . I'll also explain what beetlecam.py does in more detail, and go over the web application.


Viewing all articles
Browse latest Browse all 22462

Trending Articles