![]() |
|
get out of while loop and stop repeat - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: get out of while loop and stop repeat (/thread-37037.html) Pages:
1
2
|
get out of while loop and stop repeat - Frankduc - Apr-25-2022 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 ideathan you RE: get out of while loop and stop repeat - menator01 - Apr-26-2022 Break will get you out of a while loop. RE: get out of while loop and stop repeat - Frankduc - Apr-26-2022 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 RE: get out of while loop and stop repeat - deanhystad - Apr-26-2022 So you don't want to break out of the loop? If the loop ends the function exits RE: get out of while loop and stop repeat - Frankduc - Apr-26-2022 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 = "3@gmail.com"
receiver = "3@gmail.com"
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 commentsty RE: get out of while loop and stop repeat - Gribouillis - Apr-26-2022 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
RE: get out of while loop and stop repeat - deanhystad - Apr-26-2022 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? RE: get out of while loop and stop repeat - Frankduc - Apr-26-2022 Thank you Its a quick fix and it will do. RE: get out of while loop and stop repeat - Frankduc - Apr-26-2022 (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. RE: get out of while loop and stop repeat - deanhystad - Apr-26-2022 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. |