3

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()
3

You are using the cleanup function. As soon as that is called (when you exit the program) it will reset the GPIO to be (high impedance) inputs.

It may work as intended if you remove the cleanup calls.

  • I don't understand. If I run sudo python3 myfile.py holdopen it should use the function the gate_hold_open() which doesn't have a cleanup before exiting. – brainiak4431 Nov 2 '15 at 9:05
  • Yes, sorry, I didn't read the code carefully enough. I still suspect cleanup is being called. What do the logs contain? – joan Nov 2 '15 at 9:16
  • That's the other thing. The log file is being created but nothing is getting logged. Somewhere in my code there is a problem after the GPIO command is set, but before any logging takes place. I don't know if it's the exception handling? I'm going to try taking out all the try and except stuff. – brainiak4431 Nov 2 '15 at 9:20
  • I suspect the logging is failing, causing the catchall except to be called. – joan Nov 2 '15 at 9:23
  • I took out all the logging and all the exceptions. I even tried changing the pin to BCM4. It's just not working. – brainiak4431 Nov 2 '15 at 9:55
2

I finally figured it out. Just a simple syntax problem ruined the whole thing. The checks to see which argument was passed at command line and then run the function was wrong. if gate_command is "open": should really be if gate_command == "open": I do the same changes to the other checks. Now the functions are called properly.

Additionally there is an exception being thrown, presumably the warning message from RPi.GPIO about pin 2 being a pull up resistor. To fix this all the sys.exit() should be changed to os._exit(1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.