使用Arduino开发ESP32(二十四):WiFi_AP Server例程,

前言
官方例程原名叫做
WiFiAccessPpoint,我改的更贴切点

代码和SimpleWiFi Server例程形似
它们所要达成的功能也是一样的

二者之间的区别是,SimpleWiFi Server是和用户处于同一wifi网络下,然后二者通信,WiFi_AP Server是esp32自己创建wifi网络,然后用户连接再通信

其他无了。。。

想了解例程运行流程的,可以看看我的SimpleWiFi Server这篇博客
代码

/*
  WiFiAccessPoint.ino creates a WiFi access point and provides a web server on it.

  Steps:
  1. Connect to the access point "yourAp"
  2. Point your web browser to http://192.168.4.1/H to turn the LED on or http://192.168.4.1/L to turn it off
     OR
     Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port

  Created for arduino-esp32 on 04 July, 2018
  by Elochukwu Ifediora (fedy0)
*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

#define LED_BUILTIN 2   // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED

// Set these to your desired credentials.
const char *ssid = "ESP32";
const char *password = "123456789";

WiFiServer server(80);


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  // You can remove the password parameter if you want the AP to be open.
  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();             // read a byte, then
        Serial.write(c);                    // 串口打印
        if (c == '\n') {                    // 如果是换行符

          //连续两个换行符代表,客户端http请求结束
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // esp32响应的内容
            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 out of the while loop:
            break;
          } else {    // 如果要存储新行,则先清空currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          Serial.println("_______________________开灯");               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          Serial.println("_______________________关灯");                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

运行结果
在这里插入图片描述

  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答你的问题。为了实现这个功能,你可以参考以下步骤: 1. 首先,你需要使用 Arduino IDE 和 ESP32开发板库来编写代码。 2. 在代码中,你需要配置 ESP32 的 Wi-Fi 模式AP STA 模式。这可以通过以下代码实现: ``` WiFi.mode(WIFI_AP_STA); ``` 3. 接下来,你需要配置 ESP32AP 和 STA 的连接参数,以便 ESP32 可以连接到 Wi-Fi 网络并提供 AP 服务。这可以通过以下代码实现: ``` WiFi.begin(ssid, password); WiFi.softAP(ap_ssid, ap_password); ``` 其中 `ssid` 和 `password` 是你要连接的 Wi-Fi 网络的 SSID 和密码,`ap_ssid` 和 `ap_password` 是你要设置的 AP 的 SSID 和密码。 4. 然后,你需要创建一个网页,允许用户更改 Wi-Fi 配置和 AP 配置。这可以使用 HTML、CSS 和 JavaScript 来完成。 5. 当用户更改了配置并提交表单时,你需要在 ESP32 上处理表单数据,并将其保存到 LittleFS 文件系统中。这可以通过以下代码实现: ``` File configFile = LittleFS.open("/config.json", "w"); if (configFile) { configFile.print(jsonData); configFile.close(); } ``` 其中 `jsonData` 是从表单中获取的 JSON 数据。 6. 最后,你需要编写代码来读取 LittleFS 文件系统中的配置数据,并将其应用于 ESP32 的 Wi-Fi 配置和 AP 配置。这可以通过以下代码实现: ``` File configFile = LittleFS.open("/config.json", "r"); if (configFile) { size_t size = configFile.size(); std::unique_ptr<char[]> buf(new char[size]); configFile.readBytes(buf.get(), size); configFile.close(); // Parse JSON data and apply Wi-Fi and AP configuration // ... } ``` 其中 `// Parse JSON data and apply Wi-Fi and AP configuration` 部分需要你根据你的 JSON 数据格式和 ESP32 的配置方式来编写代码。 希望这些步骤能够帮助你实现你的需求。如果你有任何问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值