Advertisement
Guest User

henlo 2.1

a guest
Jan 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import RPi.GPIO as GPIO
  2. import time
  3. import signal
  4. import sys
  5. import lcddriver
  6. import psycopg2
  7.  
  8. try:
  9.     conn = psycopg2.connect("host='dumbo.db.elephantsql.com' dbname='lpdstrtn' user='lpdstrtn' password='password='OQBOtTdWyf5xbdDqwGFyi_T_rQ2KcjNw'")
  10.     cur = conn.cursor()
  11. except:
  12.     print("I am unable to connect to the database.")
  13.  
  14. def updatevolume(containernummer, volume):
  15.     sql = "UPDATE container SET volume = %s WHERE containernummer = %s"
  16.     try:
  17.         cur.execute(sql, (volume, containernummer))
  18.         conn.commit()
  19.     except:
  20.         print("Can't UPDATE container")
  21.  
  22. while True:
  23.     lcd = lcddriver.lcd()
  24.     lcd.lcd_clear()
  25.  
  26.  
  27.     GPIO.setmode(GPIO.BCM)
  28.  
  29.  
  30.     pinTrigger1 = 18
  31.     pinEcho1 = 24
  32.     pinTrigger2 = 19
  33.     pinEcho2 = 26
  34.    
  35.    
  36.  
  37.     def close(signal, frame):
  38.            
  39.             GPIO.cleanup()
  40.             sys.exit(0)
  41.             lcd.lcd_clear()
  42.  
  43.     signal.signal(signal.SIGINT, close)
  44.  
  45.     # set GPIO input and output channels
  46.    
  47.     GPIO.setup(pinTrigger2, GPIO.OUT)
  48.     GPIO.setup(pinEcho2, GPIO.IN)
  49.     GPIO.setup(pinTrigger1, GPIO.OUT)
  50.     GPIO.setup(pinEcho1, GPIO.IN)
  51.  
  52.     #1e sensor kalibratie
  53.     # Calibreren   
  54.     GPIO.output(pinTrigger1, True) 
  55.     time.sleep(0.00001)
  56.     GPIO.output(pinTrigger1, False)
  57.  
  58.     startTijd = time.time()
  59.     stopTijd = time.time()
  60.  
  61.     while 0 == GPIO.input(pinEcho1):
  62.         startTijd = time.time()
  63.  
  64.            
  65.     while 1 == GPIO.input(pinEcho1):
  66.         stopTijd = time.time()
  67.  
  68.            
  69.     TimeElapsed = stopTijd - startTijd
  70.        
  71.     afstand = (TimeElapsed * 34300) / 2
  72.  
  73.     print ("Kalibratie waarde 1: %.1f cm" % afstand)
  74.     time.sleep(1)
  75.     if afstand < 400:
  76.        
  77.        
  78.         lcdWaarde = "Afstand: %.1f cm" % afstand
  79.         lcd.lcd_display_string(lcdWaarde ,1)
  80.         lcd.lcd_display_string("Kalibratie 1 volt", 2)
  81.         time.sleep(2)
  82.         K1= afstand
  83.        
  84.    
  85.     else:
  86.         lcd.lcd_display_string("Kalibratie 1 mislukt", 1)
  87.         lcd.lcd_display_string("Herstart in 2s", 2)
  88.         time.sleep(2)
  89.         continue
  90.     #2e sensor kalibratie
  91.     GPIO.output(pinTrigger2, True) 
  92.     time.sleep(0.00001)
  93.     GPIO.output(pinTrigger2, False)
  94.  
  95.     startTijd = time.time()
  96.     stopTijd = time.time()
  97.  
  98.     while 0 == GPIO.input(pinEcho2):
  99.         startTijd = time.time()
  100.  
  101.            
  102.     while 1 == GPIO.input(pinEcho2):
  103.         stopTijd = time.time()
  104.  
  105.            
  106.     TimeElapsed = stopTijd - startTijd
  107.        
  108.     afstand = (TimeElapsed * 34300) / 2
  109.  
  110.     print ("Kalibratie waarde 2: %.1f cm" % afstand)
  111.     time.sleep(1)
  112.     if afstand < 400:
  113.        
  114.         lcdWaarde = "Afstand: %.1f cm" % afstand
  115.         lcd.lcd_display_string(lcdWaarde ,1)
  116.         lcd.lcd_display_string("Kalibratie 2 vol", 2)
  117.         time.sleep(2)
  118.         K2 = afstand
  119.         if (K2 - K1) < 3 and (K2 - K1) > -3:
  120.             diepte1 = K1
  121.             diepte2 = K2
  122.             break
  123.         else:
  124.             lcd.lcd_clear()
  125.             lcd.lcd_display_string("Verschil te hoog", 1)
  126.             time.sleep(2)
  127.             lcd.lcd_clear()
  128.             lcd.lcd_display_string("Sensor 1" + str(K1) + "cm", 1)
  129.             lcd.lcd_display_string("Sensor 2" + str(K2) + "cm", 2)
  130.             time.sleep(2)
  131.             lcd.lcd_clear()
  132.             lcd.lcd_display_string("Kalibratie mislukt", 1)
  133.             lcd.lcd_display_string("Herstart in 2s", 2)
  134.             time.sleep(2)
  135.            
  136.            
  137.            
  138.            
  139.    
  140.     else:
  141.         lcd.lcd_display_string("Kalibratie 2 mislukt", 1)
  142.         lcd.lcd_display_string("Herstart in 2s", 2)
  143.         time.sleep(2)
  144.  
  145.  
  146. while True:
  147.     #Sensor 1
  148.     GPIO.output(pinTrigger1, True) 
  149.     time.sleep(0.00001)
  150.     GPIO.output(pinTrigger1, False)
  151.  
  152.     startTijd1 = time.time()
  153.     stopTijd1 = time.time()
  154.  
  155.    
  156.     while 0 == GPIO.input(pinEcho1):
  157.         startTijd1 = time.time()
  158.  
  159.    
  160.     while 1 == GPIO.input(pinEcho1):
  161.         stopTijd1 = time.time()
  162.  
  163.    
  164.     TimeElapsed1 = stopTijd1 - startTijd1
  165.    
  166.     afstand1 = (TimeElapsed1 * 34300) / 2
  167.    
  168.     percentage1 = (100 - int(afstand1 / diepte1 * 100))
  169.     print (percentage1,"% 1")
  170.     time.sleep(0.1)
  171.    
  172.     #Sensor 2
  173.     GPIO.output(pinTrigger2, True) 
  174.     time.sleep(0.00001)
  175.     GPIO.output(pinTrigger2, False)
  176.  
  177.     startTijd2 = time.time()
  178.     stopTijd2 = time.time()
  179.  
  180.    
  181.     while 0 == GPIO.input(pinEcho2):
  182.         startTijd2 = time.time()
  183.  
  184.    
  185.     while 1 == GPIO.input(pinEcho2):
  186.         stopTijd2 = time.time()
  187.  
  188.    
  189.     TimeElapsed2 = stopTijd2 - startTijd2
  190.    
  191.     afstand2 = (TimeElapsed2 * 34300) / 2
  192.    
  193.     percentage2 = (100 - int(afstand2 / diepte2 * 100))
  194.     print (percentage2,"% 2")
  195.    
  196.     #Waarde berekenen
  197.     percentage = (percentage1 + percentage2) * 0.5
  198.     if percentage < 0:
  199.             percentage = percentage + (-1*percentage)
  200.     lcdWaarde = str(percentage) + "%"
  201.  
  202.     #updatevolume(containernummer, volume)
  203.     updatevolume(2, lcdWaarde)
  204.  
  205.     if percentage < 101 and percentage > -1:
  206.         lcd.lcd_clear()
  207.         lcd.lcd_display_string("Status: "+ lcdWaarde, 1)
  208.    
  209.     time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement