Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simulation of alarm system
#1
Hello,

I am trying to simulate an alarm system on a raspberry pi. I would like Led2 to blink when intruder is detected.
I would like also to add an email alert when intruder is detected. I have no idea how to send an email using pi.
For now i reshaped a code i have found on the net.
I can activate the system by pushing button 1 and Led2 will light if intruder is detected. If i click a second time alarm is off.

from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD) 
button1=16 
button2=12 
LED1=22
LED2=18 
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP) 
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP) 
GPIO.setup(LED1,GPIO.OUT,) 
GPIO.setup(LED2,GPIO.OUT) 
BS1=False 
BS2=False
while(1): 
        if GPIO.input(button1)==0: 
                if BS1==False: 
                        print("Alarm on")
                        GPIO.output(LED1,True)
                        BS1=True 
                        sleep(.5) 
                else: 
                        print("Alarm off")
                        GPIO.output(LED1,False)
                        GPIO.output(LED2,False)
                        BS1=False
                        sleep(.5)
        if GPIO.input(button2)==0 and GPIO.input(button1)==1:
                print("There is an intruder")
                if BS1==True:
                        GPIO.output(LED2,True)
                        BS2=True
                        sleep(.5)
                else:
                        GPIO.output(LED2,False)
                        BS2=True
                        sleep(.5)
Any idea how it could be fix?
Thank you
Reply
#2
Sending an email from Raspberry Pi

https://bc-robotics.com/tutorials/sendin...pberry-pi/
https://raspberrypi-guide.github.io/prog...%20More%20
Reply
#3
I have read many tutorials on the matter but the second link is the simplest way i have seen so far.

Thank you
Reply
#4
You do not want to sleep. If you are sleeping you cannot see a button getting pushed.

Instead of sleeping, you need to identify when the STATE changes and take appropriate actions. Your machine has 3 states: Disarmed, Armed, Alarm. The code below represents these three states using the variables armed and alarm.
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
Be warned that I don not have a Raspberry Pi, so none of this code is tested.
Reply
#5
There is a few errors poping up, and of course without a pi it was to be expected.
I tried to insert emails from the example of the second link and it seems like it work.
Its just the blinking part of the second led i cant figure out where i went wrong. But it doesn't have to blink, as long as its on when alarm start.

I know i was missing GPIO.setwarnings(False) and the time.sleep(1) should of done the trick.

Thanks for the code its an interesting way to built this with classes and defs.
Reply
#6
Your code has BS2 = True but no BS2 = False.

Your variable names are terrible. Somebody readying your code should not have to remember that button1, LED1 and BS1 are related to arming the alarm and button2, LED2 and BS2 are related to the alarm being triggered. The relationship should be explicit in the names. Good variable names will make your code much easier to read.

The main difference between your code and my code is not the classes, it is that your code sleeps and mine does not. My Button class is just a wrapper around GPIO input(n) to make the code read easier. The LED class adds a blink feature to GPIO output(n), but again is mostly a wrapper. These are "syntactic sugar" that let me write code that is more focused on the problem space (your alarm system) and less on the platform (GPIO stuff). The nice thing about the classes is they are reusable and will make my next Raspberry Pi project simpler to write.
Frankduc likes this post
Reply
#7
I agree i changed that. Like i said i recycled someone else code and was in testing phase.
Now button1 is button_alarm_led1 and button2 change for sensor_led2.
I fixed the blink part, i forgot GPIO.output(led2,false)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Alarm system with state, class, enum. Frankduc 0 1,271 May-04-2022, 01:26 PM
Last Post: Frankduc
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,660 Jan-11-2021, 06:30 PM
Last Post: ykumar34
Question Difference between Python's os.system and Perl's system command Agile741 13 6,862 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Help with simulation Geeseball 0 2,072 Oct-18-2018, 12:19 PM
Last Post: Geeseball
  Problems with Interrupts/ callback functions in Python for an Alarm Clock project Henry1 1 4,867 Dec-16-2016, 10:17 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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