我通过蓝牙连接到我的ESP32,并把密码测试移动伺服到一定的位置,但它一直通过终端告诉我它的密码错误
我尝试过将所有变量分类为一个字符串,还尝试过添加一个“测试”,看看这是否会改变结果。
#include <SoftwareSerial.h>
#include <ESP32Servo.h>
#include <BluetoothSerial.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
Servo myservo;
String password = "test";
String getinput = "";
const int lockPosition = 90;
const int unlockPosition = 180;
void setup() {
myservo.attach(13);
myservo.write(lockPosition);
Serial.begin(115200);
SerialBT.begin("ESP32 Lock"); // Name of the device
Serial.println("The device is ready to pair");
}
void loop() {
if (SerialBT.available()) {
getinput = SerialBT.readStringUntil('\n');
Serial.print("Received: ");
Serial.println(getinput);
if (getinput ==password) {
SerialBT.println("Correct password, unlocking");
myservo.write(unlockPosition);
} else {
myservo.write(lockPosition);
SerialBT.println("Wrong Password");
}
}
}
1条答案
按热度按时间7gcisfzg1#
该问题似乎是通过用户输入在发送到esp 32时白色而发生的。通过使用.trim()删除任何前导或尾随空格解决了该问题
变量.trim();