<gh_stars>0
import RPi.GPIO as GPIO
import time
from libdw import pyrebase

#Database Set-Up

projectid = "cleanbean-9e2f5"
dburl = "https://" + projectid + ".firebaseio.com"
authdomain = projectid + ".firebaseapp.com"
apikey = "<KEY>"
email = "<EMAIL>"
password = "<PASSWORD>"

config = {
    "apiKey": apikey,
    "authDomain": authdomain,
    "databaseURL": dburl,
}


firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
user = auth.sign_in_with_email_and_password(email, password)
db = firebase.database()
 
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
#set GPIO Pins
GPIO_TRIGGER = 6
GPIO_ECHO = 5
 
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

binHeight = 18.6

def trashHeight(distance):
    return 1-(binHeight-distance)/binHeight

def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)
 
    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()
 
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 34300) / 2
 
    return distance
 
if __name__ == '__main__':
    try:
        distold = binHeight
        distance_list = []
        trash_height_list = []
        while True:
            dist = distance()
            distance_list.append(dist)
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
            print(trashHeight(dist))
            trash_height = trashHeight(dist)
            db.child("Bin_1").set(trash_height, user['idToken']) 
        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()

import RPi.GPIO as GPIO
import time
from libdw import pyrebase

#Database Set-Up

projectid = "cleanbean-9e2f5"
dburl = "https://" + projectid + ".firebaseio.com"
authdomain = projectid + ".firebaseapp.com"
apikey = "<KEY>"
email = "<EMAIL>"
password = "<PASSWORD>"

config = {
    "apiKey": apikey,
    "authDomain": authdomain,
    "databaseURL": dburl,
}


firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
user = auth.sign_in_with_email_and_password(email, password)
db = firebase.database()
 
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
#set GPIO Pins
GPIO_TRIGGER = 6
GPIO_ECHO = 5
 
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

binHeight = 18.6

def trashHeight(distance):
    return 1-(binHeight-distance)/binHeight

def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)
 
    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()
 
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 34300) / 2
 
    return distance
 
if __name__ == '__main__':
    try:
        distold = binHeight
        distance_list = []
        trash_height_list = []
        while True:
            dist = distance()
            distance_list.append(dist)
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
            print(trashHeight(dist))
            trash_height = trashHeight(dist)
            db.child("Bin_1").set(trash_height, user['idToken']) 
        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()
#Database Set-Up #GPIO Mode (BOARD / BCM) #set GPIO Pins #set GPIO direction (IN / OUT) # set Trigger to HIGH # set Trigger after 0.01ms to LOW # save StartTime # save time of arrival # time difference between start and arrival # multiply with the sonic speed (34300 cm/s) # and divide by 2, because there and back # Reset by pressing CTRL + C