I am writing a program to capture motion, take a picture, sleep, blink etc. Here is the code so far without the functions because those aren't the problem.
try:
starting()
while duration < 3:
previous_state = current_state
current_state = GPIO.input(sensor)
if current_state != previous_state:
new_state = "High" if current_state else "low"
if current_state:
blink_led(5)
pic = '/home/pi/pic%s.jpg' % num
num += 1
camera.capture(pic)
data = open(pic, 'rb')
s3.Bucket('bucket').put_object(Key=pic, Body=data)
sleep(5)
duration += 1
The problem here is that I want it to sleep for 5 seconds but if the upload takes a while then it is basically sleeping for more than 5 seconds. Depending on my internet connection. I have tried
while s3.Bucket('bucket').put_object(Key=pic, Body=data):
sleep(5)
duration += 1
but then it gets stuck in an infinite loop and if I do an if clause at the end like:
if duration == 3:
s3.Bucket('bucket').put_object(Key=pic, Body=data)
it will only upload the last photo. I have also tried doing at the end:
if duration == 3:
s3.Bucket('bucket').put_object(Key='/home/pi/pic1.jpg', Body=data)
s3.Bucket('bucket').put_object(Key='/home/pi/pic2.jpg', Body=data)
s3.Bucket('bucket').put_object(Key='/home/pi/pic3.jpg', Body=data)
It can only upload the first then it gets hung up. How should I write the uploading as a background process so it doesn't sleep longer than 5 seconds? I feel like I have tried everything other than asynchronous which I think is specific to the raspberry pi