# 前言
esp8266 除了可以连接 wifi 路由器,也可以自身作为一个路由器供其他人来连接,只要将模式设置为 AP 模式即可.
# 写一个简单的 demo
| #include <ESP8266WiFi.h> | |
| #include <WiFiClient.h> | |
| #include <ESP8266WebServer.h> | |
| #ifndef APSSID | |
| #define APSSID "ESPap" | |
| #define APPSK "11111111" | |
| #endif | |
| const char *ssid = APSSID; | |
| const char *password = APPSK; | |
| ESP8266WebServer server(80); | |
| void handleRoot() { | |
| server.send(200, "text/html", "<h1>You are connected</h1>"); | |
| } | |
| void setup() { | |
| Serial.begin(115200); | |
| delay(1000); | |
| Serial.println(); | |
| Serial.print("Configuring access point..."); | |
| WiFi.softAP(ssid, password); | |
| IPAddress myIP = WiFi.softAPIP(); | |
| Serial.print("AP IP address: "); | |
| Serial.println(myIP); | |
| server.on("/", handleRoot); | |
| server.begin(); | |
| Serial.println("HTTP server started"); | |
| } | |
| void loop() { | |
| server.handleClient(); | |
| } | 
在这里设置 WIFI 名称为 ESPap 密码为 8 个 1, 短短不到 30 行代码就写成了一个最简单的路由器.
# 测试
上电之后使用手机搜索 wifi, 果然看到有个名为 ESPap 的 wifi, 遂连接之.

连接之后,IP 是 192.168.4.1

