kempfa
Posts: 8
Joined: Wed Nov 22, 2017 8:57 pm

Sliding door

Thu Jun 07, 2018 9:55 am

Hello,

I am still a beginner in Python programming.

My latest project is this:
I have a fly-screen sliding door that my cats can open with their paws. But they're not closing the door! ;)

I would now like to use a Raspberry PI, a door contact and a relay that drives a small electric motor.
A rubber wheel is mounted to the motor, to close the door again.

After the door has been opened, the program should wait 10 seconds and then query the status of the door contact, if this is set to 0, then
the relay should start the motor and close the door. As soon as the door contact is closed again, the motor should stop.

However, if there is any object in the door, for example, so that the door contact cannot return the status closed,
the engine must stop. If this is not the case, the engine will run hot and break down.

Here's the script I've written so far.

Thanks for your help!

Code: Select all

import time
import RPi.GPIO as io
import subprocess
import os
import random
import RPi.GPIO as GPIO

## set GPIO mode to BCM
io.setmode(io.BCM)

# Pin 18 (GPIO 18) set as Output
GPIO.setup(18, GPIO.OUT)

## enter the number of whatever GPIO pin you're using
door_pin = 21

## use the built-in pull-up resistor
io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP)  # activate input with PullUp
# io.setup(Relay_pin, io.out)

## initialize door
door=0

## this loop will execute the if statement that is true
while True:
    ## if the switch is open
    if io.input(door_pin) and door == 0:
        print("Door is open!")
        door=1
        time.sleep(5) # Waitingtime til the door gets closed
        # Switch Output on
        GPIO.output(18, GPIO.LOW)
        print("Relay is on")
        time.sleep(12) # wait 12 seconds before the next action
        if io.input(door_pin) and door == 0:
            print("Door is still open!")
            kill()

    ## if the switch is closed and door does not equal 1
    if io.input(door_pin)==False and door == 1:
        print("Door is closed")
        door = 0
        # Switch Output off
        GPIO.output(18, GPIO.HIGH)
        print("Relay is off")

    time.sleep(0.5) # wait 0.5 seconds before the next action

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Sliding door

Thu Jun 07, 2018 10:27 am

How are you detecting that there is an object blocking the door? Do you have a sensor for this?

What part of the program do you require help with? You didn't mention this.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

kempfa
Posts: 8
Joined: Wed Nov 22, 2017 8:57 pm

Re: Sliding door

Tue Jun 12, 2018 12:05 pm

Hello scotty101,

first of all thank you for your reply.

I thought the script could check the status of the existing door sensor after 10 seconds and if it is not 0, the motor will stop.

Thx!

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Sliding door

Tue Jun 12, 2018 2:43 pm

scotty101 wrote:
Thu Jun 07, 2018 10:27 am
What part of the program do you require help with? You didn't mention this.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

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

Re: Sliding door

Tue Jun 12, 2018 3:33 pm

However, if there is any object in the door, for example, so that the door contact cannot return the status closed,
the engine must stop. If this is not the case, the engine will run hot and break down.
assuming this is the part of your code you want help with.

having looked at your code you seemed to be importing RPi.GPIO twice as GPIO and io so I removed the io and just used GPIO through out.

Code: Select all

import time
import RPi.GPIO as GPIO

## set GPIO mode to BCM
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Pin 18 (GPIO 18) set as Output
GPIO.setup(18, GPIO.OUT)

# Pin 21 (GPIO 21) set as Input use the built-in pull-up resistor
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # activate input with PullUp
GPIO.output(18, GPIO.HIGH) # we turn off the motor on program start

# this loop will execute the if statement that is true
while True:

    # checks to see if door closed and just waits
    while GPIO.input(21) == 0:  
        time.sleep(1)
        
    print ("Door opened waiting 5 sec before closing")
    time.sleep(5)
    
    # Switch Output on
    GPIO.output(18, GPIO.LOW)
    print("Relay is on")
    
    # start timer and and stop motor if door closes or time > 10 sec
    timer = 0
    while timer < 11:
        if GPIO.input(21) == 0:
            GPIO.output(18, GPIO.HIGH)
            print("Door is closed")
            print("Relay is off")
            timer = 11
        timer = timer + 1
        print (timer)
        time.sleep(1)  

        # only runs if timer times out and door not shut.
        if timer == 11:
            GPIO.output(18, GPIO.HIGH)
            print("Door failed to close")
            print("Relay is off")
            print ("waiting 30 sec before trying to close door")
            time.sleep(30)
 
So this code waits for the door to open then waits for 5 seconds before trying to close it, if how ever after the motor has started the door is not closed after 11 seconds the motor is turned off and the program then waits a further 30 seconds before trying again.
you could add a fail safe that only allows 1 or 2 attempts to close the door before locking out the activation of the relay, until the door is manually closed, if required.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Beginners”