Pico W: web server switches LED
Wi-Fi, web server, switch an LED from the browser.
- RP2040 (Pico W)
- Wi-Fi router
Open in the simulatorNo account needed: press Simulate and it runs.
How it works
Raspberry Pi Pico W with a real Wi-Fi stack (cyw43 driver + the firmware's lwIP) on a virtual network: it connects to the router "CircuitLab", gets an address over DHCP and starts a web server. In the "Network" panel (at the bottom), load the board's page and switch the LED; button SW1 on GP15 appears on the page. Traffic log with DHCP, DNS and HTTP.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | A1 GNDSW1 2 |
| BTN | A1 GP15SW1 1 |
- SW1
- Push button
Parts
Firmware
sketch.ino · 37 lines
// Pico W as a web server: a page with buttons switches the onboard LED and shows the button on GP15
// Load the board's page in the "Network" panel (http://<IP>/). Network: Wi-Fi router in the schematic.
#include <WiFi.h>
#include <WebServer.h>
const char* SSID = "CircuitLab";
const char* PASS = "secret123";
const int BUTTON = 15;
WebServer server(80);
bool led = false;
String page() {
String s = "<!doctype html><html><head><meta charset='utf-8'><title>Pico W</title>"
"<style>body{font-family:sans-serif;margin:24px}a{display:inline-block;padding:8px 16px;margin:4px;border-radius:6px;background:#0f766e;color:#fff;text-decoration:none}</style></head><body>";
s += "<h1>Pico W web server</h1>";
s += "<p>LED is <b>" + String(led ? "ON" : "OFF") + "</b></p>";
s += "<p><a href='/on'>LED on</a><a href='/off'>LED off</a></p>";
s += "<p>Button on GP15: " + String(digitalRead(BUTTON) == LOW ? "pressed" : "released") + "</p>";
s += "<p>Uptime: " + String(millis() / 1000) + " s</p></body></html>";
return s;
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
WiFi.begin(SSID, PASS);
Serial.print("Connecting to "); Serial.println(SSID);
while (WiFi.status() != WL_CONNECTED) delay(100);
Serial.print("Connected, IP: "); Serial.println(WiFi.localIP());
server.on("/", []() { server.send(200, "text/html", page()); });
server.on("/on", []() { led = true; digitalWrite(LED_BUILTIN, HIGH); server.send(200, "text/html", page()); });
server.on("/off", []() { led = false; digitalWrite(LED_BUILTIN, LOW); server.send(200, "text/html", page()); });
server.begin();
}
void loop() { server.handleClient(); }Open in the simulator