import time
import adafruit_dht
import example_oled
# Set up the DHT22 sensor
dht_pin = Pin(21, Pin.IN)
dht = adafruit_dht.DHT22(dht_pin)
# Set up the OLED display
pin_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
skrin = example_oled.SSD1306_I2C(128, 64, i2c=pin_oled)
# Set up the normal LED
led_merah = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)
def display_weather_data(temperature, humidity):
oled.fill(0)
oled.text("Temperature: {:.2f} C".format(temperature), 0, 0)
oled.text("Humidity: {:.2f} %".format(humidity), 0, 20)
oled.show()
try:
while True:
try:
temperature = dht_sensor.temperature
humidity = dht_sensor.humidity
display_weather_data(temperature, humidity)
if temperature > 25:
GPIO.output(led_pin, GPIO.HIGH)
else:
GPIO.output(led_pin, GPIO.LOW)
except RuntimeError as e:
print("Error reading DHT22 sensor:", e)
time.sleep(2)
except KeyboardInterrupt:
pass
finally:
oled.fill(0)
oled.show()
GPIO.cleanup()