I have this script triggered at 3am from crontab on a raspberry pi. The idea is to take a timelapse video over the course of a day but only between sun up and sunset. The code when triggered calculates the sun set and sun rise times, then awaits sunrise. Then it should start taking the pictures, but it doesn't seem to be triggering at sunrise for some reason. I think this maybe due to 'if home.date == sunrise:' being too accurate? How can I get it to ignore fractions of a second, or even seconds altogether?
This is the full code-
import os
import time
import ephem
# find time of sun rise and sunset
sun = ephem.Sun()
home = ephem.Observer()
home.lat, home.lon = '52.8709', '-1.797' #your lat long
sun.compute(home)
sunrise, sunset = home.next_rising(sun)),
home.next_setting(sun))
daylightminutes = (sunset - sunrise) * 1440 # find howmany minutes of daylight there are
daylightminutes = (round(daylightminutes))
def dostuff() :
if home.date == sunrise:
import datetime
import sys
FRAMES = 60#daylightminutes -600 # number of images you want in timelapse video
FPS_IN = 4 # number of images per second you want in video
FPS_OUT = 4 # number of fps in finished video 24 is a good value
TIMEBETWEEN = 6 # number of seconds between pictures, 60 = 1 minute
#take the pictures needed for the time lapse video
frameCount = 0
while frameCount < FRAMES:
imageNumber = str(frameCount).zfill(7)
os.system("raspistill -rot 270 -o /home/pi/image%s.jpg"%(imageNumber))
frameCount += 1
time.sleep(TIMEBETWEEN - 6) #Takes roughly 6 seconds to take a picture
#record current time and date in variable datetime
datetime = time.strftime("%Y%m%d-%H%M")
# make the timelapse video out of the images taken
os.system("avconv -r %s -i /home/pi/image%s.jpg -r %s -vcodec libx264 -crf 20 -g 15 -vf crop=2592:1458,scale=1280:720 /home/pi/timelapse%s.mp4" %(FPS_IN,'%7d',FPS_OUT,datet$
#send the timelapse video to dropbox
from subprocess import call
photofile = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload /home/pi/timelapse%s.mp4 /home/pi/timelapse%s.mp4" %(datetime,datetime)
call ([photofile], shell=True)
#remove the timelapse video copy and all images it is made up of that are held localy on the Rpi
os.system("rm /home/pi/timelapse%s.mp4"%(datetime))
os.system("rm /home/pi/image*")
sys.exit()
while home.date <= sunrise:
dostuff()
If you can spot any other issues that may be the cause of the problem let me know.
Cheers
Steve