import network, urequests, ujson
from machine import Pin, PWM
from time import ticks_ms
import dht
SSID = "Wokwi-GUEST"
PASSWORD = ""
STATE_URL = "https://h.javo.uz/api/state"
led1 = Pin(26, Pin.OUT)
led2 = Pin(27, Pin.OUT)
servo = PWM(Pin(13), freq=50)
dht_sensor = dht.DHT22(Pin(15))
def move_servo(angle):
duty = int((angle / 180) * 102 + 26)
servo.duty(duty)
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print("Connecting", end="")
while not wlan.isconnected():
print(".", end="")
print(" Connected")
connect_wifi()
POLL_INTERVAL = 100
last_poll = 0
while True:
now = ticks_ms()
if now - last_poll >= POLL_INTERVAL:
last_poll = now
# Send temperature
try:
dht_sensor.measure()
temp = round(dht_sensor.temperature(), 1)
payload = ujson.dumps({"temperature": temp})
urequests.post(STATE_URL, headers={"Content-Type": "application/json"}, data=payload).close()
print("[TEMP] Sent:", payload)
except Exception as e:
print("❌ Temp error:", e)
# Get state
try:
res = urequests.get(STATE_URL)
data = res.json()
res.close()
led1.value(1 if data.get("led1") == "ON" else 0)
led2.value(1 if data.get("led2") == "ON" else 0)
move_servo(int(data.get("servo", 90)))
print("[STATE] Updated:", data)
except Exception as e:
print("❌ State fetch error:", e)