ESP8266 Huzzah - mDNS issue moving from Arduino IDE to PlatformIO

Hi,

I have an ESP8266 Huzzah and am moving my project from the Arduino IDE to PlatformIO in VSCode.

I’m struggling to get mDNS working when building/uploading from PlatformIO.

The mDNS responder example (ESP8266mDNS->mDNS_Web_Server) works as expected when uploaded from the Arduino IDE.

In PlatformIO, using identical code (except adding #include <Arduino.h>, as below) I see no compile errors or warnings and can upload without any issue.

In the serial monitor I see “mDNS responder started” and “TCP server started” as normal, but then cannot connect to http://esp8266.local, nor does the device show up in utilities like mdns-scan. I can ping the device by IP address, so the basic network connection seems to be working.

I’m running version 2.5.1 of the Espressif 8266 platform in PlatformIO, and things are otherwise working as expected.

I’m pretty fresh to ESP8266 and PlatformIO so a bit lost on how to proceed with debugging from here. Any pointers on what I may be missing would be very much appreciated!

Thanks

Code used in VSCode / PlatformIO:

#include <Arduino.h>

/*
  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
  - Point your browser to esp8266.local, you should see a response.

 */


#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>

const char* ssid = "ssid";
const char* password = "pwd";

// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);

void setup(void)
{ 
  Serial.begin(115200);
 
  // Connect to WiFi network
  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)
{
  // 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");
}

What’s the exact version of the ESP8266 platform package in your ArduinoIDE and the board settings you use for compilation?

Hi,

The Arduino IDE board manager shows esp8266 version 2.3.0.

I may not understand correctly what you mean by board settings for compilation, but in Arduino IDE I think it’s just the defaults for Adafruit ESP8266 Huzzah as below. Is this what you’re referring to?

image

Thanks

This is a really old version from 23 June 2016. Are you sure o_O?

Usually with these bugs I try to first reproduce the exact environment in which they work, but a core version from 2016 is so extremely old that there is no platform-espressif8266 version which has this old core. So we can’t even select that core in PIO.

Can you please upgrade your ESP8266 core version to the latest 2.7.1 and see if your sketch still works? You can always downgrade your version again later if it doesn’t work.

1 Like

Yikes, that must have been left over from a very old install… I’ve updated to 2.7.1 and note that some of the methods have changed for mDNS. Therefore, the example I was trying to use in PlatformIO was simply out of date.

The example code now works as expected in PlatformIO (though I still wrangle with other issues in my actual project!)

Lesson learned! Many thanks for your help.

1 Like

You’re not alone in that one… I’ve noticed mDNS seems to have stopped working for some of my sketches of late, so I’ve got to see what changed. I don’t have as big of a jump - only 2.4-2.5 to 2.7.1, but either something broke, or my Linux install isn’t doing mDNS discovery properly.

Edit: Actually, no, those devices are running 2.7.0 so are subject to the mDNS crash hotfixed in 2.7.1.