Last active
November 19, 2023 04:12
-
-
Save idkrn123/6c24f89f768784ad18b2d34823e16622 to your computer and use it in GitHub Desktop.
a silly little raspberry pi rgb led temperature monitor script. not optimized at all, will rewrite in c as soon as i care enough.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click, RPi.GPIO as GPIO, time, subprocess, re | |
@click.command() | |
@click.option('--brightness', default=50, prompt='Enter brightness', type=int) | |
@click.option('--r_pin', default=32, prompt='Enter R pin', type=int) | |
@click.option('--g_pin', default=33, prompt='Enter G pin', type=int) | |
@click.option('--b_pin', default=12, prompt='Enter B pin', type=int) | |
@click.option('--temp_floor', default=40.0, prompt='Enter temperature floor', type=float) | |
@click.option('--temp_ceiling', default=80.0, prompt='Enter temperature ceiling', type=float) | |
def main(brightness, r_pin, g_pin, b_pin, temp_floor, temp_ceiling): | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setwarnings(False) | |
pins = [r_pin, g_pin, b_pin] | |
GPIO.setup(pins, GPIO.OUT) | |
rp, gp, bp = (GPIO.PWM(pin, 100) for pin in pins) | |
[pwm.start(0) for pwm in (rp, gp, bp)] | |
try: | |
while True: | |
temp = float(re.search(r'temp=([\d\.]+)', subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True).stdout.decode()).group(1)) | |
if temp > temp_ceiling: | |
color = (100, 0, 0) | |
else: | |
ratio = (temp - temp_floor) / (temp_ceiling - temp_floor) | |
color = (ratio * 100, (1 - ratio) * 100, 0) | |
[pwm.ChangeDutyCycle(max(0, min(c * brightness / 100, 100))) for pwm, c in zip((rp, gp, bp), color)] | |
time.sleep(1) | |
except KeyboardInterrupt: pass | |
finally: | |
[pwm.stop() for pwm in (rp, gp, bp)] | |
GPIO.cleanup() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment