用ZigBee采集dht11温湿度传感器代码
时间: 2023-08-29 08:07:17 浏览: 85
以下是一个基于ZigBee模块采集DHT11温湿度传感器数据的示例代码,使用的是Arduino Uno和XBee系列2 ZigBee模块:
```
#include <dht.h>
#include <SoftwareSerial.h>
SoftwareSerial xbee(2, 3); // XBee ZigBee模块连接到软件串口引脚2和3
dht DHT;
void setup() {
Serial.begin(9600); // 串口初始化
xbee.begin(9600); // ZigBee模块初始化
}
void loop() {
int chk = DHT.read11(4); // DHT11读取数据引脚连接到数字引脚4
float temp = DHT.temperature;
float hum = DHT.humidity;
// 如果读取成功
if (chk == DHTLIB_OK) {
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("C, Humidity: ");
Serial.print(hum);
Serial.println("%");
// 发送数据到 ZigBee 模块
xbee.print("T:");
xbee.print(temp);
xbee.print(",H:");
xbee.print(hum);
xbee.write('\n');
} else {
Serial.println("DHT11 read failed");
}
delay(2000); // 2秒后再次读取
}
```
在这个示例代码中,我们首先导入了DHT和SoftwareSerial库。然后在setup函数中初始化了串口和ZigBee模块。在loop函数中,我们使用DHT库读取DHT11传感器的温度和湿度数据,并将其发送到ZigBee模块。最后使用delay函数等待2秒再次读取数据。
需要注意的是,这只是一个示例代码,实际使用中需要根据具体的硬件连接和需求进行修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)