Set the Port (Check the bottom right corner of Thonny to ensure it says "MicroPython (ESP32)" and the correct COM port is selected.)
Copy and paste the code into Thonny
Run the program (Click the green arrow button).
from machine import Pin
import time
# 1. Setup the built-in LED (GPIO 2) as an Output
# On most ESP32 boards, the blue LED is on Pin 2
led = Pin(2, Pin.OUT)
print("Blinking started! Look at your NodeMCU board.")
while True:
led.value(1) # Turn LED ON
print("LED ON")
time.sleep(0.5) # Wait 0.5 seconds
led.value(0) # Turn LED OFF
print("LED OFF")
time.sleep(0.5) # Wait 0.5 seconds
Copy and paste the code into the editor.
Modify the number (Change student_id to your seat number).
Run the program (Click the green arrow button).
import network
import socket
from machine import Pin
import time # Import time for safety delays
import gc # Import garbage collection for stability
# ======================================================
# [ STUDENT SECTION: CHANGE YOUR ID BELOW ]
# ======================================================
student_id = "01" # <--- Change this to your seat number
# ======================================================
# --- SAFETY BUFFER (VERY IMPORTANT!) ---
# After saving as main.py, this gives you 3 seconds to press
# "Stop" in Thonny before the loop starts. Prevents lock-outs.
print("System booting in 3 seconds...")
time.sleep(3)
# 1. Define Onboard LED Pin (GPIO 2 for NodeMCU)
led = Pin(2, Pin.OUT)
# 2. Wi-Fi Access Point (AP) Configuration
wifi_name = "SmartLight_ID_" + student_id
wifi_password = "password123"
ap = network.WLAN(network.AP_IF)
ap.active(False) # Reset Wi-Fi state
time.sleep(0.5)
ap.config(essid=wifi_name, password=wifi_password)
ap.active(True)
# Wait for Wi-Fi module to be ready
while not ap.active():
pass
print("-" * 30)
print("IoT Station Started!")
print("Wi-Fi SSID: " + wifi_name)
print("Server URL: http://192.168.4.1")
print("-" * 30)
# 3. Web Interface Design (HTML)
def web_page():
html = """<html><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial, sans-serif; text-align: center; background-color: #f4f4f4; padding-top: 50px; }
.button { display: block; width: 80%; padding: 40px; font-size: 30px;
margin: 20px auto; color: white; border: none; cursor: pointer;
border-radius: 15px; text-decoration: none; font-weight: bold; }
.on { background-color: #3498db; }
.off { background-color: #34495e; }
h1 { color: #2c3e50; }
</style></head>
<body>
<h1>IoT Station #""" + student_id + """</h1>
<p>Control the Onboard Blue LED</p>
<hr>
<a href="/on" class="button on">LED ON</a>
<a href="/off" class="button off">LED OFF</a>
</body></html>"""
return html
# 4. Initialize Server Socket
gc.collect() # Clear memory before starting
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# FIX: Allow port reuse to prevent "Address in use" errors after Reset
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 80))
s.listen(5)
while True:
try:
# Clear memory at the start of each loop for stability
gc.collect()
conn, addr = s.accept()
# Set a timeout so a bad connection doesn't hang the whole system
conn.settimeout(3.0)
print("Got a connection from %s" % str(addr))
request = conn.recv(1024).decode()
# Handle the URL commands
if '/on' in request:
led.on()
elif '/off' in request:
led.off()
# Generate and send the HTTP response
response = web_page()
conn.send('HTTP/1.1 200 OK\nContent-Type: text/html\n\n' + response)
conn.close()
except Exception as e:
# Print error to Shell and ensure connection is closed
print("Connection Error:", e)
if 'conn' in locals():
conn.close()
Turn off Mobile Data (4G/5G). This is important to prevent the phone from switching away from the NodeMCU Wi-Fi.
Connect to Wi-Fi: Find your SSID (e.g., SmartLight_ID_01) and connect.
Open your Browser (Chrome, Safari, etc.).
Go to 192.168.4.1 to see your control panel.