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