"""
Gabriel de Almeida
Mariane Ayumi Hirata Trindade
"""

import network
import time
import random
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
sensor = dht.DHT22(machine.Pin(5))

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



def disp():
    oled.fill(0)
    oled.text("Temp (C):", 0, 0)
    oled.text("{:.2f}".format(v_temp_dht), 0, 10)
    oled.text("Moving Avg (C):", 0, 30)
    oled.text("{:.2f}".format(v_umid_dht), 0, 40)
    oled.show()
    
while True:
    temp()
    umid()
    dht_temperatura()
    dht_umidade()
    disp()
    Ts()
$abcdeabcde151015202530fghijfghij