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)))) {
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.
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
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?