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

def conexao():
    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())

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

conexao()

server = "http://api.thingspeak.com/"
apikey = "TKC8ENW9YMVZP2F2"
field1 = 1
field2 = 2
field3 = 3
field4 = 4

WIDTH = 128 
HEIGHT = 64 
sensor = dht.DHT22(Pin(19)) 
slc_pin = Pin(1) 
sda_pin = Pin(0) 
i2c =  I2C(0, scl=slc_pin, sda=sda_pin, freq=400000) 
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)

while True:
    sensor.measure()
    temperature1 = sensor.temperature()
    humidity1 = sensor.humidity()
    # Temperatura sensor
    temperature1 = round(temperature1, 1)
    print(f"T = {temperature1}°C")
    # Umidade sensor
    humidity1 = round(humidity1, 2)
    print(f"U = {humidity1}")
    # Temperatura randômica
    temperaturer = random.randint(0, 60)
    print(f"T = {temperaturer}°C")
    # Umidade randômica
    humidityr = random.randint(0, 60)
    print(f"U = {humidityr}")

    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(apikey, temperature1, humidity1, temperaturer, humidityr)
$abcdeabcde151015202530354045505560fghijfghij
Loading
pi-pico-w