from machine import Pin, I2C
import ssd1306
import dht
import time
import network
import ntptime
# Informasi identitas pengguna
nama = "ROMI Rahmat H"
nim = "217002020"
kelas = "IOT 3"
# 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(18, Pin.IN)
dht_sensor = dht.DHT22(dht_pin)
# WiFi connection
wifi_ssid = "Wokwi-GUEST" # Nama jaringan WiFi
wifi_password = "" # Kata sandi WiFi
# Connect to WiFi
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(wifi_ssid, wifi_password)
while not wifi.isconnected():
oled.fill(0)
oled.text('Connecting', 0, 0)
oled.text('to WiFi...', 0, 10)
oled.show()
pass
oled.fill(0)
oled.text('Connected ', 0, 0)
oled.text('to WiFi!', 0, 10)
oled.show()
# NTP synchronization
oled.fill(0)
oled.text('Online', 0, 0)
oled.text('updating time...', 0, 10)
oled.show()
ntptime.settime()
def celsius_to_fahrenheit(celsius):
return celsius * 9/5 + 32
def celsius_to_kelvin(celsius):
return celsius + 273.15
def display_identity():
oled.fill(0)
oled.text('Nama:', 0, 0)
oled.text(nama, 8, 8)
oled.text('NIM:', 0, 20)
oled.text(nim, 8, 28)
oled.text('Kelas:', 0, 36)
oled.text(kelas, 8, 44)
oled.show()
def display_temperature():
# 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)
oled.fill(0)
oled.text('Celcius:', 0, 0)
oled.text('{:.2f}'.format(temperature_celsius), 8, 8)
oled.text('Humidity:', 0, 20)
oled.text('{:.2f}%'.format(humidity), 8, 28)
oled.text('Fahrenheit:', 0, 36)
oled.text('{:.2f}'.format(temperature_fahrenheit), 8, 44)
oled.text('Kelvin:', 0, 52)
oled.text('{:.2f}'.format(temperature_kelvin), 8, 60)
oled.show()
def display_time():
oled.fill(0)
year, month, day, hour, minute, second, *_ = time.localtime()
oled.text('Waktu:', 0, 0)
oled.text("{:02d}-{:02d}-{} {:02d}:{:02d}:{:02d}".format(day, month, year, hour, minute, second), 8, 8)
oled.show()
while True:
# Tampilkan informasi identitas pengguna
display_identity()
time.sleep(5)
# Tampilkan suhu
display_temperature()
time.sleep(5)
# Tampilkan waktu yang telah disinkronkan
display_time()
time.sleep(5)