J.Pasha J.Pasha - 18 days ago 6
Python Question

python program start time

i have a raspberry pi 3 (os:rasbian) and i want to run this code for exp at 13:00
when i leave the at home 12:00 but i want program will start 1 hour later.
how can i modify the program.
thanks

import time
import picamera
with picamera.PiCamera() as camera:
camera.start_preview()
try:
for i, filename in enumerate(camera.capture_continuous('/home/pi/Google Drive/{timestamp:%H-%M-%S}-{counter:03d}.jpg')):
print(filename)
time.sleep(3)
if i == 3:
break
finally:
camera.stop_preview()

Answer

I would do it like this:

import time
import picamera

# Pause program until Enter is pressed (press when You leave the home)
start = raw_input('Press Enter to start the counter: ') # or input(), if You use Python3

# Show message
print('Camera will start recording in 1 hour')

# Capture Ctrl-C
try:

    # Sleep for an hour
    time.sleep(3600)

# Maybe You want to start immediately, pressed Ctrl-C
except KeyboardInterrupt:
    print('Starting camera now')

with picamera.PiCamera() as camera:
    camera.start_preview()
    try:
        for i, filename in enumerate(camera.capture_continuous('/home/pi/Google Drive/{timestamp:%H-%M-%S}-{counter:03d}.jpg')):
            print(filename)
            time.sleep(3)
            if i == 3:
                break
    finally:
        camera.stop_preview()

This way, You can start program anytime You want and let it wait in the background. You activate it before You leave. You can also force it to start immediately if You need.

Comments