概念: Zeroconf (Zero-configuration networking 零配置联网)
功能:
1 自动配置IP地址 (Link-local 地址),
2 自动配置并解析域名,这项技术被称为 mDNS (Multicast Domain Name Service,多点发送域名服务)
3 在网络传播和接收自己与其他设备所能提供的服务。DNS-SD(DNS-based Service Discovery 基于DNS的服务探索)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
ESP8266WebServer server(80);
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
WiFi.begin("kangtine","87602261");
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.println(".");
}
Serial.print("dns 配置中");
if(WiFi.status() == WL_CONNECTED) //If WiFi connected to hot spot then start mDNS
{
if (MDNS.begin("lsq")) { //Start mDNS with name esp8266
Serial.println("MDNS started");
}
}
Serial.print("Wi-Fi connected,IP:");
Serial.println(WiFi.localIP());
server.on("/",[](){
server.send(200,"text/html","hello from <b>ESP8266</b>.");
});
server.onNotFound([](){
server.send(404,"text/plain","File Not found!");
});
server.begin();
MDNS.addService("http","tcp",80);
Serial.println("HTTP server started.");
int n = MDNS.queryService("http","tcp");
if(n>0){
for(int i=0;i<n;i++){
Serial.print(i+1);
Serial.print(MDNS.hostname(i));
Serial.print(MDNS.IP(i));
Serial.print(MDNS.port(i));
}
}else{
Serial.print("no service found");
}
}
void loop() {
// put your main code here, to run repeatedly:
MDNS.update(); 一开始我没加这一句。始终无法访问。。。后来加着这一句之后才能访问。。。
server.handleClient();
}