-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy path6.2.3-THRecorder.ino
62 lines (55 loc) · 1.25 KB
/
6.2.3-THRecorder.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
OpenJumper Example
环境记录器
奈何col 2013.3.14
www.openjumper.com
*/
#include <SD.h>
#include <dht11.h>
dht11 DHT11;
#define DHT11_PIN 2 // DHT11引脚
#define LIGHT_PIN A0// 光敏引脚
const int chipSelect = 4;// TF卡CS选择引脚
void setup()
{
// 初始化串口
Serial.begin(9600);
//将SS引脚设置为输出状态,UNO为10号引脚
pinMode(10, OUTPUT);
// 初始化SD卡
Serial.println("Initializing SD card");
if (!SD.begin(chipSelect))
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop()
{
// 读取DHT11的数据
Serial.println("Read data from DHT11");
DHT11.read(DHT11_PIN);
// 读取光敏模块数据
Serial.println("Read data from Light Sensor");
int light=analogRead(LIGHT_PIN);
// 打开文件并将DHT11检测到的数据写入文件
Serial.println("Open file and write data");
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{
dataFile.print(DHT11.humidity);
dataFile.print(",");
dataFile.print(DHT11.temperature);
dataFile.print(",");
dataFile.println(light);
dataFile.close();
}
else
{
Serial.println("error opening datalog.txt");
}
//延时一分钟
Serial.println("Wait for next loop");
delay(60000);
}