fWeidinger

Untitled

Dec 13th, 2019
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import RPi.GPIO as GPIO
  5. import time
  6.  
  7. # Configuration
  8. PWR_LED_PIN = 26
  9. TEMP_LED_PIN = 21
  10. PWR_BUTTON_PIN = 15
  11. WARNING_TEMP = 65.0
  12. HIGH_TEMP = 70.0
  13. CRITICAL_TEMP = 75.0
  14. SHUTDOWN_DELAY_SECS = 5
  15. SHUTDOWN_FLASH_DELAY = 0.25
  16. WARNING_TEMP_FLASH_DELAY = 0.25
  17.  
  18. shutdown = 0
  19. lastpwrledstate = 1
  20. lastwarningledstate = 0
  21. shutdowntimer = 0
  22. shutdown_now = 0
  23.  
  24. #Control Leds
  25. try:
  26. while (1):
  27. GPIO.setmode(GPIO.BCM)
  28. GPIO.setup(TEMP_LED_PIN, GPIO.OUT)
  29. GPIO.setup(PWR_LED_PIN, GPIO.OUT)
  30. GPIO.setup(PWR_BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  31.  
  32. from gpiozero import CPUTemperature
  33.  
  34. cpu = CPUTemperature()
  35.  
  36. #Check Pwr Button
  37. if GPIO.input(PWR_BUTTON_PIN) == GPIO.HIGH:
  38. shutdown = 1
  39.  
  40. #Light Pwr Led
  41. if shutdown == 0:
  42. GPIO.output(PWR_LED_PIN, GPIO.HIGH)
  43. else:
  44. if shutdowntimer >= SHUTDOWN_DELAY_SECS and shutdown_now == 0:
  45. from subprocess import call
  46. call("sudo shutdown -P now", shell=True)
  47. shutdown_now = 1
  48.  
  49. if lastpwrledstate == 1:
  50. GPIO.output(PWR_LED_PIN, GPIO.LOW)
  51. lastpwrledstate = 0
  52. else:
  53. GPIO.output(PWR_LED_PIN, GPIO.HIGH)
  54. lastpwrledstate = 1
  55. shutdowntimer += SHUTDOWN_FLASH_DELAY
  56. time.sleep(SHUTDOWN_FLASH_DELAY)
  57.  
  58. #Light Temp Led when temperature is over warning temp
  59. if float(cpu.temperature) < WARNING_TEMP:
  60. GPIO.output(TEMP_LED_PIN, GPIO.LOW)
  61. elif float(cpu.temperature) >= CRITICAL_TEMP:
  62. shutdown = 1
  63. elif float(cpu.temperature) >= HIGH_TEMP:
  64. if lastwarningledstate == 1:
  65. GPIO.output(TEMP_LED_PIN, GPIO.LOW)
  66. lastwarningledstate = 0
  67. else:
  68. GPIO.output(TEMP_LED_PIN, GPIO.HIGH)
  69. lastwarningledstate = 1
  70. time.sleep(WARNING_TEMP_FLASH_DELAY)
  71. else:
  72. GPIO.output(TEMP_LED_PIN, GPIO.HIGH)
  73. except():
  74. GPIO.cleanup()
  75. sys.exit()
Add Comment
Please, Sign In to add comment