-2

我正在做一个项目,我有一个 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 指令集,将不胜感激。

4

1 回答 1

-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());
}
于 2018-07-17T13:03:40.493 回答