from machine import Pin, I2C
import ssd1306
import dht
import time
# ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# DHT22 Pin assignment
dht_pin = Pin(16, Pin.IN)
dht_sensor = dht.DHT22(dht_pin)
def celsius_to_fahrenheit(celsius):
return celsius * 9/5 + 32
def celsius_to_kelvin(celsius):
return celsius + 273.15
while True:
# Baca suhu dan kelembaban dari DHT22
dht_sensor.measure()
temperature_celsius = dht_sensor.temperature()
humidity = dht_sensor.humidity()
# Konversi suhu ke Fahrenheit dan Kelvin
temperature_fahrenheit = celsius_to_fahrenheit(temperature_celsius)
temperature_kelvin = celsius_to_kelvin(temperature_celsius)
# Bersihkan layar
oled.fill(0)
# Tampilkan nilai suhu dan kelembaban di layar OLED
oled.text('Humidity:', 0, 10)
oled.text('{:.2f}%'.format(humidity), 80, 10)
oled.text('Celsius:', 0, 25)
oled.text('{:.2f}'.format(temperature_celsius), 80, 25)
oled.text('Fahrenheit:', 0, 40)
oled.text('{:.2f}'.format(temperature_fahrenheit), 80, 40)
oled.text('Kelvin:', 0, 55)
oled.text('{:.2f}'.format(temperature_kelvin), 80, 55)
oled.show()
# Tunda selama 2 detik sebelum membaca sensor lagi
time.sleep(2)