I am trying to send an email inside a while loop but if the sendmail fails, I want the script to log the error and continue:
while True:
input_state = GPIO.input(5)
if input_state == True:
now = datetime.datetime.now().strftime("%m-%d-%y-%H%M%S")
logging.debug('Motion detected at ' + now)
name = 'tempimage.jpg'
camera.capture(name)
new_name = '/home/pi/image' + str(now) + '.jpg'
os.rename(name, new_name)
try:
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( '<login to gmail>', '<password>' )
server.sendmail( 'Front Porch', '<myphone number>@vtext.com', 'Picture just taken on Front Porch' ) #Chuck
server.close()
else:
logging.debug('Send of text failed ' + now)
time.sleep(3)
else:
now = datetime.datetime.now().strftime("%m-%d-%y-%H%M%S")
logging.debug('No Motion ' + now)
time.sleep(3)
When I execute this at boot it stalls at the try. I know this is a Duh moment in the making but really need it to work. Any help greatly appreciated.
trywith noexceptorfinally. This isn't going to stall at thetry; this is going to outright fail to compile.except AppropriateExceptionTypeinstead ofelse.