sleep deep
sleep deep

Reputation: 15

ESP32 BluetoothSerial cannot check password before read data

I use ESP32 with BluetoothSerial for receive command from bluetooth. I want to check password before read command data.

void loop() {
    
        while (SerialBT.available() > 0)   {  // detect new input
         rcvdstring = SerialBT.readString();
         cmdInt();  //invoke the command interpreter
      }
      
    }
    
    void cmdInt(void)
    {
     
        rcvdstring.trim();  // remove leading&trailing whitespace, if any
        // find index of separator ('=')
        sepIndex = rcvdstring.indexOf('=');
        if (sepIndex==-1) {  // no parameter in command
          cmdstring = rcvdstring;
          noparm = 1;   
        }
        else  {
        // extract command and parameter
          cmdstring = rcvdstring.substring(0, sepIndex);
          cmdstring.trim();
          parmstring = rcvdstring.substring(sepIndex+1); 
          parmstring.trim();
          noparm = 0;
        }
    
        //check password
        if(cmdstring.equalsIgnoreCase("1234")){
            SerialBT.println("Password correct!");
    
            //If password correct then can read this command
            if (cmdstring.equalsIgnoreCase("wifissid"))   {
              SerialBT.println(ssid);
    
            }
            else   { // Command no match
                SerialBT.println("Invalid command.");
            }  
              
        }else{
          SerialBT.println("Password wrong!");
        }
        
     }

when I enter password 1234 then output is.

Password correct!
Invalid command.

when I enter command wifissid output is

Password wrong!

Look like it check password and command in same time. I want to check password before read command. How to fix it?

Upvotes: 0

Views: 336

Answers (1)

philipp kreuzbichler
philipp kreuzbichler

Reputation: 21

It would be helpful to see the whole string which has been sent by your bluetooth device. However looking at your code, youre checking if your cmdstring is "1234" and "wifissid" at the same time. So of course this will never be the case.

Your code should look more like this:

bool passwordCorrect = false;
void loop() {
    while (SerialBT.available() > 0)   {  // detect new input
        rcvdstring = SerialBT.readString();
        cmdInt();  //invoke the command interpreter
    }
}

void cmdInt()
{
 
    rcvdstring.trim();  // remove leading&trailing whitespace, if any
    // find index of separator ('=')
    sepIndex = rcvdstring.indexOf('=');
    if (sepIndex==-1) {  // no parameter in command
      cmdstring = rcvdstring;
      noparm = 1;   
    }
    else{
    //extract command and parameter
      cmdstring = rcvdstring.substring(0, sepIndex);
      cmdstring.trim();
      parmstring = rcvdstring.substring(sepIndex+1); 
      parmstring.trim();
      noparm = 0;
    }

    //check password
    if(cmdstring.equalsIgnoreCase("1234")){
        SerialBT.println("Password correct!");
        passwordCorrect = true;
    }else{
      SerialBT.println("Password wrong!");
    }
    //If password correct then can read this command
    if (cmdstring.equalsIgnoreCase("wifissid") && passwordCorrect)   {
      SerialBT.println(ssid);
    }else   
    { 
        // Command no match
        SerialBT.println("Invalid command.");
    }
}

I did not test this, but it might give you an idea whats wrong.

Upvotes: 1

Related Questions