来自 Node.js 的 Ardiuno 蓝牙连接

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

我有一个连接到 arduino UNOr3 的 HC05 模块,以及一个基本的蓝牙串行端口 Nodejs 库,用于发送单行数据,以便我可以确保它正常工作。虽然我的蓝牙模块已连接到我的nodejs并显示已发送,但arduino端没有收到任何内容。我也尝试过将端口(即 rx 和 tx 引脚)移至 0,1,但没有成功。

JS:

var BluetoothSerialPort = new (require("bluetooth-serial-port").BluetoothSerialPort)();
const arduinoMACAddress = 'XXXXXXXX'; //assume that the MAC address is correct (privacy reasons, I don't want to include it)

BluetoothSerialPort.findSerialPortChannel(arduinoMACAddress, function(channel) {
    BluetoothSerialPort.connect(arduinoMACAddress, channel, function() {
        console.log('Connected to Arduino');
        BluetoothSerialPort.write(Buffer.from('test', 'utf-8'), function(err, bytesWritten) {
            if (err) {
                console.error('Error writing data:', err);
            } else {
                console.log(`Sent ${bytesWritten} bytes to Arduino`);
            }
        });
    }, function() {
        console.log('Cannot connect to Arduino');
    });
});

Ardiuno 方面:

#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// Set up a new SoftwareSerial object
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
    // Define pin modes for TX and RX
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    
    // Set the baud rate for the SoftwareSerial object
    mySerial.begin(9600);
}

void loop() {
    if (mySerial.available() > 0) {
        Serial.println("Connected");
        mySerial.read();
    }
}
c++ node.js arduino bluetooth
1个回答
0
投票

Arduino 代码不会显示任何内容,因为您只初始化了 SoftwareSerial 端口,而不是您希望从中读取文本的串行端口。奇怪的是,这不会在编译过程中引发错误。在下面的代码中,我已将端口初始化为 9600 波特,并添加了几行,以便从 HC05 模块接收到的任何内容也会显示出来(以及一行以确认两个端口均已设置)。

#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// Set up a new SoftwareSerial object
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
    Serial.begin(9600);
    // Define pin modes for TX and RX
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    
    // Set the baud rate for the SoftwareSerial object
    mySerial.begin(9600);
    
    Serial.println("Ready");
}

void loop() {
    if (mySerial.available() > 0) {
        Serial.println("Data available");
        char a = mySerial.read();
        Serial.print(a);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.