-->
Page 1 of 1

Is there any example code for websocket & mdns for esp8266

PostPosted: Sun Dec 18, 2022 5:53 am
by eleckits
I want to connect with esp8266 server like ws://esp8266.local:81 instead of ws://192.168.0.2:81

is there any working example?
thanks

Re: Is there any example code for websocket & mdns for esp82

PostPosted: Sat Jan 28, 2023 7:58 am
by rooppoorali
Yes, there are examples of connecting to an ESP8266 server using a hostname instead of an IP address. One way to do this is by using the mDNS (multicast Domain Name System) protocol, which allows you to resolve hostnames to IP addresses on the local network.

You can use the ESP8266mDNS library to register the ESP8266's hostname on the local network and then connect to it using the hostname instead of the IP address. Here is an example of how to use the library:

Code: Select all#include <ESP8266mDNS.h>

void setup() {
  // Start the Wi-Fi connection
  WiFi.begin("your_SSID", "your_password");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }

  // Start the mDNS responder
  if (MDNS.begin("esp8266")) {
    Serial.println("mDNS responder started");
  } else {
    Serial.println("Error setting up mDNS responder!");
  }
}

void loop() {
  // Your code here
}


Once the ESP8266 is connected to the network, you can use the hostname esp8266.local to connect to it, e.g. ws://esp8266.local:81.

Keep in mind that, the hostname might not be resolved in all Operating System and you need to check it first.

By the way, if you are interested in ESP32 WebSocket server, you can see this:

https://www.theengineeringprojects.com/ ... erver.html