Serialization 通过蓝牙进行传感器读取通信
我有两个蓝牙模块(HC05)连接到单独的arduinos。一个充当主人,另一个充当奴隶。一个LDR连接到从机部件,从机部件将连续读取数据,并通过蓝牙将数据发送给主设备 模块已成功配对。我甚至可以使用连接到从设备的按钮控制连接到主设备的led 4天以来,我一直在努力获取master串行监视器上的LDR读数 项目的从属部分(具有LDR):Serialization 通过蓝牙进行传感器读取通信,serialization,bluetooth,android-sensors,arduino-uno,arduino-ide,Serialization,Bluetooth,Android Sensors,Arduino Uno,Arduino Ide,我有两个蓝牙模块(HC05)连接到单独的arduinos。一个充当主人,另一个充当奴隶。一个LDR连接到从机部件,从机部件将连续读取数据,并通过蓝牙将数据发送给主设备 模块已成功配对。我甚至可以使用连接到从设备的按钮控制连接到主设备的led 4天以来,我一直在努力获取master串行监视器上的LDR读数 项目的从属部分(具有LDR): #包括 软件串行BTSerial(10,11);//RX | TX #定义ldrPin A0 int-ldrValue=0; 无效设置(){ 引脚模式(9,输出)
#包括
软件串行BTSerial(10,11);//RX | TX
#定义ldrPin A0
int-ldrValue=0;
无效设置(){
引脚模式(9,输出);//该引脚将HC-05引脚34(键引脚)拉高,以将模块切换到AT模式
数字写入(9,高);
引脚模式(ldrPin,输入);
BTSerial.begin(9600);
Serial.begin(9600);
}
void循环()
{
ldrValue=模拟读取(ldrPin);
BTSerial.println(ldrValue);
Serial.println(ldrValue);
延迟(1000);
}
该项目的主要部分将获得收益并在串行监视器上显示:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
const byte numChars = 1024;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
BTSerial.begin(9600);
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (BTSerial.available() > 0 && newData == false) {
rc = BTSerial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
#包括
软件串行BTSerial(10,11);//RX | TX
常量字节numChars=1024;
char receivedChars[numChars];//存储接收到的数据的数组
布尔newData=false;
无效设置(){
引脚模式(9,输出);//该引脚将HC-05引脚34(键引脚)拉高,以将模块切换到AT模式
数字写入(9,高);
BTSerial.begin(9600);
Serial.begin(9600);
Serial.println(“
由于我是arduino新手,有人能帮我一下吗。要将字符串直接读入变量,可以使用:
BTSerial.readString()
而不是:
BTSerial.read()
就像在官方中一样而不是BTSerial.read(),试试BTSerial.readString(),就像在中一样,它会更容易@vishruth kumarthank you!!它工作了!太好了!我会把它作为官方答案发布
<Arduino is ready>
This just in ... h
This just in ... h
This just in ... h
This just in ... h
This just in ... h
This just in ... h
BTSerial.readString()
BTSerial.read()