Skip to content

Commit

Permalink
added code for measure dist using ultrasonic sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddarth Sharma committed Aug 1, 2017
1 parent a8df3a6 commit 8a5bd42
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
66 changes: 65 additions & 1 deletion rpi/stream_sensor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,69 @@

import socket
import time
from rpi.utils import (server_address)
import RPi.GPIO as GPIO

# TODO: Complete implementation

def measure():
# Send a 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)

start = time.time()

while GPIO.input(GPIO_ECHO) == 0:
start = time.time()

while GPIO.output(GPIO_ECHO) == 1:
stop = time.time()

elapsed = stop - start
distance = (elapsed * 34300) / 2

return distance


def measure_average():
# returns the average.
distance1 = measure()
time.sleep(0.1)

distance2 = measure()
time.sleep(0.1)

distance3 = measure()
distance = (distance1 + distance2 + distance3) / 3
return distance


if __name__ == '__main__':
sock = socket.socket()
sock.connect(server_address)
print("Connected to the server! Starting to measure the distance...")

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# Define the GPIO pins for trigger and echo
GPIO_TRIGGER = 23
GPIO_ECHO = 24

GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

# Set trigger to false and allow module to settle
GPIO.output(GPIO_TRIGGER, False)
time.sleep(0.5)

try:
while True:
# Send distance to server every 0.5sec
distance = measure_average()
print("Distance: {0:.1f}".format(distance))
sock.send(distance)
time.sleep(0.5)
finally:
sock.close()
GPIO.cleanup()
7 changes: 3 additions & 4 deletions rpi/stream_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import picamera
import struct
import socket


from rpi.utils import (server_address
)
# Create a TCP/IP socket and establish connection with
# the server
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Starting connection')

server_address = ('10.104.64.231', 45713)
server_address = ('192.168.0.88', 45713)

client_socket.connect(server_address)

print('Connected to server')
Expand Down
6 changes: 6 additions & 0 deletions rpi/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""utils.py: File containing utility functions"""

server_address = ('10.104.64.231', 45713)
# server_address = ('192.168.0.88', 45713)


2 changes: 1 addition & 1 deletion test/capture_sensor_data_server_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def capture_data(self):
print("Distance: {0:.1f} cm".format(sensor_data))

# Test for 1 min
if time.time() > 60:
if time.time() - start > 300:
break
finally:
self.connection.close()
Expand Down

0 comments on commit 8a5bd42

Please sign in to comment.