from machine import Pin, I2C
from time import localtime, sleep_ms
from ntptime import settime
from network import WLAN, STA_IF
from requests import get
from json import loads
import ssd1306
import gc
URL = 'https://api.open-meteo.com/v1/forecast?latitude=-32.035&longitude=-52.0986¤t=temperature_2m,rain,is_day'
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
tentativa = 0
rede = WLAN(STA_IF) #Estação ou Acess Point
rede.active(True) #ativar rede
if not rede.isconnected():
rede.connect('Wokwi-GUEST', '')
while not rede.isconnected() and tentativa <= 100:
sleep_ms(500)
tentativa += 1
agora = localtime()
print("Antes", agora)
try:
if rede.isconnected():
settime()
else:
print("Ok")
except:
print("Ruim")
print(f"Agora: {agora}")
sleep_ms(2000)
resposta = get(URL)
if resposta.status_code == 200:
dados = loads(resposta.text)
t = f'{dados["current"]["temperature_2m"]}{dados["current_units"]["temperature_2m"]}{dados["current"]["is_day"]}'
print(t)
agora = localtime()
print(f"Futuro: {agora}")
oled.text(f"Hora:{agora[3]}:{agora[4]}", 10, 10)
oled.text(f"data:{agora[2]}/{agora[1]}/{agora[0]}", 10, 20)
oled.text(f'{dados["current"]["temperature_2m"]}C', 10, 30)
if '{dados["current"]["is_day"]}'!= 0:
oled.text(f'noite', 10, 40)
else:
oled.text(f'dia', 10, 40)
oled.show()
oled.show()