DHT22 messen und auf SD-Karte schreiben
#include "DHT.h"
#include <SD.h>
 
#define DHTPIN 9     
#define DHTTYPE DHT22 //DHT11, DHT21, DHT22
 
DHT dht(DHTPIN, DHTTYPE);
const int chipSelect = 4;
 
void setup() 
{
  Serial.begin(9600); 
  Serial.println("DHT22 - Test!");
 
  dht.begin();
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
  
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}
 
void loop() 
{
  float h = dht.readHumidity();     //Luftfeuchte auslesen
  float t = dht.readTemperature();  //Temperatur auslesen
 
  if (isnan(t) || isnan(h)) 
  {
    Serial.println("DHT22 konnte nicht ausgelesen werden");
  } 
  else
  {
   
  }
  String dataString = "";
  char datenFeuchtigkeit [10];
  char datenTemperatur [10];
  
  dtostrf(h,5,2,datenFeuchtigkeit);
  dtostrf(t,5,2,datenTemperatur);
  
  dataString = dataString + String(millis());
  dataString = dataString + ";";
  dataString = dataString + datenFeuchtigkeit;
  dataString = dataString + ";";
  dataString = dataString + datenTemperatur;
  File dataFile = SD.open("Messung.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }  
  else {
    Serial.println("error opening datalog.txt");
  }
  delay (1000);
}