本章节我们将介绍如何实现将Ardurion收集的传感器数据存入sd卡中(火焰传感器、温湿度传感器为例)
准备器材:
Arduino开发板一块
杜邦线若干
温度传感器、火焰传感器
SD卡模块及一张SD卡
火焰传感器实例代码
#include <SPI.h>
#include <SD.h>
#include<time.h>
int fireSensor = A0;
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(fireSensor,INPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// re-open the file for reading:
myFile = SD.open("demo.txt");
/* if (myFile) {
Serial.println("zk.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
***/
}
void loop() {
String dataString="";
Serial.println(analogRead(fireSensor));
int sensor = analogRead(fireSensor);
dataString += String(sensor);
dataString += ",";
myFile = SD.open("demo.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(dataString);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
delay(5000);//延时5s
}
温湿度传感器实例代码
#include <DHT11.h>
#include <SPI.h>
#include <SD.h>
#include<time.h>
dht11 DHT11;
File myFile;
#define DHT11PIN A0
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
myFile = SD.open("zk.txt");
}
void loop()
{
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
//湿度读取
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
//温度读取
String dataString="";
int Humidity = DHT11.humidity;
int Temperature = DHT11.temperature;
dataString += String(Humidity);
dataString += ".00";
dataString += ",";
dataString += String(Temperature);
dataString += ".00";
myFile = SD.open("demo.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(dataString);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
delay(5000);
}
将代码烧写到Arduino开发板,如果顺利传感器手机的数据成功写入SD卡,我们可以使用读卡器查看SD卡中的内容,如下图:
打开demo.txt对传感器收集数据进行分析处理