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]);
}
}
}
}