duckredbeard
Posts: 35
Joined: Mon Apr 29, 2019 1:53 am

First attempt at combining two programs

Sat May 18, 2019 1:10 am

I have two programs that run just fine by themselves. One program monitors two different pins for ground inputs. If the pins get a ground, they do a requests.post and my phone gets a message. These are from door switches. The other program is a single PIR sensor that watches for movement, that movement detection also does a requests.post.

Instead of running them separately, I want to combine them. I got this much running but want to know if there are any necessary "cleanups" that should be done.

Code: Select all

import requests
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

r = requests.post("http://bit.ly/xxxxxxx")  # Door monitor notification
r = requests.post("http://bit.ly/xxxxxxx")  # Motion monitor notification

buttonRDPin = 15  # This pin is not connected, just a placeholder
buttonRUPin = 16

buttonLDPin = 38 # This pin is not connected, just a placeholder
buttonLUPin = 40

GPIO.setup(buttonRUPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(buttonRDPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Again, just a placeholder

GPIO.setup(buttonLUPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(buttonLDPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Again, just a placeholder

ledONPin = 32  # Light on breadboard advising of service started
GPIO.setup(ledONPin, GPIO.OUT)
GPIO.output(ledONPin, GPIO.HIGH)
GPIO.setup(7, GPIO.IN)  # PIR input

llogic = 1  # These don't seem to do anything.
rlogic = 1  # Not gonna delete them because this all works.
while True:
    buttonState = GPIO.input(buttonRUPin)
    if buttonState == False:
        if rlogic == 0:
            r = requests.post("http://bit.ly/xxxxxxx")
            rlogic = 1
    else:
        if rlogic == 1:
            r = requests.post("http://bit.ly/xxxxxxx")
            rlogic = 0
    buttonState = GPIO.input(buttonLUPin)
    if buttonState == False:
        if llogic == 0:
            r = requests.post("http://bit.ly/xxxxxxx")
            llogic = 1
    else:
        if llogic == 1:
            r = requests.post("http://bit.ly/xxxxxxx")
            llogic = 0

    if GPIO.input(7):
        r = requests.post("http://bit.ly/xxxxxxx")  # Motion detected
        time.sleep(15)
    time.sleep(0.1)

You can see the last "if GPIO.input(7)" stuff is just the PIR dropped on the end. I moved all the pin assignments to the top. Once I did that It all started working and I haven't done anything since.

What I get now is two notifications on my phone, letting me know that garage doors and motion are being monitored. I may consolidate that down to one notification (this is done by Tasker in my phone). I also get notifications when either garage door is opened or closed, or if there is motion in the garage. This is the function that I want.

I expect to be adding at least 4 more PIR sensors and 3 more door switches to this, so any advice on where or how they should be added would be appreciated. The only GPIOs I am really using now are 32, 40, 16, and 7 (board numbering).
You can't know your limits until you exceed them.

pcmanbob
Posts: 5650
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: First attempt at combining two programs

Sat May 18, 2019 10:46 am

The code looks fine to me ,

To explain why you need the 2 logic = 1 lines, if they were not there the first time the code runs you would get an error because they will no have been set, because they are not set/re-set until after the if or else statements execute, so you would be trying to check them before they were set.

Hope that makes sense.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”