from network import WLAN, STA_IF
from time import localtime, time, sleep_ms
from ntptime import settime
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
from framebuf import FrameBuffer, MONO_HLSB
import gc
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)
st = bytearray(b'\x00\x00\x00\x00\x00\x00p\x00\x00\x00\xd8\x00\x07\x00\x88\x00\x05\x01\n\x00\x0c\x81\x0f\x00\x08\x81\x08\x80\x08\xc1\x08\x808A\x08\x80hC\x08\x80LC\t\x80LB\x19\x00DB\x11\x00Db\x11\x00Db\x11\x00D&3\x00D&#\x00D&"\x00F$"\x00B$b\x00B$b\x0cb4b>`\x1cB"`\x00\x06b \x00\x04\xc2 \x00\x04\xc6 \x00\x05\x8c \x00\x07\x0c \x00\x07\x18 \x00\x06\x100\x00\x0c \x10\x00\x0c`\x10\x00\x00`\x18\x00\x00\xc0\x18\x00\x00\x80\x0c\x00\x01\x80\x0c\x00\x03\x00\x07\x00\x07\x00\x03\x80\x0c\x00\x00\xe08\x00\x00\x7f\xf0\x00\x00\x0f\x80\x00')
llap = FrameBuffer(st, 32, 42, MONO_HLSB)
st=None
gc.collect()
xlogo = b'\x00\x00`\x00\x00\x00\xf0\x00\x00\x01\x98\x00\x00\x01\x98\x00\x07\x81\x9e\x00\x0f\x81\x1f\x00\x08\xc1\x19\x80\x08\xc1\x19\x80\x0c\xc3\x11\x80<\xc3\x11\x80|C1\x80\xccC1\x80\xccc1\x00\xccb1\x00\xc4b3\x00\xc6f3\x00\xc6f#\x00\xc6&c\x00F&c\x00f&b\x00f4b\x1cf<f>b<F#`\x00\x06c`\x00\x06\xc6 \x00\x06\xc6 \x00\x07\x8c0\x00\x07\x0c \x00\x07\x180\x00\x0e\x100\x00\x0e 0\x00\x0c`\x10\x00\x00`\x18\x00\x00\xc0\x18\x00\x00\x80\x0c\x00\x01\x80\x0c\x00\x03\x00\x07\x00\x07\x00\x03\x80\x0c\x00\x00\xf08\x00\x00\x7f\xf0\x00\x00\x0f\x80\x00'
x = FrameBuffer(bytearray(xlogo),
32,42, MONO_HLSB)
def mostrarTimestamp():
ts = localtime(time()-3*3600)
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.blit(llap, 0, 0) # Escreve a imagem nas coordenadas
oled.blit(x, 100, 32) # Escreve a imagem nas coordenadas
oled.show()
while True:
mostrarTimestamp()
sleep_ms(500)