0

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

1 Answers1

0

(a) Once you create an Observer, its date stays fixed until you reach in manually and change it. So the home.date you keep checking against sunrise will never change and thus never actually reach sunrise. You might want to call the PyEphem now() routine instead, so that your idea of the current time can progress.

(b) You are correct that it is extremely unlikely that you will happen to do your == comparison at exactly the millisecond of sunrise so you probably want to compare using an inequality instead.

(c) Until sunrise arrives, your loop would appear to drive the CPU at 100% checking thousands of times a second whether sunrise has arrived yet. You might want to time.sleep(1) while waiting to keep your Raspberry Pi cooler.

Brandon Rhodes
  • 83,755
  • 16
  • 106
  • 147
  • Thanks Brandon, I knew it had tone something like that. I am a noob but will see how I call now(), same with inequality, and I take your point about the unreasonable workload, will have a play see what I come up with. Cheers Steve – user2791188 Mar 09 '14 at 20:01
  • Remember that lots of `print` statements can help you see what is going on, and can help you watch whether your values are advancing in date and time or sitting still. My approach to code that is not working is often to add `print` statements until I can finally see what is going on! – Brandon Rhodes Mar 10 '14 at 00:25
  • Hi Brandon, i have changed home.date for ephem.now() with some print statements and it is changeing now. also added a second delay to the while loop. when i print the sunrise it doesnt display any milliseconds, and neither does ephem.now(), so it seems this now has a chance of triggering, but the code jumps every other second so maybe i need to reduce the sleep to half a second and then i should have a trigger bang on time. although i dont need that kind of acuracy, i cant figure out how to get it to trigger using only the minute not the second. – user2791188 Mar 10 '14 at 16:15
  • think i may have sussed it- sunrisep = ephem.date(sunrise + 1*ephem.minute) print sunrisep def dostuff() : if sunrise <= ephem.now() <= sunrisep: how does this look? figured out how to add a minute, then, hopefuly, detect if the current time is within a minute of sunrise. let me know if there seems something amiss? but i will find out tomorrow morning anyway lol! – user2791188 Mar 10 '14 at 20:31