用arduino采集温度信号通过蓝牙模块发回到手机串口
本帖最后由 维博哥哥 于 2014-11-13 09:31 编辑实验使用温度传感器DHT11蓝牙模块HC-05,手机使用安卓spp蓝牙串口,arduino用的是arduino due#define DHT_PIN 50
#define VCC 48
#define GND 52
int led = 13;
byte ERROR;
byte dht_dat;
void setup() {
// put your setup code here, to run once:
pinMode(DHT_PIN,OUTPUT);
pinMode(VCC,OUTPUT);
pinMode(GND,OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(DHT_PIN,HIGH);
digitalWrite(VCC,HIGH);
digitalWrite(GND,LOW);
Serial.begin(9600);
Serial3.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial3.available()){
char c=Serial3.read();
if(c=='A'){
ReadDHT();
switch (ERROR){
case 0:
Serial.print("Current humidity=");
Serial.print(dht_dat,DEC);
Serial.print(".");
Serial.print(dht_dat,DEC);
Serial.print("% ");
Serial.print("temperature=");
Serial.print(dht_dat,DEC);
Serial.print(".");
Serial.print(dht_dat,DEC);
Serial.print("C ");
Serial.print("\n");
Serial3.print("Current humidity=");
Serial3.print(dht_dat,DEC);
Serial3.print(".");
Serial3.print(dht_dat,DEC);
Serial3.print("% ");
Serial3.print("\n");
Serial3.print("temperature=");
Serial3.print(dht_dat,DEC);
Serial3.print(".");
Serial3.print(dht_dat,DEC);
Serial3.print("C ");
Serial3.print("\n");
break;
case 1:
Serial.println("Error 1:DHT start condition 1 not met.");
break;
case 2:
Serial.println("Error 2:DHT start condition 2 not met.");
break;
case 3:
Serial.println("Error 3:DHT checksum error.");
break;
default:
Serial.println("Error :Unrecognized code encountered.");
break;
}
delay(800);
}
}
}
void ReadDHT(){
ERROR=0;
/*for(int i=0;i<10;i++){
digitalWrite(led,0);
delay(1000);
digitalWrite(led,1);
delay(1000);
}*/
byte dht_in;
byte i;
digitalWrite(DHT_PIN,LOW);
delay(20);
digitalWrite(DHT_PIN,HIGH);
delayMicroseconds(40);
pinMode(DHT_PIN,INPUT);
dht_in=digitalRead(DHT_PIN);
if(dht_in){
ERROR=1;
return;
}
delayMicroseconds(80);
dht_in=digitalRead(DHT_PIN);
if(!dht_in){
ERROR=2;
return;
}
delayMicroseconds(80);
for(i=0;i<5;i++)
dht_dat=read_dht_dat();
pinMode(DHT_PIN,OUTPUT);
digitalWrite(DHT_PIN,HIGH);
byte dht_check_sum=
dht_dat+dht_dat+dht_dat+dht_dat;
if(dht_dat!=dht_check_sum)
{
ERROR=3;
}
}
byte read_dht_dat(){
byte i=0;
byte result=0;
for(i=0;i<8;i++){
while(digitalRead(DHT_PIN)==LOW);
delayMicroseconds(30);
if(digitalRead(DHT_PIN)==HIGH)
result |=(1<<(7-i));
while(digitalRead(DHT_PIN)==HIGH);
}
return result;
}
打开串口工具,然后手机发送A,手机和串口窗口都能同时收到温湿度信息 我想请问一下你用的这个器材如果是其他的话可以实现吗?是arduino板还有其他的温度传感器可以实现吗??
我用的是arduino due 其它温度传感器当然也可以 Toooo 发表于 2014-11-28 08:45 static/image/common/back.gif
我想请问一下你用的这个器材如果是其他的话可以实现吗?是arduino板还有其他的温度传感器可以实现吗??
可以啊,其他arduino板子也可以 #define VCC 48
#define GND 52
不是有直接的电源地吗?为什么要这样? suoma 发表于 2017-5-30 15:25
不是有直接的电源地吗?为什么要这样?
可以自己启动一下 感谢楼主分享,小白学习一下。 请问楼主代码里面的笑脸是什么意思?:dizzy::dizzy:新人求问,认真的 请问楼主有hc-05和板子的连线图吗,想学习一下,
还有就是这个蓝牙模块用之前要设定主从模式吗?因为我也在做跟楼主一样的要传回温度数据,但是太小白了,不懂得太多,请楼主多多指教,谢谢啦 纯小小白 发表于 2017-6-6 19:58
请问楼主代码里面的笑脸是什么意思?新人求问,认真的
笑脸,字符显示出来不一样 纯小小白 发表于 2017-6-19 15:37
请问楼主有hc-05和板子的连线图吗,想学习一下,
还有就是这个蓝牙模块用之前要设定主从模式吗?因为我也 ...
不需要设置主从模式
楼主,代码编译不过啊,出现 'Serial3' was not declared in this scope。怎么回事?
页:
[1]