当前位置:主页 > 电子世界 > Arduino > 正文

求助各位大神!!!有关esp8266WIFI模块的问题!

01-09 Arduino

我在利用WiFi模块内置处理器搭建一个服务器,支持手机通用浏览器访问,并以此控制室内灯光开关。可是代码写完以后,可以正常连接WIFI,但是串口显示器就像下图一样,后面打着打着没有了,不知道这是为什么,而且用手机也不能连接到设置的服务页面,求问各位大神们这是为什么?

R}A8D6`S14(O]1J3)6F]9R0.png(8.44 KB, 下载次数: 0)

下载附件

2018-1-9 15:15 上传

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>

const char ssid[] = "P10"; //SSID
const char pass[] = "18810970082"; //Password
const char* host = "iot";

ESP8266WebServer server(80);

//实现了简单的HTTP服务器
String getContentType(String filename){
  if(server.hasArg("download")) return "application/octet-stream";
  else if(filename.endsWith(".htm")) return "text/html";
  else if(filename.endsWith(".html")) return "text/html";
  else if(filename.endsWith(".css")) return "text/css";
  else if(filename.endsWith(".js")) return "application/javascript";
  else if(filename.endsWith(".png")) return "image/png";
  else if(filename.endsWith(".gif")) return "image/gif";
  else if(filename.endsWith(".jpg")) return "image/jpeg";
  else if(filename.endsWith(".ico")) return "image/x-icon";
  else if(filename.endsWith(".xml")) return "text/xml";
  else if(filename.endsWith(".pdf")) return "application/x-pdf";
  else if(filename.endsWith(".zip")) return "application/x-zip";
  else if(filename.endsWith(".gz")) return "application/x-gzip";
  return "text/plain";
}

bool handleFileRead(String path){
  Serial.println("handleFileRead: " + path);
  if(path.endsWith("/")) path += "index.html";
  String contentType = getContentType(path);
  String pathWithGz = path + ".gz";
  if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
    if(SPIFFS.exists(pathWithGz))
      path += ".gz";
    File file = SPIFFS.open(path, "r");
    size_t sent = server.streamFile(file, contentType);
    file.close();
    return true;
  }
  return false;
}

void setup( ){

  Serial.begin(115200);
SPIFFS.begin();

  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println(ssid);
  Serial.print("Connected to ");
  
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  IPAddress ip = WiFi.localIP();
  
  if (!MDNS.begin(host, ip)) {
    Serial.println("Error setting up MDNS responder!");
    while(1) {
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());

  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
server.on ("/gpio02", []() {
     String state = server.arg("state");
     if (state == "SET") {
         digitalWrite(1, HIGH);
     } else if (state == "RESET") {
         digitalWrite(1, LOW);
     }
  });
  server.on ("/gpio16", []() {
     String state = server.arg("state");
     if (state == "SET") {
         digitalWrite(2, HIGH);
     } else if (state == "RESET") {
         digitalWrite(2, LOW);
     }
  });
server.onNotFound([](){
    if(!handleFileRead(server.uri()))
      server.send(404, "text/plain", "404 Not Found.");
  });

  server.begin();
  Serial.println("HTTP server started");

  MDNS.setInstanceName("IOT605 WiFiBoard");
  MDNS.addService("http", "tcp", 80);
}

void loop(){
  server.handleClient();
}



这是我的源代码

/*移动1*/
博客主人小梁
男,文化程度不高性格有点犯二,闲着没事喜欢研究各种代码,资深技术宅。
  • 20825文章总数
  • 6643289访问次数
  • 76543建站天数
  • 热门文章

    标签

    友情链接