废话不多说了,直接上代码。
参考了网上的代码
注意!是PYTHON2的!
import urllib import urllib2 import RPi.GPIO as GPIO import time def checkdist(): #发出触发信号 GPIO.output(2,GPIO.HIGH) #保持10us以上(我选择15us) time.sleep(0.000015) GPIO.output(2,GPIO.LOW) while not GPIO.input(3): pass #发现高电平时开时计时 t1 = time.time() while GPIO.input(3): pass #高电平结束停止计时 t2 = time.time() #返回距离,单位为米 return (t2-t1)*340/2 GPIO.setmode(GPIO.BCM) #第3号针,GPIO2 GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW) #第5号针,GPIO3 GPIO.setup(3,GPIO.IN) time.sleep(4) try: a="" #声明A,PO最后一步就差这里 while True: print 'Distance: %0.2f m'%checkdist() url = 'http://***.**/TinyWebDB_Service.php?/storeavalue' a=str(checkdist()) values = {'tag':'2011','value':a} #Tag随你便 data = urllib.urlencode(values) print data req = urllib2.Request(url, data) response = urllib2.urlopen(req) the_page = response.read() print the_page #打印响应 time.sleep(0.5) except KeyboardInterrupt: GPIO.cleanup()