Created
August 4, 2014 11:22
-
-
Save chareice/57a7cec672f1a3d219cd to your computer and use it in GitHub Desktop.
Arduino温度湿度 光照 声音数据采集
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Bridge.h> | |
#include <FileIO.h> | |
#include "DHT.h" | |
#define DHTPIN A0 | |
#define DHTTYPE DHT11 | |
#define SOUND A1 | |
#define LIGHT A2 | |
DHT dht(DHTPIN, DHTTYPE); | |
String getTempAndHum(); | |
String getSound(); | |
String getLight(); | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
Bridge.begin(); | |
dht.begin(); | |
while (!Serial); | |
Serial.println("Filesystem datalogger\n"); | |
FileSystem.begin(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
String logger = ""; | |
logger += getTempAndHum(); | |
logger += " "; | |
logger += getSound(); | |
logger += " "; | |
logger += getLight(); | |
File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND); | |
if (dataFile) { | |
dataFile.println(logger); | |
dataFile.close(); | |
// print to the serial port too: | |
} else { | |
Serial.println("file error"); | |
} | |
Serial.println(logger); | |
delay(500); | |
} | |
String getTempAndHum() { | |
String result; | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
// check if returns are valid, if they are NaN (not a number) then something went wrong! | |
if (isnan(t) || isnan(h)) | |
{ | |
result = "Failed to read from DHT" ; | |
} | |
else | |
{ | |
result = "Hum:"; | |
result += String(h); | |
result += " Temp:"; | |
result += String(t); | |
} | |
return result; | |
} | |
String getSound() { | |
String result = "Sound:"; | |
int sound = 0; | |
sound = analogRead(SOUND); | |
result += String(sound); | |
return result; | |
} | |
String getLight() { | |
String result = "Light:"; | |
int light = 0; | |
light = analogRead(LIGHT); | |
result += String(light); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment