Raspberry Pi4如何通过蓝牙与Arduino Nano BLE进行读写?
How to perform read and write between Raspberry Pi 4 and Arduino Nano BLE Via Bluetooth?
我能够通过用于 Rpi4 的 bluepy 和用于 Arduino Nano BLE 的 ArduinoBLE.h 连接 Raspberry Pi 4 和 Arduino Nano BLE。不幸的是,当我尝试从 Rpi4 写入 Arduino Nano BLE 时,我没有看到预期的读取和写入输出。我没有看到 Arduino Nano BLE 的任何完美示例,因为它是最近发布的带有内置 BLE 的硬件。如果有人能帮助我实现他们之间的沟通,那将非常有帮助。提前致谢。下面是我的 Raspberry Pi.
代码
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 内置示例。
#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());
}
}
我自己弄明白了,一直是write中的值出错了。下面是正确的。我希望现在您能找到通过蓝牙无线连接 raspberry Pi 4 和 Arduino Nano BLE 的完美解决方案。
c.write(bytes("0001".encode())
我能够通过用于 Rpi4 的 bluepy 和用于 Arduino Nano BLE 的 ArduinoBLE.h 连接 Raspberry Pi 4 和 Arduino Nano BLE。不幸的是,当我尝试从 Rpi4 写入 Arduino Nano BLE 时,我没有看到预期的读取和写入输出。我没有看到 Arduino Nano BLE 的任何完美示例,因为它是最近发布的带有内置 BLE 的硬件。如果有人能帮助我实现他们之间的沟通,那将非常有帮助。提前致谢。下面是我的 Raspberry Pi.
代码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 内置示例。
#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());
}
}
我自己弄明白了,一直是write中的值出错了。下面是正确的。我希望现在您能找到通过蓝牙无线连接 raspberry Pi 4 和 Arduino Nano BLE 的完美解决方案。
c.write(bytes("0001".encode())