Compare strings

As part of a bigger project i am trying to use wireless setup data obtained from a text file on an SD card and compare it to SSID's serched by the wifi program to determine which one to connect to.
The text file is secified below;

//Set up data file
File myFile;      //this is the data / settings file and should hold all menu settings in case of reboot
struct parameters {
  String NetSSID;
  String NetPass;
  String NetMAC;
  String NetIP;
  String NetSecType;
}

I can recover the NetSSID string and print it to the serial monitor no problem.

The wifi routine that has been taken from the example, reads and displays the list of SSID's of which one of them matches the retrieved from the SD card.

void listNetworks() {
  // scan for nearby networks:
  Serial.println("** Scan Networks **");
  int numSsid = WiFi.scanNetworks();
  if (numSsid == -1)
  { 
    Serial.println("Couldn't get a wifi connection");
    while(true);
  } 

  // print the list of networks seen:
  Serial.print("number of available networks:");
  Serial.println(numSsid);

  // print the network number and name for each network found:
  for (int thisNet = 0; thisNet<numSsid; thisNet++) {
    Serial.print(thisNet);
    Serial.print(") ");
    Serial.print(WiFi.SSID(thisNet));
    Serial.print("\tSignal: ");
    Serial.print(WiFi.RSSI(thisNet));
    Serial.print(" dBm");
    Serial.print("\tEncryption: ");
    printEncryptionType(WiFi.encryptionType(thisNet));
    //if (stringOne.equals(stringTwo)) {
    //Serial.println(settings.NetSSID);
    //Serial.println(WiFi.SSID(thisNet));
    if (String(settings.NetSSID).equals(String(WiFi.SSID(thisNet)))) {
      Serial.println("(Matched network)"); 
    }
    else
    {
      Serial.println("Not matched");
    }
  }
}

My issue is that this line will only produce a match if i enter the SSID name as a string rather than refering to it through "settings.NetSSID"

if (String(settings.NetSSID).equals(String(WiFi.SSID(thisNet)))) {

What am i doing wrong ?

Andrew

What am i doing wrong ?

So many things that it's hard to know where to begin.

if (String(settings.NetSSID).equals(String(WiFi.SSID(thisNet)))) {

settings.NetSSID is already a String. Creating another one is completely useless.

What type does WiFi.SSID() return? Is is really necessary to create another String from that?

The == operator is overloaded for the String class, and is far more intuitive than the equals() method.

if(settings.NetSSID == WiFi.SSID(thisNet))

Of course, it is possible that NetSSID contains a carriage return or line feed or both that prevent them from actually being equal. It's not possible to tell, Mr. Snippet.

PaulS:
What type does WiFi.SSID() return?

How do i tell ?

I have tried the same line with both the "string" and no difference.
Have also checked that there are no line feeds or CR's and dont think there is, again how can i be sure.
Thanks
Andrew

How do i tell ?

Look in the WiFi library header files.

Wifi.h contains:

    /*
     * Return the current SSID associated with the network
     *
     * return: ssid string
     */
    char* SSID();

Have also checked that there are no line feeds or CR's and dont think there is, again how can i be sure.

How have you checked?

Serial.print("NetSSID = [");
Serial.print(settings.NetSSID);
Serial.println("]");

Is how I'd do it. You'd see
NetSSID = [xxxxxxxxx]
if there are no crs or lfs in NetSSID or
NetSSID = [xxxxxxxxx
]
if there are.

Thanks, worked out the checking for line feeds bit and it is ok, no line feeds or CR's

This works... i am sitting in a KLM airline lounge right now. SD card data changed to suit.

if (String(WiFi.SSID(thisNet))== "KLM-Lounge") {

This does not

if (WiFi.SSID(thisNet)== "KLM-Lounge") {

This does not.

 if (String(WiFi.SSID(thisNet)) == settings.NetSSID) {

WAs just about to copy and paste the output from the serial monitor here so you could see what i had done when i noticed something.

Output as shown in serial monitor.
Reading setup data...
SSID=KLM-Lounge<
Scanning available networks...
** Scan Networks **
number of available networks:10
0) >KLM-Lounge< Signal: -51 dBm Encryption: None
Not matched

but when pasted in to notepad;

Reading setup data...
SSID=KLM-Lounge <
Scanning available networks...
** Scan Networks **
number of available networks:10
0) >KLM-Lounge< Signal: -51 dBm Encryption: None
Not matched

There is a space after the KLM-Lounge and before the <.
I trimed the white spaces out and it now works.
How did it get there as there is only a carraige return or line feed in the txt file. And i removed them already.
And what is it?

Andrew

How did it get there as there is only a carraige return or line feed in the txt file. And i removed them already.
And what is it?

I can't answer that. My crystal ball is at the cleaners. If you posted your code, I wouldn't need it...