Sabtu, 10 November 2018

Code sensor Ultrasonic


import RPi.GPIO as GPIO                    #Import GPIO library
import time                                #Import time library
GPIO.setmode(GPIO.BCM)                     #Set GPIO pin numbering
GPIO.setwarnings(False)

TRIG = 23                              #Associate pin 23 to TRIG
ECHO = 24                               #Associate pin 24 to ECHO

print "Distance measurement in progress"

GPIO.setup(TRIG,GPIO.OUT)                  #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN)                   #Set pin as GPIO in

while True:

  GPIO.output(TRIG, False)                 #Set TRIG as LOW
  #print "Waitng For Sensor To Settle"
  time.sleep(2)                            #Delay of 2 seconds

  GPIO.output(TRIG, True)                  #Set TRIG as HIGH
  time.sleep(0.00001)                      #Delay of 0.00001 seconds
  GPIO.output(TRIG, False)                 #Set TRIG as LOW

  while GPIO.input(ECHO)==0:               #Check whether the ECHO is LOW
    pulse_start = time.time()              #Saves the last known time of LOW pulse

  while GPIO.input(ECHO)==1:               #Check whether the ECHO is HIGH
    pulse_end = time.time()                #Saves the last known time of HIGH pulse

  pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable

  distance = pulse_duration * 17150        #Multiply pulse duration by 17150 to get distance
  distance = round(distance, 2)            #Round to two decimal points

  if distance > 2 and distance < 400:      #Check whether the distance is within range
    print "Distance:",distance - 0.5,"cm"  #Print distance with 0.5 cm calibration
  else:
    print "Out Of Range"                   #display out of range
 

MENGGUNAKAN 2 ULTRASONIC


import serial

from time import gmtime, strftime
from random import randint

import RPi.GPIO as GPIO                    #Import GPIO library
import time                                #Import time library
GPIO.setmode(GPIO.BCM)                     #Set GPIO pin numbering
import MySQLdb
GPIO.setwarnings(False)

TRIG = 23                              #Associate pin 23 to TRIG
ECHO = 24                                 #Associate pin 24 to ECHO
TRIG1 = 18                                #Associate pin 23 to TRIG
ECHO2 = 27  

db = MySQLdb.connect("localhost","root","12345","db_pemesanan_parkir")
cursor = db.cursor()

st=0
st2=0
print "Distance measurement in progress"

GPIO.setup(TRIG,GPIO.OUT)                  #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN)                   #Set pin as GPIO in
GPIO.setup(TRIG1,GPIO.OUT)                  #Set pin as GPIO out
GPIO.setup(ECHO2,GPIO.IN)                   #Set pin as GPIO in

while True:

  GPIO.output(TRIG, False)                 #Set TRIG as LOW
  #print "Waitng For Sensor To Settle"
  time.sleep(1)                            #Delay of 2 seconds

  GPIO.output(TRIG, True)                  #Set TRIG as HIGH
  time.sleep(0.00001)                      #Delay of 0.00001 seconds
  GPIO.output(TRIG, False)                 #Set TRIG as LOW

  while GPIO.input(ECHO)==0:               #Check whether the ECHO is LOW
    pulse_start = time.time()              #Saves the last known time of LOW pulse

  while GPIO.input(ECHO)==1:               #Check whether the ECHO is HIGH
    pulse_end = time.time()                #Saves the last known time of HIGH pulse

  pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable

  distance = pulse_duration * 17150        #Multiply pulse duration by 17150 to get distance
  distance = round(distance, 2)            #Round to two decimal points

  R=distance - 0.5
  if R > 2 and R < 5 and st<1:      #Check whether the distance is within range
    st=1
    print "TERISI:",R,"cm"  #Print distance with 0.5 cm calibration
    tgl=strftime("%Y-%m-%d", gmtime())
    jam=strftime("%H:%M:%S", gmtime())
    ket=str(R)
    area="IA"
    status="TERISI"
    sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707001'" % (tgl,jam,area,status,ket)
    #print (sql)
    try:
         cursor.execute(sql)
         db.commit()
    except:
         db.rollback()
        

  if R> 5 and st>0:
    print "KOSONG:" ,R,"cm"                  #display out of range
    st=0
    tgl=strftime("%Y-%m-%d", gmtime())
    jam=strftime("%H:%M:%S", gmtime())
    ket=str(R)
    area="IA"
    status="KOSONG"
    sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707001'" % (tgl,jam,area,status,ket)
    #print (sql)
    try:
         cursor.execute(sql)
         db.commit()
    except:
         db.rollback()


  GPIO.output(TRIG1, False)                 #Set TRIG as LOW
  #print "Waitng For Sensor To Settle"
  time.sleep(1)                            #Delay of 2 seconds

  GPIO.output(TRIG1, True)                  #Set TRIG as HIGH
  time.sleep(0.00001)                      #Delay of 0.00001 seconds
  GPIO.output(TRIG1, False)                 #Set TRIG as LOW

  while GPIO.input(ECHO2)==0:               #Check whether the ECHO is LOW
    pulse_start = time.time()              #Saves the last known time of LOW pulse

  while GPIO.input(ECHO2)==1:               #Check whether the ECHO is HIGH
    pulse_end = time.time()                #Saves the last known time of HIGH pulse

  pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable

  distance = pulse_duration * 17150        #Multiply pulse duration by 17150 to get distance
  distance = round(distance, 2)            #Round to two decimal points

  R=distance - 0.5
  if R > 2 and R < 5 and st2<1:      #Check whether the distance is within range
    st2=1
    print "ADA:",R,"cm"  #Print distance with 0.5 cm calibration
    tgl=strftime("%Y-%m-%d", gmtime())
    jam=strftime("%H:%M:%S", gmtime())
    ket=str(R)
    area="BI"
    status="ADA"
    #sql ="INSERT INTO `tb_parkir` (`tanggal`, `jam`, `area`, `status`, `keterangan`) VALUES ('%s', '%s', '%s', '%s', '%s') " % (tgl,jam,area,status,ket)
    sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707002'" % (tgl,jam,area,status,ket)
    #print (sql)
    try:
         cursor.execute(sql)
         db.commit()
    except:
         db.rollback()
        

  if R> 5 and st2>0:
    print "TIDAK ADA:" ,R,"cm"                  #display out of range
    st2=0
    tgl=strftime("%Y-%m-%d", gmtime())
    jam=strftime("%H:%M:%S", gmtime())
    ket=str(R)
    area="IB"
    status="TIDAK ADA"
    sql ="UPDATE `tb_parkir` set `tanggal`='%s', `jam`='%s', `area`='%s', `status`='%s', `keterangan`='%s' where kode_parkir='PKR1707002'" % (tgl,jam,area,status,ket)
    #print (sql)
    try:
         cursor.execute(sql)
         db.commit()
    except:
         db.rollback()

   
 

Tidak ada komentar:

Posting Komentar