def api_leds_control(color): if request.method == "POST": if color in LEDS: mod = (int(request.data.get("state")) + 1) % 2 GPIO.output(LEDS[color], int(request.data.get("state"))) time.sleep(0.2) GPIO.output(LEDS[color], int(mod)) return {color: GPIO.input(LEDS[color])}
def run(self): global led_state """ Infinite loop waiting on button presses """ print("Starting button thread") while True: while GPIO.input(BUTTON_PIN) == HIGH: time.sleep(0.01) # wait 10 ms to give CPU chance to do other things print("Button Pressed") if led_state: GPIO.output(LED_PIN, GPIO.LOW) led_state = False else: GPIO.output(LED_PIN, GPIO.HIGH) led_state = True mqttc.publish(MQTT_STATE, payload=(MQTT_ON if led_state else MQTT_OFF), retain=True) while GPIO.input(BUTTON_PIN) == LOW: time.sleep(0.01) # wait 10 ms to give CPU chance to do other things print("Button Released") print("Exiting button thread")
def fan_switch(state): """ Switch fan ON or OFF """ try: fan_state = int(GPIO.input(PIN)) # Check if fan is already in the desired state if fan_state != state: GPIO.output(PIN, state) except Exception as gpio_err: logging.error('GPIO exception: %s', gpio_err)
# Set Variables var_gpio_out = 82 var_gpio_in = 81 # GPIO Setup GPIO.setwarnings(True) GPIO.setmode(GPIO.ROCK) GPIO.setup(var_gpio_out, GPIO.OUT, initial=GPIO.HIGH) # Set up GPIO as an output, with an initial state of HIGH GPIO.setup(var_gpio_in, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set up GPIO as an input, pullup enabled # Test Output print("") print("Testing GPIO Input/Output:") var_gpio_state = GPIO.input(var_gpio_out) # Return state of GPIO print("Output State : " + str(var_gpio_state)) # Print results sleep(1) GPIO.output(var_gpio_out, GPIO.LOW) # Set GPIO to LOW # Test PWM Output p=GPIO.PWM(var_gpio_out, 60) # Create PWM object/instance print("") print("Testing PWM Output - DutyCycle - High Precision:") print("60Hz at 50% duty cycle for 1 second") p.start(50) sleep(1) print("60Hz at 25% duty cycle for 1 second") p.ChangeDutyCycle(25) sleep(1)
RESET = 3 POWER = 5 LED = 7 FAN = 8 # Tell the script if this is running on a ROCK64 or ROCKPRO64 GPIO.setrock("ROCK64") # Setup GPIO.setup(PCB, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(RESET, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(POWER, GPIO.IN) GPIO.setup(LED, GPIO.OUT) GPIO.setup(FAN, GPIO.OUT) IGNORE_PWR_OFF = False if(GPIO.input(POWER) == "0"): # System was started with power switch off IGNORE_PWR_OFF = True # Turn on LED AND FAN GPIO.output(LED, GPIO.HIGH) GPIO.output(FAN, GPIO.HIGH) # Function that blinks LED once when button press is detected def Blink_LED(): GPIO.output(LED, GPIO.LOW) time.sleep(0.2) GPIO.output(LED, GPIO.HIGH) # Monitor for Inputs while True:
print("") print("Testing GPIO Input/Output:") # Set Variables var_gpio_out1 = 16 var_gpio_in1 = 18 # GPIO Setup GPIO.setwarnings(True) GPIO.setmode(GPIO.BOARD) # Test Output GPIO.setup(var_gpio_out1, GPIO.OUT, initial=GPIO.HIGH ) # Set up GPIO as an output, with an initial state of HIGH var_gpio_state = GPIO.input(var_gpio_out1) # Return state of GPIO print("Output State : " + str(var_gpio_state)) # Print results sleep(0.5) GPIO.output(var_gpio_out1, GPIO.LOW) # Set GPIO to LOW GPIO.cleanup(var_gpio_out1) # Cleanup GPIO # Test Input GPIO.setup(var_gpio_in1, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set up GPIO as an input, pullup enabled var_gpio_state = GPIO.input(var_gpio_in1) # Return state of GPIO print("Input State : " + str(var_gpio_state)) # Print results GPIO.cleanup(var_gpio_in1) # Cleanup GPIO print("") print("Test Complete")