#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>
)=====";

 

posted on 2020-01-22 10:27  孤独斗士  阅读(1219)  评论(0编辑  收藏  举报