首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过蓝牙在Raspberry Pi 4和Arduino Nano BLE之间进行读写?

如何通过蓝牙在Raspberry Pi 4和Arduino Nano BLE之间进行读写?
EN

Stack Overflow用户
提问于 2019-12-27 16:59:22
回答 1查看 1.2K关注 0票数 2

我能够通过Rpi4的bluepy和Arduino Nano BLE的Arduino Nano BLE连接树莓派4 an和Arduino Nano BLE。不幸的是,当我试图从Rpi4写到Arduino Nano BLE时,我看不到预期的读写输出。我没有看到任何Arduino Nano BLE的完美例子,因为它最近发布了内置BLE的硬件。如果有人能帮助我实现他们之间的交流,那将是非常有帮助的。提前谢谢。下面是我的Raspberry Pi的代码。

代码语言:javascript
复制
import bluepy.btle as btle
p = btle.Peripheral("de:fc:54:87:b0:04")
services=p.getServices()
s = p.getServiceByUUID(list(services)[2].uuid)
c = s.getCharacteristics()[0]
c.write(bytes("2", "utf-8"))
p.disconnect()

我使用了Arduino Nano BLE库中的Arduino内置示例。

代码语言:javascript
复制
#include <ArduinoBLE.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN; // pin to use for the LED

void setup() {
  Serial.begin(9600);
  while (!Serial);
  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);
  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }
  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);
  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);
  // add service
  BLE.addService(ledService);
  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);
  // start advertising
  BLE.advertise();
  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    //prints the centrals MAC address:
    Serial.println(central.address());
    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) { // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH); // will turn the LED on
        } else { // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW); // will turn the LED off
        }
      }
    }
    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-28 18:34:00

我自己弄明白了,这是写中的值一直在出错。下面是正确的一条。我希望现在你能找到一个完美的解决方案来连接树莓派4和Arduino Nano BLE无线通过蓝牙。

代码语言:javascript
复制
c.write(bytes("0001".encode())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59498289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档