The BITX40 is dead. Long Live the BITX40!

Come Join the QRP-Tech Group!

Time Lapse Camera with the Raspberry Pi Zero W: Part 2

In April of 2018, I posted the article “Time Lapse Camera with the Raspberry Pi Zero W: Part 1” and introduced the project. At the end of that article I said that I needed more progress to cal it complete. I said “The photos aren’t time stamped except in the filename, and I need to get the photos off the Raspberry Pi Zero W and into a format that anyone in the house could easily view, even away from home. Just like a Big Boy security camera. It will involve learning more Python, some linux programs like ffmpeg, and eventually a rain proof enclosure so it can be mounted outdoors.”

While I have to admit it’s not exactly as perfect as I’d hoped on the software side of things (mostly because I’m not very good at programming and stopped work when I had it ‘good enough’) the hardware side is complete. Let’s have a look, and be sure to go all the way to the end for Pictures and Video!

A Look at The Hardware

The main ingredient to the Raspberry Pi Zero W Time Lapse Camera is of course the Raspberry Pi Zero W which you can get over at Seeed Studio. I am using a 5v regulator with a 12v power supply rather than trying to use a 5V power supply. My junk box is full of old wall warts and so all I needed was a coaxial power plug. Those are easy to come by and could even be salvaged from something else.

Of course you also need the camera kit for the Raspberry Pi Zero, and you can find one to your liking here. A word of advice: Unless you’re going to illuminate the area with some infrared LED’s, be sure to get a camera with the IR filter in it. I got one without the IR filter, and then bought a switchable filter which you can see in the pictures. I even made a H-Bridge for it to turn it on and off based on measuring the luminescence of photos every 5 minutes. But, it did no good. I eventually unplugged it and left the IR filter intact.

The case that I have was built by a friend who has access to CNC routers and the like, but something else would work as well. A piece of black construction paper cuts back the glare and finishes off the build.

In the pictures you’ll also see an OTG cable with a wifi adapter. This is because the wifi on the Pi Zero W died on me. I was not pleased! But it was cheaper than buying a new Pi, and also has 5ghz capability, so it was an upgrade. It’s also a great way to add wireless to your Raspberry Pi Zero.

The Software

The software has been written by hand or borrowed from various online sources. It’s a mashup of Bash and Python. While the 32GB MicroSD card has enough room for a couple of days worth of photos, I have a Linux server that’s configured to get the latest photos every 15 minutes. The Pi Zero W deletes old photos daily, and that way it never runs out of space.The Linux server is far more powerful than the Pi and combines the previous days photos into a time lapse every day. It works quite nicely and has been stable for months.

Here’s the software below. It’s a bit of a mess!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
crons for the rpi:
@reboot /usr/bin/python3 /home/pi/wordcam.py
#*/1 * * * * /bin/bash /home/pi/filter_decide.sh #If you use a switching filter. Optional.
*/5 * * * * /bin/date >> ~/i_am_alive
@daily mv ~/today/* ~/yesterday;/usr/bin/find ~/yesterday -ctime +1 -exec rm -f {} \;

------------
filter_decide.sh, optional

#uses brightness.py to measure brightness of an image, and writes "dark" or "light"
#to a file which is later read by the cam program itself.

#!/bin/bash
bn=$(/usr/bin/python3 /home/pi/brightness.py `ls -1r /home/pi/images/*.jpg | head -1`)
threshold="0.05"
if (( $( echo "$bn < $threshold" | bc -l) )); then
        echo "dark" > /home/pi/brightstatus
        else
        echo "light" > /home/pi/brightstatus

fi

------------
BRIGHTNESS controlled IR filter
# https://gist.github.com/kmohrf
# pi@khcam:~ $ ./brightness.py images/05-13-18--05-07-58.jpg
# images/05-13-18--05-07-58.jpg   0.09469119831919676
# cron job runs every 5 minutes, tests the luminance. If it's < 0.08 then the IR filter is OFF.
#  brightness.py also optional
# Measures luminescence writes it to STDOUT.

#!/usr/bin/python3
import sys
from PIL import Image


def calculate_brightness(image):
    greyscale_image = image.convert('L')
    histogram = greyscale_image.histogram()
    pixels = sum(histogram)
    brightness = scale = len(histogram)

    for index in range(0, scale):
        ratio = histogram[index] / pixels
        brightness += ratio * (-scale + index)

    return 1 if brightness == 255 else brightness / scale


if __name__ == '__main__':
    for file in sys.argv[1:]:
        image = Image.open(file)
        print(calculate_brightness(image))

------------
wordcam.py
#!/usr/bin/python3
import PIL
from PIL import Image, ImageFont, ImageDraw
font = ImageFont.truetype('/home/pi/DejaVuSans.ttf',25)

from time import sleep
import time
import picamera
WAIT_TIME = 25
with picamera.PiCamera() as camera:
        camera.resolution = (2048, 1536)
        for filename in camera.capture_continuous('/home/pi/today/{timestamp:%m-%d-%Y--%H-%M-%S}.jpg'):
#               Reads the filter status and writes it to the image
#               with open('filter.status') as f:
#                       filter_status = f.read()
#               f.closed
                im1 = Image.open(filename)
                draw = ImageDraw.Draw(im1)
                ts = time.ctime()
                draw.rectangle(((0, 5), (700, 30)), fill="black")
                draw.text((0, 5),'Time Lapse Cam '+str(ts),(255,255,0),font=font)
#               draw.text((500, 5),'IR Filter: '+str(filter_status),(255,255,0),font=font)
                draw = ImageDraw.Draw(im1)
                im1.save(filename, quality=100)
                sleep(15)


------------
ir_filter_off.py
# Turn off the IR filter
#!/usr/bin/python3
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(14,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.output(14,GPIO.HIGH)
time.sleep(1)
GPIO.output(14,GPIO.LOW)
with open('filter.status', 'w') as f:
        write_data =f.write('off')
f.closed
GPIO.output(14,GPIO.LOW)
GPIO.output(15,GPIO.LOW)
------------
ir_filter_on.py
# Turn on the IR filter
#!/usr/bin/python3
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(14,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.output(15,GPIO.HIGH)
time.sleep(1)
GPIO.output(15,GPIO.LOW)
with open('filter.status', 'w') as f:
        write_data =f.write('on')
f.closed
GPIO.output(15,GPIO.LOW)
GPIO.output(14,GPIO.LOW)

------------
if the video is at 10 frames per second, then it'll be easy to calculate by video time which frames to display.
use a video player that can jog, and then give links to proper images based on video time.


------------
On the server:
------------
getlatest.sh
date=$(date '+%m-%d-%Y')
echo "---------------------------------------" >> ~/cron.log
echo "Cron Start: "$(date) >> ~/cron.log;
pgrep rsync -c  > /dev/null || rsync -av -e ssh  pi@10.0.0.19:today/$date*.jpg ~/images/today/ > /dev/null 2>&1;
#pgrep rsync -c  > /dev/null || rsync -av -e ssh  pi@10.0.0.19:today/$date*.jpg ~/images/today/
echo "Cron End: "$(date) >> ~/cron.log


------------
 makedaily.sh
#!/usr/bin/bash
date=$(date -d "yesterday 13:00" '+%m-%d-%Y')
#echo $date
#mkdir ~/images/$date; cd ~/images/$date; rsync -vr -e ssh pi@10.0.0.19:timelapse/$date* .
ffmpeg -threads 1 -framerate 18 -pattern_type glob -hwaccel vdpau -i "/home/khcam/images/today/$date*.jpg" -c:v libx264 ~/images/video/timelapse-$date.mp4
mkdir /home/khcam/images/$date
mv /home/khcam/images/today/$date*.jpg /home/khcam/images/$date



------------
 crontab -e
10 0 * * * ~/makedaily.sh
#*/15 * * * * echo "---------------------------------------" >> ~/cron.log; echo "Cron Start: "$(date) >> ~/cron.log; pgrep rsync -c  > /dev/null || rsync -av -e ssh  pi@10.0.0.169:images ~/ > /dev/null 2>&1; echo "Cron End: "$(date) >> ~/cron.log
*/15 * * * * ~/getlatest.sh

Lastly, I have Apache configured on the server so that I can view the videos that are generated. Here are pictures of the build, and one of the resulting lapses.

1 comment

    • Robin on November 6, 2019 at 8:37 AM
    • Reply

    Excellent! Been wanting to find a fun and practical use for the old Pi3 and some JB cameras!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.