import dht
from machine import Pin, SoftI2C, RTC
import ssd1306
import time
import ntptime
import network
import utime
# Function to set the RTC time from NTP
def set_rtc_time():
ntptime.host = "pool.ntp.org"
ntptime.settime()
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
i2c = SoftI2C(sda=Pin(9), scl=Pin(8))
display = ssd1306.SSD1306_I2C(128, 64, i2c)
dht_pin = 1
sensor = dht.DHT22(Pin(1))
# Set RTC time initially
set_rtc_time()
rtc = RTC()
def draw_rectangle(x, y, width, height):
display.rect(x, y, width, height, 1) # Draw a filled rectangle
def display_values(temp, hum, time_str, sleep_message=None):
display.fill(0)
# Draw rectangles around time, temperature, and humidity
draw_rectangle((128 - len(time_str) * 8) // 2 - 2, 6, len(time_str) * 8 + 4, 16) # Time
draw_rectangle(78, 32, 50, 15) # Temperature
draw_rectangle(0, 32, 50, 15) # Humidity
# Display clock at the top center
display.text(time_str, (128 - len(time_str) * 8) // 2, 10)
# Display temperature on the right
display.text("{:.2f}C".format(temp), 80, 35)
# Display humidity on the left
display.text("{:.2f}%".format(hum), 0, 35)
display.show()
while True:
try:
# Read temperature and humidity
sensor.measure()
Temp = sensor.temperature()
Hum = sensor.humidity()
# Get current time from RTC
rtc_time = rtc.datetime()
local_time = utime.mktime((rtc_time[0], rtc_time[1], rtc_time[2], rtc_time[4], rtc_time[5], rtc_time[6], 0, 0))
local_time += 7200 # Adjust for Prague time zone (UTC+1)
current_time = utime.localtime(local_time)
time_str = "{:02d}:{:02d}:{:02d}".format(current_time[3], current_time[4], current_time[5])
# Display values
display_values(Temp, Hum, time_str)
except Exception as e:
print("Error reading DHT22:", e)
time.sleep(2)
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1