0

I have the code to pass the data from dht22 to sd but for only one and i want to pass the data from 1 dht22 with one file and the data from other sensor to other file and logging in to sd. Anyone can help me?

#include
#include
#include "DHT.h"
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
File data_file;

int CS_pin = 10;
long sec=00;
long mint=00;
long hr=00;

void setup() {
Serial.begin(9600);
pinMode(CS_pin, OUTPUT);
dht.begin(); //This command will start to receive the values from dht22
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is initialized and it is ready to use");
} else
{
Serial.println("SD card is not initialized");
return;
}
}

void loop() {
data_file = SD.open("data.txt", FILE_WRITE);
if (data_file) {
getData();
}
// if the file didn't open, print an error:
else {
Serial.println("error opening data.txt");
}
delay(1000);
}

void getData(){
for(long sec = 00; sec < 60; sec=sec+2){
float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
data_file.print(hr);
data_file.print(":");
data_file.print(mint);
data_file.print(":");
data_file.print(sec);
data_file.print(", ");
data_file.println(temp);
Serial.print(hr);
Serial.print(":");
Serial.print(mint);
Serial.print(":");
Serial.print(sec);
Serial.print(", ");
Serial.println(temp);
if(sec>=58){
mint= mint + 1;
}
if (mint>59){
hr = hr + 1;
mint = 0;
}
data_file.flush(); //saving the file
delay(2000);
}
data_file.close(); //closing the file
}

Thanks

4
  • 1
    It isn’t clear what you’re asking. You want to collect data from 2 sensors? What 2 files are you referring to? You have 2 source files? How are they structured? Why do you need 2 source files? Why not write a function that collects data from 2 sensors?
    – Duncan C
    Commented Feb 9, 2019 at 15:10
  • To me it was pretty clear, what the question was. And OP also acknowledged that in his comment below my answer...
    – oh.dae.su
    Commented Feb 11, 2019 at 14:13
  • @oh.dae.su you're a better man (or woman) than I. I took it to mean 2 different sketch files but outputting the data to 2 different files on the SD card makes a lot more sense.
    – Duncan C
    Commented Feb 16, 2019 at 16:55
  • @Duncan C my comment was not intended as a direct answer to you, but in general in response to the question being put on-hold as unclear... ;-)
    – oh.dae.su
    Commented Feb 16, 2019 at 18:24

1 Answer 1

1

First you need to setup the second DHT sensor and a second file. Please replace the X by the number of the pin your second sensor is connected to after #define DHTPIN2

#define DHTPIN 8
#define DHTPIN2 X
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
File data_file, data_file2;

In setup() you need to also start the second dht sensor:

dht.begin(); //This command will start to receive the values from dht22
dht2.begin(); //This command will start to receive the values from dht22

In loop() you have to open the second data file in addition to the first one:

data_file = SD.open("data.txt", FILE_WRITE);
if (data_file) {
getData();
}
// if the file didn't open, print an error:
else {
Serial.println("error opening data.txt");
}
data_file2 = SD.open("data2.txt", FILE_WRITE);
if (data_file2) {
getData2();
}
// if the file didn't open, print an error:
else {
Serial.println("error opening data2.txt");
}

Although not the most efficient/elegant solution, this requires you to duplicate your void getData() function as void getData2() to work for the second sensor/file, by replacing dht by dht2 and data_file with data_file2. This would do the logging on the two sensors in sequence.
If you want the logging in parallel you need to add the measurement and writing of the second sensor/file into the void getData() function.

4
  • Thanks for your help. Now finally i can do it ;)
    – Some One
    Commented Feb 10, 2019 at 13:34
  • You are welcome. Glad I could help. Btw. please read the stack exchange tour page arduino.stackexchange.com/tour to learn, how things work out on this site, e.g. how to deal with answers to your questions.
    – oh.dae.su
    Commented Feb 10, 2019 at 17:48
  • I don't know why but the program now say File does not name a type.
    – Some One
    Commented Feb 16, 2019 at 15:58
  • @jawad bouhali please either ask a separate question including the code and the exact error message or edit your question and append this information to it.
    – oh.dae.su
    Commented Feb 17, 2019 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.