使用hm-10 BLE在arduino之间传输数据

时间:2018-07-17 12:41:35

标签: arduino bluetooth-lowenergy at-command hm-10

我正在一个项目中,我有一个arduino记录房间的湿度和温度水平(使用DHT11传感器),另一个是通过蓝牙接收该数据的arduino。

我正在使用hm-10 BLE模块。

到目前为止,数据收集器可以通过BLE传输数据,接收者可以从我的手机接收BLE数据,但是我不知道如何将两个模块配对,以便接收器可以从数据中接收数据-收藏家。

我在网上找到的所有解决方案都涉及使用AT指令集,而我正在使用SoftwareSerial.h库。

我的数据收集器的代码如下:

//Include the DHT (humidity and temperature sensor) library, and the serial library
#include <dht.h>
#include <SoftwareSerial.h>

//Define the constants for the input (DHT11) and output pins
#define RXpin 7
#define TXpin 8
#define DHT11_PIN 9

//Initialise a Serial channel as softSerial
SoftwareSerial softSerial(RXpin, TXpin);
//Initialise DHT object
dht DHT;
//Set initial measurement to be temperature (not humidity)
bool humidity = false;

void setup() {

  //Start the serial function
  Serial.begin(9600);
  //Start the softSerial channel
  softSerial.begin(9600);

}//void setup()


void loop() {

  //Reset the reading variable
  float(reading);

  //Take in the values recorded by the DHT11
  int chk = DHT.read11(DHT11_PIN);

  //Store the necessary measurement in the reading variable
  if (!humidity) {
    reading = DHT.temperature;
  } else {
    reading = DHT.humidity;
  }

  //Output the reading on the softSerial channel
  softSerial.print(reading);

  //The DHT11 can only take one measurement per second, so waiting two seconds ensures there will be no null readings
  delay(2000);

  //Swap current measurement
  humidity = !humidity;

}//void loop()

任何关于如何将其与另一个hm10模块连接以便它们可以交换信息而不必将所有内容都重写为AT指令集的想法都将受到赞赏。

1 个答案:

答案 0 :(得分:-1)

您需要学习AT命令以将HM-10配置为主模式或从模式,所有这些仍可以使用SoftSerial库完成,获取AT的HM-10数据表,并在这些站点上寻求帮助。

http://www.instructables.com/id/How-to-Use-Bluetooth-40-HM10/

https://www.hackster.io/achindra/bluetooth-le-using-cc-41a-hm-10-clone-d8708e

请参见下面的示例代码:

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(4, 5);

void setup() {
   Serial.begin(9600);
   BTSerial.begin(9600);
   BTSerial.write("AT+DEFAULT\r\n");
   BTSerial.write("AT+RESET\r\n");
   BTSerial.write("AT+NAME=Controller\r\n");
   BTSerial.write("AT+ROLE1\r\n");
   BTSerial.write("AT+TYPE1"); //Simple pairing
}

void loop()
{
   if (BTSerial.available())
       Serial.write(BTSerial.read());
   if (Serial.available())
       BTSerial.write(Serial.read());
}