from network import WLAN, STA_IF
from time import sleep_ms, localtime, time, gmtime
from ntptime import settime
from machine import Timer, Pin, I2C
from ssd1306 import SSD1306_I2C
from framebuf import FrameBuffer, MONO_HLSB
import gc
from requests import get
from rede import Rede
#ativar o hardware de rádio e bloco wifi ESP32
rede = WLAN(STA_IF)
rede.active(True)
#inicia a conexão com uma rede wifi
if not rede.isconnected():
rede.connect('Wokwi-GUEST','')
hora_alarme = 23
minuto_alarme = 16
#loop que só vai parar quando chegar a 20 ou conectar
vez = 0
while not rede.isconnected() and vez < 20:
sleep_ms(500)
vez +=1
def mostraInstante(fuso):
seg = (time() + fuso*3600) #para a parte do fuso horário
t1 = gmtime(seg)
#print(t1)
horas = t1[3]
minuto = t1[4]
segundo = t1[5]
dia = t1[2]
mes = t1[1]
ano = t1[0]
dias_semana = ["Seg", "Ter", "Qua", "Qui", "Sex", "Sab", "Dom"]
dia_nome = dias_semana[t1[6]]
#resultado = f"{t1[0]}-{t1[1]}-{t1[2]}-T-{t1[3]}:{t1[4]}:{t1[5]}"
return horas, minuto, segundo, dia, mes, ano, dia_nome
#global diz que a varivael pertence ao código como um todo e não a somente a função(global)
#vai travar quando chegar na hora marcada
tim_relogio = Timer(0)
tim_clima = Timer(1)
taNaHora = False
def atualizaRelogio(t):
global taNaHora
taNaHora = True
buzzer = Pin(10, Pin.OUT)
buzzer.value(0)
url = 'https://api.open-meteo.com/v1/forecast?latitude=-32.035&longitude=-52.0986&hourly=is_day,cloud_cover¤t=is_day,rain,temperature_2m,weather_code'
#print(localtime())
settime()
print(rede.ifconfig())
tim_relogio.init(period = 6000,mode = Timer.PERIODIC,callback = atualizaRelogio)
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)
def dispertaralarme(hora_alarme, minuto_alarme):
#hora_alarme = input('De 0 a 24:')
#minuto_alarme = input('De 0 a 59:')
hora_alarme = [0]
minuto_alarme = [1]
alarme = f'{hora_alarme}:{minuto_alarme}'
return hora_alarme, minuto_alarme
def mostrarTimestamp(hora):
global localtime
tempo = hora
m = f'{tempo[0]:02d}:{tempo[1]:02d}:{tempo[2]:02d}'
#oled.fill(0) # Limpa o display
oled.text(m, 30, 30) #meio
oled.show()
n = 0
temperatura_api = "-- C"
#radiacao = 0
#nuvens_ceu = 0
#chuvendo = 0
ceu = 0
def clima(t):
global url, temperatura_api, radiacao, nuvens_ceu, chuvendo, ceu
resposta = get(url)
print (resposta.status_code)
#resposta.text
#resposta.json()
conteudo = resposta.json()
temperatura = conteudo['current']['temperature_2m']
#print(temperatura)
print('Feito!') #teste
#print(conteudo)
#radiacao = conteudo['current']['weather_code']
#nuvens_ceu = conteudo['current']['weather_code']
#chuvendo = conteudo['current']['weather_code']
ceu = conteudo['current']['weather_code']
temperatura_api = f"{temperatura} C"
resposta.close()
tim_clima.init(period=60000, mode=Timer.PERIODIC, callback=clima)
atualiza = True
agora = mostraInstante(-3)
mostrarTimestamp(agora)
nuvem_fb = bytearray([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00,
0x00, 0x03, 0xfe, 0x00, 0x00, 0xff, 0xff, 0x00, 0x01, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0x80,
0x03, 0xff, 0xff, 0x80, 0x07, 0xff, 0xff, 0xf0, 0x1f, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xfc,
0x3f, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xfc,
0x3f, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xff, 0xf0, 0x03, 0xff, 0xff, 0xe0,
0x00, 0x1f, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
nuvem_fb = FrameBuffer(nuvem_fb, 32, 32, MONO_HLSB)
sol_fb = bytearray([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00,
0x01, 0xc0, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x02, 0x01, 0xc0, 0x20, 0x00, 0x07, 0x00,
0x00, 0x70, 0x00, 0x03, 0x80, 0x00, 0xe0, 0x00, 0x01, 0xc0, 0x01, 0xc0, 0x00, 0x00, 0x83, 0xe0,
0x80, 0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x1f, 0xfc, 0x00, 0x00, 0x00, 0x3f, 0xfe, 0x00,
0x00, 0x00, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0x3c, 0x7f, 0xff, 0x1e, 0x00,
0x7c, 0x7f, 0xff, 0x1f, 0x00, 0x3c, 0x7f, 0xff, 0x1e, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0x00,
0x3f, 0xfe, 0x00, 0x00, 0x00, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x1f, 0xfc, 0x00, 0x00, 0x00, 0x0f,
0xf8, 0x00, 0x00, 0x00, 0x83, 0xe0, 0x80, 0x00, 0x01, 0xc0, 0x01, 0xc0, 0x00, 0x03, 0x80, 0x00,
0xe0, 0x00, 0x07, 0x00, 0x00, 0x70, 0x00, 0x02, 0x01, 0xc0, 0x20, 0x00, 0x00, 0x01, 0xc0, 0x00,
0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00])
sol_fb = FrameBuffer(sol_fb, 33, 33, MONO_HLSB)
gota_chuva_fb = bytearray([
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x3f, 0x9e, 0x00, 0x0f, 0xff, 0xff, 0x00,
0x1f, 0xff, 0xff, 0x80, 0x1f, 0xff, 0xff, 0xc0, 0x1f, 0xff, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xf0,
0x07, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xf0, 0x00, 0x39, 0xff, 0xe0, 0x00, 0x00, 0xfc, 0x00,
0x02, 0x40, 0x30, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x08, 0x01, 0x00, 0x00,
0x01, 0x10, 0x24, 0x00, 0x00, 0x02, 0x40, 0x00, 0x12, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x04, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
gota_chuva_fb = FrameBuffer(gota_chuva_fb, 31, 31, MONO_HLSB)
while True:
if taNaHora:
taNaHora = False
sleep_ms(1000)
agora = mostraInstante(-3)
n += 1
"""if atualiza:
clima(0)
sleep_ms(10)
atualiza = False"""
#print(f'{agora[0]}:{agora[1]}:{agora[2]}')
#print(localtime())
oled.fill(0) # Limpa o display
oled.text(f'{agora[0]:02d}:{agora[1]:02d}:{agora[2]:02d}', 30, 30) #ordem return
oled.text(f'{agora[3]:02d}/{agora[4]:02d},{agora[6]}', 30, 40)
oled.text(f'Alarme={hora_alarme:02d}:{minuto_alarme:02d}',21, 50)
oled.text(temperatura_api, 65, 0)
#oled.blit(nuvem_fb, 0, 0)
#oled.blit(sol_fb, 0, 0)
#oled.blit(gota_chuva_fb, 0, 0)
if ceu == 0 or ceu == 1:
oled.blit(sol_fb, 0, 0)
else:
pass
if ceu == 2 or ceu == 3:
oled.blit(nuvem_fb, 0, 0)
else:
pass
if ceu == 61 or ceu == 63 or ceu == 65:
oled.blit(gota_chuva_fb, 0, 0)
else:
pass
oled.show()
if atualiza:
clima(0)
sleep_ms(10)
atualiza = False
if agora[0] == hora_alarme and agora[1] == minuto_alarme:
buzzer.value(1)
else:
buzzer.value(0)
"""while True:
if atualiza:
clima()
sleep_ms(10)
atualiza = False"""Loading
aitewinrobot-esp32c3-supermini
aitewinrobot-esp32c3-supermini