Chat freely about anything...

User avatar
By Skybadger
#83657 HI all
Using arduino 1.8.9 on W10, ESP8266 build 2.5.2, lwip 2.
Using WIfi as a station, declared hostname + NTP services for local time.

I have both ESP-01 and -12 devices on my lan, one of which i need to query another via a http client request, basically to return a REST api compass bearing to a motor controller.
However basing a trial off the basichttpclient example, i don't resolve the hostname of the lan device hosting the compass rest api.
All my hosts have local host names that register via DHCP, are visible in DNS (Windows 2K8) and resolvable from the desktop browser and respond from the desktop using curl requests.

What is interesting that all the clients issue health statements to a local MQTT server via publish() statements and they find that host in the MQTT.connect statement using the LAN DNS name, so DNS resolution for that host is partially working.
I also setup an experiment using gethosstbyame to demonstrate this.
Code: Select allvoid loop()
{
    int output;
    int attempts = 0;
    IPAddress resolvedIP;

    //This resolves when using ip addresses. Need to work out how to make it resolve for names.
    //Expect it to work for WAN addresses  e.g www.skybadger.net and it does.
    //Just doesnt work for lan host names - either FQDN or non-FQDN
    String hostname = "espTHP01";    //Lan host allocated via DHCP
    Serial.printf("[GetHostByName ] trying to resolve %s\n", hostname.c_str() );
    while (  ( output = WiFi.hostByName( hostname.c_str(), resolvedIP, (uint32_t) 2000 ) ) != 1 && ( attempts < 9)  )
    {
        delay(2000);
        Serial.printf("[GetHostByName ] attempt %d ... output : %d\n", attempts, output );
        if ( output )
          Serial.printf("[GetHostByName ] attempt %d ... output : %s\n", attempts, resolvedIP.toString().c_str() );
        attempts++;
    }

    hostname = "espTHP01.i-badger.co.uk"; //Lan host using FQDN of local domain and DNS
    Serial.printf("[GetHostByName ] trying to resolve %s\n", hostname.c_str() );
    while (  ( output = WiFi.hostByName( hostname.c_str(), resolvedIP, (uint32_t) 2000 ) ) != 1 && ( attempts < 9)  )
    {
        delay(2000);
        Serial.printf("[GetHostByName ] attempt %d ... output : %d\n", attempts, output );
        if ( output )
          Serial.printf("[GetHostByName ] attempt %d ... output : %s\n", attempts, resolvedIP.toString().c_str() );
        attempts++;
    }

    hostname = "obbo.i-badger.co.uk"; //Lan host using FQDN of MQTT server that is normally resolved for MQTT publishing connections
    Serial.printf("[GetHostByName ] trying to resolve %s\n", hostname.c_str() );
    while (  ( output = WiFi.hostByName( hostname.c_str(), resolvedIP, (uint32_t) 2000 ) ) != 1 && ( attempts < 9)  )
    {
        delay(2000);
        Serial.printf("[GetHostByName ] attempt %d ... output : %d\n", attempts, output );
        if ( output )
          Serial.printf("[GetHostByName ] attempt %d ... output : %s\n", attempts, resolvedIP.toString().c_str() );
        attempts++;
    }

    hostname = "www.skybadger.net"; // Local internet-facing server using an external DNS name - resolves
    Serial.printf("[GetHostByName ] trying to resolve %s\n", hostname.c_str() );
    while (  ( output = WiFi.hostByName( hostname.c_str(), resolvedIP, (uint32_t) 2000 ) ) != 1 && ( attempts < 9)  )
    {
        delay(2000);
        Serial.printf("[GetHostByName ] attempt %d ... output : %d\n", attempts, output );
        if ( output )
          Serial.printf("[GetHostByName ] attempt %d ... output : %s\n", attempts, resolvedIP.toString().c_str() );
        attempts++;
    }

    hostname = "www.google.com"; //Internet server on t'internet - resolves
    Serial.printf("[GetHostByName ] trying to resolve %s\n", hostname.c_str() );
    while (  ( output = WiFi.hostByName( hostname.c_str(), resolvedIP, (uint32_t) 2000 ) ) != 1 && ( attempts < 9)  )
    {
        delay(2000);
        Serial.printf("[GetHostByName ] attempt %d ... output : %d\n", attempts, output );
        if ( output )
          Serial.printf("[GetHostByName ] attempt %d ... output : %s\n", attempts, resolvedIP.toString().c_str() );
        attempts++;
    }

    Serial.print("[HTTP] begin...\n");   
    if ( hClient.begin( String("http://" + hostname + "/") ) )
    {  // HTTP   
      Serial.print("[HTTP] GET...\n");
      // start connection and send HTTP header
     
      int httpCode = hClient.GET();

      // httpCode will be negative on error
      if (httpCode > 0)
      {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
        {
          String payload = hClient.getString();
          Serial.println(payload);
        }
      }
      else
      {
        Serial.printf("[HTTP] GET... failed, error: %s\n", hClient.errorToString(httpCode).c_str());
      }

      hClient.end();
    }
    else
    {
      Serial.printf("[HTTP} Unable to connect\n");
    }
}



Has anyone got any guidane on getting DNS resolution on the local LAN working *without* using mDNS ?
Cheers
Mike