Files
RPI-GPIO-fan-temp-control-o…/control_relay.py
2025-11-16 16:18:59 +01:00

34 lines
684 B
Python

import RPi.GPIO as GPIO
import sys
# Use BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Set GPIO pin 18 as output
relay_pin = 18
GPIO.setup(relay_pin, GPIO.OUT)
# Function to turn on the relay
def turn_on_relay():
GPIO.output(relay_pin, GPIO.HIGH)
# Function to turn off the relay
def turn_off_relay():
GPIO.output(relay_pin, GPIO.LOW)
# Check command line arguments
if len(sys.argv) != 2:
print("Usage: python3 control_relay.py <on|off>")
sys.exit(1)
command = sys.argv[1]
if command == "on":
turn_on_relay()
print("Relay is ON")
elif command == "off":
turn_off_relay()
print("Relay is OFF")
else:
print("Invalid command. Use 'on' or 'off'.")