def ler_sensor_ultrassonico(): MQTT_PORT = 1883 TOPICO = '/softway/iot' MQTT_TIMEOUT = 60 GPIO.setmode(GPIO.BCM) # Set GPIO pin numbering TRIG = 23 # Associate pin 23 to TRIG ECHO = 24 # Associate pin 24 to ECHO ID_LINE = 1 MQTT_ADDRESS = '10.17.0.2' GPIO.setup(TRIG, GPIO.OUT) # Set pin as GPIO out GPIO.setup(ECHO, GPIO.IN) # Set pin as GPIO in def send_message(msg, MQTT_ADDRESS): # Send MQTT Message client = mqtt.Client() client.connect(MQTT_ADDRESS, MQTT_PORT, MQTT_TIMEOUT) result, mid = client.publish(TOPICO, msg) print('Mensagem enviada ao canal: %d, [MQTT_ADDRESS: %s]' % (mid, MQTT_ADDRESS)) for numero in range(4): timeout = 0.8 * 60 * 60 # Horas em segundos (48 min) timeout_start = time.time() while time.time() < timeout_start + timeout: GPIO.output(TRIG, False) # Set TRIG as LOW time.sleep(2) # Delay of 2 seconds GPIO.output(TRIG, True) # Set TRIG as HIGH time.sleep(0.00001) # Delay of 0.00001 seconds GPIO.output(TRIG, False) # Set TRIG as LOW while GPIO.input(ECHO) == 0: # Check whether the ECHO is LOW pulse_start = time.time() # Saves the last known time of LOW pulse while GPIO.input(ECHO) == 1: # Check whether the ECHO is HIGH pulse_end = time.time() # Saves the last known time of HIGH pulse pulse_duration = pulse_end - pulse_start # Get pulse duration to a variable distance = pulse_duration * 17150 # Multiply pulse duration by 17150 to get distance distance = round(distance, 2) # Round to two decimal points if distance > 5: mensagem = "{\"id\":" + str(ID_LINE) + "}" print("++ MSG-SEND:", mensagem, "\n") send_message(mensagem, MQTT_ADDRESS) # Send message via MQTT protocol time.sleep(0.3) print("Resetando sensores...") time.sleep(0.3) print("Resetando sensores......") time.sleep(0.4) print("Resetando sensores.........")
def is_charging(): hw_v = get_hw_version_str() if hw_v == "gamma": chg_sense_gpio = 503 gpio.setup(chg_sense_gpio, gpio.IN) return bool(gpio.input(chg_sense_gpio)) else: raise NotImplementedException("Version not supported!")
def publish(): rospy.init_node('Leak') leakPub = rospy.Publisher('/leak', Bool, queue_size=1) diagPub = rospy.Publisher('/diagnostics', DiagnosticArray, queue_size=1) #GPIO.setmode(GPIO.BCM) #GPIO.setup(CHANNEL, GPIO.IN) gpio.setup(CHANNEL, gpio.IN) freq = rospy.Rate(1) leak = Bool() diag = DiagnosticArray() while not rospy.is_shutdown(): levelData=0 leak.data = gpio.input(CHANNEL) if (leak.data): levelData=2 diag.status = [DiagnosticStatus(name='Leak', message=str(leak.data), level=levelData)] leakPub.publish(leak) diagPub.publish(diag) freq.sleep()
def get_image(self, angle_idx): if self.is_test: self.angles_imgs_counter[angle_idx] += 1 img = self.angles_imgs_lst[angle_idx][ self.angles_imgs_counter[angle_idx]] # img = cv2.resize(img,(RESIZE_SIZE,RESIZE_SIZE)) if (IS_PI): self.socket.send_image(img) # asynchronus send, w/ timeout gui_img_manager.add_img(img) return img else: if (IS_PI): gpio.setmode(gpio.BOARD) gpio.setup(btn, gpio.IN, pull_up_down=gpio.PUD_DOWN) should_enter = True try: print("plz press me honz") while True: if (gpio.input(btn) == 1): should_enter = True elif (should_enter): img = self.one_still() #img = cv2.resize(img,(RESIZE_SIZE,RESIZE_SIZE)) self.socket.send_image(img) should_enter = False gpio.cleanup() return img except KeyboardInterrupt: print('cam err!') else: # gui img = self.socket.get_image() gui_img_manager.add_img(img) return img
def loop_master_connection(conn): #Run the master GPIO command interpereter over the socket conn run=1 got_OK=0 n=0 try: thread.start_new_thread(master_connection_init, (conn,)) while not got_OK: #Loop until connected n=n+1 print "CONNECTING (TRY "+str(n)+")" sendb(conn, 1, 1) #Send a 1 (acknowlage me!) time.sleep(4) except KeyboardInterrupt: print "Got OK, client connected" sendb(conn,10) while run: #recive command command=recvo(conn) if command==3: #3 is shutdown sendb(conn, 4) #4 is shutting down sendb(conn, 5)#tell the relay (if existant) to shut down conn.close() run=0 elif command==22: #22 is initilize pin as output ext=recvo(conn) #read pin to be initilized as output try: print "Initilizing pin "+str(ext)+" as output" gpio.setup(ext, gpio.OUT) #initilize as output sendb(conn, 10) #send back OK except BaseException as e: throw_error(4,e,0) #Throw ERROR4_GPIO_ERROR, dont quit the program, this is recoverable sendb(conn, 11) #Send back 11, error elif command==23: #23 is init as input ext=recvo(conn) #recive pin number to init try: print "Initilizing pin "+str(ext)+" as input" gpio.setup(ext, gpio.IN) #initilize as input sendb(conn, 10) except BaseException as e: throw_error(4,e,0) #Throw error sendb(conn, 11) elif command==24: #24 is read from pin ext=recvo(conn) #recive pin to read from try: print "Reading value from pin "+str(ext) sendb(conn, int(gpio.input(ext))) #read value and send sendb(conn, 10) except BaseException as e: throw_error(4,e,0) #Throw error, send 0 value in place of being read, send 11 so they know an error occoured, not just value is 0 sendb(conn, 0) sendb(conn, 11) elif command==25: #25 is turn pin on ext=recvo(conn) #recive pin# to turn on try: print "Turning pin "+str(ext)+" on" gpio.output(ext, 1) #turn on sendb(conn, 10) except BaseException as e: throw_error(4,e,0) #throw error sendb(conn, 11) elif command==26: #26 is turn pin off ext=recvo(conn) #recive pin# to turn off try: print "Turning pin "+str(ext)+" off" gpio.output(ext, 0) #turn off sendb(conn, 10) except BaseException as e: throw_error(4,e,0) #throw error sendb(conn, 11) elif command==30: #30 is a keepalive/ping signal, send back 10 (OK) so they know we are alive print "Client sent keep-alive/ ping" sendb(conn, 10)
def connected(self): if not self.chg_sense_gpio_setup: gpio.setup(self.chg_sense_gpio, gpio.IN) self.chg_sense_gpio_setup = True return bool(gpio.input(self.chg_sense_gpio))
print( 'Frozen Python to test GPIO.\n Press board switches to change LED colour.\n' ) import gpio gpio.init() gpio.output(gpio.red) gpio.output(gpio.green) gpio.output(gpio.blue) gpio.input(gpio.sw1) gpio.input(gpio.sw2) gpio.up(gpio.red) exit = 0 while exit == 0: switch1 = gpio.read(gpio.sw1) switch2 = gpio.read(gpio.sw2) if switch1 == 0 and switch2 == 0: exit = 1 elif switch1 == 0: gpio.up(gpio.green) gpio.down(gpio.red) gpio.down(gpio.blue) elif switch2 == 0: gpio.up(gpio.blue) gpio.down(gpio.green) gpio.down(gpio.red) else: gpio.up(gpio.red) gpio.down(gpio.green) gpio.down(gpio.blue) print('Blinking loop') import time
# this script turns on the LED when button is pressed and turns it off when button is released import time as t # in VS import gpio as GPIO # on Rasp # import RPi.GPIO as GPIO print("Script started") _pin_output_led_red = 14 _pin_input_button = 15 GPIO.setmode(GPIO.BCM) GPIO.setup(_pin_output_led_red, GPIO.OUT) GPIO.setup(_pin_input_button, GPIO.IN, GPIO.PUD_UP) while True: if GPIO.input(_pin_input_button) == False: print("Button pressed") GPIO.output(_pin_output_led_red, GPIO.HIGH) else: print("Button released") GPIO.output(_pin_output_led_red, GPIO.LOW) t.sleep(0.01)
def motion(self): return gpio.input(self.__pin)
import numpy as np import gpio import hv #________________________________________________________________________ #INITIALIZING OUR BUTTONS #________________________________________________________________________ gpio.init_input("P", 0) #HV button, ramps up HV gpio.init_input("P", 1) #calibrate button, runs calibration function gpio.init_input("P", 2) #reads in template A gpio.init_input("P", 3) #reads in template B gpio.init_input("P", 4) #reads in template C gpio.init_input("N", 7) #inspect button, runs inspection function while True: if gpio.input("P", 2) == 0: print "acquire 1" if gpio.input("P", 3) == 0: print "acquire 2" if gpio.input("P", 4) == 0: print "acquire 3" if gpio.input("P", 0) == 0: print "hv button" if gpio.input("P", 1) == 0: print "calibrate button" if gpio.input("N", 7) == 0: print "inspect button"
_bt_up_p2 = False _bt_down_p2 = False _bt_left_p2 = False _bt_right_p2 = False _bt_a_p2 = False _bt_b_p2 = False _bt_x_p2 = False _bt_y_p2 = False _bt_l_p2 = False _bt_r_p2 = False _bt_select_p2 = False _bt_start_p2 = False _bt_left_trigger_p2 = False _bt_right_trigger_p2 = False print gpio.input(bt_a_p1) while True: #------ player 1 -----------# #bt a ===================== if (not _bt_a_p1) and (gpio.input(bt_a_p1) == 0): _bt_a_p1 = True gamepad.emit(uinput.BTN_0, 1) if (_bt_a_p1) and (gpio.input(bt_a_p1) == 1): _bt_a_p1 = False gamepad.emit(uinput.BTN_0, 0) #bt b ===================== if (not _bt_b_p1) and (gpio.input(bt_b_p1) == 0): _bt_b_p1 = True gamepad.emit(uinput.BTN_1, 1)
""" function BytesToHex(Bytes) """ def BytesToHex(Bytes): return ''.join(["0x%02X " % x for x in Bytes]).strip() # Setup GPIO ports gpio.setup(nRESET_OUT, gpio.OUT) gpio.setup(nIRQ_ANY, gpio.IN) print("Performing AnyBus module reset...") gpio.output(nRESET_OUT, 0) time.sleep(0.1) nIRQ = gpio.input(nIRQ_ANY) if nIRQ == 0: logging.error("Error: nIRQ is already active!") gpio.output(nRESET_OUT, 1) while True: nIRQ = gpio.input(nIRQ_ANY) if nIRQ == 0: break print("AnyBus module is ready (nIRQ active)...") time.sleep(1.5) # AnyBus Module Type Request spi.mode = SPI.MODE_0 spi.bits_per_word = 8
#!/usr/bin/env python3 import gpio import rospy import time from std_msgs.msg import Bool CHANNEL = 395 gpio.setup(CHANNEL, gpio.IN) if __name__ == '__main__': rospy.init_node("StartButton") start_pub = rospy.Publisher("/start", Bool, queue_size=10) r = rospy.Rate(4) while not rospy.is_shutdown(): if not gpio.input(CHANNEL): start_pub.publish(Bool(True)) r.sleep()
gpio.output(PIN_CLOCK, 0) for gamepad in gamepads: gpio.setcfg(gamepad.data_pin, gpio.INPUT) while True: # init read gamepads gpio.output(PIN_LATCH, 1) sleep(PULSE_TIME) gpio.output(PIN_LATCH, 0) sleep(PULSE_TIME) # read first bit for gamepad in gamepads: gamepad.buttons_value = gpio.input(gamepad.data_pin) for i in range(0, 7): # init read next bit gpio.output(PIN_CLOCK, 1) sleep(PULSE_TIME) gpio.output(PIN_CLOCK, 0) sleep(PULSE_TIME) # read next bit for gamepad in gamepads: gamepad.buttons_value = (gamepad.buttons_value << 1) | gpio.input( gamepad.data_pin) # analyse gamepad values for gamepad in gamepads:
def input(pin): if pin in pinMap : return gpio.input(pinMap[pin]) else : logger.info("pinname " + pin + "is not supported!")