def main(gpio_input_id, gpio_output_id): gpio_in = gpio.GPIO(gpio_input_id, gpio.DIRECTION_INPUT) gpio_out = gpio.GPIO(gpio_output_id, gpio.DIRECTION_OUTPUT) with gpio.request_gpios((gpio_in, gpio_out)): assert gpio.DIRECTION_INPUT == gpio_in.get_direction() assert gpio.DIRECTION_OUTPUT == gpio_out.get_direction() gpio_out.set_high() assert gpio_out.is_high() assert gpio_in.is_high() gpio_out.set_low() assert not gpio_out.is_high() assert not gpio_in.is_high() gpio.GPIO.set_debug(False) for i in range(1000): gpio_out.set_high() gpio_out.set_low() gpio.GPIO.set_debug(True) edges = (gpio.EDGE_RISING, gpio.EDGE_FALLING, gpio.EDGE_BOTH, gpio.EDGE_NONE) for edge in edges: gpio_in.set_edge(edge) assert edge == gpio_in.get_edge() test_wait_for_interrupt(gpio_in, gpio_out) test_interrupt_handler(gpio_in, gpio_out)
def main(led, btn): with gpio.request_gpios((led, btn)): while True: if btn.poll(100): if btn.is_high(): led.set_high() else: led.set_low()
def activated(): gpio_red = gpio.GPIO(GPIO.gpio_id("GPIO-B"), gpio.DIRECTION_OUTPUT) gpio_green = gpio.GPIO(GPIO.gpio_id("GPIO-C"), gpio.DIRECTION_OUTPUT) gpio_blue = gpio.GPIO(GPIO.gpio_id("GPIO-D"), gpio.DIRECTION_OUTPUT) with gpio.request_gpios((gpio_red, gpio_green, gpio_blue)): gpio_green.set_low() gpio_blue.set_low() gpio_blue.set_high()
def get(): """Get temperature.""" gpio_cs = gpio.GPIO(18, gpio.DIRECTION_OUTPUT) gpio_cs.set_high() with gpio.request_gpios([gpio_cs]): gpio_cs.set_low() rx = spi.xfer(channel_select) gpio_cs.set_high() adc_value = (rx[1] << 8) & 0b1100000000 adc_value = adc_value | (rx[2] & 0xff) return adc_value
def main(gpio_input_id): gpio_in = gpio.GPIO(gpio_input_id, gpio.DIRECTION_INPUT) with gpio.request_gpios((gpio_in)): assert gpio.DIRECTION_INPUT == gpio_in.get_direction() while True: if gpio_in.is_high(): print 'high' os.system("chmod +x trigger.sh") os.system("sudo ./trigger.sh") else: print 'low' time.sleep(1)
def activate_defences(): gpio_red = gpio.GPIO(GPIO.gpio_id("GPIO-B"), gpio.DIRECTION_OUTPUT) gpio_green = gpio.GPIO(GPIO.gpio_id("GPIO-C"), gpio.DIRECTION_OUTPUT) gpio_blue = gpio.GPIO(GPIO.gpio_id("GPIO-D"), gpio.DIRECTION_OUTPUT) counter = 10 with gpio.request_gpios((gpio_red, gpio_green, gpio_blue)): gpio_green.set_low() gpio_blue.set_low() while counter > 0: gpio_red.set_high() sleep(0.5) gpio_red.set_low() sleep(0.5) counter = counter - 1
def get_adc(self): """Read analog pin.""" if re.search(TARGET_ID, platform.platform()): from libsoc import gpio with gpio.request_gpios([self.gpio_cs]): self.gpio_cs.set_high() sleep(0.00001) self.gpio_cs.set_low() rx = self.spi.xfer(self.channel) self.gpio_cs.set_high() adc_value = (rx[1] << 8) & 0b1100000000 adc_value = adc_value | (rx[2] & 0xff) # adc_value = (rx[1] & 3) << 8 | rx[2] else: adc_value = uniform(850, 900) return adc_value
def main(): spi.SPI.set_debug(1) gpio_cs = gpio.GPIO(18, gpio.DIRECTION_OUTPUT) with gpio.request_gpios([gpio_cs]): tx = b'\x01\x80\x00' with spi.SPI(0, 0, spi.MODE_0, 10000, spi.BITS_8) as spi_dev: while True: gpio_cs.set_high() sleep(0.00001) gpio_cs.set_low() rx = spi_dev.rw(3, tx) gpio_cs.set_high() adc_value = (rx[1] << 8) & 0b1100000000 adc_value = adc_value | (rx[2] & 0xff) print("adc_value: %d" % adc_value) sleep(1)
def main(gpio_input_id, gpio_output_id): gpio_in = gpio.GPIO(gpio_input_id, gpio.DIRECTION_INPUT) gpio_out = gpio.GPIO(gpio_output_id, gpio.DIRECTION_OUTPUT) with gpio.request_gpios((gpio_in, gpio_out)): assert gpio.DIRECTION_INPUT == gpio_in.get_direction() assert gpio.DIRECTION_OUTPUT == gpio_out.get_direction() gpio_out.set_high() assert gpio_out.is_high() print("The LED initial status is ON") int_count = 0 while 1: test_interrupt_handler(gpio_in, gpio_out) int_count = int_count + 1 print("interrupt times is %d" %int_count)
def main(): spi.SPI.set_debug(1) gpio_cs = gpio.GPIO(SPI0_CS_N, gpio.DIRECTION_OUTPUT) res = gpio.request_gpios([gpio_cs]) # enable SPI gpio_cs.set_high() sleep(0.00001) gpio_cs.set_low() spi_dev = spi.SPI(0, 0, spi.MODE_0, 10000, spi.BITS_8) # enable debug spi_dev.set_debug(1) get_chip_id(spi_dev) soft_reset(spi_dev) pass
def run(self): # GPIO open with gpio.request_gpios((self.gpio_in, self.gpio_out)): # set LED GPIO out default to 1 self.gpio_out.set_high() assert self.gpio_out.is_high() print("The LED initial status is ON") # loop for processing BUTTON GPIO interrupt while self.working == True: # start interrput self.gpio_in.set_direction(gpio.DIRECTION_INPUT, gpio.EDGE_RISING) self.gpio_in.wait_for_interrupt(-1) # KEY debouncer code debounce = 0 for x in range(3): time.sleep(0.02) if self.gpio_in.is_high() == 1: debounce = debounce + 1 if debounce >= 2: if self.gpio_out.is_high() == 1: self.gpio_out.set_low() LedStatus = 0 # send LED GPIO set to 0 signal self.LedSignal.emit(LedStatus) print("The LED turns OFF") else: self.gpio_out.set_high() LedStatus = 1 # send LED GPIO set to 1 signal self.LedSignal.emit(LedStatus) print("The LED turns ON")
def main(gpio_input_id, gpio_output_id): gpio_in = gpio.GPIO(gpio_input_id, gpio.DIRECTION_INPUT) gpio_out = gpio.GPIO(gpio_output_id, gpio.DIRECTION_OUTPUT) with gpio.request_gpios((gpio_in, gpio_out)): assert gpio.DIRECTION_INPUT == gpio_in.get_direction() assert gpio.DIRECTION_OUTPUT == gpio_out.get_direction() gpio_out.set_high() assert gpio_out.is_high() print("The LED initial status is ON") print("please enter 'Q' to quit") gpio_in.set_direction(gpio.DIRECTION_INPUT, gpio.EDGE_RISING) class int_handler(object): hits = 0 def callback(self): self.hits += 1 test_interrupt_handler(gpio_in, gpio_out) cb = int_handler() h = gpio_in.start_interrupt_handler(cb.callback) while 1: n = sys.stdin.readline().strip('\n') #print(n) if n == "Q": print("Do you really want to quit? yes or no") m = sys.stdin.readline().strip('\n') if m == "yes": h.stop() h.join() exit() print("interrupt times is %d" % cb.hits)
while True : print("Send message") # For certificate based connection myMQTTClient = AWSIoTMQTTClient("dragon") # For Websocket connection # myMQTTClient = AWSIoTMQTTClient("myClientID", useWebsocket=True) # Configurations # For TLS mutual authentication myMQTTClient.configureEndpoint("a8mgf2amxmior.iot.us-east-2.amazonaws.com", 8883) # For Websocket # myMQTTClient.configureEndpoint("YOUR.ENDPOINT", 443) myMQTTClient.configureCredentials("/home/linaro/Desktop/awsConnect/root-CA.crt", "/home/linaro/Desktop/awsConnect/dragon.private.key", "/home/linaro/Desktop/awsConnect/dragon.cert.pem") # For Websocket, we only need to configure the root CA # myMQTTClient.configureCredentials("YOUR/ROOT/CA/PATH") myMQTTClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing myMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz myMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec myMQTTClient.configureMQTTOperationTimeout(5) # 5 sec myMQTTClient.connect() myMQTTClient.publish("temp", readTempSense(), 0) myMQTTClient.publish("light", readLighSense(), 0) myMQTTClient.publish("earthquake", hasEarthQuake(), 0) myMQTTClient.disconnect() sleep(5) if __name__ == '__main__': with gpio.request_gpios([READ_SENSE]): sendMessage()
spi = spidev.SpiDev() spi.open(0,0) spi.max_speed_hz=10000 spi.mode = 0b00 spi.bits_per_word = 8 channel_select_temperatura=[0x01, 0x80, 0x00] #ADC1 channel_select_luminosidade=[0x01, 0xA0, 0x00] #ADC2 timer = 0 if __name__=='__main__': _thread.start_new_thread(read_Tilt, ()) gpio_cs = gpio.GPIO(18, gpio.DIRECTION_OUTPUT) with gpio.request_gpios([gpio_cs]): while True: gpio_cs.set_high() sleep(0.00001) gpio_cs.set_low() rx = spi.xfer(channel_select_temperatura) gpio_cs.set_high() adc_value = (rx[1] << 8) & 0b1100000000 adc_value = adc_value | (rx[2] & 0xff) #Realiza a conversao para celsius adc_value = (adc_value*5.0/1023-0.5)*100 gpio_cs.set_high()
def sensor_activated(): gpio_in = gpio.GPIO(GPIO.gpio_id("GPIO-A"), gpio.DIRECTION_INPUT) with gpio.request_gpios(gpio_in): in_val = gpio_in.is_high() return in_val
from time import sleep from libsoc import gpio from libsoc import GPIO # GPIO.set_debug(True) gpio_out = gpio.GPIO(GPIO.gpio_id("GPIO-B"), gpio.DIRECTION_OUTPUT) with gpio.request_gpios(gpio_out): while True: gpio_out.set_high() sleep(1) gpio_out.set_low() sleep(1)