Things used in this project

Schematics

Raspberry Pi Hardware setup
Raspberrypi

Code

usoniciot.pyPython
Main python program
import requests
from usoniciotstream import reading
while True:
    
    key = 'SERIAL'
    sensvalue = reading(0)
    x = int(sensvalue)
    url = "http://www.taurussystem.com/ws/webresources/Sensor/set?key=%s&&sensor=2001&&value=%s" % (key, x)
    print url
    r = requests.head(url)
    print r
usoniciotstream.pyPython
put it in the same folder as of usonic.py
#!/usr/bin/python

# remember to change the GPIO values below to match your sensors
# GPIO output = the pin that's connected to "Trig" on the sensor
# GPIO input = the pin that's connected to "Echo" on the sensor

def reading(sensor):
	import time
	import RPi.GPIO as GPIO
	
	# Disable any warning message such as GPIO pins in use
	GPIO.setwarnings(False)
	
	# use the values of the GPIO pins, and not the actual pin number
	# so if you connect to GPIO 25 which is on pin number 22, the 
	# reference in this code is 25, which is the number of the GPIO 
	# port and not the number of the physical pin
	GPIO.setmode(GPIO.BCM)
	
	if sensor == 0:
		
		# point the software to the GPIO pins the sensor is using
		# change these values to the pins you are using
		# GPIO output = the pin that's connected to "Trig" on the sensor
		# GPIO input = the pin that's connected to "Echo" on the sensor
		GPIO.setup(17,GPIO.OUT)
		GPIO.setup(27,GPIO.IN)
		GPIO.output(17, GPIO.LOW)
		
		# found that the sensor can crash if there isn't a delay here
		# no idea why. If you have odd crashing issues, increase delay
		time.sleep(0.3)
		
		# sensor manual says a pulse ength of 10Us will trigger the 
		# sensor to transmit 8 cycles of ultrasonic burst at 40kHz and 
		# wait for the reflected ultrasonic burst to be received
		
		# to get a pulse length of 10Us we need to start the pulse, then
		# wait for 10 microseconds, then stop the pulse. This will 
		# result in the pulse length being 10Us.
		
		# start the pulse on the GPIO pin 
		# change this value to the pin you are using
		# GPIO output = the pin that's connected to "Trig" on the sensor
		GPIO.output(17, True)
		
		# wait 10 micro seconds (this is 0.00001 seconds) so the pulse
		# length is 10Us as the sensor expects
		time.sleep(0.00001)
		
		# stop the pulse after the time above has passed
		# change this value to the pin you are using
		# GPIO output = the pin that's connected to "Trig" on the sensor
		GPIO.output(17, False)

		# listen to the input pin. 0 means nothing is happening. Once a
		# signal is received the value will be 1 so the while loop
		# stops and has the last recorded time the signal was 0
		# change this value to the pin you are using
		# GPIO input = the pin that's connected to "Echo" on the sensor
		while GPIO.input(27) == 0:
		  signaloff = time.time()
		
		# listen to the input pin. Once a signal is received, record the
		# time the signal came through
		# change this value to the pin you are using
		# GPIO input = the pin that's connected to "Echo" on the sensor
		while GPIO.input(27) == 1:
		  signalon = time.time()
		
		# work out the difference in the two recorded times above to 
		# calculate the distance of an object in front of the sensor
		timepassed = signalon - signaloff
		
		# we now have our distance but it's not in a useful unit of
		# measurement. So now we convert this distance into centimetres
		distance = timepassed * 17000
		
		# return the distance of an object in front of the sensor in cm
		return distance
		
		# we're no longer using the GPIO, so tell software we're done
		GPIO.cleanup()

	else:
		print "Incorrect usonic() function varible."

print reading(0)

Credits

Rishabh lrhqec0xq2
Rishabh Sinha

Electronic Hobbyist. Code blooded Geek. Entrepreneur. Hacker. 14. Human.

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Test Your Internet Speed Using a Raspberry Pi + Ubidots
Easy
  • 260
  • 4

Protip

Use your Raspberry Pi to measure any internet connection speed with Ubidots.

Chicken Coop Livestream
Easy
  • 2,422
  • 10

Full instructions

We have two chickens, and their coop is located at the back of the garden. I decided to build a Pi-based streaming device to check on them.

Raspberry Pi Shutdown / Restart Button
Easy
  • 5,147
  • 20

Full instructions

Building an installation project? Shut down or reboot your project safely, without a keyboard or SSH!

DIY Raspberry Pi Indoor Outdoor Webcam
Easy
  • 1,402
  • 6

Full instructions

Ever want to monitor your home but don't want to buy a $100 1080p webcam? Well I did, and you can too!

3d printed case for a RaspPi 3, an Arduino and a 5" display
Easy
  • 293
  • 4

Full instructions

A simple and compact frame to mount a 5" monitor, a Raspberry Pi, an Arduino, and a battery, which is extensible for other sensors.

DYI hydroponics system powered by UniPi
Easy
  • 1,373
  • 10

Full instructions

DYI cloud connected hydroponics system powered by UniPi and Cloud4RPi

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog