added .py to control relay switch for fan

This commit is contained in:
2025-11-16 13:59:33 +01:00
parent 8a56028930
commit e45a764a21

37
control_relay.py Normal file
View File

@@ -0,0 +1,37 @@
import RPi.GPIO as GPIO
import time
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'.")
# Clean up GPIO settings
#GPIO.cleanup()