Pico W: Webserver schaltet LED
WLAN, Webserver, LED per Browser schalten.
- RP2040 (Pico W)
- Wi-Fi router
Im Simulator öffnenOhne Konto: Simulieren drücken, und es läuft.
So funktioniert es
Raspberry Pi Pico W mit echtem WLAN-Stack (cyw43-Treiber + lwIP der Firmware) im virtuellen Netz: verbindet sich mit dem Router „CircuitLab“, holt per DHCP eine Adresse und startet einen Webserver. Im Panel „Netzwerk“ (unten) die Seite des Boards laden und die LED schalten; der Taster SW1 an GP15 erscheint auf der Seite. Verkehrsprotokoll mit DHCP, DNS und HTTP.
Verdrahtung
Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.
| Netz | Pins |
|---|---|
| GND | A1 GNDSW1 2 |
| BTN | A1 GP15SW1 1 |
- SW1
- Taster
Bauteile
Firmware
sketch.ino · 37 Zeilen
// 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(); }Im Simulator öffnen