1

I’m trying to make code run a minute before sunrise, but since updating the code (via another question) as I’ve changed time zones from GMT, I’m having trouble getting the syntax right when removing one minute.

sunriselessonemin = (ephem.date(sunrise)) + (1*ephem.minute)

Sunrise is obtained by

sunrise, sunset = ephem.localtime(home.next_rising(sun)),ephem.localtime(home.next_setting(sun))

Can anyone help a noob? Merci

Edited to be more clearer:-)

Here is my original code. The raspberry pi i use reboots via crontab at 4am, on starting, this script runs. It ran fine in the UK, but now im not in that time zone, i needed to add in some local timezone work as per brandons previous advice.

import sys
import os
import time
import ephem

#find time of sun rise and sunset
sun = ephem.Sun()
home = ephem.Observer()
home.lat, home.lon = '45.226691', '0.013133' #your lat long 
sun.compute(home)
sunrise, sunset = ephem.localtime(home.next_rising(sun)),ephem.localtime(home.next_setting(sun))
daylightminutes = (sunset - sunrise) * 1440 # find howmany minutes of daylight there are
sunriselessonemin = ephem.date(sunrise + 1*ephem.minute)

print "prog started at(home.date) =  %s" %(home.date)
print "datetime = %s" % time.strftime("%Y/%-m/%-d %H:%M:%S")
print "sunrise = %s" %sunrise
print "sunset = %s" %sunset
print "daylight mins = %s" %(daylightminutes)

testmode = "yes" #yes or no

def dostuff() :
    if testmode == "yes" or sunrise <= ephem.now() <= sunriselessonemin: #if time now is within a minute of sunrise, start taking pictures
        print "it's sunrise!"
        if testmode == "yes" : 
            print "TESTMODE - Taking 10 images with 10 seconds in between and uploading made mp4 to Dropbox"
        FRAMES = daylightminutes # number of images you want in timelapse video
        if testmode == "yes" : 
            FRAMES = 10
        FPS_IN = 8 # number of images per second you want in video
        FPS_OUT = 8 # number of fps in finished video 24 is a good value
        TIMEBETWEEN = 60 # number of seconds between pictures, 60 = 1 minute
        #take the pictures needed for the time lapse video
        if testmode == "yes" : 
            TIMEBETWEEN = 10
        frameCount = 1
        while frameCount < (FRAMES + 1):
            print "taking image number ", frameCount, " of ", daylightminutes
            datetimenowis = ephem.now() 
            imageNumber = str(frameCount).zfill(7)
            os.system("raspistill -o /home/pi/image%s.jpg"%(imageNumber)) # -rot 270 need for cam on side (put -rot 270 before -o)
            os.system("/usr/bin/convert /home/pi/image%s.jpg -pointsize 72 -fill white -annotate +40+1590 'Chicken Cam %s' /home/pi/image%s.jpg"%(imageNumber,datetimenowis,imageNumber))
            frameCount += 1
            time.sleep(TIMEBETWEEN - 10) #Takes roughly 6 seconds to take a picture & 4 to add text to image
        #record current time and date in variable datetime
        datetimenowis = time.strftime("%Y%m%d-%H%M")
        print "It's sunset, processing images into one mp4 video.  Time now is ",  datetimenowis
        # 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,datetimenowis))
        #send the timelapse video to dropbox
        print "Sending mp4 video to dropbox."
        from subprocess import call
        photofile = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload /home/pi/timelapse%s.mp4 timelapse%s.mp4" %(datetimenowis,datetimenowis) 
        call ([photofile], shell=True)
        print "mp4 uploaded to dropbox!  Cleaning up."
        #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"%(datetimenowis))
        os.system("rm /home/pi/image*")
        print "Finished, exiting program."
        sys.exit()

while ephem.now() <= sunrise:
        time.sleep(1)
        dostuff()

The issue at the momnet, is that if i try to tringer the main code at one minute befoe sunset. the code fails here.

    pi@raspberrypi ~ $ sudo python timelapseV3.py
Traceback (most recent call last):
  File "timelapseV3.py", line 13, in <module>
    sunriselessonemin = ephem.date(sunrise + 1*ephem.minute)
TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 
'float'

I cant seem to start the code a minute before sunrise like i could before.

cheers

Peter Nazarenko
  • 411
  • 1
  • 7
  • 16
Lmm Cams
  • 21
  • 2

1 Answers1

0

Once you run localtime() on a PyEphem date, you move it away from being a value that PyEphem can reason about and instead create a value that Python’s native datetime module knows about. To use the value in PyEphem, keep a copy of the raw value returned by PyEphem, and do the math with it instead:

sunrise, sunset = home.next_rising(sun), home.next_setting(sun)
sunrise_localtime, sunset_localtime = ephem.localtime(sunrise), ephem.localtime(sunset)
daylightminutes = (sunset_localtime - sunrise_localtime) * 1440 # find howmany minutes of daylight there are
sunriselessonemin = ephem.date(sunrise + 1*ephem.minute)

You should find that this runs without error. Good luck!

Brandon Rhodes
  • 83,755
  • 16
  • 106
  • 147