Thanks for taking the time to view my project! I'm a big plant person so I have a bunch of plants and wanted to do some time lapses on them. Watch them grow and move around! I didn't have a nice camera that was capable of incremental photos. In the past I have used my iPad mini to do some time lapses but those were just over a couple hours. I want long term like days into weeks. So I decided to use a Raspberry Pi I had laying around and see what I could make with that. I first started using a webcam but the quality was poor. I upgraded to the RPI Camera v2 and got MUCH better quality photos. It took some tweaking of the settings in raspistill to get photos just the way you want them.
First we can hookup the hardware. Since there are not many neopixels, I just used the 5v rail on the RPi to power them. I know, I know they can draw a lot for the RPi... but if you want to use a external 5v source, make sure you connect the grounds together. Now the complicated part. We need to shift the logic from the RPI from 3.3v to 5v for the neopixels. I use the 74AHCT125 to achieve this. I included a wire diagram in the attachment section. It pretty easy though. Once we connect that up to our neopixels, connect the data line going to the input of the 74AHCT125 to RPI - GPIO 18. Then use a small wire jumper and connect ground to GPIO 23 to tell our script we want flash. I think thats it for hardware...
Ok let's get the software started by making sure we are up to date.
sudo apt-get update
sudo apt-get full-upgrade
if not already installed:
sudo apt-get install python3-dev
sudo apt-get install python3-pip
Upgrade out pip & setuptools:
sudo -H pip3 install --upgrade pip
sudo pip3 install --upgrade setuptools
Now lets install Circuit Python per Adafruit's guide:
cd ~
sudo pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo python3 raspi-blinka.py
If you get a dialog asking you to proceed, select 'Yes.'
Once finished, reboot.
Now let's make the script to test out our config. Use your favorite editor (I use nano) and let's make a new file.
sudo nano blinkatest.py
Copy & paste this code into your newly created file
import board
import digitalio
import busio
print("Hello blinka!")
# Try to great a Digital input
pin = digitalio.DigitalInOut(board.D4)
print("Digital IO ok!")
# Try to create an I2C device
i2c = busio.I2C(board.SCL, board.SDA)
print("I2C ok!")
# Try to create an SPI device
spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)
print("SPI ok!")
print("done!")
Now let's run it & see what we get.
python3 blinkatest.py
It should spit out something like Digital IO ok, I2C ok, and SPI ok assuming the initial install script worked.
Now install the Neopixel library from Adafruit.
sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel
Ok, that should be it for the libraries we need! Now let's look at the two scripts for taking the pic and operating the flash. See below... How they operate is as follows:
The main script executes the python3 script (line 3) to set the neopixels to on & writes a "1" to a status text file (line 4) indicating they are on, takes the picture using raspistill (line 8), then turns off the neopixels (line 11) and writes a "0" to our status file (line 12) meaning they are off.
The reason for the status text file is so you don't need to have two python scripts to turn it on and off. It just simply reads the status file to see which way to toggle it. I'm sure they may be betters ways but I'm semi new to the Pi and this is what I came up with. Line 6 sets a variable named DATE with the current date/time for our file name. You can see its substitute for the file name in the raspistill command.
Run this to open/create your script file.
sudo nano capture.sh
You need to change your file path to where you want it to store. Use absolute paths.
#!/bin/bash -e
python3 /home/pi/timelapse/flashScript.py
echo "1" > /home/pi/timelapse/status.txt
DATE=$(date +"%b%d%C%g_%H%M%S")
raspistill -n -md 3 -ISO 100 -mm average -drc high -ex auto -br 50 -awb auto -q 85 -sa 25 -ifx none -vf -o /home/pi/timelapse/fastLapse/$DATE.jpg
python3 /home/pi/timelapse/flashScript.py
echo "0" > /home/pi/timelapse/status.txt
exit 0
The actual camera command arguments can be found here listing all the possibilities and options. I recommend trying different settings outside the script in your shell. Some major ones for me anyway is ISO, DRC, and VF. ISO can be used for lower light applications along with DRC set to high. DRC lightens the dark parts & darkens the bright parts. EX (exposure) can be set too for darker shots.
Exit your editor and save the file, in my case capture.sh in your home directory. We need to make it executable so it can be run as a script. Run this command in the directory you saved your script:
sudo chmod +x capture.sh
Now lets make the python3 script for the flash. Open your editor again using this command:
sudo nano flashScript.py
Paste the below code. You need to change your number of pixels in your ring or strip to the actual count you have. In my case its 24 (line 11). Line 26 is where you set your flash color. I like mine slightly more yellow in color so thus setting the blue lower @160. It goes (red, green, blue) & 255 is full on.
I added something else to this script should you want to temporally disable the flash without changing the code. Line 17, looks at GPIO 23 (that was setup in line 8) to see if it is grounded or not. If it is grounded or still pulled up. If it is grounded with a jumper from ground to pic 23 on the board, it bypasses turning the neopixels on, thus no flash. You could also comment out running the python3 script in the main capture.sh file. I find a quick jumper wire is easier in my situation.
import sys
from time import sleep
import board
import neopixel
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
pixel_pin = board.D18
num_pixels = 24
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=1.0, auto_write=True, pixel_order=ORDER)
while True:
if GPIO.input(23):
sleep(0.1)
else:
with open("/home/pi/timelapse/status.txt", 'r') as f:
state = int(f.read())
if state:
pixels.fill((0,0,0))
else:
pixels.fill((255,255,160))
sys.exit()
Save your file in the same directory as your python script & exit.
Ok, this should be ready to run. Start the main capture.sh by running this command:
sudo ./capture.sh
You should see your neopixels turn on for about 5 seconds while it takes the pic. Go into your directory where you set the file to be saved in your flashScript.py to make sure its there. If your running with the RPi desktop enabled you can open it with that to view it. If your in command line, we need to retrieve the file to open it. This is where Samba comes in.
Samba is a SMB file server. It allows you to read/write files to the directory you share. Let setup the server quick and get that file. I'm going to be more brief here since there are many guides on the internet to setup Samba. Run this:
sudo apt install samba
sudo nano /etc/samba/smb.conf
Delete everything out of the config file by hitting 'command k' a bunch of times to delete the lines. Paste in this:
[global]
workgroup = WORKGROUP
interfaces = wlan0
bind interfaces only = no
security = user
encrypt passwords = true
map to guest = Never
guest account = pi
log file = /var/log/samba/log.%m
max log size = 1000
logging = file
server role = standalone server
[Samba Share]
comment = home folder
path = /home/pi/
browseable = yes
valid users = pi
guest ok = no
read only = no
create mask = 0775
directory mask = 0775
max connections = 20000
Save your config & exit. Now add your user so we can access the samba share:
smbpasswd -a pi
Lets restart our server to apply the changes:
sudo systemctl restart smbd
Get your IP address so we can connect without using discovery:
hostname -I
On another computer or using a app on your phone, go to Connect to server on a Mac or 'Command K' and enter in:
smb://10.32.1.1/
or on windows:
//10.32.1.1/
You want to put your IP address of the samba server we just created in place of the 10.32.1.1 address. You should see a folder pop up if everything worked correctlty. Now you can browse to your file and. open the image. This is also how we will get our images off the server when we are done.
Lastly we need to tell our capture.sh script to run automatically. CRON is what we will use. Good ol' CRON. Its build into the OS and is for scheduling tasks such as backups or whatever. We need to run our script as a superuser (sudo) for our neopixels to work. To tell the OS we want to run our script with elevated permissions, we edit the sudo crontab. Begin with this:
sudo crontab -e
Scroll to the last line and enter this in:
*/1 * * * * /home/pi/timelapse/./capture.sh > /dev/null 2>&1
Save the file & the program will start running. This particular command tells it to take a picture every minute. That adds up to a lot of pictures so make sure your disk has space.
If you want to change it to every 5 min, change the first field to "*/5". Every hour, change the second field where the asterisk is, the hour field, to a "1" and change the first field, the min field, to a "0".
I think thats it. Sit back and watch the pictures roll in!
A quick note to make the actual time-lapse, you can use programs on the RPi but that will take forever honestly. On a Mac, you can open Quicktime, go under file to "open image sequence", select your size & frame rate, and it starts rendering it. depending on you computer and how many photos, it takes a couple min. For a 10 days lapse every 6 min, it took my Mac about 5 min to render it. Once its done save the file and there ya go!
Checkout one I did here:
Comments