J-M-L:
In the current code Move
float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity
After the read22 otherwise the values are not up to date
If you want to use the other library, install it in the arduino IDE first
You would have to modify a bit your code, add the include for the right library
#include "DHT.h"
#define DHTPIN 8 // what digital pin we're connected to
#define DHTTYPE DHT22
Create your object to handle your sensors as a global
Create the DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
And initialize your object in the setup()
void setup() {
Serial.begin(115200);
// ... Your other initialization
dht.begin(); // get it going
}
Then in the loop you keep,globally the same structure but when it's time to read you do something like
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
} else {
// all ok. Put your code here to write the SD etc
}
As mentioned;
my code so far:
//Humidity Sensor-Arduino: (Left to Right) 1-5v, 2-Data (see below), 4-GND (10k Pullup resistor between 1 and 3)
//RTC-Arduino: GND-GND, VCC-3.3v, SDA-A4, SCL-A5
//SD Reader-Arduino: CS-4, SCK(Clock)-13, MISO-12, MOSI-11, VCC-3.3/5v, GND-GND
//DHT22=Humidity/Tempurature Sensor, DS3231=RTC
#include "DHT.h"
#include <DS3231.h>
#include <SD.h>
#define DHTPIN 8 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.
DS3231 rtc(A4, A5); // Init the DS3231 using the hardware interface
Time t; // Init a Time-data structure
const int chipSelect = 4;
float refresh_rate = 5000; //change this to increase or decrease the time between samples
unsigned long lastUpdate = 0; // Keep track of last update time
void setup() {
Serial.begin(115200);
dht.begin(); //Begin Humidity Sensor
rtc.begin(); //Begin RTC
Serial.println("DHTxx test!");
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT); // make sure that the default chip select pin is set to output, even if you don't use it:
if (!SD.begin(chipSelect)) { // see if the card is present and can be initialized:
Serial.println("Card failed, or not present"); // don't do anything more:
return;
}
Serial.println("card initialized.");
//The following lines can be uncommented to set the date and time
//rtc.setDOW(WEDNESDAY); // Set Day-of-Week
//rtc.setTime(20, 58, 00); // Set the time (00:00:00 24hr format)
//rtc.setDate(07, 22, 2016); // Set the date (January 1st, 2014)
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity(); //Read Humidity
float t = dht.readTemperature(); // Read temperature as Celsius (the default)
char *strTime; // pointer to a time string
strTime = rtc.getTimeStr(FORMAT_LONG); // Get formatted Time
// Check if any reads failed
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
} else {
// all ok. Put your code here to write the SD etc
if (millis() > (lastUpdate + refresh_rate))
{
Serial.print(" Time = ");
Serial.print(strTime);
Serial.print(" Temperature = ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(" Humidity = ");
Serial.print(h);
Serial.println(" % ");
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.print(strTime);
dataFile.print(" - ");
dataFile.print(t);
dataFile.print(" *C");
dataFile.print(" - ");
dataFile.print(h);
dataFile.println(" %");
dataFile.close();
lastUpdate = millis();
}
else {
Serial.println("error opening datalog.txt");
}
}
//float hif = dht.computeHeatIndex(f, h); // Compute heat index in Fahrenheit (the default)
//float hic = dht.computeHeatIndex(t, h, false); // Compute heat index in Celsius (isFahreheit = false)
}
}
I really appreciate your help! if you have any pointers to improve how ive done things that would be awesome! thanks again