Humidity meter - sketch


    This is the arduino sketch for the humidity meter. It will read the DHT sensor every 30 seconds and save the temperature and humidity to the sd card. It also outputs the data (and some helpful errors) to the serial monitor. I've only bolted together code from three people (Tom Igoe, Lady Ada and Matt G) which was designed for other jobs. I'm sure there is a nicer way of doing this, but this way works well enough for the moment.

/*
  Humidity logger - Craig Manning
  based on:
  SD card datalogger by Tom Igoe,
  DHT reading by ladyada,
  temp to ascii Matt G
*/

char* tempToAscii(double temp)
{
  char ascii[32];
  int frac;
  frac=(unsigned int)(temp*1000)%1000;  //get three numbers to the right of the deciaml point

  itoa((int)temp,ascii,10);
  strcat(ascii,".");
  itoa(frac,&ascii[strlen(ascii)],10); //put the frac after the deciaml

  return ascii;
}


 /*
 The circuit:
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
  ** DHT - pin 2
 */

#include <SD.h>
#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;


void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  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)) {
    Serial.println("Failed to read from DHT");
  } else {
  dataString += (tempToAscii(h));
  dataString += ",";
  dataString += (tempToAscii(t));
  }


// open the file.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
// print to the serial port too:
    Serial.println(dataString);
  }
// if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
//Tried 30000 (30 seconds) first but data/time gave readings every 30.5s so this should reduce the time error
  delay(29500);
}

Comments

Popular Posts