WebServer
效果图
已连接
web端
platformio.ini
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html[env:esp32]
platform = espressif32
framework = arduino
board = esp32dev
源代码
/* ESP32 HTTP IoT Server Example for Wokwi.comhttps://wokwi.com/arduino/projects/320964045035274834When running it on Wokwi for VSCode, you can connect to thesimulated ESP32 server by opening http://localhost:8180in your browser. This is configured by wokwi.toml.
*/#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>// #define WIFI_SSID "Wokwi-GUEST"
// #define WIFI_PASSWORD ""
#define WIFI_SSID "iQOO Neo6"
#define WIFI_PASSWORD "yyuuiioo"
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6WebServer server(80);const int LED1 = 2;
const int LED2 = 4;bool led1State = false;
bool led2State = false;void sendHtml()
{String response = R"(<!DOCTYPE html><html><head><meta charset="UTF-8"><!-- 其他原有头部标签内容 --><title>ESP32 Web Server Demo</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>html { font-family: sans-serif; text-align: center; }body { display: inline-flex; flex-direction: column; }h1 { margin-bottom: 1.2em; } h2 { margin: 0; }div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }.btn { background-color: #5B5; border: none; color: #fff; padding: 0.5em 1em;font-size: 2em; text-decoration: none }.btn.OFF { background-color: #333; }</style></head><body><h1>ESP32 Web Server</h1><div><h2>蓝色LED 灯</h2><a href="/toggle/1" class="btn LED1_TEXT">LED1_TEXT</a><h2>绿色的LED 灯</h2><a href="/toggle/2" class="btn LED2_TEXT">LED2_TEXT</a></div></body></html>)";response.replace("LED1_TEXT", led1State ? "ON" : "OFF");response.replace("LED2_TEXT", led2State ? "ON" : "OFF");server.send(200, "text/html", response);
}void setup(void)
{Serial.begin(115200);pinMode(LED1, OUTPUT);pinMode(LED2, OUTPUT);WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);Serial.print("Connecting to WiFi ");Serial.print(WIFI_SSID);// Wait for connectionwhile (WiFi.status() != WL_CONNECTED){delay(2000);Serial.print("Wait for connection .");}Serial.println(" Connected!");Serial.print("IP address: ");Serial.println(WiFi.localIP());server.on("/", sendHtml);server.on(UriBraces("/toggle/{}"), [](){String led = server.pathArg(0);Serial.print("Toggle LED #");Serial.println(led);switch (led.toInt()) {case 1:led1State = !led1State;if (led1State){Serial.print("打开");}else{Serial.print("关闭");}Serial.println("蓝色LED");digitalWrite(LED1, led1State);break;case 2:led2State = !led2State;if (led2State){Serial.print("打开");}else{Serial.print("关闭");}Serial.println("打开绿色LED");digitalWrite(LED2, led2State);break;}sendHtml(); });server.begin();Serial.println("HTTP server started (http://" + WiFi.localIP().toString() + ":80)");
}void loop(void)
{server.handleClient();delay(2);
}