如何使用蓝牙将数据从 MIT App Inventor 发送到 Arduino

问题描述 投票:0回答:1

我正在使用 Heltec Wireless(不带 anteena)从我的移动应用程序接收数据,该应用程序由 MIT App Inventor 制作。

从发明者的角度来看,一切看起来都很好。我有一个滑块,我想用它来控制 LED 的亮度。

但是我在 MCU com 端口接收到的数据是 2 位数字,而我发送的是 3 位数字数据。手机发送 30,而在 com 端口上显示 3。因此,我不确定这里有什么问题。

这是 MIT 应用程序发明者发送数据的部分:-

这是 Arduino 代码:-

#include "BluetoothSerial.h"
    
//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line be

low
const char *pin = "1234"; // Change this to more secure PIN.

String device_name = "Project MEEM Model";

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

String serial = "";
int bright = 0;

void setup() {
  Serial.begin(9600);
  pinMode(25, OUTPUT);
  SerialBT.begin(device_name); //Bluetooth device name
  Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
  //Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
  #ifdef USE_PIN
    SerialBT.setPin(pin);
    Serial.println("Using PIN");
  #endif
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());

    serial = SerialBT.read();

  //Serial.print("LED brightness : ");
  //Serial.println(serial);
  
   bright = serial.toInt();

   
  analogWrite(25, bright);
  
  //Serial.print("LED brightness : ");
  //Serial.println(bright);

   
  }

我厌倦了实验,所以需要社区大神的帮助:)

测试:从手机发送“125”到arduino

android arduino bluetooth wireless arduino-ide
1个回答
0
投票

我只需使用 .readString 而不是 .read 来解决这个问题。谢谢乌克巴兹。

© www.soinside.com 2019 - 2024. All rights reserved.