Python Forum
get out of while loop and stop repeat
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get out of while loop and stop repeat
#1
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
Reply
#2
Break will get you out of a while loop.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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
Reply
#4
So you don't want to break out of the loop? If the loop ends the function exits
Reply
#5
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
Reply
#6
        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
Frankduc likes this post
Reply
#7
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?
Reply
#8
Thank you
Its a quick fix and it will do.
Reply
#9
(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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to stop while loop from another script Absolutewind 5 918 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,355 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Repeat request by else stsxbel 2 1,185 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  Avoid multiple repeat in indent Frankduc 8 2,907 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  Stop/continue While loop block Moris526 68 25,635 Mar-28-2021, 09:21 PM
Last Post: Larz60+
  How to discard list repeat values akanowhere 7 3,718 Dec-28-2020, 09:14 PM
Last Post: akanowhere
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,802 Nov-23-2020, 11:01 PM
Last Post: perfringo
  while loop will not stop looping TheTechRobo 5 3,742 Apr-20-2020, 01:47 PM
Last Post: TheTechRobo
  is there a way: repeat key word args Skaperen 2 2,249 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Python Script to repeat Photoshop action in folders and subfolders silfer 2 4,568 Jul-25-2019, 03:12 PM
Last Post: silfer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020