Skip to content

Instantly share code, notes, and snippets.

@mvirkkunen
Created November 17, 2025 17:50
Show Gist options
  • Select an option

  • Save mvirkkunen/28fd055bb56421e492f26db0284674c7 to your computer and use it in GitHub Desktop.

Select an option

Save mvirkkunen/28fd055bb56421e492f26db0284674c7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import serial
import time
port = serial.Serial(port="/dev/serial/by-id/usb-FTDI_DHT22_A9Y513JN-if00-port0", baudrate=125000, timeout=0.1)
while True:
while port.read(1024):
pass
port.break_condition = True
time.sleep(0.030)
port.break_condition = False
data = port.read(42)
if data and len(data) == 42 and data[1] == 0:
bits = 0
for byte in data[2:42]:
bits <<= 1
if not (byte & 0x10):
bits |= 1
checksum = ((bits >> 8) + (bits >> 16) + (bits >> 24) + (bits >> 32)) & 0xff
if checksum == bits & 0xff:
humid = ((bits >> 24) & 0xffff) / 10
temp = ((bits >> 8) & 0xffff) / 10
print(f"{temp}°C | {humid}%")
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment