A rewrite of the Raspberry Pi Garage Door Opener using Flask
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.8 KiB

  1. # Load libraries
  2. import RPi.GPIO as GPIO #Import RPi GPIO library
  3. import time
  4. from time import localtime, strftime
  5. import os
  6. # Set up the GPIO pins
  7. GPIO.setwarnings(False)
  8. GPIO.setmode(GPIO.BOARD)
  9. PIN_TRIG = 40
  10. PIN_ECHO = 38
  11. def collect_data():
  12. GPIO.setup(PIN_TRIG, GPIO.OUT) #Setup the gpio trigger pin as input
  13. GPIO.setup(PIN_ECHO, GPIO.IN) #Setup the gpio echo pin as output
  14. time.sleep(0.5) #Wait for 0.5 seconds for sensor to settle
  15. GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low
  16. GPIO.output(PIN_TRIG, GPIO.HIGH) #Set trigger to high
  17. time.sleep(0.00001) #Wait for 0.1 milliseconds before setting to low again
  18. GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low again
  19. while GPIO.input(PIN_ECHO)==0:
  20. pulse_start_time = time.time() #Set the start time of when the waves are emitted by the sensor
  21. while GPIO.input(PIN_ECHO)==1:
  22. pulse_end_time = time.time() #Record the time the waves traveled back to the sensor
  23. pulse_duration = pulse_end_time - pulse_start_time #Calculate how long it took for the round trip of the waves
  24. distance = round(pulse_duration * 17150, 2) #Convert the time it took to centimeters and round to 2 decimals
  25. date_time = strftime("%Y-%m-%d %H:%M:%S", localtime())
  26. if distance <= 80: #Check if the distance is greater than 80cm (This will depend on the garage)
  27. distance = str(distance)
  28. message = 'At ' + date_time + ', the garage was opened at a distance of ' + distance + ' centimeters!'
  29. command = '../telegram-bot-bash/bin/send_broadcast.sh --doit' + ' "' + message + '"' + '> /dev/null'
  30. os.system(command)
  31. if __name__ == '__main__':
  32. while True:
  33. try:
  34. collect_data()
  35. except:
  36. print("quitting")
  37. quit()
  38. time.sleep(5)