1

I have set the .local address on Raspberry Pi, and it is accessible from the PC on address raspberrypi.local.

Now I want to be able to make an HTTP request from NodeMCU to the Raspberry using its .local address.

I found this answer which mentions that NodeMCU needs to have mDNS resolver set: ESP8266 nodemcu resolving raspberry's local dns

How do I set that mDNS on NodeMCU?

1 Answers1

1

Found a solution!

Here's commented code.

You need to include ESP8266WiFi and ESP8266mDNS libraries.

// hostString will be used to identify this device, 
// but not relevant as we're not providing mDNS services
char hostString[16] = {0};

void findMDNS() {
  // Need to make sure that we're connected to the wifi first
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  } 

  if (!MDNS.begin(hostString)) {
    Serial.println("Error setting up MDNS responder!");
  }

  // We now query our network for 'device-info' service
  // over tcp, and get the number of available devices 
  int n = MDNS.queryService("device-info", "tcp");
  if (n == 0) {
    Serial.println("no services found");
  }
  else {
    for (int i = 0; i < n; ++i) {
      // Going through every available service,
      // we're searching for the one whose hostname 
      // matches what we want, and then get its IP
      if (MDNS.hostname(i) == RASPBERRY_HOSTNAME) {
        JENKINS_HOST = String(MDNS.IP(i)[0]) + String(".") +\
          String(MDNS.IP(i)[1]) + String(".") +\
          String(MDNS.IP(i)[2]) + String(".") +\
          String(MDNS.IP(i)[3]);
      }
    }
  }
}
  • Maybe I'm a bit off track here, but, what's "JENKINS_HOST" supposed to be? I can't make sense off it. Looks like it should be an IP address? – Msprg Dec 24 '21 at 23:09