Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import signal
  4. import sys
  5. import lcddriver
  6. import psycopg2
  7. percentage = 0
  8.  
  9. while True:
  10. lcd = lcddriver.lcd()
  11. lcd.lcd_clear()
  12.  
  13. GPIO.setmode(GPIO.BCM)
  14.  
  15. pinTrigger1 = 18
  16. pinEcho1 = 24
  17. pinTrigger2 = 19
  18. pinEcho2 = 26
  19. pinTrigger3 = 21
  20. pinEcho3 = 20
  21.  
  22.  
  23. def close(signal, frame):
  24.  
  25. GPIO.cleanup()
  26. sys.exit(0)
  27. lcd.lcd_clear()
  28.  
  29.  
  30. signal.signal(signal.SIGINT, close)
  31.  
  32. # set GPIO input and output channels
  33. GPIO.setup(pinTrigger3, GPIO.OUT)
  34. GPIO.setup(pinEcho3, GPIO.IN)
  35. GPIO.setup(pinTrigger2, GPIO.OUT)
  36. GPIO.setup(pinEcho2, GPIO.IN)
  37. GPIO.setup(pinTrigger1, GPIO.OUT)
  38. GPIO.setup(pinEcho1, GPIO.IN)
  39. # Connecten database
  40. try:
  41. conn = psycopg2.connect(
  42. "dbname='lpdstrtn' " "user='lpdstrtn' " "host='dumbo.db.elephantsql.com' " "password='OQBOtTdWyf5xbdDqwGFyi_T_rQ2KcjNw'")
  43. except:
  44. lcd.lcd_display_string("Connectie", 1)
  45. lcd.lcd_display_string("Mislukt", 2)
  46. print("I am unable to connect to the database.")
  47.  
  48. time.sleep(2)
  49. continue
  50. cur = conn.cursor()
  51. lcd.lcd_display_string("Connectie", 1)
  52. lcd.lcd_display_string("Voltooid", 2)
  53. time.sleep(2)
  54.  
  55.  
  56. def updatevolume(containernummer, volume):
  57. sql = "UPDATE container SET volume = %s WHERE containernummer = %s"
  58. try:
  59. cur.execute(sql, (volume, containernummer))
  60. conn.commit()
  61. except:
  62. print("Can't UPDATE container")
  63.  
  64.  
  65. # 1e sensor kalibratie
  66. GPIO.output(pinTrigger1, True)
  67. time.sleep(0.00001)
  68. GPIO.output(pinTrigger1, False)
  69.  
  70. startTijd = time.time()
  71. stopTijd = time.time()
  72.  
  73. while 0 == GPIO.input(pinEcho1):
  74. startTijd = time.time()
  75.  
  76. while 1 == GPIO.input(pinEcho1):
  77. stopTijd = time.time()
  78.  
  79. TimeElapsed = stopTijd - startTijd
  80.  
  81. afstand = (TimeElapsed * 34300) / 2
  82.  
  83. print ("Kalibratie waarde 1: %.1f cm" % afstand)
  84. time.sleep(1)
  85. if afstand < 400:
  86.  
  87. lcdWaarde = "Afstand: %.1f cm" % afstand
  88. lcd.lcd_display_string(lcdWaarde, 1)
  89. lcd.lcd_display_string("Kalibratie 1 volt", 2)
  90. time.sleep(2)
  91. K1 = afstand
  92.  
  93.  
  94. else:
  95. lcd.lcd_display_string("Kalibratie 1 mislukt", 1)
  96. lcd.lcd_display_string("Herstart in 2s", 2)
  97. time.sleep(2)
  98. continue
  99. # 3e sensor kalibratie
  100. GPIO.output(pinTrigger3, True)
  101. time.sleep(0.00001)
  102. GPIO.output(pinTrigger3, False)
  103.  
  104. startTijd = time.time()
  105. stopTijd = time.time()
  106.  
  107. while 0 == GPIO.input(pinEcho3):
  108. startTijd = time.time()
  109.  
  110. while 1 == GPIO.input(pinEcho3):
  111. stopTijd = time.time()
  112.  
  113. TimeElapsed = stopTijd - startTijd
  114.  
  115. afstand = (TimeElapsed * 34300) / 2
  116.  
  117. print ("Kalibratie waarde 1: %.1f cm" % afstand)
  118. time.sleep(1)
  119. if afstand < 400:
  120.  
  121. lcdWaarde = "Afstand: %.1f cm" % afstand
  122. lcd.lcd_display_string(lcdWaarde, 1)
  123. lcd.lcd_display_string("Kalibratie 3 volt", 2)
  124. time.sleep(2)
  125. K3 = afstand
  126.  
  127.  
  128. else:
  129. lcd.lcd_display_string("Kalibratie 3 mislukt", 1)
  130. lcd.lcd_display_string("Herstart in 2s", 2)
  131. time.sleep(2)
  132. continue
  133. # 2e sensor kalibratie
  134. GPIO.output(pinTrigger2, True)
  135. time.sleep(0.00001)
  136. GPIO.output(pinTrigger2, False)
  137.  
  138. startTijd = time.time()
  139. stopTijd = time.time()
  140.  
  141. while 0 == GPIO.input(pinEcho2):
  142. startTijd = time.time()
  143.  
  144. while 1 == GPIO.input(pinEcho2):
  145. stopTijd = time.time()
  146.  
  147. TimeElapsed = stopTijd - startTijd
  148.  
  149. afstand = (TimeElapsed * 34300) / 2
  150.  
  151. print ("Kalibratie waarde 2: %.1f cm" % afstand)
  152. time.sleep(1)
  153. if afstand < 400:
  154.  
  155. lcdWaarde = "Afstand: %.1f cm" % afstand
  156. lcd.lcd_display_string(lcdWaarde, 1)
  157. lcd.lcd_display_string("Kalibratie 2 vol", 2)
  158. time.sleep(2)
  159. K2 = afstand
  160. if (K2 - K1) < 4 and (K2 - K1) > - 4:
  161. if (K3 - K1) < 4 and (K3 - K1) > - 4:
  162. if (K2 - K3) < 4 and (K2 - K3) > - 4:
  163. diepte1 = K1
  164. diepte2 = K2
  165. diepte3 = K3
  166. break
  167. else:
  168. lcd.lcd_clear()
  169. lcd.lcd_display_string("Verschil te hoog", 1)
  170. time.sleep(2)
  171. lcd.lcd_clear()
  172. lcd.lcd_display_string("Sensor 1" + str(K1) + "cm", 1)
  173. lcd.lcd_display_string("Sensor 2" + str(K2) + "cm", 2)
  174. time.sleep(2)
  175. lcd.lcd_clear()
  176. lcd.lcd_display_string("Kalibratie mislukt", 1)
  177. lcd.lcd_display_string("Herstart in 2s", 2)
  178. time.sleep(2)
  179. else:
  180. lcd.lcd_clear()
  181. lcd.lcd_display_string("Verschil te hoog", 1)
  182. time.sleep(2)
  183. lcd.lcd_clear()
  184. lcd.lcd_display_string("Sensor 1" + str(K1) + "cm", 1)
  185. lcd.lcd_display_string("Sensor 2" + str(K2) + "cm", 2)
  186. time.sleep(2)
  187. lcd.lcd_clear()
  188. lcd.lcd_display_string("Kalibratie mislukt", 1)
  189. lcd.lcd_display_string("Herstart in 2s", 2)
  190. time.sleep(2)
  191. else:
  192. lcd.lcd_clear()
  193. lcd.lcd_display_string("Verschil te hoog", 1)
  194. time.sleep(2)
  195. lcd.lcd_clear()
  196. lcd.lcd_display_string("Sensor 1" + str(K1) + "cm", 1)
  197. lcd.lcd_display_string("Sensor 2" + str(K2) + "cm", 2)
  198. time.sleep(2)
  199. lcd.lcd_clear()
  200. lcd.lcd_display_string("Kalibratie mislukt", 1)
  201. lcd.lcd_display_string("Herstart in 2s", 2)
  202. time.sleep(2)
  203.  
  204.  
  205.  
  206.  
  207.  
  208. else:
  209. lcd.lcd_display_string("Kalibratie 2 mislukt", 1)
  210. lcd.lcd_display_string("Herstart in 2s", 2)
  211. time.sleep(2)
  212.  
  213.  
  214. while True:
  215. # Sensor 1
  216. GPIO.output(pinTrigger1, True)
  217. time.sleep(0.00001)
  218. GPIO.output(pinTrigger1, False)
  219.  
  220. startTijd1 = time.time()
  221. stopTijd1 = time.time()
  222.  
  223. while 0 == GPIO.input(pinEcho1):
  224. startTijd1 = time.time()
  225.  
  226. while 1 == GPIO.input(pinEcho1):
  227. stopTijd1 = time.time()
  228.  
  229. TimeElapsed1 = stopTijd1 - startTijd1
  230.  
  231. afstand1 = (TimeElapsed1 * 34300) / 2
  232.  
  233. percentage1 = (100 - int(afstand1 / diepte1 * 100))
  234. print (percentage1, "% 1")
  235. time.sleep(0.1)
  236.  
  237. # Sensor 2
  238. GPIO.output(pinTrigger2, True)
  239. time.sleep(0.00001)
  240. GPIO.output(pinTrigger2, False)
  241.  
  242. startTijd2 = time.time()
  243. stopTijd2 = time.time()
  244.  
  245. while 0 == GPIO.input(pinEcho2):
  246. startTijd2 = time.time()
  247.  
  248. while 1 == GPIO.input(pinEcho2):
  249. stopTijd2 = time.time()
  250.  
  251. TimeElapsed2 = stopTijd2 - startTijd2
  252.  
  253. afstand2 = (TimeElapsed2 * 34300) / 2
  254.  
  255. percentage2 = (100 - int(afstand2 / diepte2 * 100))
  256. print (percentage2, "% 2")
  257. # Sensor 3
  258. GPIO.output(pinTrigger3, True)
  259. time.sleep(0.00001)
  260. GPIO.output(pinTrigger3, False)
  261.  
  262. startTijd3 = time.time()
  263. stopTijd3 = time.time()
  264.  
  265. while 0 == GPIO.input(pinEcho3):
  266. startTijd3 = time.time()
  267.  
  268. while 1 == GPIO.input(pinEcho3):
  269. stopTijd3 = time.time()
  270.  
  271. TimeElapsed3 = stopTijd3 - startTijd3
  272.  
  273. afstand3 = (TimeElapsed3 * 34300) / 2
  274.  
  275. percentage3 = (100 - int(afstand3 / diepte3 * 100))
  276. print (percentage3, "% 3")
  277. time.sleep(0.1)
  278. # Waarde berekenen
  279. percentage = (percentage1 + percentage2 + percentage3) / 3
  280. if percentage < 0:
  281. percentage = percentage + (-1 * percentage)
  282. percentage = ("%.2f" % percentage)
  283. lcdWaarde = str(percentage) + "%"
  284.  
  285. # updatevolume(containernummer, volume)
  286. updatevolume(102, percentage)
  287.  
  288. if percentage < 101 and percentage > -1:
  289. lcd.lcd_clear()
  290. lcd.lcd_display_string("Status: " + lcdWaarde, 1)
  291.  
  292.  
  293. time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement