Search networks and perform actions based on ones found


#1

I have network names that I know and want the device to do something when it sees these networks I was using this code:

WiFiAccessPoint aps[20];
int found = WiFi.scan(aps, 20);
for (int i=0; i<found; i++) {

    WiFiAccessPoint& ap = aps[i];
    //if(ap.ssid == "FiOS-34K20"){!!!!
    
    Serial.print("SSID: ");
    Serial.println(ap.ssid);
    Serial.print("Security: ");
    Serial.println(ap.security);
    Serial.print("Channel: ");
    Serial.println(ap.channel);
    Serial.print("RSSI: ");
    Serial.println(ap.rssi);
    //delay(5000);
  //}!!!!
}

The part I am interested in I commented out and placed !!! next to, why would this if statement not work what kind of object is “ap.ssid” since this doesn’t work I am assuming it is not a string.


#2

Hi @JDvetinno

The WiFiAccessPoint is a struct with ssid defined as char ssid[33]; so I don’t think that == is the right operator.

Try this instead:

if (0==strcmp(ap.ssid,"FiOS-34K20")) {
...
}

#3

Thanks for the help! I will need to brush up on structures again, I have been dealing with python for too long lol!


#4

This code as it is does throw the error “I Value required as left operand of assignment” just a heads up.


#5

OK darn type on my code. You need a double equals there. Corrected!


#6

Yea that’s it now I understand that error a lot better also so something learned!


#7

This is also a good example for the C/C++ practice @bko showed here to place the constant on the left side of the equality operator.
If you should happen to use the assignment operator (=) instead of equality (==) you will get a compiletime error and not a successful build producing a program that will behave in an unexpected manner.