0

I have this python code that on motion detected send images via email and record videos locally (note the commented line):

from gpiozero import MotionSensor
from picamera import PiCamera
from time import time
from time import sleep
from datetime import datetime
import threading
from subprocess import call
import os
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate

def send_mail(files=None):
    msg = MIMEMultipart()
    msg['From'] = 'email@domain.ext'
    msg['To'] = 'email@domain.ext'
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = 'subject'
    msg.attach(MIMEText('text'))
    for f in files or []:
    with open(f, 'rb') as fil:
            part = MIMEApplication(
                fil.read(),
                Name=basename(f)
            )
        part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
        msg.attach(part)
    smtp = smtplib.SMTP('smtp.office365.com:587')
    smtp.starttls()
    smtp.login('email@domain.ext','password')
    smtp.sendmail('email@domain.ext','email@domain.ext',msg.as_string())
    smtp.close()
    print('email sent')

def MP4Box(t):
    call(['MP4Box','-add','/home/pi/Desktop/CAMERA/'+t+'.h264','/home/pi/Desktop/CAMERA/'+t+'.mp4'])
        print('end conversion')
        os.remove('/home/pi/Desktop/CAMERA/'+t+'.h264')

pir = MotionSensor(4)
camera=PiCamera()
camera.resolution=(1920,1080)
camera.start_preview()
sleep(2)
#while True:
if 1==1:
    pir.wait_for_motion()
    print('motion detected')
    now=datetime.now().strftime('%Y%m%d_%H%M%S')
    camera.capture('/home/pi/Desktop/CAMERA/'+now+'.jpg')
    print('frame captured')
    thr=threading.Thread(target=send_mail,args=([['/home/pi/Desktop/CAMERA/'+now+'.jpg']]),kwargs={})
    thr.start()
    print('start recording')
    now=datetime.now().strftime('%Y%m%d_%H%M%S')
    camera.start_recording('/home/pi/Desktop/CAMERA/'+now+'.h264')
    pir.wait_for_no_motion()
    print('motion ended')
    camera.stop_recording()
    print('end recording')
    thr=threading.Thread(target=MP4Box,args=([now]),kwargs={})
    thr.start()
    print('start conversion')

it works, but if I uncomment the while True: line and comment the if 1==1: line the Pi (Pi Zero W) freezes every time and after reboot I find in the CAMERA directory a .jpg file (0 bytes) and a .h264 file (0 bytes). why does this happen?

  • put the while line after the if line and try again – jsotola Jul 12 at 15:15
  • I'll try, in the meantime can you explain me why that could cause freezes? – Miky Jul 12 at 16:17
  • i was thinking that it has something to do with the indentation level of the comment after the while statement ... now I am thinking that it has to do with the program entering an endless loop ... if 1==1: is a pointless statement that is just a no-op, which means that the code executes only once every time ... the while block has no exit as far as i can tell, so the code loops continuously .... perhaps one of the functions, that are called, has an abnormal return when called more than once – jsotola Jul 12 at 16:40
  • I use if 1==1: to maintain the indentation and change from once to loop faster. the script is meant to loop continuously – Miky Jul 12 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.