Created
May 16, 2025 13:10
-
-
Save stephanlachnit/c6c1bebd5b110220c575f6e56455bdaf to your computer and use it in GitHub Desktop.
Temperature Sensor readout for Constellation Tutorial
This file contains hidden or 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
from threading import Lock | |
import serial | |
class TempSensor(): | |
def __init__(self, port: str = '/dev/ttyACM0'): | |
self._lock = Lock() | |
self._serial = serial.Serial( | |
port=port, | |
baudrate=9600, | |
bytesize=serial.EIGHTBITS, | |
parity=serial.PARITY_NONE, | |
stopbits=serial.STOPBITS_ONE, | |
timeout=1.5, | |
) | |
def read_temperature(self) -> None | float: | |
with self._lock: | |
self._serial.reset_input_buffer() | |
try: | |
line = self._serial.readline().decode() | |
return float(line[13:18]) | |
except: | |
pass | |
return None | |
def close(self) -> None: | |
self._serial.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment