Set Credientials Security Var

Im trying to set the security type from WiFi.scan but I’m unable to figure out how to define the security type as a variable.

Instead of listing it as below I would like to use a variable called securyitID

switch(aps[scanID].security) { //0=Open 1=WEP 2=WPA 3=WPA2
            case 0:
              WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, UNSEC);
              break;
            case 1:
              WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, WEP);
              break;
            case 2:
              WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, WPA);
              break;
            case 3:
              WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, WPA2);
              break;
          }

Ideal case - BUT I can figure out how to pass a variable to the set credenitals. I’ve tried Sting, int, char* and none of those work.

String securityID = WEP;

switch(aps[scanID].security) { //0=Open 1=WEP 2=WPA 3=WPA2
            case 0:
             securityID = UNSEC;
              break;
            case 1:
             securityID = WEP;
              break;
            case 2:
              securityID = WPA;
              break;
            case 3:
              securityID = WPA2;
              break;
          }

 WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, securityID);

If you want to find out the answers to such questions yourself, you can always consult the open source repo

bool setCredentials(const char *ssid, const char *password, unsigned long security, unsigned long cipher=WLAN_CIPHER_NOT_SET) 

or
here

typedef enum {
    WLAN_SEC_UNSEC = 0,
    WLAN_SEC_WEP,
    WLAN_SEC_WPA,
    WLAN_SEC_WPA2,
    WLAN_SEC_WPA_ENTERPRISE,
    WLAN_SEC_WPA2_ENTERPRISE,
    WLAN_SEC_NOT_SET = 0xFF
} WLanSecurityType;

typedef enum {
    WLAN_CIPHER_NOT_SET = 0,
    WLAN_CIPHER_AES = 1,
    WLAN_CIPHER_TKIP = 2,
    WLAN_CIPHER_AES_TKIP = 3   // OR of AES and TKIP
} WLanSecurityCipher;
...
typedef struct WiFiAccessPoint {
   size_t size;
   char ssid[33];
   uint8_t ssidLength;
   uint8_t bssid[6];
   WLanSecurityType security;
   WLanSecurityCipher cipher;
   uint8_t channel;
   int maxDataRate;   // the mdr in bits/s
   int rssi;        // when scanning

#ifdef __cplusplus

   WiFiAccessPoint()
   {
       memset(this, 0, sizeof(*this));
       size = sizeof(*this);
   }
#endif
} WiFiAccessPoint;

security and cipher are unsigned long not a string.

And your cases would be just the same (e.g. case WEP:) - don’t rely on the values to stay the same forever.
So for your case you could do away with the switch completely.

  unsigned long secID = aps[scanID].security;
  ...
  WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, secID);
2 Likes

I’ve tried that too but doesnt work. This is the compile notes I got:

screenFunctions.cpp: In function 'void runWifiSetupCarousel()':
screenFunctions.cpp:1277:16: error: jump to case label [-fpermissive]
           case WIFISUCCESS:
                ^
screenFunctions.cpp:1248:25: note:   crosses initialization of 'long unsigned int secID'
           unsigned long secID = aps[scanID].security;
                         ^
screenFunctions.cpp:1283:16: error: jump to case label [-fpermissive]
           case WIFIERROR:
                ^
screenFunctions.cpp:1248:25: note:   crosses initialization of 'long unsigned int secID'
           unsigned long secID = aps[scanID].security;
                         ^
screenFunctions.cpp: In function 'void updatePasswordField()':
screenFunctions.cpp:1517:14: warning: unused variable 'currentCharWidth' [-Wunused-variable]
         byte currentCharWidth = pxs.getTextWidth(currentChar);
              ^
screenFunctions.cpp: In function 'bool breakRefillBuffer()':
screenFunctions.cpp:4527:7: warning: control reaches end of non-void function [-Wreturn-type]
       }

Using this works though:

WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, aps[scanID].security);

Hi @wesner0019

Your last errors were due to trying to define and initialize variables in code that can only be reached in certain code branches but not in all branches of the case statement without an enclosing “block” of curly braces so the compiler knows the scope of the variable.

1 Like

@bko I don’t receive compiler errors though when I don’t try to define this:

So some thing with these satements is causing the compile to fail.

unsigned long secID = aps[scanID].security;
  ...
  WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, secID);

Hi @wesner0019

Where is the case statement around this? That is most likely where the problem is. You aren’t showing enough code to see the problem.

@bko Its within this switch statement:

switch(menuID) {
          //--------------------------------------------
          case WIFISCAN:
          //one shot screen so keeping graphics here
          clearScreen();
          drawIcon(img_icWifi_png_comp, messageScreenIconYPos);
          setWhite19Text();
          printMidText(String("Scanning for"), messageScreenTitleYPos);
          printMidText(String("WiFi connections"), messageScreenTitleYPos + lineHeight19);

          WiFi.on();
          delay(1000);
          // Particle.disconnect();
          //WiFi.disconnect();
          //WiFi.clearCredentials(); //dont need to clear creds every time
          WAPPassword = "";
          scanFound = WiFi.scan(aps, 12);

          //delay(1000);

          menuID = WIFISELECT;
          break;

          //--------------------------------------------

          case WIFISELECT:
          if(touchML){
            scanID--;
            if(scanID < 0) scanID = scanFound - 1;
            eraseBackground(0,104,260,lineHeight19);
            // pxs.setColor(WHITE);
            // pxs.print(5, 35, String(aps[scanID].ssid));
            setLtGreen19Text();
            printMidText(String(aps[scanID].ssid), 104);
            // printMidText(String(scanID), 104);
          } else if(touchMR){
            scanID++;
            if(scanID > scanFound-1) scanID = 0;
            eraseBackground(0,104,260,lineHeight19);
            // pxs.setColor(WHITE);
            // pxs.print(5, 35, String(aps[scanID].ssid));
            setLtGreen19Text();
            printMidText(String(aps[scanID].ssid), 104);
            // printMidText(String(scanID), 104);
          } else if(touchLL){
            //cancel to the Settings carousel
            // menuID = WIFISCAN;
            currCarousel = WIFICAROUSEL;
          } else if(touchLR){
            if(scanFound <= 0){
              menuID = WIFISCAN;
            } else {
              wifiTouchEnable = true;
              menuID = WIFIPASSWORD;
            }
          }
          break;

          //--------------------------------------------

          case WIFIPASSWORD:
          if(!wifiTouch){
            if(touchUL){
              //set caps
              currentCharGroup = 0;
              updateCharButtons();
              updateCurrentChar();
            } else if(touchUM){
              //set lowercase
              currentCharGroup = 1;
              updateCharButtons();
              updateCurrentChar();
            } else if(touchUR){
              //set numbers
              currentCharGroup = 2;
              updateCharButtons();
              updateCurrentChar();
            } else if(touchML){
              //cycle characters left
              wifiTouch = true;
              switch(currentCharGroup){
                case 0:
                letterArray--;
                if(letterArray >=255) letterArray = 25;
                break;
                case 1:
                letterArray--;
                if(letterArray >=255) letterArray = 25;
                break;
                case 2:
                symbolArray--;
                if(symbolArray >=255) symbolArray = 41;
                break;
              }
              updateCurrentChar();
            } else if(touchMM){
              //enter character
              wifiTouch = true;
              WAPPassword.concat(currentChar);
              updatePasswordField();

              if(WAPPassword.length() == 1){
                prevNavToggle = 99;
                createNavigationToggle(DELETESUBMIT);
              }
              if(WAPPassword.length() == 8){
                prevNavToggle = 99;
                createNavigationToggle(DELETESUBMIT);
              }

            } else if(touchMR){
              //cycle characters right
              wifiTouch = true;
              switch(currentCharGroup){
                case 0:
                letterArray++;
                if(letterArray > 25) letterArray = 0;
                break;
                case 1:
                letterArray++;
                if(letterArray > 25) letterArray = 0;
                break;
                case 2:
                symbolArray++;
                if(symbolArray > 41) symbolArray = 0;
                break;
              }
              updateCurrentChar();
            } else if(touchLL){
              //either cancel or delete characters
              wifiTouch = true;
              if(WAPPassword.length() == 0){
                wifiTouchEnable = false;

                menuID = WIFISELECT;
              } else {
                WAPPassword.remove(WAPPassword.length() - 1);
                updatePasswordField();
                if(WAPPassword.length() == 0){
                  prevNavToggle = 99;
                  createNavigationToggle(BACKSUBMIT);
                  setWhite19Text();
                  pxs.setBackground(LT_BLUE_TEXT_FIELD);
                  pxs.setColor(WHITE);
                  pxs.print(26, 33, "Enter Password");
                }
                if(WAPPassword.length() == 7){
                  prevNavToggle = 99;
                  createNavigationToggle(DELETESUBMIT);
                }
              }

            } else if(touchLR && WAPPassword.length() >= 8){
              //submit
              wifiTouchEnable = false;
              menuID = WIFICONNECT;
            }
            // wifiTouch = true;

          }
          break;

          //--------------------------------------------

          case WIFICONNECT:
          //one shot screen so keeping graphics here
          clearScreen();
          drawIcon(img_icWifi_png_comp, messageScreenIconYPos);
          setWhite19Text();
          printMidText(String("Please wait,"), messageScreenTitleYPos);
          printMidText(String("trying to connect"), messageScreenTitleYPos + lineHeight19);

          WiFi.on();
          delay(900);
          // Security
          WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, aps[scanID].security);
          /*switch(aps[scanID].security) { //0=Open 1=WEP 2=WPA 3=WPA2
            case 0:
              WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, UNSEC);
              break;
            case 1:
              WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, WEP);
              break;
            case 2:
              WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, WPA);
              break;
            case 3:
              WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, WPA2);
              break;
          }*/
          numCreds = getWiFiCreds();
          Particle.connect();

          if (waitFor(Particle.connected, 20000)) {
            menuID = WIFISUCCESS;
          } else {
            menuID = WIFIERROR;
            // clearCredentialsComplete();
          }
          break;

          //--------------------------------------------

          case WIFISUCCESS:

          break;

          //--------------------------------------------

          case WIFIERROR:

          break;
        }
        drawWifiSetupCarousel();

      }

Where and how are WIFISUCCESS and the likes declared?
And you are redeclaring case lables that you have already used (e.g. WIFISUCCESS at the top and the bottom of the switch() block - that's what the error message complains about.

I can't see any error messages related to the code changes I suggested tho'

So you haven't tried what I said, but something similar but not in the correct way :wink:
If done right it'd be bound to work.

@ScruffR, I did try what you said but that also causes compile errors

unsigned long secID = aps[scanID].security;
  ...
  WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, secID);

Using this compiles though:

WiFi.setCredentials(String(aps[scanID].ssid), WAPPassword, aps[scanID].security);

As always, when you get a compile error: Post the error message.
The error message will probably point to some other reason than what my snippet was meant to illustrate.

And I’m absolutely sure this builds.
Just try it with this

unsigned long secID;
const char* ssid = "mySSID";
const char* pwd = "myPWD";
void setup()
{
  WiFi.setCredentials(ssid, pwd, secID);
}

BTW, aps[scandID].ssid already is a string, so no need to convert it to a String again.

1 Like