# Library
from machine import Pin
from time import *
import dht
import time
import urequests as rq
import network
# define pin mode
red = Pin(18, Pin.OUT)
yellow = Pin(5, Pin.OUT)
green = Pin(4, Pin.OUT)
# Setup DHT 22
dht22 = dht.DHT22(Pin(15))
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# Update do am -> thu nghiem
# rs = rq.get("https://api.thingspeak.com/update?api_key=2CRAWAQEKNXGO6FR&field2=50")
temp = -1
humi = -1
# loop
while True:
dht22.measure()
cr_temp = dht22.temperature() # eg. 23.6 (°C)
cr_humi = dht22.humidity() # eg. 41.3 (% RH)
if (cr_temp != temp):
temp = cr_temp
rq.get("https://api.thingspeak.com/update?api_key=2CRAWAQEKNXGO6FR&field1={}".format(temp))
if (cr_humi != humi):
humi = cr_humi
rq.get("https://api.thingspeak.com/update?api_key=2CRAWAQEKNXGO6FR&field2={}".format(humi))
sleep(30)