Created
November 20, 2023 22:56
-
-
Save idkrn123/c1ad09fb615979ae00e8125f7f7de279 to your computer and use it in GitHub Desktop.
edit of `pi_temp_mon.py` which uses a single serial channel to communicate with an arduino running the `serial_colors.ino` sketch i previously published
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 | |
import serial | |
import time | |
import subprocess | |
import re | |
@click.command() | |
@click.option('--brightness', default=50, prompt='Enter brightness', 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) | |
@click.option('--serial_port', default='/dev/ttyS0', prompt='Enter serial port', type=str) | |
def main(brightness, temp_floor, temp_ceiling, serial_port): | |
ser = serial.Serial(serial_port, 9600) | |
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 = (255, 0, 0) # Red | |
else: | |
ratio = (temp - temp_floor) / (temp_ceiling - temp_floor) | |
color = (int(ratio * 255), int((1 - ratio) * 255), 0) | |
color = tuple(max(0, min(int(c * brightness / 100), 255)) for c in color) | |
ser.write(bytearray(color)) | |
time.sleep(1) | |
except KeyboardInterrupt: | |
pass | |
finally: | |
ser.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment