ESP32 WiFi-AP模式下点灯控制程序
- 这是在VSCode里面的Platform环境下编译并上传的。
- 如需在ArduinoIDE环境下编译上传,直接拷贝过去粘贴,即可。注意选择对应的开发板。我选择的是
ESP Dev Module
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#define LED_BUILTIN 2
const char *ssid = "ESP32-AP";
const char *password = "";
WiFiServer server(80);
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
}
void loop()
{
WiFiClient client = server.available();
if (client)
{
Serial.println("New Client.");
String currentLine = "";
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.write(c);
if (c == '\n')
{
if (currentLine.length() == 0)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
client.println();
break;
}
else
{
currentLine = "";
}
}
else if (c != '\r')
{
currentLine += c;
}
if (currentLine.endsWith("GET /H"))
{
digitalWrite(LED_BUILTIN, HIGH);
}
if (currentLine.endsWith("GET /L"))
{
digitalWrite(LED_BUILTIN, LOW);
}
}
}
client.stop();
Serial.println("Client Disconnected.");
}
}
- 编译信息
- 手机连接ESP32-AP网络,浏览器登陆“192.168.4.1”页面进行点灯控制
|