import network import socket from machine import Pin import time SSID = "SANGATLUCU" PASSWORD = "SANGATLUXU" # LED onboard ESP8266 (D4 / GPIO2) = active LOW led = Pin(2, Pin.OUT) led.value(1) # awal MATI (karena active LOW) def connect_wifi(): wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): print("Connecting to WiFi...") wlan.connect(SSID, PASSWORD) while not wlan.isconnected(): time.sleep(0.5) print(".", end="") print("\nWiFi connected!") ip = wlan.ifconfig()[0] print("IP address:", ip) return ip def web_page(): # Karena active LOW: state = "ON" if led.value() == 0 else "OFF" html = f""" ESP8266 LED WEB

Kontrol LED ESP8266 via Web

Status LED: {state}

""" return html ip = connect_wifi() addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1] s = socket.socket() s.bind(addr) s.listen(1) print("Web server ready. Open browser to:") print("http://{}/".format(ip)) while True: conn = None try: conn, addr = s.accept() request = conn.recv(1024).decode() # Ambil baris pertama request: "GET /on HTTP/1.1" first_line = request.split("\r\n")[0] path = first_line.split(" ")[1] # "/on" atau "/off" if path == "/on": led.value(0) # NYALA (active LOW) elif path == "/off": led.value(1) # MATI response = web_page() conn.send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n") conn.sendall(response) except Exception as e: print("Error:", e) finally: if conn: conn.close()