import network
import time
from random import randint
import urequests
import dht
from machine import Pin

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())

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&field3={v_temp}'
    requests = urequests.post(url)
    requests.close()                                                                               
    print("Enviou Temperatura")

    time.sleep(2)

    url2 = f"https://api.thingspeak.com/update?api_key=ZGL8VR5F9SC3USG7&field2={v_umid}"
    requests = urequests.post(url2)
    requests.close()
    print("Enviou Umidade")

    time.sleep(2)

    url3 = f"https://api.thingspeak.com/update?api_key=ZGL8VR5F9SC3USG7&field4={v_temp_dht}"
    requests = urequests.post(url3)
    requests.close()
    print("Enviou dht temperatura")

    time.sleep(2)


    url4 = f"https://api.thingspeak.com/update?api_key=ZGL8VR5F9SC3USG7&field5={v_umid_dht}"
    requests = urequests.post(url4)
    requests.close()
    print("Enviou dht umidade")



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

    


$abcdeabcde151015202530fghijfghij