|
本帖最后由 ahua7336 于 2021-7-7 09:12 编辑
ESP32开启AP模式后手机连接热点为什么ESP32会重启一次!!
开启热点代码如下:- //用于配置
- void wifi_Config()
- {
-
- WiFi.mode(WIFI_AP); //配置为AP模式
- boolean result = WiFi.softAP(AP_SSID, ""); //开启WIFI热点
- if (result)
- {
- IPAddress myIP = WiFi.softAPIP();
- //打印相关信息
- Serial.println("");
- Serial.print("Soft-AP IP address = ");
- Serial.println(myIP);
- Serial.println(String("MAC address = ") + WiFi.softAPmacAddress().c_str());
- Serial.println("waiting ...");
- digitalWrite(LED_PIN, HIGH);
- }
- else
- { //开启热点失败
- Serial.println("WiFiAP Failed");
- delay(3000);
- ESP.restart(); //复位esp32
- }
- if (MDNS.begin("esp32")) //开启MDNS
- {
- Serial.println("MDNS responder started");
- }
- //首页
- server.on("/", []()
- {
- //server.send(200, "text/html", ROOT_HTML + scanNetworksID + "</body></html>");
- server.send(200, "text/html", ROOT_HTML + wifi_ssid + "mins</span></P></form></body>");
- });
- //连接
- server.on("/connect", []()
- {
- //获取输入内容
- wifi_ssid = server.arg("gap");
- //wifi_pass = server.arg("pass");
- //Serial.println("WiFi Connect SSID:" + wifi_ssid + " PASS:" + wifi_pass);
-
- if (wifi_ssid != "")
- {
-
- scanNetworksID = false;
- EEPROM.begin(3); //申请操作到地址
- EEPROM.write(1, wifi_ssid.toInt()); //写数据
- EEPROM.write(2, scanNetworksID); //写数据
- if (EEPROM.commit())
- { //保存更改的数据
- js = millis();
- server.send(200, "text/html", "<html><body><font size="10">OK!</font></body></html>");
- Serial.println("interval:" + wifi_ssid);
- WiFi.softAPdisconnect(true); //关闭AP模式
- server.close(); //关闭连接
- WiFi.softAPdisconnect(); //设置接入点账号密码为空
- digitalWrite(LED_PIN, LOW);
- }
- else
- {
- server.send(200, "text/html", "<html><body><font size="10">NO!</font></body></html>");
- WiFi.softAPdisconnect(true);
- server.close();
- WiFi.softAPdisconnect();
- delay(2000);
- ESP.restart();
- }
- }
- else
- {
- server.send(200, "text/html", "<html><body><font size="10">NO!</font></body></html>");
- WiFi.softAPdisconnect(true);
- server.close();
- WiFi.softAPdisconnect();
- delay(2000);
- ESP.restart();
- }
- });
- server.begin();
- }
复制代码
|
|