CTSavery
Posts: 1
Joined: Mon Jul 03, 2017 6:59 pm

Checking state of GPIO pin

Tue Oct 17, 2017 11:57 pm

So I'm having some issues with checking the state of a GPIO pin without influencing its state.

I have a few scripts that control turning on and off some lights using a relay. In one script I have something like:

Code: Select all

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
pin = 17  # Red LED strip in snake tank

GPIO.setup(pin, GPIO.OUT)
GPIO.setup(pin, GPIO.LOW)
print("And the world becomes just a little more red.")
time.sleep(2)
Now I'd like to have a web server that allows me to trigger this script using a web form. I want to prepopulate the form with the current state of the light which is where I'm running into the problem. I don't seem to be able to figure out if the light is on or off without influencing the state of the light.

My attempt is as follows:

Code: Select all

def get_states():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)

    red_light_pin = 17
    fluorescent_light = 4
    GPIO.setup(red_light_pin, GPIO.IN)
    GPIO.setup(fluorescent_light, GPIO.IN)
    states = {'red': GPIO.input(red_light_pin),
              'fluorescent': GPIO.input(fluorescent_light)}
    GPIO.cleanup()
    return states
The problem is, this resets the state of the pin of if its LOW, it becomes HIGH. Any ideas how I can approach this differently to avoid the problem? Or is there a better way to check the current state of the pins?

If it helps to see the whole project, it's on GitHub here: https://github.com/TechnoConserve/home-control

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

Re: Checking state of GPIO pin

Wed Oct 18, 2017 10:46 am

Hi.

So just so ideas on what might be causing your problems.

first off you are setting up gpio17 as an output to control your relay in your first python program.

but in your second python program you go on to set the same gpio17 as an input which means it will no longer be controlling your relay.

and then to really mess things up you run gpio.cleanup in the second python code which will reset all your gpio to default state.

So I have written a short example program to show you how to do it.

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
pin = 17  # Red LED strip in snake tank

#setup gpio and set ouput high
GPIO.setup(pin, GPIO.OUT)
GPIO.setup(pin, GPIO.HIGH)

time.sleep(5)

#set output low to turn on LED
GPIO.setup(pin, GPIO.LOW)
print("And the world becomes just a little more red.")

#now we test the state of gpio17
states = GPIO.input(pin)
print states

time.sleep(5)

#set output high to turn off LED
print("And the world becomes dark.")
GPIO.setup(pin, GPIO.HIGH)


#now we test the state of gpio17
states = GPIO.input(pin)
print states
so you still set up you gpio17 as output to control your relay/LED

and as the program runs it will turn it on and off as commanded.
but now we also read input from the same gpio17 without needing to change its setup.
and when we do we get what state it currently is, so if you run the code you will see it reporting the state of gpio17 after each time we change its output state.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

Return to “Python”

Who is online

Users browsing this forum: No registered users and 21 guests