Guest User

Untitled

a guest
May 26th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Measure distance to water level in sump pit using
  4. # HC-SR04 ultrasonic ranging module
  5. # Sensor info:
  6. # 2cm - 4m. range with ~3mm. accuracy, 15 degree measuring angle
  7. # 5VDC, 15mA working current
  8. # Suggestion in spec to connect ground before 5VDC line when hot plugging
  9. # Measure surface area no less than 0.5 m^2 (really?)
  10.  
  11. # range_sensor.py
  12. # Prints the current date/time and measurement in cm. as CSV
  13. # Calls another process if reading falls below threshold
  14.  
  15. import RPi.GPIO as GPIO
  16. import time
  17. import datetime
  18. import os
  19. import subprocess
  20.  
  21. # Minimum distance to water level (cm) / threshold
  22. MIN_DISTANCE = 24.5
  23.  
  24. # Calculate median of a list
  25. def median(lst):
  26. lst = sorted(lst)
  27. if len(lst) < 1:
  28. return None
  29. if len(lst) %2 == 1:
  30. return lst[((len(lst)+1)/2)-1]
  31. else:
  32. return float(sum(lst[(len(lst)/2)-1:(len(lst)/2)+1]))/2.0
  33.  
  34. # Sensor Pinout
  35. TRIG = 23
  36. ECHO = 24
  37.  
  38. # Set mode and data direction
  39. GPIO.setmode(GPIO.BCM)
  40. GPIO.setup(TRIG, GPIO.OUT)
  41. GPIO.setup(ECHO, GPIO.IN)
  42.  
  43. # Speed of sound constant
  44. # 343 m / sec. is rougly the speed of sound in dry air at 20C
  45. # The speed decreases with temperature and increases a bit with more humidity
  46. # This is a cool, damp basement, so we'll assume 341 m / sec. (15C / 59F with 60%RH)
  47. # Sensor measures round-trip time, so we divide by 2 to get half of that distance
  48. SPEED = 17050
  49.  
  50. distances = []
  51. sampleCount = 0
  52. while sampleCount < 30:
  53. # Distance Measurement In Progress
  54.  
  55. # Limit sample rate to every 10ms (even though spec recommends 60ms)
  56. time.sleep(0.01)
  57.  
  58. # Send 10uS pulse
  59. GPIO.output(TRIG, True)
  60. time.sleep(0.00001)
  61. GPIO.output(TRIG, False)
  62.  
  63. # Waiting for ECHO to go high...
  64. # Max. wait 1 second using pulse_end as temp variable
  65. pulse_start = time.time()
  66. pulse_end = time.time()
  67. while GPIO.input(ECHO)==0 and pulse_start - pulse_end < 1:
  68. pulse_start = time.time()
  69.  
  70. # Waiting for ECHO to go low... (max. wait 1 second)
  71. pulse_end = time.time()
  72. while GPIO.input(ECHO)==1 and pulse_end - pulse_start < 1:
  73. pulse_end = time.time()
  74.  
  75. # Append distance measurement to samples array
  76. distances.append((pulse_end - pulse_start) * SPEED)
  77. sampleCount = sampleCount + 1
  78.  
  79. # Cleanup (set GPIOs to input, etc.)
  80. GPIO.cleanup()
  81. # Print date/time and median measurment in cm.
  82. median = round(median(distances), 1)
  83. print datetime.datetime.now().strftime("%Y-%m-%d %H:%M") + "," + str(median)
  84. # Notify of problem if water level is high
  85. if median < MIN_DISTANCE:
  86. subprocess.call(["node", os.path.dirname(os.path.realpath(__file__)) + "/email_warning.js", str(median)])
Add Comment
Please, Sign In to add comment