[英]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?
Currently, I am working on the part that transmits data through Bluetooth using Arduino Nano 33 BLE.目前,我正在研究使用 Arduino Nano 33 BLE 通过蓝牙传输数据的部分。
The part of sending the value from Arduino to Raspberry Pi was completed using Bluetooth during the work, but the output value received using Python from the Raspberry Pi was output as {' rsp ': ['wr']} instead of a number.从Arduino发送值到树莓派的部分是在工作过程中使用蓝牙完成的,但是从树莓派接收到的Python输出值输出为{' rsp ': ['wr']}而不是数字。
I am trying to proceed through this method while browsing various documents.我正在尝试在浏览各种文档时通过这种方法进行。 How can I get the output value as a numeric value rather than an output value like {' rsp ': ['wr']} ?
如何将输出值作为数值而不是像 {' rsp ': ['wr']} 这样的输出值?
If the value cannot be received as a number, should it be transformed into a Python code written in socket??如果不能以数字形式接收值,是否应该将其转换为用socket编写的Python代码??
First, it is an example related to the Arduino battery, which is a commonly used code, and I tried to transform it in the way I want based on that code.首先,它是一个与Arduino电池相关的例子,这是一个常用的代码,我试图根据该代码以我想要的方式对其进行转换。
In that part, I changed the !Serial part to the Serial part so that it works even when not connected to the computer port.在那个部分,我将 !Serial 部分更改为 Serial 部分,以便它即使在未连接到计算机端口时也能工作。
In that case, I don't think there's a problem because it works just as well as I thought.在那种情况下,我认为没有问题,因为它和我想的一样好。
Arduino Sample Code 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());
}
}
Here is the raspberry pi code这是树莓派代码
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()
When the code is executed, the result is as follows:执行代码时,结果如下:
{'rsp': ['wr']}
In the above code, I want to output a numeric value from the result.在上面的代码中,我想从结果中输出一个数值。 How do I modify the code in Python or Arduino on Raspberry Pi so that the output value in Python on Raspberry Pi comes out as a number?
如何修改 Raspberry Pi 上的 Python 或 Arduino 中的代码,以便 Raspberry Pi 上的 Python 中的输出值以数字形式出现?
It looks like you want to write either a zero or a one to the Arduino from the Raspberry Pi.看起来您想从 Raspberry Pi 向 Arduino 写入 0 或 1。 You are using
bytes("0001".encode())
but this will use the ASCII values for the characters.您正在使用
bytes("0001".encode())
但这将使用字符的 ASCII 值。 For example:例如:
>>> list(bytes("0001".encode()))
[48, 48, 48, 49]
>>> list(bytes("0000".encode()))
[48, 48, 48, 48]
There are a couple of ways to create bytes of a numeric value:有几种方法可以创建数值的字节:
>>> 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]
Note: I've wrapped all of the commands in list()
just to show the results in common formatting and is unnecessary for your code注意:我在
list()
包装了所有命令只是为了以通用格式显示结果,对于您的代码来说是不必要的
I would also recommend being more specific about the services and characteristics you are geting by specifying the UUIDs.我还建议通过指定 UUID 来更具体地了解您获得的服务和特征。 For example:
例如:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.