Skip to content

Instantly share code, notes, and snippets.

@shyampurk
Created March 15, 2016 05:57
Show Gist options
  • Save shyampurk/82c470572445065b98f7 to your computer and use it in GitHub Desktop.
Save shyampurk/82c470572445065b98f7 to your computer and use it in GitHub Desktop.
Smart Trash Can Demo
def distanceMeasurement():
prev_distance = 0
while 1:
ultrasonicSensor_init()
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
#Starts the timer
while GPIO.input(ECHO)==0:
pulse_start = time.time()
#Waits for the timer to end once the pin is high
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
if(prev_distance != distance and prev_distance > (distance+3) or prev_distance < (distance-3)):
prev_distance = distance
mqttc.publish("garbaseData", "{\"container\":\""+GARBAGE_ID+"\",\"level\":"+str(distance)+"}")
print "Distance:",distance,"cm"
GPIO.cleanup()
def on_message(client, userdata, msg):
message = dict()
message = json.loads(msg.payload)
print pubnub.publish(channel="garbageApp-resp", message=message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment