"""
nome:Loenardo Augusto de Souza Silva
nome:Bruna de Oliveira Lima
"""


import network
import time
from random import randint
import urequests
import dht
from machine import Pin, I2C
import ssd1306

print("Connecting to WiFi", end="")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
while not wlan.isconnected():
    print(".", end="")
    time.sleep(0.1)
print(" Connected!")
print(wlan.ifconfig())


i2c = machine.I2C(0, sda=machine.Pin(0), scl=machine.Pin(1))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

v_temp = 0
v_umid = 0

v_temp_dht = 0
v_umid_dht = 0

def temperatura():
  global v_temp
  v_temp = randint(0, 60)
  print(f"Valor randomico Temperatura {v_temp}")

def umidade():
    global v_umid
    v_umid = randint(0, 100)
    print(f"Valor randomico Umidade  {v_umid}")

sensor = dht.DHT22(Pin(5))
def temperatura_dht():
    sensor.measure() 
    global v_temp_dht
    v_temp_dht = sensor.temperature()
    print(f"Valor DHT Temperatura {v_temp_dht}")


def umidade_dht():
    sensor.measure() 
    global v_umid_dht
    v_umid_dht = sensor.humidity()
    print(f"Valor DHT Umidade {v_umid_dht}")


def Ts():
    url = f'https://api.thingspeak.com/update?api_key=ZGL8VR5F9SC3USG7&field1={v_temp_dht}&field2={v_umid}&field3={v_temp}&field4={v_umid_dht}'
    requests = urequests.post(url)
    requests.close()                                                                               
    print("Enviou Temperatura")



def escrever_oled():
    oled.fill(0)
    oled.text("Temp (C):", 0, 0)
    oled.text("{:.2f}".format(v_temp_dht), 0, 10)
    oled.text("Umid (%):", 0, 30)
    oled.text("{:.2f}".format(v_umid_dht), 0, 40)
    oled.show()


while True:
    temperatura()
    umidade()
    temperatura_dht()
    umidade_dht()
    escrever_oled()
    Ts()
    

    


$abcdeabcde151015202530fghijfghij