零知WIFI教程-mDNS服务 多播DNS使用示例

本文介绍多播DNS-mDNS的使用,关于mDNS的详细信息,参考该文档:https://tools.ietf.org/html/rfc6762;在ESP8266的应用程序中,很多都是没有界面的,在ESP做WEB服务时候,就很难获取和记住其数字标识的IP地址,mDNS就很容易解决该问题;下面是在零知开源平台使用mDNS的示例。

一、软件和硬件

硬件我们本次使用零知-ESP8266;

软件使用零知开发工具,自带示例:

二、方法步骤

(1)先在零知开发工具中打开mDNS_Web_Server示例,或者复制下面的代码到零知开发工具中:


							
	/**********************************************************
	*    文件: x.ino      by 零知实验室([url]www.lingzhilab.com[/url])
	*    -^^- 零知开源,让电子制作变得更简单! -^^-
	*    时间: 2019/05/28 12:22
	*    说明: mDNS responder sample
	************************************************************/
	/*
	  ESP8266 mDNS responder sample
	  
	  This is an example of an HTTP server that is accessible
	  via [url]http://esp8266.local[/url] 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 ([url]http://avahi.org/[/url]).
	    - For Windows, install Bonjour ([url]http://www.apple.com/support/bonjour/[/url]).
	    - For Mac OSX and iOS support is built in through Bonjour already.
	  - Point your browser to [url]http://esp8266.local[/url], you should see a response.
	  
	*/
	  
	  
	#include <ESP8266WiFi.h>
	#include <ESP8266mDNS.h>
	#include <WiFiClient.h>
	  
	#ifndef STASSID
	#define STASSID "ssid"
	#define STAPSK  "passwd"
	#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('\r');
	  
	  // 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\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at ";
	    s += ipStr;
	    s += "</html>\r\n\r\n";
	    Serial.println("Sending 200");
	  } else {
	    s = "HTTP/1.1 404 Not Found\r\n\r\n";
	    Serial.println("Sending 404");
	  }
	  client.print(s);
	  
	  Serial.println("Done with client");
	}
							
						

(2)验证后上传代码到零知-ESP8266;

(3)然后我们打开浏览器,打开网址:http://esp8266.local

这和直接访问IP:192.168.0.130是一样的。访问时候,串口监视器也可以看到连接信息: