from time import localtime, sleep_ms
from network import WLAN, STA_IF
from ntptime import settime
from machine import Pin, I2C
import ssd1306
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
display = ssd1306.SSD1306_I2C(128, 64, i2c)
rede = WLAN(STA_IF)
rede.active(True)
if not rede.isconnected():
rede.connect('Wokwi-GUEST','')
tentativas = 0
while not rede.isconnected() and tentativas < 50:
sleep_ms(200)
tentativas += 1
try:
if rede.isconnected():
settime()
else:
print ('Deu ruim')
except:
print ('Deu MUITO ruim')
t = localtime()
d = f"{t[0]}-{t[1]:02d}-{t[2]:02d}"
h = f"{t[3]:02d}:{t[4]:02d}:{t[5]:02d}"
display.fill(0)
display.text("Data:", 10, 10)
display.text(d, 10, 25)
display.text("Hora:", 10, 40)
display.text(h, 10, 55)
display.show()