Comparing WiFi.SSID()s?

So I’m trying to scan for networks in an area where several access points broadcast with the same name, and I only want each one to appear once in my list.
I have tried various things, and am now attempting a string comparison to see if this SSID has been seen before, but I am getting very odd results, where networks with different names are being treated as the same, and ones with the same name as different, apparently without logic!
I’m sure I have made a stupid error, but please help!

for (int i = 0; i < NoOfNetworks; i++) {
    boolean InList = false;
    for (int j = 0; j < i; j++) {
      if (strcmp(WiFi.SSID(j),WiFi.SSID(i))) {
        InList = true;
      }
    }
    if (!InList) {
      Serial.print("New SSID: ");
      Serial.println(WiFi.SSID(i));
      // Print SSID and RSSI for each network found
      st += "<input type='radio' name='ssid' value='";
      st += WiFi.SSID(i);
      st += "'>";
      st += WiFi.SSID(i);
      st += " (";
      st += WiFi.RSSI(i);
      st += ")";
      st += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*";
      st += "
";
    }
    else{
      Serial.print(WiFi.SSID(i));
      Serial.println(" already in list");
    }
  }

This gives the following on the Serial monitor:

New SSID: cafe
New SSID: cafe
cafe-PPSK already in list
cafe-PPSK already in list
cafe already in list
cafe-PPSK already in list
cafe-PPSK already in list
cafe already in list
cafe-PPSK already in list
cafe already in list
cafe already in list
cafe-PPSK already in list
cafe already in list
cafe already in list
cafe-PPSK already in list
cafe-PPSK already in list
cafe-PPSK already in list
cafe-PPSK already in list

and the list only contains “cafe” and “cafe”

What am I doing wrong? :’(

I just fixed it by changing:

if (strcmp(WiFi.SSID(j),WiFi.SSID(i))) {
        InList = true;
      }

Into:

if ((String(WiFi.SSID(j))==String(WiFi.SSID(i)))) {
        InList = true;
      }

Anyone able to shed any light on what is going on here?
:confused:

rokison:
Anyone able to shed any light on what is going on here?
:confused:

Not without seeing all your code.

      Serial.println(WiFi.SSID(i));

You should always print some kind of marker before and after the string.

      Serial.print(">");
      Serial.print(WiFi.SSID(i));
      Serial.println("<");

That makes it possible to see the difference between cafe and cafe , for instance.

Wrapping the instances in Strings should not have had any impact, unless SSID isn’t really a string. If it is just a char array, that would explain the issue.

PaulS,
If it were a char array, could you explain how this might have changed things?
Many thanks

Can you cite the definition of a string? It is a NULL-terminated array of chars. That first bit, NULL-terminated, is crucial. Without a NULL, the char array is NOT a string.

Since you posted only a snippet of code, there is no way we can determine what type SSID is. We can not see whether the class that populates that member is putting a NULL in the array, if indeed SSID is even a char array.