-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathultrasound-binary-test.py
executable file
·66 lines (55 loc) · 1.31 KB
/
ultrasound-binary-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
62
63
64
65
66
#!/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()
if dis > 200:
dis = 1
else:
dis = 0
print dis
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://192.168.1.5:3000/events/create", data={"api_key":"NR71J65VHMH5WZ26A1K4", "sensor_id": 2, "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()