-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathultrasound-test.py
executable file
·61 lines (51 loc) · 1.3 KB
/
ultrasound-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
import requests
from datetime import datetime
import calendar
import RPi.GPIO as GPIO
import time
# These are the Pi header pin numbers; they correspond to GPIO 17 and 18
TRIG = 11
ECHO = 12
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
def distance():
GPIO.output(TRIG, 0)
time.sleep(0.000002)
GPIO.output(TRIG, 1)
time.sleep(0.00001)
GPIO.output(TRIG, 0)
while GPIO.input(ECHO) == 0:
a = 0
time1 = time.time()
while GPIO.input(ECHO) == 1:
a = 1
time2 = time.time()
during = time2 - time1
return during * 340 / 2 * 100
def loop():
counter = 60
while True:
counter = counter - 1
timestamp = datetime.now()
dis = distance()
print dis, 'cm'
print ''
time.sleep(0.5)
if counter == 0:
print timestamp
counter = 60
headers = {'Content-type': 'multipart/form-data', 'Accept': 'application/json'}
r = requests.post("http://www.sensoree.net/events/create", data={"api_key":"OP1BVFIABXF95B2PD74Y", "sensor_id": 7, "value":dis, "notified":"false", "capture_time":timestamp}, headers=headers)
print(r.status_code, r.reason)
print(r.text[:10000] + '...')
def destroy():
GPIO.cleanup()
if __name__ == "__main__":
setup()
try:
loop()
except KeyboardInterrupt:
destroy()