import network
import time
from machine import Pin
import dht
import urequests
sensor = dht.DHT22(Pin(14))
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!")
########### Function to send data to API ################
def send_data_to_api(celsius, fahrenheit, kelvin):
url = "https://patcharaphonapi.aimaccount.com/upload?" + "celsius=" + str(celsius) + "&fahrenheit=" + str(fahrenheit) + "&kelvin=" + str(kelvin)
try:
response = urequests.get(url)
print("HTTP Status Code:", response.status_code)
payload = response.text
print("Payload:", payload)
except Exception as e:
print("Error:", e)
########### Function to send data to API ################
while True:
sensor.measure()
celsius = sensor.temperature()
print("อุณหภูมิ ", celsius, "C")
fahrenheit = (celsius * (9/5)) + 32
print("อุณหภูมิ ", fahrenheit, "F")
kelvin = celsius + 273.15
print("อุณหภูมิ ", kelvin, "K")
send_data_to_api(celsius, fahrenheit, kelvin)
time.sleep(30)