May-04-2022, 01:26 PM
Hello,
I have this assigment to do. An interface for an alarm system. I came up with this code at first.
Now i have to turn this into this example from the teacher.
Now deanhystad came with this interesting code that looks a lot like what the teacher wants.
Now i must create a code like the one deanhystad has suggested and include an interface like this one, i am trying to do, but is not working very well for next week.
I dont know where to begin. Any suggestion is welcome.
Thank you
I have this assigment to do. An interface for an alarm system. I came up with this code at first.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
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 = "f@gmail.com" receiver = "f@gmail.com" message = f """\ Subject: Message de Raspberry Pi To: {receiver} From: {sender} Alarme de la maison!""" def decompte_E(t): while t: min , sec = divmod (t, 60 ) timer = '{:02d}:{:02d}' . format ( min , sec) print (timer, end = '\r' ) time.sleep( 1 ) t - = 1 if t = = 0 : print ( 'Code refusé!' ) else : print ( 'Code accepté!' ) t = 10 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)) 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 : print ( 'Vous avez 10 secondes pour entrer votre code!' ) decompte_E( int (t)) GPIO.output(DEL2, True ) time.sleep(. 2 ) GPIO.output(DEL2, False ) time.sleep(. 2 ) led_off2 = True sleep(. 5 ) if courriel: courriel() courriel = None else : GPIO.output(DEL2, False ) led_off2 = True sleep(. 5 ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
Created on 22 avril 2022 @author : basanets ''' from enum import Enum from RPiSim import GPIO from threading import Thread import sys import signal import time PORTE_PIN = 5 BOUTON_PIN = 17 SIRENE_PIN = 18 DEL_PIN = 23 class Etat(Enum): OFF = 1 DELAI_E = 2 ARME = 3 DELAI_S = 4 SIRENE = 5 etat = Etat.OFF class Event(Enum): porte = 1 btnArm = 2 code = 3 boutonInterface = 4 finDelais = 5 ############################################### """ Les fonctions """ ############################################### def terminer(signum, frame): print ( "Terminer" ) GPIO.output(SIRENE_PIN, GPIO.LOW) GPIO.cleanup() sys.exit( 0 ) def event_btnArm(channel): print ( "event bouton arm" ) setEtat(Event.btnArm) def event_porte(channel): print ( "event porte" ) setEtat(Event.porte) def setEtat(event): global etat print ( "set etat" ) if event = = Event.btnArm: if etat = = Etat.OFF: print ( "Delais de sortie" ) etat = Etat.DELAI_S thread = Thread(target = blinkDEL, args = ()) thread.start() # Afficher ecran Désarmé elif etat = = Etat.ARME: print ( "Desarmer" ) etat = Etat.OFF GPIO.output(DEL_PIN, GPIO.LOW) GPIO.output(SIRENE_PIN, GPIO.LOW) # Affficher ecran armé elif etat = = Etat.DELAI_S: print ( "Desarmer" ) etat = Etat.OFF thread.stop() GPIO.output(DEL_PIN, GPIO.LOW) # Affficher ecran armé elif etat = = Etat.SIRENE: print ( "Desarmer" ) etat = Etat.OFF GPIO.output(DEL_PIN, GPIO.LOW) GPIO.output(SIRENE_PIN, GPIO.LOW) # Affficher ecran armé elif etat = = Etat.DELAI_E: print ( "Desarmer" ) etat = Etat.OFF thread.stop() GPIO.output(DEL_PIN, GPIO.LOW) # Affficher ecran armé elif event = = Event.finDelais: if etat = = Etat.DELAI_S: print ( "Armer" ) etat = Etat.ARME GPIO.output(DEL_PIN, GPIO.HIGH) # Afficher ecran Désarmé elif etat = = Etat.DELAI_E: print ( "Sirene" ) etat = Etat.SIRENE GPIO.output(SIRENE_PIN, GPIO.HIGH) elif event = = Event.porte: if etat = = Etat.ARME: print ( "Ouverture d'une porte" ) etat = Etat.DELAI_E thread = Thread(target = blinkDEL, args = ()) thread.start() def blinkDEL(): DELAI = 5 delai = 0 while delai < = DELAI: if GPIO. input (DEL_PIN): GPIO.output(DEL_PIN, GPIO.LOW) else : GPIO.output(DEL_PIN, GPIO.HIGH) time.sleep( 1 ) delai = delai + 1 setEtat(Event.finDelais) ################################### """ Mon programme """ ################################### """ Configuration des GPIOs """ signal.signal(signal.SIGINT, terminer) try : GPIO.setmode(GPIO.BCM) GPIO.setwarnings( False ) """ Bouton poussoir on/off """ GPIO.setup(BOUTON_PIN,GPIO.MODE_IN,pull_up_down = GPIO.PUD_UP) GPIO.add_event_detect(BOUTON_PIN, GPIO.FALLING, callback = event_btnArm) """ Porte """ GPIO.setup(PORTE_PIN,GPIO.MODE_IN,pull_up_down = GPIO.PUD_UP) GPIO.add_event_detect(PORTE_PIN, GPIO.FALLING, callback = event_porte) """ Sirene """ GPIO.setup(SIRENE_PIN,GPIO.MODE_OUT, initial = GPIO.LOW) """ DEL """ GPIO.setup(DEL_PIN,GPIO.MODE_OUT, initial = GPIO.LOW) except Exception: print ( "Problème avec les GPIO" ) while True : time.sleep( 0.5 ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
from re import A import time import enum import RPi.GPIO as GPIO class LEDModes(enum.IntEnum): OFF = 0 ON = 1 BLINK = 2 class LED(): """Class to control LED. Turn on, off or blink""" def __init__( self , pin, period = 0.5 ): self .pin = pin self .period = period GPIO.setup(pin, GPIO.OUT) self .mode = LEDModes.OFF @property def mode( self ): return self ._mode @mode .setter def mode( self , value): """Set to OFF, ON or BLINK""" self ._mode = value self .state = value in (LEDModes.ON, LEDModes.BLINK) GPIO.output( self .pin, self .state) self .blink_time = time.time() def update( self ): """Call periodically to support blinking""" if self ._mode = = LEDModes.BLINK: t = time.time() if t - self .blink_time > = self .period: self .state = not self .state GPIO.output( self .pin, self .state) self .blink_time + = self .period class Button(): """Class that makes buttons easier to work with""" def __init__( self , pin): self .pin = pin GPIO.setup(pin, GPIO.IN, pull_up_down = GPIO.PUD_UP) def pressed( self ): """Return True if button pressed""" return GPIO. input ( self .pin) = = 0 GPIO.setmode(GPIO.BOARD) arm_button = Button( 16 ) arm_lamp = LED( 22 ) alarm_button = Button( 12 ) alarm_lamp = LED( 18 ) armed = alarm = False while True : new_armed = arm_button.pressed() new_alarm = alarm_button.pressed() and new_armed if new_armed ! = armed: armed = new_armed arm_lamp.mode = LEDModes.ON if armed else LEDModes.OFF if new_alarm ! = alarm: alarm = new_alarm if alarm: # send email alarm_lamp.mode = LEDModes.BLINK else : alarm_lamp.mode = LEDModes.OFF alarm_lamp.update() # Update alarm lamp so it will blink |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# -*- coding: utf-8 -*- import pygame from pygame. locals import * import sys import time #screen = pygame.display.set_mode((128, 128)) clock = pygame.time.Clock() pygame.init() #display = pygame.display.set_mode((300, 300)) pygame.display.set_caption( 'Panneau de contr\u00f4le' ) FPS_CLOCK = pygame.time.Clock() #counter, text = 10, '10'.rjust(3) #pygame.time.set_timer(pygame.USEREVENT, 1000) #font = pygame.font.SysFont('Consolas', 30) #pygame.init() BLEU = ( 0 , 0 , 255 ) BLANC = ( 255 , 255 , 255 ) ROUGE = ( 255 , 0 , 0 ) GREEN = ( 0 , 255 , 0 ) BLACK = ( 0 , 0 , 0 ) GRIS = ( 200 , 200 , 200 ) BLANC = ( 255 , 255 , 255 ) NOIR = ( 0 , 0 , 0 ) screen = pygame.display.set_mode([ 700 , 500 ]) # Charger un police de caract\u00e8re sysfont = pygame.font.get_default_font() font = pygame.font.SysFont( None , 48 ) def bouton_A(screen, pos, texte): rayon = 55 pygame.draw.circle(screen, GREEN, pos, rayon) txtBtn = font.render(texte, True , NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) def bouton_D(screen, pos, texte): rayon = 55 pygame.draw.circle(screen, ROUGE, pos, rayon) txtBtn = font.render(texte, True , NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) def bouton_nb(screen, pos, texte): rayon = 25 pygame.draw.circle(screen, GRIS, pos, rayon) txtBtn = font.render(texte, True , NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) def decompte(): #screen = pygame.display.set_mode((128, 128)) #clock = pygame.time.Clock() counter, text = 10 , '10' .rjust( 3 ) pygame.time.set_timer(pygame.USEREVENT, 1000 ) font = pygame.font.SysFont( 'Consolas' , 30 ) run = True while run: for e in pygame.event.get(): if e. type = = pygame.USEREVENT: counter - = 1 text = str (counter).rjust( 3 ) if counter > 0 else 'Merci!' if e. type = = pygame.QUIT: run = False screen.blit(font.render(text, True , ( 255 , 255 , 255 )), ( 300 , 408 )) pygame.display.flip() clock.tick( 60 ) def horloge(): theFont = pygame.font.Font( None , 42 ) clock = pygame.time.Clock() while True : clock.tick( 1 ) theTime = time.strftime( "%H:%M:%S" , time.localtime()) timeText = theFont.render( str (theTime), True ,( 255 , 255 , 255 ),( 0 , 0 , 0 )) screen.blit(timeText, ( 518 , 428 )) #pygame.display.update() sysfont = pygame.font.get_default_font() font = pygame.font.SysFont( None , 36 ) def image(): image = pygame.image.load(r 'C:/home/pi/Documents/3.png' ) display_surface.blit(image, ( 93 , 406 )) running = True while running: for event in pygame.event.get(): if event. type = = pygame.QUIT: running = False #mouse_presses = pygame.mouse.get_pressed() #if mouse_presses[0]: if event. type = = pygame.MOUSEBUTTONDOWN: x , y = pygame.mouse.get_pos() print (x,y) if x > 104 and x < 204 and y > 196 and y < 301 : img = font.render( "Entrez votre code pour d\u00e9sarmer le systeme d'alarme!" , True , NOIR) screen.blit(img, ( 20 , 100 )) img = font.render( "Veuillez entrer votre code pour armer le systeme d'alarme!" , True , BLANC) screen.blit(img, ( 20 , 100 )) decompte() if x < 549 and x > 444 and y > 192 and y < 309 : img = font.render( "Veuillez entrer votre code pour armer le systeme d'alarme!" , True , NOIR) screen.blit(img, ( 20 , 100 )) img = font.render( "Entrez votre code pour d\u00e9sarmer le systeme d'alarme!" , True , BLANC) screen.blit(img, ( 20 , 100 )) #if event.type == pygame.USEREVENT: #counter -= 1 #text = str(counter).rjust(3) if counter > 0 else 'Merci!' #if event.type == pygame.QUIT: #run = False #screen.fill((255, 255, 255)) #screen.blit(font.render(text, True, (255, 255, 255)), (300, 408)) #pygame.display.flip() #clock.tick(60) #screen.fill(BLANC) bouton_A(screen, ( 150 , 250 ), "Activ\u00e9" ) bouton_D(screen, ( 500 , 250 ), "D\u00e9sactiv\u00e9" ) bouton_nb(screen, ( 265 , 200 ), "1" ) bouton_nb(screen, ( 320 , 200 ), "2" ) bouton_nb(screen, ( 375 , 200 ), "3" ) bouton_nb(screen, ( 265 , 255 ), "4" ) bouton_nb(screen, ( 320 , 255 ), "5" ) bouton_nb(screen, ( 375 , 255 ), "6" ) bouton_nb(screen, ( 265 , 310 ), "7" ) bouton_nb(screen, ( 320 , 310 ), "8" ) bouton_nb(screen, ( 375 , 310 ), "9" ) #pygame.draw.circle(screen, GRIS, (260, 250), 25) #pygame.display.flip() img = font.render( "Bienvenue" , True , ROUGE) screen.blit(img, ( 20 , 20 )) pygame.display.flip() pygame.display.update() #horloge() #image() FPS_CLOCK.tick( 30 ) pygame.quit() |
Thank you