Skip to content

Instantly share code, notes, and snippets.

@stephanlachnit
Created May 16, 2025 13:10
Show Gist options
  • Save stephanlachnit/c6c1bebd5b110220c575f6e56455bdaf to your computer and use it in GitHub Desktop.
Save stephanlachnit/c6c1bebd5b110220c575f6e56455bdaf to your computer and use it in GitHub Desktop.
Temperature Sensor readout for Constellation Tutorial
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