• esp8266 Server模式


    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    #include "index.h"
    ESP8266WebServer server(80);
    int LED_PIN=14;  //led灯脚
    void setup() {
      Serial.begin(115200);
      // put your setup code here, to run once:
      WiFi.begin("kangtine","87602261");//设置网络链接
      pinMode(LED_PIN,OUTPUT);
      while(WiFi.status()!=WL_CONNECTED){
          delay(500);
          Serial.print(".");
        }
        
        Serial.print("Wi-Fi connected,IP:");
        Serial.println(WiFi.localIP());
        server.on("/",rootRouter);  //路由设置
        server.on("/sw",[](){  //控制LED灯
            String state=server.arg("led");
            if(state=="on")
              digitalWrite(LED_PIN,LOW);
            if(state=="off")
              digitalWrite(LED_PIN,HIGH);
              server.send(200,"text/html","led is on <br>"+state+"</br>");
          });
         server.on("/index.html",rootRouter);  
         server.onNotFound([](){
            server.send(404,"text/plain","File Not found!");
          });
        server.begin();
        Serial.println("HTTP server started.");
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      server.handleClient();
    }
    
    void rootRouter(){
        server.send(200,"text/html",PAGE_INDEX);//将网页存储到flash中
      }
    const char PAGE_INDEX[] PROGMEM= R"=====(
      <!DOCTYPE html ">
    <html">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>ESP8266物联网</title>
    </head>
    
    <body>
      <h1>ESP8266物联网</h1>
      <p>你正在浏览ESP8266提供的信息</p>
    </body>
    </html>
    )=====";
  • 相关阅读:
    C++ malloc 和 new 的函数说明
    C++ const 和static的总结以及使用
    动态库与静态库的区别
    C++引用和指针的区别
    gdb的调试常用命令
    FFMPEG的函数翻译文档
    STL在数组算法的使用
    iOS开发 给Label加下划线、中划线
    更改字符串颜色(长度不确定,有服务器返回)
    iOS 获取键盘高度
  • 原文地址:https://www.cnblogs.com/Lonelychampion/p/12228028.html
Copyright © 2020-2023  润新知