“温度湿度计”的版本间的差异
(→文档) |
(→软件调试) |
||
第43行: | 第43行: | ||
*打开Arduino IDE,将下列代码复制到IDE中。 | *打开Arduino IDE,将下列代码复制到IDE中。 | ||
<source lang="cpp"> | <source lang="cpp"> | ||
+ | //传感器部分================================ | ||
+ | #include <Wire.h> | ||
+ | #include "I2Cdev.h" | ||
+ | //温湿度 | ||
+ | #include <AM2321.h> | ||
+ | AM2321 am2321; | ||
+ | #include <SoftwareSerial.h> | ||
+ | |||
+ | SoftwareSerial mySerial(4, 5); // RX, TX | ||
+ | |||
+ | //#define Serial_com mySerial | ||
+ | #define Serial_com Serial1 | ||
+ | |||
+ | float sensor_tem, sensor_hum, sensor_lux, sensor_light; | ||
+ | |||
+ | String dataToSend; | ||
+ | char buf[10]; | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | |||
+ | Serial_com.begin(9600); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | //获取温湿度============================================== | ||
+ | am2321.read(); | ||
+ | sensor_tem = am2321.temperature / 10.0; | ||
+ | sensor_hum = am2321.humidity / 10.0; | ||
+ | |||
+ | sensor_light = map(analogRead(A0), 0, 1023, 0, 255); | ||
+ | |||
+ | dtostrf(sensor_tem, 1, 1, buf); | ||
+ | dataToSend = "{T:" + String(buf) + "}"; | ||
+ | Serial_com.print(dataToSend); | ||
+ | Serial.println(dataToSend); | ||
+ | delay(1000); | ||
+ | |||
+ | dtostrf(sensor_hum, 1, 1, buf); | ||
+ | dataToSend = "{H:" + String(buf) + "}"; | ||
+ | Serial_com.print(dataToSend); | ||
+ | Serial.println(dataToSend); | ||
+ | delay(1000); | ||
+ | |||
+ | dtostrf(sensor_light, 1, 1, buf); | ||
+ | dataToSend = "{L:" + String(buf) + "}"; | ||
+ | Serial_com.print(dataToSend); | ||
+ | Serial.println(dataToSend); | ||
+ | delay(1000); | ||
+ | |||
+ | dataToSend = ""; | ||
+ | } | ||
</source> | </source> | ||
* Setup 1:选择正确的板卡和COM端口,编译通过后直接下载。 | * Setup 1:选择正确的板卡和COM端口,编译通过后直接下载。 |
2016年2月11日 (四) 12:55的版本
Language | English |
---|
目的将获取到的环境数据通过手机查看。 原理通过温湿度传感器和光敏传感器将获取到的环境数据结合mCookie-BT蓝牙传送到手机上。 设备
文档Android客户端:
注意:新版BT是蓝牙4.0,需要Android 4.3以上的系统才能支持。这里只提供Android的APP。
准备
软件调试
//传感器部分================================
#include <Wire.h>
#include "I2Cdev.h"
//温湿度
#include <AM2321.h>
AM2321 am2321;
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 5); // RX, TX
//#define Serial_com mySerial
#define Serial_com Serial1
float sensor_tem, sensor_hum, sensor_lux, sensor_light;
String dataToSend;
char buf[10];
void setup() {
Serial.begin(9600);
Serial_com.begin(9600);
}
void loop() {
//获取温湿度==============================================
am2321.read();
sensor_tem = am2321.temperature / 10.0;
sensor_hum = am2321.humidity / 10.0;
sensor_light = map(analogRead(A0), 0, 1023, 0, 255);
dtostrf(sensor_tem, 1, 1, buf);
dataToSend = "{T:" + String(buf) + "}";
Serial_com.print(dataToSend);
Serial.println(dataToSend);
delay(1000);
dtostrf(sensor_hum, 1, 1, buf);
dataToSend = "{H:" + String(buf) + "}";
Serial_com.print(dataToSend);
Serial.println(dataToSend);
delay(1000);
dtostrf(sensor_light, 1, 1, buf);
dataToSend = "{L:" + String(buf) + "}";
Serial_com.print(dataToSend);
Serial.println(dataToSend);
delay(1000);
dataToSend = "";
}
软件说明
#define my_Serial Serial1
dtostrf(sensor_tem, 1, 1, buf);
dataToSend = "{T:" + String(buf) + "}";
Serial_com.print(dataToSend);
Serial.println(dataToSend);
delay(1000);
视频 |