def distance(measure='cm'): gpio.setmode(gpio.BOARD) gpio.setup(12, gpio.OUT) gpio.setup(16, gpio.IN) time.sleep(0.3) gpio.ouput(12, True) time.sleep(0.00001) gpio.output(12, False) while gpio.input(16) == 0: nosig = time.time() while gpio.input(16) == 1: sig = time.time() tl = sig - nosig if measure == 'cm': distance = tl / 0.000058 elif measure == 'in: distance = tl / 0.000148 else: print('Improper choice of measurement: in or cm') distance = None gpio.cleanup() return distance
def switchPump(pumpTurnOnSignal): # timeDelta = time.time() - lastPumpTime[pump] if(pumpTurnOnSignal): GPIO.ouput(pumpPin,GPIO.HIGH) else: GPIO.ouput(pumpPin,GPIO.LOW)
def serenCall(): pid = None if not call_in_progress and GPIO.input(OFF_HOOK_PIN): print 'Initiating seren call' # Stop ringing immediately GPIO.ouput(RING_MODE_PIN, GPIO.LOW) ring_for_count = 0 # Start the call call_in_progress = True pid = subprocess.Popen([ '/home/pi/seren/seren', '-N', '-n', 'phone', '-c', PHONE_IP, '-C', 0, '-d', 'plughw:1,0', '-D', 'plughw:1,0', ]).pid elif call_in_progress and not GPIO.input(OFF_HOOK_PIN) print 'Terminating seren call' call_in_progress = False os.kill(pid, signal.SIGTERM) # Cede control to another thread time.sleep(FREE_UP_DURATION)
def stream_handler(message): global init global fan if init == True: init = False elif message["data"] == "False": print("Turning off the fan") GPIO.output(fan, GPIO.LOW) elif message["data"] == "True": print("Turning on the fan") GPIO.ouput(fan, GPIO.HIGH)
def blackTankRinseOff(self): self.update_status('Black Rinse OFF Button Pressed', 'user_action') GPIO.ouput(self.GPIO_black_tank_rinse_valve, 1) sleep(0.6) GPIO.ouput(self.GPIO_black_tank_rinse_valve, 0) self.black_tank_rinse_lbl.config(text='BLACK TANK RINSE CONTROL') self.black_tank_rinse_btn_off.config(state='disabled') self.black_tank_rinse_btn_on.config(state='normal') self.stop_countdown()
def write(z): GPIO.output(21, False) for i in z: GPIO.ouput(17, False) if i == 0: GPIO.output(4, False) else: GPIO.output(4, True) GPIO.output(17, True) GPIO.output(21, True)
def on_message(client, data, msg): print(msg.topic) if msg.topic == TOPIC_YELLOW: if GPIO.input(LEDY) == 1: GPIO.output(LEDY, 0) else: GPIO.output(LEDY, 1) else: if GPIO.input(LEDR) == 0: GPIO.ouput(LEDR, 1) else: GPIO.output(LEDR, 0)
def left_side_reverse(): print("BACKWARD LEFT") p.ChangeDutyCycle(9.5) time.sleep(0.5) p.ChangeDutyCycle(0) q.ChangeDutyCycle(9.5) time.sleep(0.5) q.ChangeDutyCycle(0) r.ChangeDutyCycle(9.5) time.sleep(0.5) r.ChangeDutyCycle(0) s.ChangeDutyCycle(9.5) time.sleep(0.5) s.ChangeDutyCycle(0) pwm1.ChangeDutyCycle(100) pwm2.ChangeDutyCycle(100) pwm3.ChangeDutyCycle(100) pwm4.ChangeDutyCycle(100) time.sleep(0.5) global Phase if (Phase == 90): GPIO.output(Front_left, GPIO.LOW) GPIO.output(Front_right, GPIO.LOW) GPIO.output(Back_left, GPIO.LOW) GPIO.output(Back_right, GPIO.LOW) pwm1.ChangeDutyCycle(70) pwm2.ChangeDutyCycle(70) GPIO.output(Front_left, GPIO.HIGH) GPIO.output(Front_right, GPIO.HIGH) GPIO.output(Back_left, GPIO.HIGH) GPIO.output(Back_right, GPIO.HIGH) else: GPIO.output(Front_left, GPIO.LOW) GPIO.output(Front_right, GPIO.LOW) GPIO.output(Back_left, GPIO.LOW) GPIO.output(Back_right, GPIO.LOW) GPIO.output(Front_left, GPIO.HIGH) GPIO.output(Front_right, GPIO.HIGH) time.sleep(0.00172) GPIO.output(Front_left, GPIO.LOW) GPIO.output(Front_right, GPIO.LOW) pwm1.ChangeDutyCycle(70) pwm2.ChangeDutyCycle(70) GPIO.output(Front_left, GPIO.HIGH) GPIO.output(Front_right, GPIO.HIGH) GPIO.ouput(Back_right, GPIO.HIGH) GPIO.output(Back_left, GPIO.HIGH) Phase = 90
def sendBitsFromMaster(data, numBits, clkPin, mosiPin): # read all the bits from the data and push over the line for bit in range(numBits, 0, -1): # count down from MSB to LSB bit = bit - 1 dec_value = 2**bit if (data / dec_value >= 1): GPIO.ouput(mosiPin, GPIO.HIGH) data = data - dec_value else: GPIO.output(mosiPin, GPIO.LOW) # pulse the clock pin to push the data through GPIO.output(clkPin, GPIO.HIGH) #POSSIBLE NEED FOR SHORT DELAY GPIO.output(clkPin, GPIO.LOW)
def blackTankRinseOn(self): ''' Black tank valve needs to be open before the rinse process begins. Is there a way to confirm that the valve is open? ''' self.update_status('Black Rinse ON Button Pressed', 'user_action') # Open the black tank valve before opening the rinse valve self.blackValveOpen() GPIO.ouput(self.GPIO_black_tank_rinse_valve, 1) sleep(0.6) GPIO.ouput(self.GPIO_black_tank_rinse_valve, 0) self.black_tank_rinse_btn_on.config(state='disabled') self.black_tank_rinse_btn_off.config(state='normal') self.start_countdown()
def seren(): if not call_in_progress and GPIO.input(OFF_HOOK_PIN): print 'Initiating seren call' # Stop ringing immediately GPIO.ouput(RING_MODE_PIN, GPIO.LOW) ring_for_count = 0 # Start the call call_in_progress = True seren_pid = subprocess.Popen(['seren', '-NS', '-c', PHONE_IP]).pid elif call_in_progress and not GPIO.input(OFF_HOOK_PIN) print 'Terminating seren call' call_in_progress = False os.kill(seren_pid, signal.SIGTERM) # Cede control to another thread time.sleep(FREE_UP_DURATION)
def forward(): print("FORWARD") p.ChangeDutyCycle(3.1) time.sleep(0.5) p.ChangeDutyCycle(0) q.ChangeDutyCycle(3.1) time.sleep(0.5) q.ChangeDutyCycle(0) r.ChangeDutyCycle(3.1) time.sleep(0.5) r.ChangeDutyCycle(0) s.ChangeDutyCycle(3.1) time.sleep(0.5) s.ChangeDutyCycle(0) pwm1.ChangeDutyCycle(100) pwm2.ChangeDutyCycle(100) pwm3.ChangeDutyCycle(100) pwm4.ChangeDutyCycle(100) time.sleep(0.5) global Phase if (Phase == 90): GPIO.output(Front_left, GPIO.LOW) GPIO.output(Front_right, GPIO.LOW) GPIO.output(Back_left, GPIO.LOW) GPIO.output(Back_right, GPIO.LOW) GPIO.output(Front_left, GPIO.HIGH) GPIO.output(Front_right, GPIO.HIGH) GPIO.output(Back_left, GPIO.HIGH) GPIO.output(Back_right, GPIO.HIGH) else: GPIO.output(Front_left, GPIO.LOW) GPIO.output(Front_right, GPIO.LOW) GPIO.output(Back_left, GPIO.LOW) GPIO.output(Back_right, GPIO.LOW) GPIO.output(Front_left, GPIO.HIGH) GPIO.output(Front_right, GPIO.HIGH) time.sleep(0.00172) GPIO.ouput(Back_right, GPIO.HIGH) GPIO.output(Back_left, GPIO.HIGH) Phase = 90
def poll_keypad(self): pins_to_sym = { (18, 17): "1", (18, 27): "2", (18, 22): "3", (23, 17): "4", (23, 27): "5", (23, 22): "6", (24, 17): "7", (24, 27): "8", (24, 22): "9", (25, 17): "*", (25, 27): "0", (25, 22): "#" } # må legge inn sleep her for row in self.row_pin: GPIO.output(row, GPIO.HIGH) sleep(0.1) # wait to maybe let it turn on for colm in self.colm_pin: if (GPIO.INPUT(colm) == GPIO.HIGH): GPIO.ouput(row, GPIO.LOW) return pins_to_sym[(row, colm)] GPIO.ouptput(row, GPIO.LOW)
def loop(): while True: if GPIO.input(touch) == LOW: GPIO.ouput(led2, GPOI.LOW) print 'not touched' elif GPIO.input(touch) == HIGH: GPIO.ouput(led2, GPOI.HIGH) print 'touched' elif GPIO.input(tilt) == LOW: GPIO.ouput(led, GPOI.LOW) print 'not tilited' else: GPIO.ouput(led, GPOI.HIGH) print 'tilted' ser = serial.Serial('/dev/serial10', 9600, timeout = 1) ser.write('AT+CMGS="+256771238781"'+'\r\n') ser.read(10) time.sleep(10) ser.write('door tilted\r\n') ser.close()
def loop(): while True: if GPIO.input(touch) == LOW: GPIO.ouput(led2, GPOI.LOW) print 'not touched' elif GPIO.input(touch) == HIGH: GPIO.ouput(led2, GPOI.HIGH) print 'touched' elif GPIO.input(touch) == HIGH and GPIO.input(tilt) == HIGH: GPIO.output(led2, GPIO.HIGH) GPIO.output(led, GPIO.HIGH) print 'touched and tilted' elif GPIO.input(tilt) == LOW: GPIO.ouput(led, GPIO.LOW) print 'not tilited'
captura=cv2.VideoCapture(1) #Iniciamos la comunicacion serial ser = serial.Serial('/dev/ttyACM0', 9600) def showVideo(cap): key=0 while(key!=27): flag, frame = cap.read() cv2.imshow('PON UNA POSE GUAPA!', frame) key = cv2.waitKey(5) & 0xFF thread = threading.Thread(target=showVideo, args=(captura,)) GPIO.setmode(GPIO.BOARD)GPIO.P GPIO.setup(11,GPIO.IN, pull_up_down=PUD_DOWN) GPIO.setup(7,GPIO.OUT) GPIO.ouput(7,0) prevInput=0 try: while True: if first: first=False thread.start() currentInput = GPIO.input(11) if not prevInput and currentInput: print "recibo" prevInput=currentInput flag, frame = captura.read() if flag: cv2.imshow('ESTA ES TU FOTO!', frame) cv2.imwrite("%d.png"%numSnapshot, frame) numSnapshot+=1
tweet2 = "This is the second Tweet" tweet3 = "This is the third Tweet" # initialise the tweet choice to 1 current_tweet = 1 # code to run try: while True: select_btn_state = GPIO.input(select_btn) tweet_btn_state = GPIO.input(tweet_btn) if select_btn_state == False: print "Changing tweet selection" if current_tweet == 1: current_tweet == 2 GPIO.ouput(t_LED, False) GPIO.output(m_LED, True) GPIO.output(b_LED, False) time.sleep(0.2) elif current_tweet == 2: current_tweet == 3 GPIO.ouput(t_LED, False) GPIO.output(m_LED, True) GPIO.output(b_LED, False) time.sleep(0.2) else: current_tweet == 1 GPIO.ouput(t_LED, True) GPIO.output(m_LED, False)
if GPIO.input(18) == GPIO.HIGH: print("Button one was pushed!") #on GPIO.output(25, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) time.sleep(0.5) #off GPIO.output(25, GPIO.LOW) GPIO.output(24, GPIO.LOW) #motor move GPIO.output(9, 1) GPIO.output(10, 0) time.sleep(0.1) #motor stop GPIO.output(10,0) GPIO.ouput(9,0) #lock elif GPIO.input(27) == GPIO.HIGH: print("Button two was pushed!") #on GPIO.output(25, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) time.sleep(1) #off GPIO.output(25, GPIO.LOW) GPIO.output(24, GPIO.LOW) time.sleep(0.5) #on GPIO.output(25, GPIO.HIGH) GPIO.output(24, GPIO.HIGH)
print 'not touched' elif GPIO.input(touch) == HIGH: GPIO.ouput(led2, GPOI.HIGH) print 'touched' elif GPIO.input(touch) == HIGH and GPIO.input(tilt) == HIGH: GPIO.output(led2, GPIO.HIGH) GPIO.output(led, GPIO.HIGH) print 'touched and tilted' elif GPIO.input(tilt) == LOW: GPIO.ouput(led, GPIO.LOW) print 'not tilited' else: GPIO.ouput(led, GPOI.HIGH) print 'tilted' def destroy(): GPIO.cleanup() if __name__ == '__main__': setup() try: loop() except KeyboardInterrupt: destroy()
def switchLeds(turnOnSignal): if(turnOnSignal): GPIO.ouput(ledPin,GPIO.HIGH) else: GPIO.ouput(ledPin,GPIO.LOW)
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GIPO.setwarnings(FALSE) GPIO.setup(11, GPIO.OUT) GPIO.ouput(11, GPIO.HIGH)
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GIPO.setwarnings(FALSE) GPIO.setup(11, GPIO.OUT) GPIO.ouput(11, GPIO.LOW)
GPIO.output(motor_L1, True) GPIO.output(motor_L2, True) now = datetime.datetime.now() nowDate = now.strftime('%H:%M') wakeup = "10:00" try: print "PIR + MOTOR module test (ctrl+c to exit)" time.sleep(2) print "Ready" while True: now = datetime.datetime.now() nowDate = now.strftime('%H:%M') if nowDate==wakeup: if GPIO.input(pir): print "motion detexted!, gogogogogo" GPIO.ouput(motor_R3, False) GPIO.ouput(motor_R4, True) GPIO.ouput(motor_L3, True) GPIO.ouput(motor_L4, False) else: print "no" except KeyboardInterrupt: print "Quit" GPIO.cleanup()
import RPi.GPIO as rp import time as t rp.setmode(rp.BOARD) rp.setup(7, rp.IN) rp.setup(31, rp.OUT) rp.setup(33, rp.OUT) rp.setwarnings(False) c = 0 while (1): if (rp.input(7) == 1): rp.output(31, 1) rp.ouput(33, 0) t.sleep(1) rp.output(31, 0) while (rp.input(7)): pass c = c + 1 t.sleep(5) rp.output(31, 0) rp.output(33, 1) t.sleep(1) rp.output(33, 0)
state = 1 if GPIO.input(18): GPIO.output(21, GPIO.LOW) if (state == 1): if GPIO.input(26): #Timer Start Button Pressed (First Round) GPIO.output(4, GPIO.HIGH) #Turn on first LED GPIO.output(17, GPIO.LOW) GPIO.output(25, GPIO.LOW) GPIO.output(12, GPIO.LOW) #time.sleep(300) #5 minute Timer time.sleep(5) #For testing purposes GPIO.output(21, GPIO.HIGH) state = 2 if GPIO.input(18): GPIO.ouput(21, GPIO.LOW) if (state == 2): # If it is the second round if GPIO.input(26): GPIO.output(4, GPIO.HIGH) #Turn on first and second LED GPIO.output(17, GPIO.HIGH) GPIO.output(25, GPIO.LOW) GPIO.output(12, GPIO.LOW) #time.sleep(300) #5 minute Timer time.sleep(5) GPIO.output(21, GPIO.HIGH) state = 3 if GPIO.input(18): GPIO.output(21, GPIO.LOW) if (state == 3): #If it is the third round
import RPi.GPIO as g import time g.setmode(g.BCM) g.setup(22, g.OUT) g.ouput(22, False) for i in range(10): g.output(22, True) time.sleep(1) g.output(22, False) time.sleep(1)
def motorSolve(solveString): #input: a string of the moves to perform #parses string and executes the moves GPIO.setmode(GPIO.BCM) #sets up all the pins stepF = 2 dirF = 3 GPIO.setup(stepF, GPIO.OUT) GPIO.setup(dirF, GPIO.OUT) stepR = 17 dirR = 27 GPIO.setup(stepR, GPIO.OUT) GPIO.setup(dirR, GPIO.OUT) stepB = 10 dirB = 22 GPIO.setup(stepB, GPIO.OUT) GPIO.setup(dirB, GPIO.OUT) stepL = 5 dirL = 6 GPIO.setup(stepL, GPIO.OUT) GPIO.setup(dirL, GPIO.OUT) stepD = 19 dirD = 26 GPIO.setup(stepD, GPIO.OUT) GPIO.setup(dirD, GPIO.OUT) stepU = 16 dirU = 20 GPIO.setup(stepU, GPIO.OUT) GPIO.setup(dirU, GPIO.OUT) #parse input toSolve = solveString.split() toSolve = toSolve[: -1] #the last one is how many moves there are, we don't need it #print("SOLVING: ", end = '') #print(toSolve) #moves for move in toSolve: #move format: F1, F is face to move, 1 is how many 90 degree turns to make #print(move) step_count = 50 #step counts are hard coded because sometimes the faces act weird and must be calibrated individually #clock also hard coded because in the future we can change it easier if move[0] == "F": step = stepF direc = dirF clock = CCW step_count = 50 if move[0] == "U": step = stepU direc = dirU clock = CCW step_count = 50 if move[0] == "B": step = stepB direc = dirB clock = CCW step_count = 50 if move[0] == "R": step = stepR direc = dirR clock = CCW step_count = 50 if move[0] == "L": step = stepL direc = dirL clock = CCW step_count = 50 if move[0] == "D": step = stepD direc = dirD clock = CCW step_count = 50 #if we're turning 3 times, just turn once the opposite direction toTurn = int(move[1]) if int(move[1]) == 3: toTurn = 1 if clock == CCW: clock = CW else: clock = CCW #perform move for i in range(toTurn): GPIO.output(direc, clock) #spins the motor 90 degrees for j in range(step_count): GPIO.ouput(step, GPIO.HIGH) time.sleep(delay) GPIO.output(step, GPIO.LOW) time.sleep(delay)
f = open('test.txt', 'r') GPIO.setmode(GPIO.BOARD) GPIO.setup(7,GPIO.OUT) GPIO.output(7, 0) time.sleep(2) #print 'starting now' while True: #a = 'abcdef' a = f.read(6) if len(a) == 0: #signifies end of file GPIO.output(7,1) time.sleep(sleep) for i in range(48) GPIO.ouput(7,0) time.sleep(sleep) break; c = '' for i in range (len(a)): c = calculateParity(a) #c = c + string_to_bits(a[i]) #GPIO.output(7,1) #time.sleep(sleep) count = 0 ack = '0' while ack == '0' and count < 5: GPIO.output(7,1) time.sleep(sleep) for j in range (len(c)): GPIO.output(7, int(c[j]))
def fireTheGun(): if (gunState): GPIO.ouput(18, GPIO.HIGH) else: GPIO.output(18, GPIO.LOW)
GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(motorpin1, GPIO.OUT) GPIO.setup(motorpin2, GPIO.OUT) input("press any key to move motor...") # motor movement GPIO.output(motorpin1, 0) GPIO.output(motorpin2, 1) #wait time.sleep(0.3) #turning off the motor GPIO.output(motorpin1, 0) GPIO.ouput(motorpin2, 0) input("press any key to move motor other direction... ") # motor movement GPIO.output(motorpin1, 1) GPIO.output(motorpin2, 0) #wait time.sleep(0.3) #turning off the motor GPIO.output(motorpin1, 0) GPIO.ouput(motorpin2, 0) print("program finished")
#!/usr/bin/python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) # sets GPIO identification by physical pin numbers resetPin = 3 # identify RPi physical pin connected to ESP8266 RST pin flashPin = 5 # identify RPi physical pin connected to ESP8266 GPIO0 pin GPIO.setup(resetPin, GPIO.OUT) # set reset pin as output GPIO.setup(flashPin, GPIO.OUT) # set flash pin as output GPIO.output(resetPin, GPIO.LOW) # drop voltage on RST pin time.sleep(.2) # need for this waiting is speculative GPIO.output(flashPin, GPIO.LOW) # drop voltage on GPIO0 time.sleep(.2) # need for this waiting is speculative GPIO.output(resetPin, GPIO.HIGH) # start booting ESP8266 time.sleep(.5) # wait for ESP8266 to boot GPIO.ouput(flashPin.GPIO.HIGH) # restore voltage on GPIO pinGPIO.cleanup() # reset pins on RPI to prevent future runtime warnings
topic = message.topic messages.append([topic, msg]) client = mqtt.Client() client.on_connect = on_connect client.on_message = on_mess broker = "127.0.0.1" client.loop_start() client.connect(broker) ##print("connected to broker") i = 0 while i < 5: #client.publish("zrvz","Hello From Python") ## client.loop_start() client.publish("zrvz/pi/GPIO4", GPIO.input(4)) ## client.loop() time.sleep(4) ## print(len(messages)) if len(messages) > 0: m = messages.pop(0) print("recieved control = ", m) GPIO.ouput(4, int(m[1])) print("value is ", GPIO.input(4)) time.sleep(4) i = i + 1 client.loop_stop() sys.exit()
def turnOffLights(self): print("Turning off laser diodes") if self.moduleA.isActive: GPIO.ouput(self.moduleA.laser,GPIO.LOW) if self.moduleB.isActive: GPIO.output(self.moduleB.laser,GPIO.LOW)
def Push(payload): if(payload[0] > 50): GPIO.ouput(4, 1) else: GPIO.output(4, 0)
import RPi.GPIO as GPIO import time # # Free Coding session for 2015-02-01 # Written by Matt Warren # GPIO.setmode(GPIO.BOARD) CONTROL_PINS = [7, 11, 13, 15] for pin in CONTROL_PINS: GPIO.setup(pin, GPIO.OUT) GPIO.ouput(pin, 0) # half-stepping pin sequence seq = [[1, 0, 0, 0], [1, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [1, 0, 0, 1]] for i in range(512): for halfstep in range(8): for pin in range(4):