ESP WiFi Router Generator

8266 / 32 / 32-S2 / C3 Hotspot + Internet
📡

Configure and generate

Set your upstream WiFi credentials and hotspot name, then click Generate Firmware Code to get your ESP sketch.

Works with ESP8266, ESP32, ESP32-S2, and ESP32-C3 — select your board above.

"); delay(500); ${rebootFn}; });` : ''; const extraIncludes = is8266 && clients ? '#include "user_interface.h"' : ''; const esp32ApdInc = is32 && clients ? '#include "esp_wifi.h"\n#include "tcpip_adapter.h"' : ''; return `// ═══════════════════════════════════════════════ // ESP WiFi Router + Web Dashboard // Board: ${currentBoard === '8266' ? 'ESP8266' : currentBoard === '32' ? 'ESP32' : currentBoard === '32s2' ? 'ESP32-S2' : 'ESP32-C3'} // Generated by ESP WiFi Router Generator // // HOW IT WORKS: // 1. ESP connects to your upstream WiFi (internet) // 2. ESP creates its own hotspot // 3. Devices on the hotspot get internet via the ESP // 4. Visit 192.168.4.1 for the web dashboard // ═══════════════════════════════════════════════ ${wifiLib} ${serverLib} ${otaLib} ${extraIncludes} ${esp32ApdInc} // ── Upstream WiFi (internet source) ────────────── const char* STA_SSID = "${upSSID}"; const char* STA_PASS = "${upPASS}"; // ── Hotspot (what others connect to) ───────────── const char* AP_SSID = "${apSSID}"; ${apPASS ? `const char* AP_PASS = "${apPASS}";` : `// No hotspot password (open network)`} ${serverType} server(80); // ───────────────────────────────────────────────── String formatUptime(unsigned long ms) { unsigned long s = ms / 1000; unsigned long m = s / 60; unsigned long h = m / 60; return String(h) + "h " + String(m % 60) + "m " + String(s % 60) + "s"; } // ───────────────────────────────────────────────── void handleRoot() { String wifiStatus = (WiFi.status() == WL_CONNECTED) ? "Connected to " + WiFi.SSID() + "" : "Disconnected"; String html = R"rawhtml( ${title}

)rawhtml"; html += "${title}"; html += R"rawhtml(

ESP WiFi Router Dashboard

Upstream Internet

)rawhtml"; html += wifiStatus; html += R"rawhtml(

)rawhtml"; ${rssiBlock} ${uptimeBlock} ${clientBlock} ${rebootBlock} ${ota ? ` html += "⬆ OTA Firmware Update";` : ''} html += ""; server.send(200, "text/html", html); } // ───────────────────────────────────────────────── void setup() { Serial.begin(115200); delay(200); Serial.println("\\n\\n── ESP WiFi Router booting ──"); // Both STA (connect to internet) + AP (hotspot) mode WiFi.mode(WIFI_AP_STA); // Start hotspot FIRST so clients can connect while we find internet ${apPassLine} IPAddress apIP(192, 168, 4, 1); IPAddress subnet(255, 255, 255, 0); WiFi.softAPConfig(apIP, apIP, subnet); Serial.print("Hotspot \\"${apSSID}\\" started at 192.168.4.1"); // Connect to upstream WiFi Serial.print("\\nConnecting to \\"${upSSID}\\""); WiFi.begin(STA_SSID, STA_PASS); int tries = 0; while (WiFi.status() != WL_CONNECTED && tries < 30) { delay(500); Serial.print("."); tries++; } if (WiFi.status() == WL_CONNECTED) { Serial.println("\\nUpstream connected: " + WiFi.localIP().toString()); } else { Serial.println("\\nUpstream not found — hotspot still works, no internet."); } ${otaSetup} // Web server routes server.on("/", handleRoot); server.on("/status", HTTP_GET, [](){ String j = "{\\"rssi\\":" + String(WiFi.RSSI()) + ",\\"uptime\\":" + String(millis()/1000) + ",\\"sta\\":\\"" + (WiFi.status()==WL_CONNECTED?"connected":"disconnected") + "\\"" + ",\\"ip\\":\\"" + WiFi.localIP().toString() + "\\"" + "}"; server.send(200,"application/json",j); }); ${rebootRoute} ${otaRoute} server.onNotFound([](){ server.send(404,"text/plain","Not found"); }); server.begin(); Serial.println("Dashboard at http://192.168.4.1"); } // ───────────────────────────────────────────────── void loop() { server.handleClient(); ${otaLoop} // The ESP32/8266 handles WiFi packet forwarding (STA→AP) automatically // when in WIFI_AP_STA mode — no extra NAT code needed for basic internet sharing yield(); // ESP8266 watchdog } `; } function buildInstall(board, ota) { const boardName = { '8266':'ESP8266','32':'ESP32','32s2':'ESP32-S2','32c3':'ESP32-C3' }[board]; const boardUrl = { '8266': 'https://arduino.esp8266.com/stable/package_esp8266com_index.json', '32': 'https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json', '32s2': 'https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json', '32c3': 'https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json', }[board]; return `# Install Guide — ${boardName} WiFi Router ## Step 1 — Arduino IDE Download from https://www.arduino.cc/en/software (version 2.x recommended) ## Step 2 — Add board support In Arduino IDE → Preferences → Additional Boards Manager URLs, paste: ${boardUrl} Then: Tools → Board → Boards Manager → search "${boardName}" → Install ## Step 3 — Select board Tools → Board → ${boardName === 'ESP8266' ? 'ESP8266 Boards' : 'ESP32 Arduino'} → ${boardName === 'ESP8266' ? 'NodeMCU 1.0 (ESP-12E)' : boardName === 'ESP32' ? 'ESP32 Dev Module' : boardName === 'ESP32-S2' ? 'ESP32-S2 Dev Module' : 'ESP32C3 Dev Module'} ## Step 4 — Required libraries ${ota ? `- ArduinoOTA (included with board package)\n` : ''}- ESP${board === '8266' ? '8266' : '32'}WebServer (included with board package) ## Step 5 — Upload 1. Create a new sketch: File → New 2. Paste the generated code from router.ino 3. Connect your ${boardName} via USB 4. Tools → Port → select your USB port 5. Click Upload (→) ## Step 6 — Use it 1. The ESP creates a hotspot named as configured 2. Connect your phone/laptop to that hotspot 3. Open a browser and go to: http://192.168.4.1 4. Internet traffic passes through automatically ## Troubleshooting - No internet on hotspot: check upstream WiFi SSID/password - Can't reach 192.168.4.1: make sure you're connected to the ESP hotspot - Upload fails: hold BOOT button while clicking upload (ESP32 only) - Serial Monitor: set baud to 115200 to see connection logs `; } /* ─────────────────────────────────────────────── UI HELPERS ─────────────────────────────────────────────── */ function showCode() { document.getElementById('emptyState').style.display = 'none'; document.getElementById('codeScroll').style.display = 'block'; document.getElementById('toolbar').style.display = 'flex'; document.getElementById('diagramBar').style.display = 'flex'; } function switchTab(n) { currentTab = n; document.getElementById('tab1').classList.toggle('active', n === 1); document.getElementById('tab2').classList.toggle('active', n === 2); renderHighlighted(n === 1 ? generatedCode : installText, n === 2); } function renderHighlighted(code, plain) { const el = document.getElementById('codeOut'); if (plain) { el.textContent = code; return; } // Simple syntax highlight let html = code .replace(/&/g,'&').replace(//g,'>') // comments .replace(/(\/\/[^\n]*)/g, '$1') // preprocessor .replace(/(#\w+)/g, '$1') // string literals .replace(/"([^"\\]*(\\.[^"\\]*)*)"/g, '"$1"') // keywords .replace(/\b(void|int|bool|const|char|byte|unsigned|long|float|String|if|else|while|for|return|true|false|nullptr)\b/g, '$1') // types / classes .replace(/\b(WiFi|Serial|server|IPAddress|ArduinoOTA|MDNS|Update|HTTPUpload)\b/g, '$1') // numbers .replace(/\b(\d+)\b/g, '$1'); el.innerHTML = html; } function copyCode() { const text = currentTab === 1 ? generatedCode : installText; navigator.clipboard.writeText(text).then(() => { const btn = document.getElementById('copyBtn'); btn.textContent = '✓ Copied'; btn.classList.add('copied'); setTimeout(() => { btn.textContent = '⎘ Copy'; btn.classList.remove('copied'); }, 2000); }); } function downloadCode() { const text = currentTab === 1 ? generatedCode : installText; const name = currentTab === 1 ? 'router.ino' : 'INSTALL.md'; const a = document.createElement('a'); a.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text); a.download = name; a.click(); }