from network import WLAN, STA_IF
from time import localtime, sleep_ms
from ntptime import settime
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
rede = WLAN(STA_IF)
rede.active(True)
if not rede.isconnected():
rede.connect('Wokwi-GUEST','')
tentativas = 0
while not rede.isconnected() and tentativas <= 20:
sleep_ms(200)
tentativas += 1
ts = localtime()
print(ts)
try:
if rede.isconnected():
settime()
except OSError as e:
print ("Deu ruim")
print (e)
ts = localtime()
print(ts)
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000) # I2C no ESP32
oled_width = 128
oled_height = 64
oled = SSD1306_I2C(oled_width, oled_height, i2c)
def mostrarTimestamp():
ts = localtime()
oled.fill(0) # Limpa o display
m = f"{ts[3]:02d}:{ts[4]:02d}:{ts[5]:02d}"
pos_x = (oled_width - len(m) * 6) // 2 # Cada caractere tem 6 pixels de largura
oled.text(m, pos_x, 10) # Escreve no display
m = f"{ts[2]:02d}/{ts[1]:02d}/{ts[0]:04d}"
pos_x = (oled_width - len(m) * 6) // 2 # Cada caractere tem 6 pixels de largura
oled.text(m, pos_x, 20) # Escreve no display
oled.show()
while True:
mostrarTimestamp()
sleep_ms(500)