Monday 30 May 2016

NatureBytes Lite - bare bones build for Raspberry Pi A+


The NatureBytes Raspberry Pi camera kit is an excellent way to capture great photos of wildlife.



The lovely injection-moulded case is perfect for keeping everything nice and dry when deployed outside. the only problem I encountered was using the supplied Raspbian image: the GUI that ships is really nice and will be well suited to people using a Pi for the first time. But on a Raspberry Pi model A+ it can be quite resource intensive and I found myself getting frustrated with the performance.


Personally I wouldn't really consider using X-windows on an A+: it just doesn't have the muscle.

So I thought I'd put together a bare-bones installation that just runs the code necessary to detect motion with the PIR and take photos. I'm not bothered about saving onto a USB drive either: I'm happy to use the SD card and then connect the Pi to a network and extract using ssh.

So here is my recipe for building a quick, low profile image that works really well on an A+. Note that this does not include any windowing system so it's command line all the way!

1) Download the latest Jessie-Lite image and write it to an SD card.

2) Boot up and install the following stuff:

sudo apt-get update
audo apt-get upgrade -y
sudo apt-get install python3 python3-dev git i2c-tools python3-pip
sudo pip3 install gpiozero picamera RPi.GPIO spidev

3) Configure the Pi: run

sudo raspi-config

and enable the Camera and I2C interface (under advanced options). You might as well expand the filesystem to fill the whole disk too.

4) If you want to use the Real Time Clock, follow these excellent instructions from the Pi Hut.

5) Add the following code to a file (e.g. nblite.py). This makes use of the great gpiozero library to handle the PIR and starts a new logfile every time the code is run. Each image is date/timestamped and saved into /home/pi. The Pi will keep taking photos every half a second as long as motion continues to be detected.

from gpiozero import MotionSensor
import logging
from datetime import datetime
import picamera
import time
logfile = "/home/pi/nb-"+str(datetime.now().strftime("%Y%m%d-%H%M"))+".csv"
logging.basicConfig(filename=logfile, level=logging.DEBUG,
    format='%(asctime)s %(message)s',
    datefmt='%Y-%m-%d, %H:%M:%S,')

pir = MotionSensor(11)

print('Starting')
logging.info('Starting')
while True:

    pir.wait_for_motion()
    logging.info('Motion detected')
    print('Motion detected')
    while pir.motion_detected:
        print('Taking photo')
        ts  ='{:%H%M%S-%d%m%Y}'.format(datetime.now())
        logging.info('Taking photo: '+ str(ts)+'.jpg')
        with picamera.PiCamera() as cam:
            cam.resolution=(1024,768)
            cam.capture('/home/pi/'+str(ts)+'.jpg')
        time.sleep(0.5)
    print('Motion Ended')

    logging.info('Motion Ended')

6) Edit the /etc/rc.local to include the line

/usr/bin/python3 /home/pi/nblite.py &

before the exit 0 final line so that the code starts every time the Pi is booted.

That's it! All done.