def back(): gpio.output(Trigger_Fb,0) # Set Trigger LOW time.sleep(0.5) gpio.output(Trigger_Fb,1) time.sleep(0.00001) # 10uS pulse gpio.output(Trigger_Fb,0) start=time.time() while gpio.input(Echo_Fb)==0: start=time.time() while gpio.input(Echo_Fb)==1: stop=time.time() elapsed = stop-start distance_Fb = elapsed * 34000 distance_Fb=distance_Fb/2 distance_Fb=round(distance_Fb,0) return(distance_Fb) print(distance_Fb)
def back(): gpio.output(Trigger_Fb, 0) # Set Trigger LOW time.sleep(0.5) gpio.output(Trigger_Fb, 1) time.sleep(0.00001) # 10uS pulse gpio.output(Trigger_Fb, 0) start = time.time() while gpio.input(Echo_Fb) == 0: start = time.time() while gpio.input(Echo_Fb) == 1: stop = time.time() elapsed = stop - start distance_Fb = elapsed * 34000 distance_Fb = distance_Fb / 2 distance_Fb = round(distance_Fb, 0) return (distance_Fb) print(distance_Fb)
def readadc(channelNumber, clockPin, kitoutputPin, kitInputPin, chipSelectPin): # First of all we only have eight channels from 0 to 7, other values are not accepted, sorry! if (channelNumber > 7) or (channelNumber < 0): print("channel number should be between 0 and 7") return -1 # chipSelectPin should be brought high and back low to initiate communication gpio.output(chipSelectPin, gpio.HIGH) gpio.output(clockPin, gpio.LOW) gpio.output(chipSelectPin, gpio.LOW) # configuration bits (configBits) sequence is constructed as follow : # start bit + single-ended bit + channel number (3 bits) + 000 (not sent; see below) # Example : let's say I am using channel number 3 then configBits = start bit (1) + # single-ended bit (1) + channel number (011) + 000 = 11011000 # another way to find the same value is channelNumber channelNumber (00000011) LOGICAL OR 0x18 (00011000) # SHIFTED three bits to left = 11011000 configBits = channelNumber configBits |= 0x18 configBits <<= 3 # send configurationn bits to the mcp3008 module # every time the MSB is extracted then sent for i in range(5): if configBits & 0x80: gpio.output(kitoutputPin, gpio.HIGH) else: gpio.output(kitoutputPin, gpio.LOW) configBits <<= 1 # clock signal gpio.output(clockPin, gpio.HIGH) gpio.output(clockPin, gpio.LOW) # reading bits from the mcp3008 module channel, the module sends 10 bit # one idle bit is sent before and another one after the data, a total of twelve bits is sent readInput = 0 for index in range(10): # clock gpio.output(clockPin, gpio.HIGH) gpio.output(clockPin, gpio.LOW) readInput <<= 1 if gpio.input(kitInputPin): readInput |= 0x1 # disable the mcp3008 module gpio.output(chipSelectPin, gpio.HIGH) return readInput
def DHT11Measures(): # -------------------------------------- # initialisation # refer to the tutorial for more information about the # GPIO pins in the A13-SOM-LTE sensor = port.PE11 gpio.init() # -------------------------------------- # trigger measure gpio.setcfg(sensor, gpio.OUTPUT) gpio.output(sensor, 1) time.sleep(0.025) gpio.output(sensor, 0) time.sleep(0.02) # ---------------------------------------- # grab data gpio.setcfg(sensor, gpio.INPUT) data = [] bit_count = 0 tmp = 0 count = 0 HumidityBit = "0" TemperatureBit = "" crc = "" threshold = 9 for i in range(0, 1500): data.append(gpio.input(sensor)) # print(data) # -------------------------------------------- # treat data to extract the values def bin2dec(string_num): return str(int(string_num, 2)) try: # You may need to add this to your code, as normally the sensor # starts by sending a short sequence of 0s then another short # sequence of 1s to show it is available before sending the # metrics data, as I could not see this in my data I just did not # use this code # Code # if data[count] == 0: # while data[count] == 0: # count += 1 # while data[count] == 1: # tmp = 1 # count += 1 # End of code # The first bit of data being normally truncated and as it is a zero for sure # (humidity is always under 100%) then we directly put a 0 in HumidityBit and we read # the next 31 bits which are structured as follows : # Humidity (7bits) - 0 byte - Temperature (8bits) - 0 for i in range(0, 31): bit_count = 0 while data[count] == 0: tmp = 1 count += 1 while data[count] == 1: bit_count += 1 count += 1 if bit_count > threshold: if 0 <= i < 7: HumidityBit += "1" if 15 <= i < 23: TemperatureBit += "1" else: if 0 <= i < 7: HumidityBit += "0" if 15 <= i < 23: TemperatureBit += "0" except Exception as e: print(e) print("A problem occurred during data gathering") exit(0) # the next 8 bits encode CRC which is equal to the sum of # the temperature value and the humidity value try: for i in range(0, 8): bit_count = 0 while data[count] == 0: tmp = 1 count += 1 while data[count] == 1: bit_count += 1 count += 1 if bit_count > threshold: crc += "1" else: crc += "0" except Exception as e: print(e) print("A problem occurred during CRC calculation") exit(0) Humidity = bin2dec(HumidityBit) Temperature = bin2dec(TemperatureBit) # -------------------------------------- # Check CRC is correct then print results if int(Humidity) + int(Temperature) - int(bin2dec(crc)) == 0: return ("Humidity = " + Humidity + "%"), ("Temperature = " + Temperature + "C") else: print("Wrong data, incorrect CRC") return None
from pyA13.gpio import port __author__ = "Stefan Mavrodiev" __copyright__ = "Copyright 2014, Olimex LTD" __credits__ = ["Stefan Mavrodiev"] __license__ = "GPL" __version__ = "2.0" __maintainer__ = __author__ __email__ = "*****@*****.**" led = connector.gpio0p0 # This is the same as port.PH2 button = connector.gpio3p40 """Init gpio module""" gpio.init() """Set directions""" gpio.setcfg(led, gpio.OUTPUT) gpio.setcfg(button, gpio.INPUT) """Enable pullup resistor""" gpio.pullup(button, gpio.PULLUP) #gpio.pullup(button, gpio.PULLDOWN) # Optionally you can use pull-down resistor try: print("Press CTRL+C to exit") while True: state = gpio.input(button) # Read button state """Since we use pull-up the logic will be inverted""" gpio.output(led, not state) except KeyboardInterrupt: print("Goodbye.")
__credits__ = ["Stefan Mavrodiev"] __license__ = "GPL" __version__ = "2.0" __maintainer__ = __author__ __email__ = "*****@*****.**" led = connector.gpio0p0 # This is the same as port.PH2 button = connector.gpio3p40 """Init gpio module""" gpio.init() """Set directions""" gpio.setcfg(led, gpio.OUTPUT) gpio.setcfg(button, gpio.INPUT) """Enable pullup resistor""" gpio.pullup(button, gpio.PULLUP) #gpio.pullup(button, gpio.PULLDOWN) # Optionally you can use pull-down resistor try: print ("Press CTRL+C to exit") while True: state = gpio.input(button) # Read button state """Since we use pull-up the logic will be inverted""" gpio.output(led, not state) except KeyboardInterrupt: print ("Goodbye.")