feat(pi2-greenhouse): AHT20 온습도 + BMP280 기압 센서 추가, 화면에 기압 표시

- DHT22를 AHT20(I2C 0x38)으로 교체. DHT22 읽기 재시도(~13초)가 종료 제한 10초를
  넘겨 SIGKILL로 끝나던 문제도 함께 해소
- BMP280/BME280(0x77) 드라이버 추가(smbus2, 데이터시트 보정식), 기압 hPa를 HA에 디스커버리
- TFT 헤더·OLED 토양 줄에 기압 표시
- config.yaml을 실제 배포 상태(센서 연결)로 갱신

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-10-11 20:03:29 +09:00
1 parent d6c7a3694d
commit 1c9f266114
4 files changed
+109 -12

No files matched your search

+17 -10
View File
@@ -1,7 +1,8 @@
# 온실 제어기 설정 (Raspberry Pi 2, BCM 핀 번호)
#
# 배선 (BCM GPIO / 물리 핀)
# DHT22 데이터 GPIO4 (핀7) + 3.3V, GND, 데이터-3.3V 사이 4.7~10kΩ 풀업(모듈형은 내장)
# AHT20 온습도 I2C 0x38 (SDA/SCL 공유), VIN=3.3V (DHT22에서 교체)
# BMP280 기압 I2C 0x77 (SDA/SCL 공유), VIN=3.3V
# ADS1115 SDA/SCL GPIO2 / GPIO3 (핀3 / 핀5), VDD=3.3V, ADDR→GND(0x48)
# 릴레이 IN1~IN5 GPIO17(핀11) 27(핀13) 22(핀15) 23(핀16) 24(핀18) — 릴레이 모듈 VCC는 5V
#
@@ -31,16 +32,22 @@ display:
sensors:
- id: air
name: 온실 공기
type: sim # 실제: type: dht22 + gpio: 4
# gpio: 4
type: aht20 # AHT20 I2C(0x38), SDA=핀3 SCL=핀5 (DHT22에서 교체 10-11)
address: 0x38
measures: [temperature, humidity]
- id: soil
name: 토양
type: sim # 실제: type: ads1115 + channel: 0 + 보정값
# channel: 0
# dry_v: 2.8 # 공기 중에서 잰 전압 (반드시 직접 측정해서 교체)
# wet_v: 1.3 # 물에 담갔을 때 전압
measures: [soil_moisture, voltage]
- id: baro
name: 기압
type: bmp280 # BMP280 I2C(0x77, SDA/SCL 공유). 온도는 칩 자체 발열이 섞여 기압만 사용
address: 0x77
measures: [pressure]
# 토양수분(ADS1115)은 아직 미연결 — 모의 값이 HA에 기록되지 않게 비활성화. 연결 후 주석 해제.
# - id: soil
# name: 토양
# type: ads1115 # + channel: 0 + 보정값
# channel: 0
# dry_v: 2.8 # 공기 중에서 잰 전압 (반드시 직접 측정해서 교체)
# wet_v: 1.3 # 물에 담갔을 때 전압
# measures: [soil_moisture, voltage]
actuators:
fan:
+87 -2
View File
@@ -64,6 +64,91 @@ class DHT22:
raise IOError(f"DHT22 읽기 실패: {last}")
class AHT20:
"""AHT20 I2C 온습도(주소 0x38). smbus2로 직접 읽는다(라이브러리 불필요). 3.3V 구동, SDA=GPIO2 SCL=GPIO3."""
def __init__(self, cfg):
self.bus = cfg.get("bus", 1); self.addr = cfg.get("address", 0x38)
@staticmethod
def _crc(data):
c = 0xFF
for b in data:
c ^= b
for _ in range(8):
c = ((c << 1) ^ 0x31) & 0xFF if c & 0x80 else (c << 1) & 0xFF
return c
def read(self):
from smbus2 import SMBus, i2c_msg
with SMBus(self.bus) as b:
if not b.read_byte_data(self.addr, 0x71) & 0x08: # 보정 안 됨 → 초기화
b.write_i2c_block_data(self.addr, 0xBE, [0x08, 0x00]); time.sleep(0.01)
b.write_i2c_block_data(self.addr, 0xAC, [0x33, 0x00]) # 측정 시작
time.sleep(0.1)
for _ in range(5):
m = i2c_msg.read(self.addr, 7); b.i2c_rdwr(m); d = list(m)
if not d[0] & 0x80:
break
time.sleep(0.02)
else:
raise IOError("AHT20 측정 중 상태 지속")
if self._crc(d[:6]) != d[6]:
raise IOError("AHT20 CRC 오류")
h = ((d[1] << 12) | (d[2] << 4) | (d[3] >> 4)) / 1048576 * 100
t = (((d[3] & 0x0F) << 16) | (d[4] << 8) | d[5]) / 1048576 * 200 - 50
if not (-40 <= t <= 85 and 0 <= h <= 100):
raise IOError(f"AHT20 범위 밖 값 t={t:.1f} h={h:.1f}")
return {"temperature": round(t, 1), "humidity": round(h, 1)}
class BMP280:
"""BMP280/BME280 기압(주소 0x77 또는 0x76). smbus2 직접 읽기, 데이터시트 보정식.
pressure(hPa)와 temperature(칩 온도 — 기판 자체 발열이 섞이니 기압만 쓰는 걸 권장)를 낸다.
BME280이면 습도 칩이므로 chip id 0x60을 허용하되 기압만 읽는다."""
def __init__(self, cfg):
self.bus = cfg.get("bus", 1); self.addr = cfg.get("address", 0x77)
self.cal = None
def _load(self, b):
cid = b.read_byte_data(self.addr, 0xD0)
if cid not in (0x56, 0x57, 0x58, 0x60):
raise IOError(f"BMP280 chip id 0x{cid:02x} 아님")
import struct
c = bytes(b.read_i2c_block_data(self.addr, 0x88, 24))
self.cal = struct.unpack("<HhhHhhhhhhhh", c)
def read(self):
from smbus2 import SMBus
with SMBus(self.bus) as b:
if self.cal is None:
self._load(b)
b.write_byte_data(self.addr, 0xF4, 0x55) # 온도 x2, 기압 x16, forced 1회 측정
time.sleep(0.06)
for _ in range(10):
if not b.read_byte_data(self.addr, 0xF3) & 0x08: # measuring 비트
break
time.sleep(0.01)
d = b.read_i2c_block_data(self.addr, 0xF7, 6)
adc_p = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4)
adc_t = (d[3] << 12) | (d[4] << 4) | (d[5] >> 4)
T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 = self.cal
v1 = (adc_t / 16384.0 - T1 / 1024.0) * T2
v2 = ((adc_t / 131072.0 - T1 / 8192.0) ** 2) * T3
tfine = v1 + v2
v1 = tfine / 2.0 - 64000.0
v2 = v1 * v1 * P6 / 32768.0 + v1 * P5 * 2.0
v2 = v2 / 4.0 + P4 * 65536.0
v1 = (P3 * v1 * v1 / 524288.0 + P2 * v1) / 524288.0
v1 = (1.0 + v1 / 32768.0) * P1
if v1 == 0:
raise IOError("BMP280 보정 오류")
p = 1048576.0 - adc_p
p = (p - v2 / 4096.0) * 6250.0 / v1
v1 = P9 * p * p / 2147483648.0
v2 = p * P8 / 32768.0
p = p + (v1 + v2 + P7) / 16.0
hpa = p / 100.0
if not 300 <= hpa <= 1100:
raise IOError(f"BMP280 범위 밖 기압 {hpa:.1f}")
return {"pressure": round(hpa, 1), "temperature": round(tfine / 5120.0, 1)}
class ADS1115:
"""ADS1115 한 채널(단일 변환). smbus2로 직접 읽는다.
soil_moisture(%)는 dry_v(공기 중 전압)와 wet_v(물에 담갔을 때 전압)로 선형 환산 — 센서마다 달라 반드시 보정.
@@ -86,7 +171,7 @@ class ADS1115:
return {"voltage": round(v, 3), "soil_moisture": round(min(max(pct, 0), 100), 1)}
DRIVERS = {"sim": SimSensor, "ds18b20": DS18B20, "dht22": DHT22, "ads1115": ADS1115}
DRIVERS = {"sim": SimSensor, "ds18b20": DS18B20, "dht22": DHT22, "aht20": AHT20, "bmp280": BMP280, "bme280": BMP280, "ads1115": ADS1115}
# ───────────────────────── 릴레이 ─────────────────────────
@@ -170,7 +255,7 @@ class Greenhouse:
def discovery(self):
d = self.dev(); av = f"{self.node}/status"
units = {"temperature": ("°C", "temperature"), "humidity": ("%", "humidity"), "soil_moisture": ("%", "moisture"), "voltage": ("V", "voltage")}
units = {"temperature": ("°C", "temperature"), "humidity": ("%", "humidity"), "soil_moisture": ("%", "moisture"), "voltage": ("V", "voltage"), "pressure": ("hPa", "pressure")}
for sid, _, s in self.sensors:
for key in s.get("measures", ["temperature"]):
unit, cls = units[key]
+2
View File
@@ -102,6 +102,8 @@ def render(gh):
# 토양
g.text((0, 38), "토양 " + val("soil.soil_moisture", "%.0f") + "%", font=_F_SMALL, fill=1)
if any(sid == "baro" for sid, _, _ in gh.sensors):
g.text((66, 38), val("baro.pressure", "%.0f") + "hPa", font=_F_SMALL, fill=1)
# 릴레이 상태 막대: 켜지면 채움(반전)
names = list(gh.acts.items())
+3
View File
@@ -134,6 +134,7 @@ def render(gh, hist):
return gh.get(ref, now)
t, hu, so = v("air.temperature"), v("air.humidity"), v("soil.soil_moisture")
pr = v("baro.pressure")
# 헤더
g.text((12, 6), "온실", font=font("ko", 18), fill=TXT)
@@ -141,6 +142,8 @@ def render(gh, hist):
if any(type(d).__name__ == "SimSensor" for _, d, _ in gh.sensors):
g.rounded_rectangle((x, 8, x + 38, 26), radius=6, fill=(90, 70, 20))
g.text((x + 6, 9), "SIM", font=font("en", 12), fill=(255, 200, 80))
if any(sid == "baro" for sid, _, _ in gh.sensors):
g.text((112, 10), "--" if pr is None else "%.0f hPa" % pr, font=font("en", 15), fill=MUTED)
_right(g, W - 12, 5, time.strftime("%H:%M"), font("en", 20), TXT)
ok = gh.cl.is_connected()
g.ellipse((W - 92, 11, W - 82, 21), fill=(110, 230, 140) if ok else None, outline=(110, 230, 140) if ok else (255, 90, 80), width=2)