from machine import Pin, I2C
from time import localtime, sleep_ms, sleep, ticks_ms, time
from ntptime import settime
from network import WLAN, STA_IF
from requests import get
from json import loads
import ssd1306
import framebuf
botao_menu = Pin(5, Pin.IN, Pin.PULL_DOWN)
botao_mais = Pin(16, Pin.IN, Pin.PULL_DOWN)
botao_menos = Pin(17, Pin.IN, Pin.PULL_DOWN)
estado_botao_menu_ant = estado_botao_menu_atual = botao_menu.value()
estado_botao_mais_ant = estado_botao_mais_atual = botao_mais.value()
estado_botao_menos_ant = estado_botao_menos_atual = botao_menos.value()
url_clima = 'https://api.open-meteo.com/v1/forecast?latitude=-32.035&longitude=-52.0986¤t=temperature_2m,is_day,weather_code&temperature_unit=celsius&timezone=America/Sao_Paulo'
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
largura_oled = 128
altura_oled = 64
oled = ssd1306.SSD1306_I2C(largura_oled, altura_oled, i2c)
tentativas_wifi = 0
wifi = WLAN(STA_IF)
wifi.active(True)
if not wifi.isconnected():
wifi.connect('Wokwi-GUEST', '')
while not wifi.isconnected() and tentativas_wifi <= 100:
sleep_ms(500)
tentativas_wifi += 1
if wifi.isconnected():
try:
settime()
except:
pass
resposta_http = get(url_clima)
if resposta_http.status_code == 200:
dados_clima = loads(resposta_http.text)
temperatura = f'{dados_clima["current"]["temperature_2m"]}C'
dia_ou_noite = 'dia' if str(dados_clima["current"]["is_day"]) == '1' else 'noite'
indice_clima = str(dados_clima["current"]["weather_code"])
icones = (
b'\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x81\x848\x08\xc0\x8c`\x08`\x88\xc0\x0c0\x01\x80\x06\x03\xe1\x00\x01\x8f\xf8\x00\x00\x1f\xfc\x1c\x03?\xfe \x0e?\xfe\x00\x00\x7f\xff\x00\x00\x7f\xff\x10<\x7f\xff?\x00\x7f\xff\x06\x00\x7f\xff\x00\x00?\xfe\x00\x0f?\xfe@\x00\x1f\xfcp\x00\x0f\xf8\x18\x00\x03\xe0\x0e\x00@\x00\x02\x00\xc0\x00\x80\x03\x88\x08`\x06\x08\x8c \x04\x18\x84\x18\x00\x10\x80\x0c\x00\x00\x80\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00',
b'\x00A\x06\x04\x00A\x84\x0c0@\x84\x18\x08 \x88 \x0c\x00\x08\xc0\x07\xcf\x81\x80\x00?\xe0\x00\x00\x7f\xf8\x00\x00\x7f\xf8<^\xff\xfe`0\xff\xfe\x00\x00\xff\xfe\x00\x00\x00\xfe\x00\x00\x00\x00\x00\x07\x80\x03\x00\x1f\xe0\x7f\x00?\xff\xff\x80?\xff\xff\x80?\xff\xff\xc0?\xff\xff\xf0?\xff\xff\xf0?\xff\xff\xf0\x1f\xff\xff\xf0\x07\xff\xff\xe0\x01\xff\xff\xe0\x01\xff\x87\x00\x00\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|\x00\x00\x01\xfe\x00\x01\xf9\xfe\x80\x01\xf9\xff\x00\x03\xff\xff\x00\x03\xff\xff\x00\x03\xff\xff\x80\x03\xff\xff\xc0\x03\xff\xff\xf0\x07\xff\xff\xf0\x1f\xff\xff\xf0\x1f\xff\xff\xf4?\xff\xff\xf8?\xff\xff\xf8?\xff\xff\xf8?\xff\xff\xf0\x1f\xff\xff\xe0\x0f\xff\xff\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
)
if indice_clima in ('0', '1'):
indice_clima = 0
elif indice_clima == '2':
indice_clima = 1
else:
indice_clima = 2
bitmap_clima = bytearray(icones[indice_clima])
framebuffer_clima = framebuf.FrameBuffer(bitmap_clima, 32, 32, framebuf.MONO_HLSB)
estado_sistema = 0
proxima_atualizacao = ticks_ms()
horario_alarme = [0, 0]
campo_edicao = 0
alarme_ativo = False
timestamp_local = time() - 3 * 3600
tempo_atual = localtime(timestamp_local)
while True:
if estado_sistema == 0:
oled.fill(0)
oled.text(f"{tempo_atual[3]}:{tempo_atual[4]:02}", 5, 10)
oled.text(f"{tempo_atual[2]}/{tempo_atual[1]}/{tempo_atual[0]}", 5, 20)
oled.text(temperatura, 5, 30)
oled.text(dia_ou_noite, 5, 50)
oled.blit(framebuffer_clima, 80, 30)
if alarme_ativo:
oled.text(f"({horario_alarme[0]}:{horario_alarme[1]:02})", 60, 10)
oled.show()
if ticks_ms() >= proxima_atualizacao:
timestamp_local = time() - 3 * 3600
tempo_atual = localtime(timestamp_local)
proxima_atualizacao = ticks_ms() + 5000
estado_botao_menu_atual = botao_menu.value()
if estado_botao_menu_atual != estado_botao_menu_ant:
if estado_botao_menu_atual == 1:
estado_sistema = 1
sleep_ms(100)
estado_botao_menu_ant = estado_botao_menu_atual
if tempo_atual[3] == horario_alarme[0] and tempo_atual[4] == horario_alarme[1] and alarme_ativo:
estado_sistema = 2
elif estado_sistema == 1:
oled.fill(0)
oled.text("Quando vai", 20, 10)
oled.text("despertar?", 20, 20)
if campo_edicao == 0:
oled.text(f"[{horario_alarme[0]}]:{horario_alarme[1]:02}", 30, 35)
elif campo_edicao == 1:
oled.text(f"{horario_alarme[0]}:[{horario_alarme[1]:02}]", 30, 35)
else:
oled.text(f"{horario_alarme[0]}:{horario_alarme[1]:02}", 30, 35)
oled.text("Aplicar?", 30, 45)
oled.show()
estado_botao_menu_atual = botao_menu.value()
if estado_botao_menu_atual != estado_botao_menu_ant:
if estado_botao_menu_atual == 1:
campo_edicao += 1
if campo_edicao > 2:
campo_edicao = 0
estado_sistema = 0
sleep_ms(200)
estado_botao_menu_ant = estado_botao_menu_atual
if campo_edicao < 2:
estado_botao_mais_atual = botao_mais.value()
if estado_botao_mais_atual != estado_botao_mais_ant:
if estado_botao_mais_atual == 1:
horario_alarme[campo_edicao] += 1
estado_botao_mais_ant = estado_botao_mais_atual
estado_botao_menos_atual = botao_menos.value()
if estado_botao_menos_atual != estado_botao_menos_ant:
if estado_botao_menos_atual == 1:
horario_alarme[campo_edicao] -= 1
estado_botao_menos_ant = estado_botao_menos_atual
horario_alarme[0] %= 24
horario_alarme[1] %= 60
else:
estado_botao_mais_atual = botao_mais.value()
if estado_botao_mais_atual != estado_botao_mais_ant:
if estado_botao_mais_atual == 1:
alarme_ativo = True
estado_sistema = 0
estado_botao_mais_ant = estado_botao_mais_atual
estado_botao_menos_atual = botao_menos.value()
if estado_botao_menos_atual != estado_botao_menos_ant:
if estado_botao_menos_atual == 1:
alarme_ativo = False
estado_sistema = 0
estado_botao_menos_ant = estado_botao_menos_atual
elif estado_sistema == 2:
oled.fill(0)
oled.text("ACORDA!", 35, 25)
oled.show()
alarme_ativo = False
horario_alarme = [0, 0]
estado_botao_menu_atual = botao_menu.value()
if estado_botao_menu_atual != estado_botao_menu_ant:
if estado_botao_menu_atual == 1:
estado_sistema = 0
sleep_ms(100)
estado_botao_menu_ant = estado_botao_menu_atual
#DEPOIS DE UM MÊS FUNCIONOU