Arduino IDE for ESP8266 项目云盒子 AP直接模式

Posted kekeoutlook

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino IDE for ESP8266 项目云盒子 AP直接模式相关的知识,希望对你有一定的参考价值。

手机直接连接esp8266辐射的WIFI,通信。

 

#include <ESP8266WiFi.h>
 
const char *ssid = "Charlie Testing AP";
const char *password = "12345678";
WiFiServer server(80);
void setup()
{
  Serial.begin(115200);
  Serial.println();
 
  Serial.print("Setting soft-AP ... ");
   
IPAddress softLocal(192,168,128,1);   
IPAddress softGateway(192,168,128,1);
IPAddress softSubnet(255,255,255,0);
 
WiFi.softAPConfig(softLocal, softGateway, softSubnet);  
 
    WiFi.softAP(ssid, password);
   
   IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
 server.begin();
 Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
 
}
 
void loop()
{
 WiFiClient client = server.available();
 if (client)
  {
    Serial.println("\n[Client connected]");
    while (client.connected())
    {
      // read line by line what the client (web browser) is requesting
      if (client.available())
      {
        String line = client.readStringUntil(‘\r‘);
        Serial.print(line);
        // wait for end of client‘s request, that is marked with an empty line
        if (line.length() == 1 && line[0] == ‘\n‘)
        {
          client.println(preparehtmlPage());
 
           
          break;
        }
      }
    }
    delay(1); // give the web browser time to receive the data
 
    // close the connection:
    client.stop();
    Serial.println("[Client disonnected]");
  }
 
 
}
 
// prepare a web page to be send to a client (web browser)
String prepareHtmlPage()
{
  String htmlPage =
     String("HTTP/1.1 200 OK\r\n") +
            "Content-Type: text/html\r\n" +
            "Connection: close\r\n" +  // the connection will be closed after completion of the response
            "Refresh: 5\r\n" +  // refresh the page automatically every 5 sec
            "\r\n" +
            "<!DOCTYPE HTML>" +
            "<html>" +
            "Analog input:  " + String(analogRead(A0)) +
            "</html>" +
            "\r\n";
  return htmlPage;<br>}

  

以上是关于Arduino IDE for ESP8266 项目云盒子 AP直接模式的主要内容,如果未能解决你的问题,请参考以下文章

Arduino IDE for ESP8266 项目云盒子 AP直接模式

Arduino IDE for ESP8266教程HTTP客户端

Arduino IDE for ESP8266教程 创建WIFI AP模式

Arduino IDE for ESP8266 项目云盒子

Arduino IDE for ESP8266教程网页控制灯 简单页面

物联网-ESP8266实战- 配置ESP8266 Arduino IDE开发环境