datetime - Python rpi GPIO output control based on GPIO input and time of day - Stack Overflow
GPIO Code Problem
python - Toggling a GPIO pin set as output - Raspberry Pi Stack Exchange
GPIO and Python - GPIO.output() not working - Raspberry Pi Stack Exchange
Videos
You can't read an output. Just store the state of the pin in a variable.
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
LED = 17
ledState = False
GPIO.setup(LED,GPIO.OUT)
ledState = not ledState
GPIO.output(LED, ledState)
Although stated elsewhere, you CAN read an output by just inputting the same GPIO pin and get the value returned you just set out before:
GPIO.setup(LED_red, GPIO.OUT) #set Pin LED_red as aoutput
GPIO.output(LED_red, GPIO.HIGH) #set Pin LED_red = HIGH (ON)
GPIO.input(LED_red) returns 1
You need to replace ventein with GPIO.output(20, 1) and ventaus with GPIO.output(20, 0).
You seem to think you have declared them as functions - you have not.
As joan's answer has already pointed out the misunderstanding here is likely how to define and use a function in Python. Joan's approach will sure solve the immediate problem and make the program work. I will however address the underlying issue that is not so much related to the Pi but general Python programming.
So how to go about defining and using a function, from here:
- Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
- Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
- The first statement of a function can be an optional statement - the documentation string of the function or docstring.
- The code block within every function starts with a colon (:) and is indented.
- The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
So instead of
ventein = GPIO.output(20, 1)
this is what you're looking for:
def ventein():
GPIO.output(20, 1)
Note the def keyword, the function name (ventein), the parentheses (empty in this case as no arguments are required), and the colon. The final return is ommitted in this case as it is optional if you do not want to pass anything back to the caller.
And when calling this new function instead of:
try:
while True:
if murotemp > 20:
ventein
it should read:
try:
while True:
if murotemp > 20:
ventein()
You read the GPIO. If there is a high voltage (3V3) it will read 1, if there is a low voltage (ground, 0V) it will read 0.
Pin 26 is (Broadcom) GPIO 7.
Ensure that you do not feed a voltage greater then 3V3 into a Pi GPIO. You will likely damage the GPIO and/or the Pi itself.
There are several Python GPIO libraries.
See http://elinux.org/RPi_GPIO_Code_Samples#Python
import RPi.GPIO as GPIO
GPIO.setup(26, 'output')
# Switch on
GPIO.output(26, GPIO.HIGH)
# To read the state
state = GPIO.input(26)
if state:
print('on')
else:
print('off')