Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Example code in WiFiDiscoveringServices not working on Arduino MKR WiFi 1010, found "\r\n" in serial input. #12

Closed
harounhajem opened this issue Oct 10, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@harounhajem
Copy link

harounhajem commented Oct 10, 2022

Describe the bug

The example code for WiFiDiscoveringServices.ino has a bug. When user inputs a service type from prompt the input will contain "\r\n" and that causes the startDiscoveringService to not return any services.

Steps to Reproduce

  1. Start a mDNS service locallyband start for example a _mqtt._tcp service.
  2. Run the example WiFiDiscoveringServices.ino
  3. Input "_mqtt" in the serial prompt.

Expected behavior

The user should be able to input any service name such as "_mqtt" or "_http" in the serial prompt and get an answer back with the service found.

Actual behavior

When user inputs "_http" or "_mqtt" in the serial prompt the mDNS service returns with nothing only showing this text, "Finished discovering services of type ".

Information

IDE: Platform.io version 3.4.3
Board: Arduino MKR Wifi 1010
OS: Windows 11
Perquisites: Run a mDNS service locally like this: "dns-sd.exe -R mqttb _mqtt._tcp . 1883"

Additional context

Found that this part in the example code, WiFiDiscoveringServices.ino, might hold the bug.
Variable serviceName will contain "\r\n" after user inputs service name to serial prompt and that causes the startDiscoveringService to not work properly.

void loop()
{
  char serviceName[256];
  int length = 0;

  while (Serial.available())
  {
    serviceName[length] = Serial.read();
    length = (length + 1) % 256;
    delay(5);
  }

  serviceName[length] = '\0';
  ....
  mdns.startDiscoveringService(serviceName, MDNSServiceTCP, 5200);
 ... .
}

I've fixed it with replacing the code with this instead.

void loop() {
  String inputString = "";
  while (Serial.available() > 0) {
    delay(10);
    char c = Serial.read();
    inputString += c;
  }
  if (!mdns.isDiscoveringService() && inputString.length() > 0) {
      // Remove newline 
      inputString.replace("\r\n", "");
      
      // Convert string to char array
      char charArray[inputString.length() + 1];
      inputString.toCharArray(charArray, inputString.length() + 1);

      mdns.startDiscoveringService(charArray, MDNSServiceTCP, 5200);
  }
@khoih-prog
Copy link
Owner

Hi @harounhajem

The new MDNS_Generic v1.4.2 has just been published. Your contribution is noted in Contributions and Thanks

Best Regards,


Releases v1.4.2

  1. Fix bug in UDP length check and in WiFi example. Check UDP Length check discards correct responses when resolving names - On Nano RP2040 Connect #13
  2. Fix bug in example WiFiDiscoveringServices
  3. Update Packages' Patches

@khoih-prog khoih-prog added the bug Something isn't working label Oct 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants