C ESP32 BluetoothSerial无法在读取数据之前检查密码
我使用带BluetoothSerial的ESP32从蓝牙接收命令。我想在读取命令数据之前检查密码C ESP32 BluetoothSerial无法在读取数据之前检查密码,c,arduino,esp32,C,Arduino,Esp32,我使用带BluetoothSerial的ESP32从蓝牙接收命令。我想在读取命令数据之前检查密码 void loop() { while (SerialBT.available() > 0) { // detect new input rcvdstring = SerialBT.readString(); cmdInt(); //invoke the command interpreter }
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!");
}
}
当我输入密码1234时,输出为
Password correct!
Invalid command.
输入命令时,wifissid输出为
Password wrong!
看起来它可以同时检查密码和命令。我想在读取命令之前检查密码。如何修复它?查看蓝牙设备发送的整个字符串会很有帮助。 但是,查看代码时,您正在检查cmdstring是否同时为“1234”和“wifissid”。当然,情况永远不会是这样 您的代码应该更像这样:
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.");
}
}
我没有测试这个,但它可能会给你一个什么是错误的想法