Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more. Accepting essential orders - here's how.
0

Trouble Adafruit_BBIO.GPIO keeping pin low
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Trouble Adafruit_BBIO.GPIO keeping pin low

by mtangy on Fri Sep 23, 2016 2:42 pm

I'm writing a python script to toggle a relay for my home automation system. The relay is low active and remains off while the pin is floating. The script is suppose to flip the pin depending on a string in a text file. I know my file IO is correct but every time i run the script relay just toggles on and off instantly and the pin doesn't remain at the logic level I assign it to. I initially thought the GPIO.cleanup() function should go at the beginning to reset the pins before reassigning them but I have put it at both the beginning and end of the script as well as completely omitting it and it pretty much has the same behavior. Any help would be much appreciated.

Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/env python

import time
import Adafruit_BBIO.GPIO as GPIO

pin = "P8_14"
#GPIO.cleanup()
GPIO.setup(pin, GPIO.OUT)

f = open('/usr/local/bin/relayState.txt', 'r+')
relayState = f.read().strip()
f.seek(0)

if relayState == 'off':
    GPIO.output(pin, GPIO.LOW)
    f.write("on ")
    print "AC ON"

elif relayState == 'on':
    GPIO.output(pin, GPIO.HIGH)
    f.write("off")
    print "AC OFF"

f.close()
#GPIO.cleanup()

mtangy
 
Posts: 21
Joined: Wed Dec 23, 2015 2:09 am

Re: Trouble Adafruit_BBIO.GPIO keeping pin low

by adafruit_support_rick on Sun Sep 25, 2016 12:46 pm

Your program will exit after it sets the relay. That probably resets the pin states. Try putting in an infinite loop and see if that solves the problem.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Please be positive and constructive with your questions and comments.