zoukankan      html  css  js  c++  java
  • 从零开始的ESP8266探索(1)-使用Server功能搭建Web Server

    https://blog.csdn.net/Naisu_kun/article/details/80398667

    文件系统

    https://blog.csdn.net/solar_Lan/article/details/74231360

     学习的网络知识

    http://www.runoob.com/ajax/ajax-examples.html

    #include <ESP8266WiFi.h>
    
    /*** 该工程可以在2.4.0版本esp8266库中运行,没在更高版本库中进行测试 ***/
    
    const char *ssid = "HUAWEI-H3VBKZ";
    const char *password = "13991320168";
    
    WiFiServer server(80);
    
    String readString = ""; //建立一个字符串对象用来接收存放来自客户的数据
    
    //响应头
    String responseHeaders =
        String("") +
        "HTTP/1.1 200 OK
    " +
        "Content-Type: text/html
    " +
        "Connection: close
    " +
        "
    ";
    
    //网页
    String myhtmlPage =
        String("") +
        "<html>" +
        "<head>" +
        "    <title>ESP8266 Web Server Test</title>" +    
        "    <script defer="defer">" +
        "        function ledSwitch() {" +
        "            var xmlhttp;" +
        "            if (window.XMLHttpRequest) {" +
        "                xmlhttp = new XMLHttpRequest();" +
        "            } else {" +
        "                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");" +
        "            }" +
        "            xmlhttp.onreadystatechange = function () {" +
        "                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {" +
        "                    document.getElementById("txtState").innerHTML = xmlhttp.responseText;" +
        "                }" +
        "            }," +
        "            xmlhttp.open("GET", "Switch", true);" +
        "            xmlhttp.send(); " +
        "        }" +
        "    </script>" +
        "</head>" +
        "<body>" +
        "    <div id="txtState">Unkwon</div>" +
        "    <input type="button" value="Switch" onclick="ledSwitch()">" +
        "</body>" +
        "</html>";
    
    bool isLedTurnOpen = false; // 记录LED状态
    
    void setup()
    {
        pinMode(D4, OUTPUT);
        digitalWrite(D4, HIGH); // 熄灭LED
    
        Serial.begin(115200);
        Serial.println();
    
        Serial.printf("Connecting to %s ", ssid);
        WiFi.begin(ssid, password);
        while (WiFi.status() != WL_CONNECTED)
        {
            delay(500);
            Serial.print(".");
        }
        Serial.println(" connected");
    
        server.begin();
        Serial.printf("Web server started, open %s in a web browser
    ", WiFi.localIP().toString().c_str());
    }
    
    void loop()
    {
        WiFiClient client = server.available(); //尝试建立客户对象
        if (client)                             //如果当前有客户可用
        {
            boolean currentLineIsBlank = true;
            Serial.println("[Client connected]");
    
            while (client.connected()) //如果客户端建立连接
            {
                if (client.available()) //等待有可读数据
                {
                    char c = client.read(); //读取一字节数据
                    readString += c;        //拼接数据
                    /************************************************/
                    if (c == '
    ' && currentLineIsBlank) //等待请求头接收完成(接收到空行)
                    {
                        //比较接收到的请求数据
                        if (readString.startsWith("GET / HTTP/1.1")) //如果是网页请求
                        {
                            client.print(responseHeaders); //向客户端输出网页响应
                            client.print(myhtmlPage);      //向客户端输出网页内容
                            client.print("
    ");
                        }
                        else if (readString.startsWith("GET /Switch")) //如果是改变LED状态请求
                        {
                            if (isLedTurnOpen == false)
                            {
                                digitalWrite(D4, LOW); // 点亮LED
                                client.print("LED has been turn on");
                                isLedTurnOpen = true;
                            }
                            else
                            {
                                digitalWrite(D4, HIGH); // 熄灭LED
                                client.print("LED has been turn off");
                                isLedTurnOpen = false;
                            }
                        }
                        else
                        {
                            client.print("
    ");
                        }
                        break;
                    }
    
                    if (c == '
    ')
                    {
                        currentLineIsBlank = true; //开始新行
                    }
                    else if (c != '
    ')
                    {
                        currentLineIsBlank = false; //正在接收某行中
                    }
                    /************************************************/
                }
            }
            delay(1);      //等待客户完成接收
            client.stop(); //结束当前连接:
            Serial.println("[Client disconnected]");
    
            Serial.println(readString); //打印输出来自客户的数据
            readString = "";
        }
    }
    

      

     

     

     

     

      

    改进密码登录模式

    #include <ESP8266WiFi.h>
    
    /*** 该工程可以在2.4.0版本esp8266库中运行,没在更高版本库中进行测试 ***/
    
    const char *ssid = "HUAWEI-H3VBKZ";
    const char *password = "13991320168";
    
    WiFiServer server(80);
    
    String readString = ""; //建立一个字符串对象用来接收存放来自客户的数据
    
    //响应头
    String responseHeaders =
        String("") +
        "HTTP/1.1 200 OK
    " +
        "Content-Type: text/html
    " +
        "Connection: close
    " +
        "
    ";
    
    //网页
    String myhtmlPage=
        String("") +
        "<html>" +
        "<head>" +
        "<meta charset="utf-8">"+
        "    <title>ESP8266 配置信息</title>" +    
        "    <script defer="defer">" +
        "        function ledSwitch() {" +
        "            var name = document.getElementById("wifiname").value;"+
        "            var psw = document.getElementById("wifipwd").value;"+       
        "            var xmlhttp;" +
        "            if (window.XMLHttpRequest) {" +
        "                xmlhttp = new XMLHttpRequest();" +
        "            } else {" +
        "                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");" +
        "            }" +
        "            xmlhttp.onreadystatechange = function () {" +
        "                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {" +
        "                    document.getElementById("txtState").innerHTML = xmlhttp.responseText;" +
        "                }" +
        "            }," +
        "            xmlhttp.open("GET", "Switch"+name+psw, true);" +
        "            xmlhttp.send(); " +
        "        }" +
        "    </script>" +
        "</head>" +  
         "<body>" 
         
        "<h3>连接WIFI:</h3>"+
        
        "<form action=""> "+
        "WIFI账号: <input type="text" id="wifiname"  />"+
        "</form>"+
        
        "<form action=""> "+
        "WIFI密码: <input type="text" id="wifipwd"   />"+
        "</form>"+
        
        "<button type="button" onclick="ledSwitch()"> 连接 </button>"+
        
        "<p>状态消息: <span id="txtState"></span></p> "+
    
        "</body>" +
        "</html>";
    
    
    
    bool isLedTurnOpen = false; // 记录LED状态
    
    void setup()
    {
        pinMode(D4, OUTPUT);
        digitalWrite(D4, HIGH); // 熄灭LED
    
        Serial.begin(115200);
        Serial.println();
    
        Serial.printf("Connecting to %s ", ssid);
        WiFi.begin(ssid, password);
        while (WiFi.status() != WL_CONNECTED)
        {
            delay(500);
            Serial.print(".");
        }
        Serial.println(" connected");
    
        server.begin();
        Serial.printf("Web server started, open %s in a web browser
    ", WiFi.localIP().toString().c_str());
    }
    
    void loop()
    {
        WiFiClient client = server.available(); //尝试建立客户对象
        if (client)                             //如果当前有客户可用
        {
            boolean currentLineIsBlank = true;
            Serial.println("[Client connected]");
    
            while (client.connected()) //如果客户端建立连接
            {
                if (client.available()) //等待有可读数据
                {
                    char c = client.read(); //读取一字节数据
                    readString += c;        //拼接数据
                    /************************************************/
                    if (c == '
    ' && currentLineIsBlank) //等待请求头接收完成(接收到空行)
                    {
                        //比较接收到的请求数据
                        if (readString.startsWith("GET / HTTP/1.1")) //如果是网页请求
                        {
                            client.print(responseHeaders); //向客户端输出网页响应
                            client.print(myhtmlPage);      //向客户端输出网页内容
                            client.print("
    ");
                        }
                        else if (readString.startsWith("GET /Switch")) //如果是改变LED状态请求
                        {
                            if (isLedTurnOpen == false)
                            {
                                digitalWrite(D4, LOW); // 点亮LED
                                client.print("LED has been turn on");
                                isLedTurnOpen = true;
                            }
                            else
                            {
                                digitalWrite(D4, HIGH); // 熄灭LED
                                client.print("LED has been turn off");
                                isLedTurnOpen = false;
                            }
                        }
                        else
                        {
                            client.print("
    ");
                        }
                        break;
                    }
    
                    if (c == '
    ')
                    {
                        currentLineIsBlank = true; //开始新行
                    }
                    else if (c != '
    ')
                    {
                        currentLineIsBlank = false; //正在接收某行中
                    }
                    /************************************************/
                }
            }
            delay(1);      //等待客户完成接收
            client.stop(); //结束当前连接:
            Serial.println("[Client disconnected]");
    
            Serial.println(readString); //打印输出来自客户的数据
            readString = "";
        }
    }
    

      

  • 相关阅读:
    119. Pascal's Triangle II
    118. Pascal's Triangle
    112. Path Sum
    111. Minimum Depth of Binary Tree
    110. Balanced Binary Tree
    108. Convert Sorted Array to Binary Search Tree
    88. Merge Sorted Array
    83. Remove Duplicates from Sorted List
    70. Climbing Stairs
    陌陌面试经历
  • 原文地址:https://www.cnblogs.com/kekeoutlook/p/9572471.html
Copyright © 2011-2022 走看看