我通过蓝牙向 arduino nano ble 33 发送了一个值,并收到了树莓派的值。如何将此值转换为数字?
Posted
技术标签:
【中文标题】我通过蓝牙向 arduino nano ble 33 发送了一个值,并收到了树莓派的值。如何将此值转换为数字?【英文标题】:I sent a value to the arduino nano ble 33 via bluetooth and received the value to the raspberry pi. How do I convert this value to a number? 【发布时间】:2021-12-13 00:03:30 【问题描述】:目前,我正在研究使用 Arduino Nano 33 BLE 通过蓝牙传输数据的部分。
在工作过程中使用蓝牙完成了从Arduino向树莓派发送值的部分,但是使用Python从树莓派接收到的输出值输出为'rsp': ['wr']而不是a号码。
我正在尝试在浏览各种文档时继续执行此方法。如何将输出值作为数值而不是像 ' rsp ': ['wr'] 这样的输出值?
如果不能以数字的形式接收该值,是否应该将其转换为用socket编写的Python代码??
首先,这是一个与Arduino电池相关的示例,这是一个常用的代码,我尝试根据该代码将其转换为我想要的方式。
在那部分,我将 !Serial 部分更改为 Serial 部分,这样即使没有连接到计算机端口也可以工作。
在这种情况下,我认为没有问题,因为它的工作原理和我想象的一样。
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());
这是树莓派代码
import bluepy.btle as btle
p1 = btle.Peripheral("2D:20:48:59:8F:B4")
services1=p1.getServices()
s1 = p1.getServiceByUUID(list(services1)[2].uuid)
c1 = s1.getCharacteristics()[0]
a1=c1.write(bytes("0001".encode()))
p1.disconnect()
代码执行时,结果如下:
'rsp': ['wr']
在上面的代码中,我想从结果中输出一个数值。如何在 Raspberry Pi 上修改 Python 或 Arduino 中的代码,以便 Raspberry Pi 上 Python 中的输出值以数字形式出现?
【问题讨论】:
您说您想从 arduino 向树莓派发送一个值,但您使用a1=c1.write(bytes("0001".encode()))
从 arduino 读取。你想从树莓派上读还是写?
另外:在编写您的 python 实现之前,请使用通用 BLE 扫描仪应用程序(例如 nRF Connect)测试您的 arduino 实现。如果它使用 nRF Connect 工作,它应该在 python 中工作
【参考方案1】:
您似乎想从 Raspberry Pi 向 Arduino 写入 0 或 1。您正在使用bytes("0001".encode())
,但这将使用字符的 ASCII 值。例如:
>>> list(bytes("0001".encode()))
[48, 48, 48, 49]
>>> list(bytes("0000".encode()))
[48, 48, 48, 48]
有几种方法可以创建数值字节:
>>> list(struct.pack('b', 0))
[0]
>>> list(struct.pack('b', 1))
[1]
>>> list(int(0).to_bytes(1, byteorder='little'))
[0]
>>> list(int(1).to_bytes(1, byteorder='little'))
[1]
注意:我已将所有命令包装在 list()
中,只是为了以通用格式显示结果,您的代码不需要
我还建议通过指定 UUID 更具体地了解您获得的服务和特征。例如:
import bluepy.btle as btle
from bluepy.btle import UUID
ledService = UUID("19B10000-E8F2-537E-4F6C-D104768A1214")
switchCharacteristic = UUID("19B10001-E8F2-537E-4F6C-D104768A1214")
p1 = btle.Peripheral("2D:20:48:59:8F:B4")
services1 = p1.getServices()
s1 = p1.getServiceByUUID(ledService)
c1 = s1.getCharacteristics(switchCharacteristic)[0]
a1 = c1.write(struct.pack('b', 1))
p1.disconnect()
【讨论】:
以上是关于我通过蓝牙向 arduino nano ble 33 发送了一个值,并收到了树莓派的值。如何将此值转换为数字?的主要内容,如果未能解决你的问题,请参考以下文章
HM10与Arduino和Android BLE之间的蓝牙低功耗大数据传输
Android + ESP32 通过蓝牙 (BLE) 发送数据