Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. import I2C_LCD_driver
  2. import time
  3. import dht11
  4. import datetime
  5. import RPi.GPIO as GPIO
  6. from keypad import keypad
  7.  
  8. GPIO.setwarnings(False)
  9. GPIO.setmode(GPIO.BOARD)
  10.  
  11. mylcd = I2C_LCD_driver.lcd()
  12. instance = dht11.DHT11(pin=4)
  13. mode = 0
  14. select = 0
  15. refresh = True
  16. GPIO.setup(38, GPIO.OUT)
  17. GPIO.setup(40, GPIO.OUT)
  18. PIN_TRIGGER = 35
  19. PIN_ECHO = 37
  20. GPIO.setup(PIN_TRIGGER, GPIO.OUT)
  21. GPIO.setup(PIN_ECHO, GPIO.IN)
  22.  
  23.  
  24. if __name__ == '__main__':
  25. kp = keypad(columnCount = 3)
  26. GPIO.output(PIN_TRIGGER, GPIO.LOW)
  27. print("Waiting for sensor to settle")
  28. time.sleep(2)
  29. #
  30. mylcd.lcd_display_string("Press any key", 1)
  31. try:
  32. while True:
  33. digit = kp.getKey()
  34. result = instance.read()
  35. if mode==0:
  36. if digit != None or refresh == True:
  37. refresh = False
  38. # navigation
  39. if digit==2:
  40. select = select-1
  41. if select<0:
  42. select = 3
  43. time.sleep(0.3)
  44. if digit==8:
  45. select = select+1
  46. if select>3:
  47. select = 0
  48. time.sleep(0.3)
  49. if digit==5:
  50. mode = select+1
  51. select = 0
  52. continue
  53. #
  54. # menu
  55. mylcd.lcd_clear()
  56. if select==0:
  57. mylcd.lcd_display_string("-> Temperatura", 1)
  58. mylcd.lcd_display_string("Wilgotnosc", 2)
  59. if select==1:
  60. mylcd.lcd_display_string("-> Wilgotnosc", 1)
  61. mylcd.lcd_display_string("Tajne haslo", 2)
  62. if select==2:
  63. mylcd.lcd_display_string("-> Tajne haslo", 1)
  64. mylcd.lcd_display_string("Odleglosc", 2)
  65. if select==3:
  66. mylcd.lcd_display_string("-> Odleglosc", 1)
  67. mylcd.lcd_display_string("Temperatura", 2)
  68. #print("Last valid input: " + str(datetime.datetime.now()))
  69. #print("menu")
  70. if mode==1:
  71. if result.is_valid():
  72. mylcd.lcd_clear()
  73. mylcd.lcd_display_string("%-3.1f C" % result.temperature)
  74. time.sleep(0.5)
  75. if digit != None:
  76. if digit=="#":
  77. mode = 0
  78. if mode==2:
  79. if result.is_valid():
  80. mylcd.lcd_clear()
  81. mylcd.lcd_display_string("%-3.1f %%" % result.humidity)
  82. time.sleep(0.5)
  83. if digit != None:
  84. if digit=="#":
  85. mode = 0
  86. if mode==3:
  87. time.sleep(0.4)
  88. passwd = []
  89. for i in range(4):
  90. mylcd.lcd_clear()
  91. digit = None
  92. if i==0:
  93. mylcd.lcd_display_string("Pass: ", 1)
  94. if i==1:
  95. mylcd.lcd_display_string("Pass: *", 1)
  96. if i==2:
  97. mylcd.lcd_display_string("Pass: **", 1)
  98. if i==3:
  99. mylcd.lcd_display_string("Pass: ***", 1)
  100. while digit == None:
  101. digit = kp.getKey()
  102. if digit == "#":
  103. mode = 0
  104. refresh = True
  105. continue
  106. passwd.append(digit)
  107. time.sleep(0.4)
  108. mylcd.lcd_clear()
  109. if passwd == [1, 2, 3, 4]:
  110. GPIO.output(38, 1)
  111. mylcd.lcd_display_string("GOOD", 1)
  112. else:
  113. GPIO.output(40, 1)
  114. mylcd.lcd_display_string("HEHE NOPE!", 1)
  115. time.sleep(2)
  116. GPIO.output(38, 0)
  117. GPIO.output(40, 0)
  118. mode = 0
  119. refresh = True
  120. if mode==4:
  121. mylcd.lcd_clear()
  122. GPIO.output(PIN_TRIGGER, GPIO.HIGH)
  123. time.sleep(0.00001)
  124. GPIO.output(PIN_TRIGGER, GPIO.LOW)
  125. while GPIO.input(PIN_ECHO)==0:
  126. pulse_start_time = time.time()
  127. while GPIO.input(PIN_ECHO)==1:
  128. pulse_end_time = time.time()
  129. pulse_duration = pulse_end_time - pulse_start_time
  130. distance = str(round(pulse_duration * 17150, 2))
  131. mylcd.lcd_display_string(distance, 1)
  132. time.sleep(0.4)
  133. digit = kp.getKey()
  134. if digit == "#":
  135. mode = 0
  136. refresh = True
  137. continue
  138.  
  139. except KeyboardInterrupt:
  140. print("Cleanup")
  141. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement