import network
import time
import random
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 temp():
  global v_temp
  v_temp = random.randint(0, 80)
  print(f"Temperatura {v_temp}")

def umid():
    global v_umid
    v_umid = random.randint(0, 100)
    print(f"Umidade {v_umid}")

    sensor = dht.DHT22(Pin(5))

def dht_temperatura():
    global v_temp_dht
    sensor.measure() 
    sensor.humidity()
    v_temp_dht = sensor.temperature()
    print(f'valor dht temperatura{v_temp_dht}')

def dht_umidade():
    global v_umid_dht
    sensor.measure() 
    sensor.temperature()
    v_umid_dht = sensor.humidity()
    print(f'valor dht temperatura{v_umid_dht}')


def Ts():
    url = f'https://api.thingspeak.com/update?api_key=CYXOISE7U6820EBS&field1={v_temp}'
    requests = urequests.post(url)
    requests.close()
    print("Enviou Temperatura")

    time.sleep(2)

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

    time.sleep(2)

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

    time.sleep(2)

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

    time.sleep(2)



while True:
    temp()
    umid()
    Ts()
    dht_temperatura()
    dht_umidade()
$abcdeabcde151015202530fghijfghij