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 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)
def do_GET(self): global led_state # pylint: disable=invalid-name """ Called when a GET request is made """ if self.path == "/on": GPIO.output(LED_PIN, GPIO.HIGH) led_state = True self.send_response(200) self.end_headers() mqttc.publish(MQTT_STATE, payload=MQTT_ON, retain=True) elif self.path == "/off": GPIO.output(LED_PIN, GPIO.LOW) led_state = False self.send_response(200) self.end_headers() mqttc.publish(MQTT_STATE, payload=MQTT_OFF, retain=True) elif self.path == "/toggle": if led_state: GPIO.output(LED_PIN, GPIO.LOW) led_state = False else: GPIO.output(LED_PIN, GPIO.HIGH) led_state = True self.send_response(200) self.end_headers() mqttc.publish(MQTT_STATE, payload=(MQTT_ON if led_state else MQTT_OFF), retain=True) elif self.path == "/status": self.send_response(200) self.send_header('Content-Type', 'application/json') self.end_headers() json_str = json.dumps({"led": led_state}) self.wfile.write(json_str.encode(encoding='utf_8'))
def main(): """ Run temperature check every 3 seconds untill stopped by user """ try: logging.info('Started fan service...') setup(PIN) while True: temp = get_temp() check_temp(temp, MIN_TEMP, MAX_TEMP) sleep(2) except KeyboardInterrupt: logging.info('Stopped fan service...') finally: fan_switch(GPIO.LOW) GPIO.cleanup(PIN)
def main(): """ Run temperature control service """ try: old_temp, old_speed = 0, 0 fan = gpio_setup() fan.start(MIN_SPEED) logging.info('Started temperature control service...') while True: temp, speed = temp_ctrl(fan, old_temp, old_speed) old_temp, old_speed = temp, speed sleep(INTERVAL) except KeyboardInterrupt: fan.stop() logging.info('Stopped temperature control service.') else: fan.stop() logging.error('Unexpected error occurred, please check script.') finally: GPIO.cleanup(GPIO_PIN)
def setup(pin): """ Setup GPIO pin """ GPIO.setmode(GPIO.BOARD) GPIO.setup(pin, GPIO.OUT) GPIO.setwarnings(False)
def __init__(self): # rospy.init_node("spinny") self.command_topic = '/motor_speed' self.sub = rospy.Subscriber(self.command_topic, Int32, self.speed_callback) print("Setting up GPIO pins") # rospy.on_shutdown(self.shut) self.pwm_pin = 16 self.power_pin = 18 GPIO.setwarnings(True) GPIO.setmode(GPIO.BOARD) GPIO.setup(self.pwm_pin, GPIO.OUT) GPIO.setup(self.power_pin, GPIO.OUT, initial=GPIO.HIGH) print("Starting PWM output") self.p = GPIO.PWM(self.pwm_pin, 50) self.p.start(0)
def gpio_setup(): """ Setup GPIO_PIN as PWM input and start fan """ try: GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(GPIO_PIN, GPIO.OUT) fan = GPIO.PWM(GPIO_PIN, PWM_FREQ) return fan except IndexError: print('\n Error: Pin "{0}" does not exist.\n') sys.exit(2) else: print('\n Error: Could not setup GPIO pin "{0}".\n'.format(GPIO_PIN)) sys.exit(4)
def mqtt_on_message(client, userdata, msg): """ Handle commands coming in through MQTT """ global led_state # pylint: disable=unused-argument print("MQTT Command Received") print("MQTT Command:" +msg.topic+" "+msg.payload.decode()) if msg.payload.decode() == MQTT_ON: GPIO.output(LED_PIN, GPIO.HIGH) led_state = True mqttc.publish(MQTT_STATE, payload=MQTT_ON, retain=True) elif msg.payload.decode() == MQTT_OFF: GPIO.output(LED_PIN, GPIO.LOW) led_state = False mqttc.publish(MQTT_STATE, payload=MQTT_OFF, retain=True) elif msg.payload.decode() == MQTT_TOGGLE: 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)
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")
MQTT_ON = "ON" MQTT_OFF = "OFF" MQTT_TOGGLE = "TOGGLE" # The RockPro version of the library seems to return strings not ints - is this # intended behavior? HIGH = "1" LOW = "0" #LED_PIN = 27 #BUTTON_PIN = 4 LED_PIN = 16 BUTTON_PIN = 13 GPIO.setmode(GPIO.BOARD) GPIO.setrock('ROCKPRO64') GPIO.setwarnings(False); GPIO.setup(LED_PIN, GPIO.OUT) GPIO.output(LED_PIN, GPIO.LOW) led_state = False time.sleep(1) GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) class MyHandlerForHTTP(BaseHTTPRequestHandler): """ Handler for HTTP messages """
#!/usr/bin/env python import R64.GPIO as GPIO from time import sleep import rospy from std_msgs.msg import Int32 pwm_pin=16 power_pin=18 GPIO.setwarnings(True) GPIO.setmode(GPIO.BOARD) GPIO.setup(pwm_pin, GPIO.OUT) GPIO.setup(power_pin, GPIO.OUT, initial=GPIO.HIGH) print("Starting PWM output") p=GPIO.PWM(pwm_pin,50) p.start(0) def speed_callback(msg): num = msg.data p.ChangeDutyCycle(num) print("Changing Duty Cycle") print(num) if __name__ == "__main__": rospy.init_node("spinny") print("Setting up GPIO pins")
#!/usr/bin/env python # Allison Creely, 2018, LGPLv3 License # Rock 64 GPIO Library for Python import R64.GPIO as GPIO from time import sleep # Set Variables var_gpio_out = 16 # GPIO Setup GPIO.setwarnings(True) GPIO.setmode(GPIO.BOARD) GPIO.setup(var_gpio_out, GPIO.OUT, initial=GPIO.HIGH ) # Set up GPIO as an output, with an initial state of HIGH # Test Output print("") print("Testing GPIO Input/Output:") while True: print("On") GPIO.output(var_gpio_out, GPIO.HIGH) sleep(1) print("Off") GPIO.output(var_gpio_out, GPIO.LOW) sleep(1)
def Blink_LED(): GPIO.output(LED, GPIO.LOW) time.sleep(0.2) GPIO.output(LED, GPIO.HIGH)
#!/usr/bin/python -u # Author: Mrfixit2001 - Enables LED and Monitors for the buttons on the Kintaro SNES Case and ROSHAMBO Case import time import os import subprocess import string import R64.GPIO as GPIO # NOTE: the R64GPIO package doesn't support "add_event_detect", so we can't use callbacks # Initialize GPIO.cleanup() GPIO.setmode(GPIO.BOARD) PCB = 10 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
print("GPIO.BCM " + str(GPIO.BCM)) print("GPIO.OUT " + str(GPIO.OUT)) print("GPIO.IN " + str(GPIO.IN)) print("GPIO.HIGH " + str(GPIO.HIGH)) print("GPIO.LOW " + str(GPIO.LOW)) print("GPIO.PUD_UP " + str(GPIO.PUD_UP)) print("GPIO.PUD_DOWN " + str(GPIO.PUD_DOWN)) print("GPIO.VERSION " + str(GPIO.VERSION)) print("GPIO.RPI_INFO " + str(GPIO.RPI_INFO)) # Set Variables var_gpio_out = 16 var_gpio_in = 18 # GPIO Setup GPIO.setwarnings(True) GPIO.setmode(GPIO.BOARD) 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 Input var_gpio_state = GPIO.input(var_gpio_in) # Return state of GPIO
print("GPIO.HIGH " + str(GPIO.HIGH)) print("GPIO.LOW " + str(GPIO.LOW)) print("GPIO.PUD_UP " + str(GPIO.PUD_UP)) print("GPIO.PUD_DOWN " + str(GPIO.PUD_DOWN)) print("GPIO.VERSION " + str(GPIO.VERSION)) print("GPIO.RPI_INFO " + str(GPIO.RPI_INFO)) 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
#!/usr/bin/python from flask import request from flask_api import FlaskAPI import R64.GPIO as GPIO import config import time LEDS = {"trigger": 16} #LEDS = {"green": 16} GPIO.setmode(GPIO.BOARD) GPIO.setup(LEDS["trigger"], GPIO.OUT) #GPIO.setup(LEDS["red"], GPIO.OUT) app = FlaskAPI(__name__) @app.route('/', methods=["GET"]) def api_root(): return { "led_url": request.url + "led/(trigger | green | red)/", "led_url_POST": { "state": "(0 | 1)" } } @app.route('/led/<color>/', methods=["GET", "POST"]) def api_leds_control(color): if request.method == "POST": if color in LEDS:
import R64.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BOARD) GPIO.setup(16, GPIO.OUT) GPIO.setup(18, GPIO.OUT, initial=GPIO.HIGH) p = GPIO.PWM(16, 50) p.start(0) sleep(5) p.ChangeDutyCycle(20) sleep(5) p.ChangeDutyCycle(40) sleep(5) p.ChangeDutyCycle(60) sleep(5) p.ChangeDutyCycle(80) sleep(5) p.ChangeDutyCycle(98) sleep(5) p.stop() GPIO.cleanup()
print("GPIO.BCM " + str(GPIO.BCM)) print("GPIO.OUT " + str(GPIO.OUT)) print("GPIO.IN " + str(GPIO.IN)) print("GPIO.HIGH " + str(GPIO.HIGH)) print("GPIO.LOW " + str(GPIO.LOW)) print("GPIO.PUD_UP " + str(GPIO.PUD_UP)) print("GPIO.PUD_DOWN " + str(GPIO.PUD_DOWN)) print("GPIO.VERSION " + str(GPIO.VERSION)) print("GPIO.RPI_INFO " + str(GPIO.RPI_INFO)) # 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
# Frank Mankel, 2018, LGPLv3 License # Rock 64 GPIO Library for Python # Thanks Allison! Thanks smartdave! import R64.GPIO as GPIO from time import sleep print("Output Test R64.GPIO Module...") # Set Variables var_gpio_out = 36 # Pin 16 - 148 is Pin 8, 149 is Pin 18 #var_gpio_in = 18 # 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:") while True: var_gpio_state = GPIO.input(var_gpio_out) # Return State of GPIO print("Output State: " + str(var_gpio_state)) if var_gpio_state == 1: GPIO.output(var_gpio_out,0) # Set GPIO to LOW print("Turning off") # Print results else: