I run my python script below using:
sudo python3 /path/to/file.py open
or sudo python3 /path/to/file.py holdopen
. These are supposed to cause a change in GPIO Pin 2 from high to low. For some reason it is not changing state (I used a multimeter to check for voltage drops). It stays at 3.3 V.
When I use simple commands outside of the script like GPIO.output(2, GPIO.LOW)
the voltage does drop to almost 0 V.
I am getting a warning about pull up resistor on BCM Pin 2 but this happens with or without the script. The voltage changes despite the warning when I give the individual command, but not with my script and the warning.
All help is appreciated.
#!/usr/bin/env python3
# -*- coding:utf8 -*-
# Import required Python libraries
import RPi.GPIO as GPIO # used for controlling GPIO pins
import os # used for getting file path
import logging # used for log file
import sys # used for command line arguments
import time
# Create log file
log_file = os.path.dirname(os.path.realpath(__file__)) + "/gate.log"
logging.basicConfig(filename=log_file, format='%(asctime)s %(message)s', level=logging.INFO) #level=logging.DEBUG
logging.info("===================SCRIPT STARTED===================")
# Setup command line
gate_command = None
if(len(sys.argv) < 2):
logging.info("No gate command given. Program exited.")
print("No gate command given. Program exited.")
logging.info("Potential gate commands: open, close, holdopen")
print("Potential gate commands: open, close, holdopen")
logging.info("Example: gate_control.py open")
print("Example: gate_control.py open")
sys.exit(0)
gate_command = sys.argv[1]
possible_gate_commands = [ "open", "close", "holdopen" ]
if gate_command not in possible_gate_commands:
logging.info("Invalid gate command given. Program exited.")
print("Invalid gate command given. Program exited.")
sys.exit(0)
# Use BCM GPIO references instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO pins to use
pinList = [2]
# Loop through pins and set initial input/output mode and state to low/high
for i in pinList:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
# Define functions
def gate_open() :
for i in pinList:
GPIO.output(i, GPIO.LOW) # turn relay on
time.sleep(0.5)
GPIO.output(i, GPIO.HIGH) # turn relay off
GPIO.cleanup() # reset gpio pins
logging.info("Gate opened.")
print("Gate opened.")
sys.exit(0)
def gate_hold_open() :
for i in pinList:
GPIO.output(i, GPIO.LOW) # turn relay on
logging.info("Gate held open.")
print("Gate held open.")
sys.exit(0)
def gate_close() :
for i in pinList:
GPIO.output(i, GPIO.HIGH) # turn relay off
GPIO.cleanup() # reset gpio pins
logging.info("Gate closed.")
print("Gate closed.")
sys.exit(0)
def keyboard_interrupt() :
GPIO.cleanup()
logging.info("Keyboard Interrupt.")
print("Keyboard Interrupt.")
sys.exit(0)
def cleanup_exit() :
GPIO.cleanup()
sys.exit(0)
# Call functions
if gate_command is "open":
try:
gate_open()
except KeyboardInterrupt:
keyboard_interrupt()
except:
logging.info("Gate Opening Error.")
print("Gate Opening Error.")
cleanup_exit()
if gate_command is "holdopen":
try:
gate_hold_open()
except KeyboardInterrupt:
keyboard_interrupt()
except:
logging.info("Gate Holding Open Error.")
print("Gate Holding Open Error.")
cleanup_exit()
if gate_command is "close":
try:
gate_close()
except KeyboardInterrupt:
keyboard_interrupt()
except:
logging.info("Gate Opening Error.")
print("Gate Opening Error.")
cleanup_exit()