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
from requests import get
from json import loads
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)
oled_width = 128
oled_height = 64
oled = SSD1306_I2C(oled_width, oled_height, i2c)
balarme = Pin(5, Pin.IN, Pin.PULL_DOWN)
bmais = Pin(6, Pin.IN, Pin.PULL_DOWN)
bmenos = Pin(7, Pin.IN, Pin.PULL_DOWN)
alarmevelho = alarmenovo = balarme.value()
maisvelho = maisnovo = bmais.value()
menosvelho = menosnovo = bmenos.value()
hh = 0
mm = 0
hora = 10
minuto = 0
estado = 0
def mostrarTimestamp(): # Função para data e hora
global hh, mm
ts = localtime(time()-3*3600)
oled.fill(0)
h = f"{ts[3]:02d}:{ts[4]:02d}:{ts[5]:02d}"
hh = ts[3]
mm = ts[4]
pos_x = (oled_width - len(h) * 6) // 2
oled.text(h, pos_x, 10)
d = f"{ts[2]:02d}/{ts[1]:02d}/{ts[0]:04d}"
pos_x = (oled_width - len(d) * 6) // 2
oled.text(d, pos_x, 20)
oled.show()
def figura(): # Função para temperatura e imagem do weather code
resposta = get("https://api.open-meteo.com/v1/forecast?latitude=-32.035&longitude=-52.0986¤t=temperature_2m,relative_humidity_2m,is_day,weather_code&timezone=America%2FSao_Paulo")
dados = loads(resposta.text)
wmo = dados['current']['weather_code']
temp = f"temperatura:{dados['current']['temperature_2m']}"
pos_x = (oled_width - len(temp) * 6) // 2
oled.text(temp, pos_x, 30)
if wmo == 0:
sun = bytearray(b'\x00\x01\x80\x00\x00\x01\x80\x00\x00\x01\x80\x00\x001\x8c\x00\x0e0\x0cp\x0f\x00\x00\xf0\x0f\x1f\xf8\xf0\x07?\xfc\xe0\x00\xff\xff\x00\x00\xff\xff\x00\x19\xff\xff\x98\x1b\xff\xff\xd8\x03\xff\xff\xc0\x03\xff\xff\xc0\x03\xff\xff\xc0\xf3\xff\xff\xcf\xf3\xff\xff\xcf\x03\xff\xff\xc0\x03\xff\xff\xc0\x03\xff\xff\xc0\x1b\xff\xff\xd8\x19\xff\xff\x98\x00\xff\xff\x00\x00\xff\xff\x00\x07?\xfc\xe0\x0f\x1f\xf8\xf0\x0f\x00\x00\xf0\x0e0\x0cp\x001\x8c\x00\x00\x01\x80\x00\x00\x01\x80\x00\x00\x01\x80\x00')
image = FrameBuffer(sun, 32, 32, MONO_HLSB)
sun = None
elif wmo == 1:
cloudy = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x80\x00\x00\x0f\xe0\x00\x00\xff\xf0\x00\x03\xff\xf8\x00?\xff\xf8\x00\xff\xff\xfe\x01\xff\xff\xff\x03\xff\xff\xff\x07\xff\xff\xff\x07\xff\xff\xff\x07\xff\xff\xfe?\xff\xff\xf8\x7f\xff\xff\xf8\xff\xff\xff\xf8\xff\xff\xff\xf8\xff\xff\xff\xfc\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x7f\xff\xff\xff?\xff\xff\xff\x00\x7f\xff\xff\x00\x7f\xff\xff\x00?\xff\xff\x00?\xff\xfe\x00\x0f\xff\xf8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
image = FrameBuffer(cloudy, 32, 32, MONO_HLSB)
cloudy = None
elif wmo == 63:
rainy = bytearray(b'\x00?\x80\x00\x00\xff\xe0\x00\x01\xff\xf0\x00\x01\xff\xf0\x00\x03\xff\xf8\x00\x07\xff\xff\x00\x07\xff\xff\xc0\x03\xff\xff\xe0\x01\xff\xff\xe0\x00\xff\xff\xf0\x00\x7f\xff\xf0\x00\xff\xff\xf0\x00\xff\xff\xfc\x01\xff\xff\xfe\x01\xff\xff\xff\x01\xff\xff\xff\x01\xff\xff\xff\x01\xff\xff\xff\x01\xff\xff\xff\x01\xff\xff\xff\x0e\xff\xff\xfe\x1e\x7f\xff\xfc\x1e\x00\x00\x00\x1ea\x8e8\x00a\xce8\x00c\xce8\x03\x03\xc1\xc0\x03\x01\xc1\xc0\x03\x1c\x01\xc0\x00\x1c\x19\xc0\x00\x1c\x19\xc0\x00\x1c\x18\x00')
image = FrameBuffer(rainy, 32, 32, MONO_HLSB)
rainy = None
else:
oled.show()
return
gc.collect()
oled.blit(image, 0, 0)
oled.show()
def despertar():
oled.fill(0)
relogio = f"{hora:02d}:{minuto:02d}"
pos_x = (oled_width - len(relogio) * 6) // 2
oled.text(relogio, pos_x, 10)
oled.show()
def mensagem():
msg = "Bom dia!"
pos_x = (oled_width - len(msg) * 6) // 2
oled.text(msg, pos_x, 40)
oled.show()
while True:
if estado == 0: # Estado do relogio
mostrarTimestamp()
alarmenovo = balarme.value()
if alarmenovo != alarmevelho:
if alarmenovo == 1: # Botão foi pressionado
quem = 0
estado = 1
alarmevelho = alarmenovo
sleep_ms(100)
if hora == hh and minuto == mm:
estado = 2
oled.fill(0)
mostrarTimestamp()
mensagem()
figura()
elif estado == 1: # Estado de ajuste
maisnovo = bmais.value()
if maisnovo != maisvelho:
if maisnovo == 1: # Botão foi pressionado
if quem == 0:
hora = hora + 1 if hora < 23 else 0
elif quem == 1:
minuto = minuto + 1 if minuto < 59 else 0
maisvelho = maisnovo
sleep_ms(100)
menosnovo = bmenos.value()
if menosnovo != menosvelho:
if menosnovo == 1: # Botão foi pressionado
if quem == 0:
hora = hora - 1 if hora > 0 else 23
elif quem == 1:
minuto = minuto - 1 if minuto > 0 else 59
menosvelho = menosnovo
sleep_ms(100)
alarmenovo = balarme.value()
if alarmenovo != alarmevelho:
if alarmenovo == 1: # Botão foi pressionado
if quem < 2:
quem = quem + 1
else:
estado = 0
alarmevelho = alarmenovo
sleep_ms(100)
despertar()
elif estado == 2: # Estado de desperte
alarmenovo = balarme.value()
if alarmenovo != alarmevelho:
if alarmenovo == 1: # Botão foi pressionado
estado = 0
alarmevelho = alarmenovo
sleep_ms(100)
+
-