Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import RPi.GPIO as GPIO
- import time
- # Configuration
- PWR_LED_PIN = 26
- TEMP_LED_PIN = 21
- PWR_BUTTON_PIN = 15
- WARNING_TEMP = 65.0
- HIGH_TEMP = 70.0
- CRITICAL_TEMP = 75.0
- SHUTDOWN_DELAY_SECS = 5
- SHUTDOWN_FLASH_DELAY = 0.25
- WARNING_TEMP_FLASH_DELAY = 0.25
- shutdown = 0
- lastpwrledstate = 1
- lastwarningledstate = 0
- shutdowntimer = 0
- shutdown_now = 0
- #Control Leds
- try:
- while (1):
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(TEMP_LED_PIN, GPIO.OUT)
- GPIO.setup(PWR_LED_PIN, GPIO.OUT)
- GPIO.setup(PWR_BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- from gpiozero import CPUTemperature
- cpu = CPUTemperature()
- #Check Pwr Button
- if GPIO.input(PWR_BUTTON_PIN) == GPIO.HIGH:
- shutdown = 1
- #Light Pwr Led
- if shutdown == 0:
- GPIO.output(PWR_LED_PIN, GPIO.HIGH)
- else:
- if shutdowntimer >= SHUTDOWN_DELAY_SECS and shutdown_now == 0:
- from subprocess import call
- call("sudo shutdown -P now", shell=True)
- shutdown_now = 1
- if lastpwrledstate == 1:
- GPIO.output(PWR_LED_PIN, GPIO.LOW)
- lastpwrledstate = 0
- else:
- GPIO.output(PWR_LED_PIN, GPIO.HIGH)
- lastpwrledstate = 1
- shutdowntimer += SHUTDOWN_FLASH_DELAY
- time.sleep(SHUTDOWN_FLASH_DELAY)
- #Light Temp Led when temperature is over warning temp
- if float(cpu.temperature) < WARNING_TEMP:
- GPIO.output(TEMP_LED_PIN, GPIO.LOW)
- elif float(cpu.temperature) >= CRITICAL_TEMP:
- shutdown = 1
- elif float(cpu.temperature) >= HIGH_TEMP:
- if lastwarningledstate == 1:
- GPIO.output(TEMP_LED_PIN, GPIO.LOW)
- lastwarningledstate = 0
- else:
- GPIO.output(TEMP_LED_PIN, GPIO.HIGH)
- lastwarningledstate = 1
- time.sleep(WARNING_TEMP_FLASH_DELAY)
- else:
- GPIO.output(TEMP_LED_PIN, GPIO.HIGH)
- except():
- GPIO.cleanup()
- sys.exit()
Add Comment
Please, Sign In to add comment