Python Forum
Convert looping home security to a one time scan of switches and sensors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert looping home security to a one time scan of switches and sensors
#1
I present to you my home security program that monitors several doors and the activity of one PIR sensor. This program runs on boot and monitors GPIOs for button events. The bit.ly links are Tasker/Join URLs that initiate keywords to my Android phone and tablet. My Android devices use these pushes to generate notifications and other interactive elements when a security event happens. I have removed personal components of that URL for privacy issues and replaced them with "XxXxXxX". If the full URL were posted, my APIs would be revealed, wreaking havoc on my monitoring efforts.

There are occasions that the indications get out of sync with reality if two doors are opened and/or closed simultaneously. This causes Tasker on the Android devices to get choked up and indicate conflicting info (two notifications indicating open and closed at the same time when the "closed" event should have cancelled the "open" notification).

What I am wanting to do is take the original script and make it more of an "interrogation" of what the states are currently. By polling the switches, it would return what is real at that moment. How do I interrogate the state of the switches and have it do the appropriate requests.post for its switch state?

Behold the code for the monitor:

from gpiozero import Button, MotionSensor, LED, PWMLED
import requests
from signal import pause
from time import sleep
import time
start = time.time()
enablepir = 0
from datetime import datetime

now = datetime.now()

current_time = now.strftime("%H:%M:%S")
#print("Current Time =", current_time)

#  Define LED indicators - these are GPIO numbers
motionmon = LED(8)  # LED indicating motion is being monitored
motionact = LED(7)  # LED indicating that motion has been detected
garagerun = PWMLED(17)  # LED blinking that indicates that this python program is running

#  Start notifications and indicators
r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone for notification that service has started
garagerun.blink(.05, 2.5)  # LED blinks to indicate program is running (off or solid indicates stoppage)
motionmon.on()  # LED is commanded on, though the ground is controlled by an external switch that also supplies the ground to the PIR sensor
print("Service started at", current_time)


#  Define inputs - these are GPIO numbers
leftdoor = Button(26)  # Input from left garage door button
rightdoor = Button(16)  # Input from right garage door button
garageentry = Button(21)  # Input from garage entry door
denentry = Button(15)  # Input from den entry door
garagepassage = Button(20)  # Input from garage passage door
stairspassage = Button(18)  # Input from door at top of stairs
hallpir = MotionSensor(13)  # Signal input from PIR in hall (motion detection)
motionenable = Button(19) # Externally switched ground from Sonoff channel 3 (see line 89)
frontdoor = Button(25) # Input from front door


#  Define what to do when switches or PIRs are active
def ropen():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone that the Right door is open


def rclosed():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone that the Right door is closed


def lopen():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone that the Left door is open


def lclosed():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone that the Left door is closed


def hallmotion():
    global start, enablepir
    if enablepir == 1:
        now = time.time()
        if now > start:
            r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone that motion has been detected in the hall
            motionact.blink(.2, .2)  # LED blinks to indicate that motion has been detected
            motionmon.off()  # LED indicating that motion monitoring is active turns off
            start = time.time()
            start = start + 5


def hallmotionstop():
    motionact.off()  # LED stops blinking when motion stops
    motionmon.on()  # LED turns on to indicate motion is being monitored


def garpassclosed():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone that the Passage door to garage is closed


def garpassopen():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone that the Passage door to garage is open


def garentopen():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone that the Garage entry door is open


def garentclosed():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to phone that the Garage entry door is closed
    
    
def motionenabled():
    global enablepir
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to my phone that motion monitor is enabled
    sleep(4)  # Change the sleep time if you need more time before motion enabled
    enablepir = 1
    
    
def motiondisabled():
    global enablepir
    enablepir = 0
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to my phone that motion monitor is disabled
    
    

def denentopen():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to my phone my phone that the den door is open
    
    

def denentclosed():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to my phone that the den door is closed
    


def stairspassopen():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to my phone that the stairs door is open



def stairspassclosed():
    r = requests.post("https://bit.ly/XxXxXxX")  # Message to my phone that the stairs door is closed
    
    
    
def frontopen():
    r = requests.post("https://bit.ly/XxXxXxX")  #  Message to my phone that the front door is open
    
    
    
def frontclosed():
    r = requests.post("https://bit.ly/XxXxXxX")  #  Message to my phone that the front door is closed
    
    
    
#  Define switch and PIR states
leftdoor.when_released = lopen
leftdoor.when_pressed = lclosed
rightdoor.when_pressed = rclosed
rightdoor.when_released = ropen
garagepassage.when_pressed = garpassopen
garagepassage.when_released = garpassclosed
garageentry.when_pressed = garentopen
garageentry.when_released = garentclosed
hallpir.when_motion = hallmotion
hallpir.when_no_motion = hallmotionstop
motionenable.when_pressed = motionenabled
motionenable.when_released = motiondisabled
denentry.when_pressed = denentclosed
denentry.when_released = denentopen
stairspassage.when_pressed = stairspassclosed
stairspassage.when_released = stairspassopen
frontdoor.when_pressed = frontclosed
frontdoor.when_released = frontopen


pause()
I have tried to resolve this on the Android devices to no avail. If one is powered down, it will display its last known state of the switches upon power up. Upon successful adaptation of this code, the rebooting Android would send an SSH to the RPi upon boot to run the new .py to interrogate and sync itself with the state of the switches. I would also be able to clear any "hiccups" that are caused by the events tripping over each other and leaving residual incorrect notifications.

I compel thee to opine...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Raspberry PI with DHT22 Sensors itpilot 6 1,304 May-12-2023, 02:52 AM
Last Post: deanhystad
  Scan for Bluetooth device korenron 0 2,631 Jan-10-2022, 01:06 PM
Last Post: korenron
  scan drives in windows from Cygwin RRR 1 1,641 Nov-29-2020, 04:34 PM
Last Post: Larz60+
  Convert to UTC without changing time pythonlearner1 1 2,121 Nov-05-2020, 09:54 PM
Last Post: pythonlearner1
  Home Directory pgoosen 9 6,415 Oct-15-2020, 12:37 PM
Last Post: DeaD_EyE
  How to scan huge files and make it in chunks ampai 2 2,610 May-28-2020, 08:20 PM
Last Post: micseydel
  Convert quarterly time series to monthly time series donnertrud 1 5,194 May-22-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Receiving XML exception from nmap.scan() results. PythonNmap 4 4,115 Jan-21-2020, 04:41 AM
Last Post: PythonNmap
  Convert weekly sequences to date and time. SinPy 0 1,456 Nov-23-2019, 05:20 PM
Last Post: SinPy
  Problem connecting to 2 switches, HELP! Netcode 0 1,231 Nov-15-2019, 10:30 PM
Last Post: Netcode

Forum Jump:

User Panel Messages

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