zoukankan      html  css  js  c++  java
  • (四)esp8266 MDNS域名服务

    (实例一)ESP8266 TFT(ST7735)彩屏-web刷图

    https://www.arduino.cn/thread-42247-1-1.html

     (实例二) 自己当AP时建立MDNS域名

    https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266mDNS/examples/mDNS_Web_Server/mDNS_Web_Server.ino

    /*
      ESP8266 mDNS responder sample
      This is an example of an HTTP server that is accessible
      via http://esp8266.local URL thanks to mDNS responder.
      Instructions:
      - Update WiFi SSID and password as necessary.
      - Flash the sketch to the ESP8266 board
      - Install host software:
        - For Linux, install Avahi (http://avahi.org/).
        - For Windows, install Bonjour (http://www.apple.com/support/bonjour/).
        - For Mac OSX and iOS support is built in through Bonjour already.
      - Point your browser to http://esp8266.local, you should see a response.
    */
    
    
    #include <ESP8266WiFi.h>
    #include <ESP8266mDNS.h>
    #include <WiFiClient.h>
    
    #ifndef STASSID
    #define STASSID "your-ssid"
    #define STAPSK  "your-password"
    #endif
    
    const char* ssid = STASSID;
    const char* password = STAPSK;
    
    // TCP server at port 80 will respond to HTTP requests
    WiFiServer server(80);
    
    void setup(void) {
      Serial.begin(115200);
    
      // Connect to WiFi network
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);
      Serial.println("");
    
      // Wait for connection
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.print("Connected to ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
    
      // Set up mDNS responder:
      // - first argument is the domain name, in this example
      //   the fully-qualified domain name is "esp8266.local"
      // - second argument is the IP address to advertise
      //   we send our IP address on the WiFi network
      if (!MDNS.begin("esp8266")) {
        Serial.println("Error setting up MDNS responder!");
        while (1) {
          delay(1000);
        }
      }
      Serial.println("mDNS responder started");
    
      // Start TCP (HTTP) server
      server.begin();
      Serial.println("TCP server started");
    
      // Add service to MDNS-SD
      MDNS.addService("http", "tcp", 80);
    }
    
    void loop(void) {
    
      MDNS.update();
    
      // Check if a client has connected
      WiFiClient client = server.available();
      if (!client) {
        return;
      }
      Serial.println("");
      Serial.println("New client");
    
      // Wait for data from client to become available
      while (client.connected() && !client.available()) {
        delay(1);
      }
    
      // Read the first line of HTTP request
      String req = client.readStringUntil('
    ');
    
      // First line of HTTP request looks like "GET /path HTTP/1.1"
      // Retrieve the "/path" part by finding the spaces
      int addr_start = req.indexOf(' ');
      int addr_end = req.indexOf(' ', addr_start + 1);
      if (addr_start == -1 || addr_end == -1) {
        Serial.print("Invalid request: ");
        Serial.println(req);
        return;
      }
      req = req.substring(addr_start + 1, addr_end);
      Serial.print("Request: ");
      Serial.println(req);
      client.flush();
    
      String s;
      if (req == "/") {
        IPAddress ip = WiFi.localIP();
        String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
        s = "HTTP/1.1 200 OK
    Content-Type: text/html
    
    <!DOCTYPE HTML>
    <html>Hello from ESP8266 at ";
        s += ipStr;
        s += "</html>
    
    ";
        Serial.println("Sending 200");
      } else {
        s = "HTTP/1.1 404 Not Found
    
    ";
        Serial.println("Sending 404");
      }
      client.print(s);
    
      Serial.println("Done with client");
    }
    

      (实例三) 自己STA连WIFI

     在代码中,我们将包含一个额外的库

    # include <ESP8266mDNS.h>
    

    在该  库中,我们现在可以访问一个 名为 MDNS 响应器的类,  我们将使用它来声明一个对象。

    MDNSResponder mdns;
    

     使用此 对象,我们可以使用  begin方法创建我们将使用的  Web地址,并将其命名为“esp-01”。此方法需要的第二个参数是我们可以使用Wi-Fi对象的本地IP方法使用的Esp8266的IP地址。

     

    mdns.begin(“esp-01”,Wifi.localIP());
    

      

    添加一个条件语句,当它  评估为true时,它会向用户打印一条  消息,说  “M DNS 响应者已启动!”。
     
    最后,  使用MDNS  内置对象调用Add service 方法 
     
    MDNS.addService( “HTTP”, “TCP”, “80”);
    

      这需要三个参数。牛逼HESE参数描述的类型  ,我们要在广播服务  网络。在这种情况下,我们将在80上使用HTTP over TCP。  

     
    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    #include <ESP8266mDNS.h>
    
    ESP8266WebServer server;
    
    char* ssid = "YOUR_SSID";
    char* password = "YOUR_PASSWORD";
    
    MDNSResponder mdns;
    
    
    void setup()
    {
     WiFi.begin(ssid,password);
     Serial.begin(115200);
     while(WiFi.status()!=WL_CONNECTED)
     {
     Serial.print(".");
     delay(500);
     }
     Serial.println("");
     Serial.print("IP Address: ");
     Serial.println(WiFi.localIP());
    
    if (mdns.begin("esp8266-01", WiFi.localIP()))
    Serial.println("MDNS responder started");
    
    server.on("/",[](){server.send(200,"text/plain","Hello World!");});
    server.begin();
    
    MDNS.addService("http", "tcp", 80);}
    
    
    void loop()
    {
    server.handleClient();
    }
    

      

  • 相关阅读:
    LRU缓存机制的实现
    用docker配置mysql主从复制
    策略模式,以及最近用到的策略模式
    2021"MINIEYE杯"中超(7~10)补题
    2021"MINIEYE杯"中超(6)补题
    2021"MINIEYE杯"中超(5)补题
    2021"MINIEYE杯"中超(4)补题
    2021"MINIEYE杯"中超(3)补题
    2021暑期个人赛4补题 (待补充)
    2021暑期个人赛2补题 (待补充)
  • 原文地址:https://www.cnblogs.com/kekeoutlook/p/10597879.html
Copyright © 2011-2022 走看看