def attach_interrupts(self): if self.return_config_bool("pcb") and GPIO.input( self.check_pin ) == GPIO.LOW: # check if there is an pcb and if so attach the interrupts GPIO.add_event_detect(self.check_pin, GPIO.RISING, callback=self.pcb_interrupt ) # if not the interrupt gets attached if GPIO.input( self.power_pin ) == GPIO.HIGH: #when the system gets startet in the on position it gets shutdown print('shutting down') os.system("sudo shutdown -h now") else: self.led(1) GPIO.add_event_detect(self.reset_pin, GPIO.FALLING, callback=self.reset_interrupt) GPIO.add_event_detect(self.power_pin, GPIO.RISING, callback=self.power_interrupt) else: #no pcb attached so lets exit print('no PCB detected') GPIO.cleanup() exit()
def __init__(self, config): gpio.setwarnings(False) gpio.cleanup() gpio.setmode(gpio.BCM) self.config = config self.doors = [Door(n, c) for (n, c) in config['doors'].items()] self.updateHandler = UpdateHandler(self) for door in self.doors: door.last_state = 'unknown' door.last_state_time = time.time() self.use_alerts = config['config']['use_alerts'] self.alert_type = config['alerts']['alert_type'] self.ttw = config['alerts']['time_to_wait'] if self.alert_type == 'smtp': self.use_smtp = False smtp_params = ("smtphost", "smtpport", "smtp_tls", "username", "password", "to_email") self.use_smtp = ('smtp' in config['alerts']) and set(smtp_params) <= set( config['alerts']['smtp']) syslog.syslog("we are using SMTP") elif self.alert_type == 'pushbullet': self.pushbullet_access_token = config['alerts']['pushbullet'][ 'access_token'] syslog.syslog("we are using Pushbullet") elif self.alert_type == 'pushover': self.pushover_user_key = config['alerts']['pushover']['user_key'] syslog.syslog("we are using Pushover") else: self.alert_type = None syslog.syslog("No alerts configured")
def GPIO_PULL_UPDW_TESTING(): checkPins = [] print('== Testing GPIO PULL_UP_DOWN ==') testPin = gpioUsedPins print("Check that nothing connects to those pins: %s" % (','.join([str(x) for x in testPin]))) GPIO.setmode(GPIO.BOARD) GPIO.setup(testPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) for pin in testPin: if (GPIO.input(pin) != InternalPullUpDnValue[PullUpDnPins[pin] if pin in PullUpDnPins else GPIO.PUD_UP]): checkPins.append(pin) GPIO.setup(testPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) for pin in testPin: if (GPIO.input(pin) != InternalPullUpDnValue[PullUpDnPins[pin] if pin in PullUpDnPins else GPIO.PUD_DOWN]): checkPins.append(pin) print("[%s] Pull Up and Down" % ('PASS' if len(checkPins) <= 0 else 'FAILED')) if (len(checkPins) > 0): print('Please check those pins: %s' % (','.join([str(x) for x in checkPins]))) GPIO.cleanup() print('===============================')
def GPIO_IO_TESTING(): print('== Testing GPIO INPUT/OUTPUT ==') for mode in ['phys', 'TB', 'BCM']: GPIO.setmode(modeMap[mode]) LPin = [pinTable[pins[0] - 1][mode] for pins in pairPins] RPin = [pinTable[pins[1] - 1][mode] for pins in pairPins] if (-1 in LPin or -1 in RPin): print('Some pins use the 3.3V or GND pin.') exit() for IPin, OPin in [(LPin, RPin), (RPin, LPin)]: GPIO.setup(IPin, GPIO.IN) GPIO.setup(OPin, GPIO.OUT) if (False in [GPIO.gpio_function(pin) == GPIO.IN for pin in IPin] or False in [GPIO.gpio_function(pin) == GPIO.OUT for pin in OPin]): print('Check GPIO.gpio_function or GPIO.setup.') exit() for volt in [GPIO.HIGH, GPIO.LOW]: GPIO.output(OPin, volt) OResult = [GPIO.input(pin) == volt for pin in OPin] IResult = [ GPIO.input(IPin[i]) == GPIO.input(OPin[i]) for i in range(len(IPin)) ] if (False in OResult): print('Check Pin[%d].' % (OPin[OResult.index(False)])) exit() if (False in IResult): print('Check Pin[%d].' % (IPin[IResult.index(False)])) exit() print("[PASS] GPIO.setmode(%s)" % (modeNameMap[mode])) GPIO.cleanup() print('===============================')
def output(self): """Input button event""" try: if not GPIO.input(self.BTN_input) and not self.prev_input: print("click") self.prev_input = True return True # elif not GPIO.input(self.BTN_input) and self.prev_input: else: self.prev_input = False except KeyboardInterrupt: GPIO.cleanup() # Get a frame from the video source return False
def triggerON(portNumber): import ASUS.GPIO as GPIO import time GPIO.setwarnings(False) GPIO.setmode(GPIO.ASUS) light = portNumber GPIO.setup(light, GPIO.OUT) GPIO.output(light, GPIO.LOW) try: print("open led: {0}".format(light)) GPIO.output(light, GPIO.HIGH) time.sleep(3) GPIO.output(light, GPIO.LOW) print("close led: {0}".format(light)) except KeyboardInterrupt: GPIO.cleanup()
def cleanup(self): GPIO.cleanup()
) # Değişkenlere atanan bilgiler string olduğu için veri tipleri float olark değiştiriliyor distance_ultrason = float( distance_ultrason ) # Değişkenlere atanan bilgiler string olduğu için veri tipleri float olark değiştiriliyor if distance_ultrason > 2 and distance_ultrason < 300: # Ultrasonik sensörün mesafesi 2-300 cm arasında ise Ultrasonik sensörlerin uyarı çıkışı LOW olarak atanır print "...no problem..." GPIO.output(Ultrason_warn, GPIO.LOW) elif distance_ultrason > 300: # Ultrasonik sensörün mesafesi 300 cm den büyük ise sensörleri kontrol et uyarısı verir ve Ultrasonik sensörlerin uyarı çıkışı HIGH olarak atanır print "check your ultrasonic sensors" GPIO.output(Ultrason_warn, GPIO.HIGH) else: # Aksi halde Ultrasonik sensörlerin uyarı çıkışı HIGH olarak atanır print "be careful" GPIO.output(Ultrason_warn, GPIO.HIGH) if (distance_IRsensor1 > 5 and distance_IRsensor2 > 5) and ( distance_IRsensor1 < 30 and distance_IRsensor2 < 30 ): # Kızılötesi analog sensörlerin mesafesi 2-30 cm arasında ise Kızılötesi analog sensörlerin uyarı çıkışı LOW olarak atanır print "...no problem..." GPIO.output(IR_analog_warn, GPIO.LOW) elif distance_IRsensor1 > 30 or distance_IRsensor > 30: # Kızılötesi analog sensörlerin mesafesi 30 cm'den büyük olursa sensörleri kontrol et uyarısı verir ve Kızılötesi analog sensörlerin uyarı çıkışı HIGH olarak atanır print "check your analog IR sensors" GPIO.output(IR_analog_warn, GPIO.HIGH) else: # Aksi halde Kızılötesi analog sensörlerin uyarı çıkışı HIGH olarak atanır print "be careful" GPIO.output(IR_analog_warn, GPIO.HIGH) except KeyboardInterrupt: # KeyboardInterrupt durumu olursa GPIO.cleanup() # GPIO portlarını temizle port.close() # portu kapat
import ASUS.GPIO as GPIO import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) LED = 11 button = 13 GPIO.setup(LED, GPIO.OUT) GPIO.setup(button, GPIO.IN) try: while True: if GPIO.input(button) == False: GPIO.output(LED, True) print("pressed") time.sleep(.1) else: GPIO.output(LED, False) print("off") time.sleep(.1) except KeyboardInterrupt: GPIO.cleanup()
def tearDown(self): GPIO.cleanup()
def drop(self): GPIO.cleanup() print("GPIO Clean up")
GPIO.setup(3, GPIO.IN) # set GPIO25 as input (button) # Define a threaded callback function to run in another thread when events are detected def my_callback(channel): if GPIO.input(3): # if port 25 == 1 print "Rising edge detected on 25" else: # if port 25 != 1 print "Falling edge detected on 25" # when a changing edge is detected on port 25, regardless of whatever # else is happening in the program, the function my_callback will be run GPIO.add_event_detect(3, GPIO.BOTH, callback=my_callback) print "Program will finish after 30 seconds or if you press CTRL+C\n" print "Make sure you have a button connected, pulled down through 10k resistor" print "to GND and wired so that when pressed it connects" print "GPIO port 25 (pin 22) to GND (pin 6) through a ~1k resistor\n" print "Also put a 100 nF capacitor across your switch for hardware debouncing" print "This is necessary to see the effect we're looking for" raw_input("Press Enter when ready\n>") try: print "When pressed, you'll see: Rising Edge detected on 252" print "When released, you'll see: Falling Edge detected on 252" sleep(30) # wait 30 seconds print "Time's up. Finished!" finally: # this block will run no matter how the try block exits GPIO.cleanup() # clean up after yourself
def pcb_interrupt(self, channel): GPIO.cleanup() # when the pcb is pulled clean all the used GPIO pins
def _exit(): if _clear_on_exit: clear() show() GPIO.cleanup()
def __init__(self, debug=False): GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.cleanup() self.piRevision = self.getPiRevision() self.i2cbus = 1 if self.piRevision == 1: self.i2cbus = 0 print "RPi.GPIO Version" , GPIO.VERSION print "Board Revision" , self.piRevision #Set some constants and initialise lists self.numOfPins = 27 #there are actually 26 but python can't count properly :) if self.piRevision > 2: self.numOfPins = 41 self.PINPUT = 4 self.POUTPUT = 1 self.PPWM = 2 self.PUNUSED = 8 self.PSONAR = 16 self.PULTRA = 32 self.PSERVOD = 64 self.PSTEPPER = 128 self.PCOUNT = 256 self.PINPUTDOWN = 512 self.PINPUTNONE = 1024 self.PPWMMOTOR = 2048 self.PPWMLED = 4096 #self.INVERT = False self.ledDim = 100 self.PWMMOTORFREQ = 10 self.dsSensorId = "" self.senderLoopDelay = 0.2 self.mFreq = 10 self.ultraFreq = 1 self.pFreq = 200 self.pinUse = [self.PUNUSED] * self.numOfPins self.servodPins = None self.pinRef = [None] * self.numOfPins self.pinCount = [0] * self.numOfPins self.countDirection = [1] * self.numOfPins self.pinEncoderDiff = [0] * self.numOfPins self.encoderStopCounting = [0] * self.numOfPins self.pinLastState = [0] * self.numOfPins self.encoderTime = [0] * self.numOfPins self.encoderTimeDiff = [0.0] * self.numOfPins self.gpioLookup = [0] * self.numOfPins self.callbackInUse = [False] * self.numOfPins self.pinValue = [0] * self.numOfPins self.pinInvert = [False] * self.numOfPins #print "pinValue" , self.pinValue #print "pin Value 3 = ", self.pinValue[3] self.pinUltraRef = [None] * self.numOfPins self.pinTrigger = [0] * self.numOfPins self.pinTriggerName = ["x"] * self.numOfPins self.anyTrigger = 0 self.pinServoValue = [None] * self.numOfPins self.gpioMyPinEventDetected = [False] * self.numOfPins self.pinTriggerLastState = [0] * self.numOfPins self.encoderCallback = 0 self.piAndBash = [self.PUNUSED] * 16 self.pinEventEnabled = True self.encoderInUse = 0 self.nunchuckLevel = 1 self.capTouch = None self.capTouchHelper = None self.ADS1015 = None self.lightDirection = 0 self.lightValue = 0 self.lightInfo = False self.autoLink = False self.linkPrefix = None self.validPins = [ 3, 5, 7, 8, 10,11,12,13, 15,16, 18,19, 21,22,23,24, 26] if self.piRevision == 1: # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 # [ 3, 5,99,99, 7,99,99,26,24,21,19,23,99,99, 8,10,99,11,12,99,99,13,15,16,18,22,99] self.gpioLookup = [99,99,99, 0,99, 1,99, 4,14,99,15,17,18,21,99,22,23,99,24,10,99, 9,25,11, 8,99, 7] if self.piRevision == 2: self.gpioLookup = [99,99,99, 2,99, 3,99, 4,14,99,15,17,18,27,99,22,23,99,24,10,99, 9,25,11, 8,99, 7] if self.piRevision > 2: self.validPins = [3, 5, 7, 8, 10,11,12,13, 15,16, 18,19, 21,22,23,24, 26,27,28, 29, 31,32,33, 35,36,37,38, 40] self.gpioLookup = [99,99,99, 2,99, 3,99, 4,14,99,15,17,18,27,99,22,23,99,24,10,99, 9,25,11, 8,99, 7,99,99, 5,99, 6,12,13,99,19,16,26,20,99,21] #self.ULTRA_IN_USE = [False] * self.PINS #self.ultraTotalInUse = 0 #self.ultraSleep = 1.0 self.debug = debug if self.debug: print "sghGC Debug enabled"
def clean(): """Clean GPIO""" GPIO.cleanup()
import ASUS.GPIO as GPIO import dht11 import time import datetime # config DHT11_PIN = 5 LED_G_PIN = 8 LED_Y_PIN = 10 LED_R_PIN = 12 DETECT_INTERVAL = 5 # initialize GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.cleanup() GPIO.setup(LED_G_PIN, GPIO.OUT) GPIO.setup(LED_Y_PIN, GPIO.OUT) GPIO.setup(LED_R_PIN, GPIO.OUT) def resetled(): GPIO.output(LED_G_PIN, GPIO.LOW) GPIO.output(LED_Y_PIN, GPIO.LOW) GPIO.output(LED_R_PIN, GPIO.LOW) resetled() # read dht11 data
import time #Import time library GPIO.setmode(GPIO.BOARD) #Set GPIO pin numbering pir = 40 #Associate pin 26 to pir GPIO.setup(pir, GPIO.IN) #Set pin as GPIO in SHUTOFF_DELAY = 10 turned_off = 1 print "Waiting for sensor to settle" time.sleep(2) #Waiting 2 seconds for the sensor to initiate print "Detecting motion" try: while True: if GPIO.input(pir): #Check whether pir is HIGH print "Motion Detected!" last_motion_time = time.time() time.sleep(2) #D1- Delay to avoid multiple detection if turned_off: subprocess.call("sh /root/pir/monitor_on.sh", shell=True) subprocess.call("sh /root/pir/pic_on.sh", shell=True) print "turned ON" #turned_off = 0 time.sleep(0.1) #While loop delay should be less than detection(hardware) delay else: if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY): subprocess.call("sh /root/pir/monitor_off.sh", shell=True) subprocess.call("sh /root/pir/pic_off.sh", shell=True) turned_off = 1 except KeyboardInterrupt: print "Quit" GPIO.cleanup(40)