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

def conexao():  # Updated function signature to accept ssid and password as arguments
    print("Connecting to WiFi", end="")
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect("Wokwi-GUEST", "")  # Use the passed ssid and password
    while not wlan.isconnected():
        print(".", end="")
        time.sleep(0.1)
    print(" Connected!")
    print(wlan.ifconfig())

conexao()
apikey = 'TKC8ENW9YMVZP2F2'

def send_to_thingspeak(apikey, temperature1, humidity1, temperaturer, humidityr):
    url = f"https://api.thingspeak.com/update?api_key={apikey}&field1={temperature1}&field2={humidity1}&field3={temperaturer}&field4={humidityr}".format(apikey, temperature1, humidity1, temperaturer, humidityr)
    response = urequests.get(url)
    print("Dados enviados para ThingSpeak!")
    response.close()

sensor = dht.DHT22(Pin(19))
scl_pin = Pin(1)
sda_pin = Pin(0)
i2c = I2C(0, scl=scl_pin, sda=sda_pin, freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
while True:
    sensor.measure()
    temperature1 = sensor.temperature()
    humidity1 = sensor.humidity()
    temperaturer = random.randint(0, 60)
    humidityr = random.randint(0, 60)

    oled.fill(0)
    oled.text("Sensor: " + str(temperature1) + "C", 4, 38)
    oled.text("Sensor: " + str(humidity1) + "%", 4, 48)
    oled.text("Randomica: " + str(temperaturer) + "C", 4, 28)
    oled.text("Randomica: " + str(humidityr) + "%", 4, 18)
    oled.show()
    send_to_thingspeak('TKC8ENW9YMVZP2F2', temperature1, humidity1, temperaturer, humidityr)
    time.sleep(30)
$abcdeabcde151015202530354045505560fghijfghij