首页 > 解决方案 > ESP32 在与 MIT App Inventor 2 交互时停止一切

问题描述

现在制作一个通过经典蓝牙与 ESP32 交互的应用程序。我正在读取霍尔传感器并在其值高于 0 时发送 0,在低于 0 时发送 1。现在,当我在 ai2 中注册该 1 并且因此在应用程序中发生一些事情时,ESP32 发生故障或其他什么。我停止从串行监视器中的传感器获取读数,并且蓝牙连接停止。似乎整个 esp 都停在了原地。我也没有向 ESP32 发送任何数据,只是从中接收数据。esp代码超级小,但app代码不多。解决此问题的唯一方法是重置 esp,这在我的用例中并不可行。有任何解决这个问题的方法吗?

    #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;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("Sport Spel"); //Bluetooth device name
}

void loop() {
  Serial.println(hallRead());
  if (SerialBT.available) 
  {
    if (hallRead() < 0)
    {
      SerialBT.write('1');
    }
    else
    {
      SerialBT.write('0');
    }
    delay(20);
  }

}

应用程序代码的图像 1

图 2

3

4

5

6

标签: androidarduinobluetoothesp32

解决方案


线

  if (SerialBT.available) 

应该

  if (SerialBT.available())

正如您在问题中所写的那样,您正在测试对象上命名的方法的地址是否availableSerialBT真,它总是如此。您想要实际调用该方法,因此您需要包含括号才能调用它。


推荐阅读