Posts: 201
Threads: 37
Joined: Dec 2021
Apr-25-2022, 11:59 PM
(This post was last modified: Apr-26-2022, 12:00 AM by Frankduc.)
hello,
I am trying to get my function out of the loop and avoid sending multiples emails.
def courriel():
print("intruder!")
try:
server= smtplib.SMTP("smtp.mailtrap.io", 2525)
server.login("096477fce46f57", "49f784782bcc12")
server.sendmail(sender, receiver, message)
print('Courriel envoy�')
except smtplib.SMTPException:
print("Impossible d'envoyer le courriel � " + receiver)
except (smtplib.socket.error, smtplib.SMTPConnectError):
print("Connexion impossible au serveur SMTP")
while(True):
if GPIO.input(bouton_alert_Del1)==0:
if led_off1==False:
print("Le syst�me d'alarme est activ�")
delai()
GPIO.output(DEL1,True)
led_off1=True
sleep(.5)
else:
print('Vous avez 10 secondes pour entrer votre code!')
decompte(int(t))
print("Le syst�me d'alarme est d�sactiv�")
GPIO.output(DEL1,False)
GPIO.output(DEL2,False)
led_off1=False
sleep(.5)
if GPIO.input(senseur_Del2)==1 and GPIO.input(bouton_alert_Del1)==1:
if led_off1==True:
GPIO.output(DEL2,True)
time.sleep(.2)
GPIO.output(DEL2,False)
time.sleep(.2)
led_off2=True
sleep(.5)
courriel() //dont want emails to be sent repeatedly without stoping the program just one time
else:
GPIO.output(DEL2,False)
led_off2=True
sleep(.5) any idea
than you
Posts: 1,145
Threads: 114
Joined: Sep 2019
Break will get you out of a while loop.
Posts: 201
Threads: 37
Joined: Dec 2021
Apr-26-2022, 12:52 AM
(This post was last modified: Apr-26-2022, 12:52 AM by Frankduc.)
Yes but it get me out of the program. Once the email is sent i should be able to stop the alarm and i cant without rerun the code. If i could only break the courriel() part
Posts: 6,799
Threads: 20
Joined: Feb 2020
So you don't want to break out of the loop? If the loop ends the function exits
Posts: 201
Threads: 37
Joined: Dec 2021
Apr-26-2022, 01:22 AM
(This post was last modified: Apr-26-2022, 01:23 AM by Frankduc.)
from time import sleep
import time
import smtplib
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
bouton_alert_Del1 = 16
senseur_Del2 = 12
DEL1 = 18
DEL2 = 22
GPIO.setwarnings(False)
GPIO.setup(bouton_alert_Del1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(senseur_Del2,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(DEL1,GPIO.OUT,)
GPIO.setup(DEL2,GPIO.OUT)
led_off1=False
led_off2=False
sender = "[email protected]"
receiver = "[email protected]"
message = f"""\
Subject: Hi Mailtrap
To: {receiver}
From: {sender}
Alarme."""
def decompte(t):
while t:
min , sec = divmod(t,60)
timer = '{:02d}:{:02d}'.format(min, sec)
print(timer, end='\r')
time.sleep(1)
t -= 1
print('Code accept�!')
t = 10
def delai(): #d�lai d'entr�e
print('Vous avez 10 secondes avant l`enclenchement de l`alarme!')
print("Entrez votre code:")
GPIO.output(DEL1,True)
time.sleep(.1)
decompte(int(t))
GPIO.output(DEL1,True)
time.sleep(1)
GPIO.output(DEL1,False)
time.sleep(1)
GPIO.output(DEL1,True)
def courriel():
print("Il y a un intrus!")
try:
server= smtplib.SMTP("smtp.mailtrap.io", 2525)
server.login("096477fce46f57", "49f784782bcc12")
server.sendmail(sender, receiver, message)
print('Courriel envoy�')
except smtplib.SMTPException:
print("Impossible d'envoyer le courriel � " + receiver)
except (smtplib.socket.error, smtplib.SMTPConnectError):
print("Connexion impossible au serveur SMTP")
while(True):
if GPIO.input(bouton_alert_Del1)==0:
if led_off1==False:
print("Le syst�me d'alarme est activ�")
delai()
GPIO.output(DEL1,True)
led_off1=True
sleep(.5)
else:
print('Vous avez 10 secondes pour entrer votre code!')
decompte(int(t)) # this part wont work with brak statement at the end
print("Le syst�me d'alarme est d�sactiv�")
GPIO.output(DEL1,False)
GPIO.output(DEL2,False)
led_off1=False
sleep(.5)
if GPIO.input(senseur_Del2)==1 and GPIO.input(bouton_alert_Del1)==1:
if led_off1==True:
GPIO.output(DEL2,True)
time.sleep(.2)
GPIO.output(DEL2,False)
time.sleep(.2)
led_off2=True
sleep(.5)
courriel() #only one email must be sent
# if i add a break statement only one email is sent but it wont loop back to the beginning of the while
# it become impossible to stop the alarm by just by hitting the push button.
else:
GPIO.output(DEL2,False)
led_off2=True
sleep(.5) see comments
ty
Posts: 4,790
Threads: 76
Joined: Jan 2018
if GPIO.input(senseur_Del2)==1 and GPIO.input(bouton_alert_Del1)==1:
if led_off1==True:
GPIO.output(DEL2,True)
time.sleep(.2)
GPIO.output(DEL2,False)
time.sleep(.2)
led_off2=True
sleep(.5)
if courriel:
courriel()
courriel = None
Posts: 6,799
Threads: 20
Joined: Feb 2020
You should not use sleep() in a program that is supposed to respond to user events. Doesn't the Raspberry Pi time library have a time() function?
Posts: 201
Threads: 37
Joined: Dec 2021
Thank you
Its a quick fix and it will do.
Posts: 201
Threads: 37
Joined: Dec 2021
(Apr-26-2022, 03:57 PM)deanhystad Wrote: You should not use sleep() in a program that is supposed to respond to user events. Doesn't the Raspberry Pi time library have a time() function?
With or without sleep it doesn't make a difference.
Once the email is sent for one time, i can set off the alarm using the push button but if i restart the alarm without rerun the script first, the email wont be sent once again.
Posts: 6,799
Threads: 20
Joined: Feb 2020
Apr-26-2022, 05:04 PM
(This post was last modified: Apr-26-2022, 05:05 PM by deanhystad.)
My sleep comment is for the entire program, not the email.
If you didn't use sleep() you would be forced to use states. If your program used states, you would only perform events when the state changed and you would never have had a problem with sending the email more than once. A poor design decision (using sleep) makes more problems.
|