Hi....I wanted to store data on the SD card from DHT 11 and NEO 6M GPS module sensors. The problem is that the code I am using shows proper values in the serial monitor for all the parameters (LAT, LONG, Date, Time, Temperature, Humidity) but only LAT and LONG values get stored in the SD card.
The Temperature and humidity values are stored as 0.00. The data and time values are stored as nothing ( completely blank). The data gets stored in SD in a proper manner when SD card and GPS is used. It is also working properly when DHT sensor and SD card are used. But when the three of them are used combinedly the problem arises while storing the data only. I am attaching the code for the reference.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <dht.h> // Include library
#define outPin 7 // Defines pin number to which the sensor is connected
dht DHT; // Creates a DHT object
#include <SD.h>
#include <SPI.h>
const int chipSelect = 4;
// Choose two Arduino pins to use for software serial
int RXPin = 3;
int TXPin = 2;
int GPSBaud = 9600;
// Create a TinyGPS++ object
TinyGPSPlus gps;
// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);
//long LAT, LONG;
float LAT,LONG;
float t, t1, h1;
String my_Str, my_SStr;
void setup()
{
// Start the Arduino hardware serial port at 9600 baud
Serial.begin(9600);
// Start the software serial port at the GPS's default baud
gpsSerial.begin(GPSBaud);
Initialize_SDcard();
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
while (gpsSerial.available() > 0)
if (gps.encode(gpsSerial.read()))
displayInfo();
//Write_SDcard();
// If 5000 milliseconds pass and there are no characters coming in
// over the software serial port, show a "No GPS detected" error
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
while(true);
}
}
void displayInfo()
{
int h, m,s,c, Day,Month, Year;
String my_Str; String my_Str1; String my_Str2; String my_Str3; String my_Str4;
String my_SStr; String my_SStr1; String my_SStr2; String my_SStr3; String my_SStr4; String my_SStr5;
int readData = DHT.read11(outPin);
float t = DHT.temperature; // Read temperature
float t1; // read temperature in Fahrenheit
float h1 = DHT.humidity; // Read humidity
{
if (gps.location.isValid())
{
Serial.print("Latitude: ");
//Serial.println(gps.location.lat(), 6);
LAT = gps.location.lat();
Serial.println(LAT);
Serial.print("Longitude: ");
//Serial.println(gps.location.lng(), 6);
LONG = gps.location.lng();
Serial.println(LONG);
Serial.print("Altitude: ");
Serial.println(gps.altitude.meters());
Serial.print("Speed in m/s = ");
Serial.println(gps.speed.mps());
//Serial.print("Number of satellites in use = ");
//Serial.println(gps.satellites.value());
}
else
{
Serial.println("Location: Not Available");
}
//Serial.print("Date: ");
if (gps.date.isValid())
{
Day=(gps.date.day());
my_Str1=String(Day);
Month=(gps.date.month());
my_Str2=String(Month);
Year=(gps.date.year());
my_Str3=String(Year);
my_Str4=String("/");
my_Str = my_Str1+ my_Str4 + my_Str2 + my_Str4 + my_Str3;
Serial.print("Date: ");
Serial.println(my_Str);
}
else
{
Serial.println("Not Available");
}
//Serial.print("Time(IST): ");
if (gps.time.isValid())
{
if (gps.time.hour() < 10) ;
h = gps.time.hour()+5;
my_SStr1=String(h);
//if (h < 10) Serial.print(F("0"));
//Serial.print(h);
//Serial.print(":");
if (gps.time.minute() < 10) ;
m = gps.time.minute()+30;
my_SStr2=String(m);
if (gps.time.second() < 10);
s= gps.time.second();
my_SStr3=String(s);
if (gps.time.centisecond() < 10);
c = gps.time.centisecond();
my_SStr4=String(c);
my_SStr5=String(":");
my_SStr = my_SStr1+ my_SStr5 + my_SStr2 + my_SStr5 + my_SStr3 + my_SStr5 + my_SStr4;
Serial.print("Time (IST): ");
Serial.println(my_SStr);
}
else
{
Serial.println("Not Available");
}
delay(1500);
}
Serial.print("Temperature = ");
Serial.print(t);
Serial.print("°C | ");
t1=((t*9.0)/5.0+32.0); // Convert celsius to fahrenheit
Serial.print(t1);
Serial.println("°F ");
Serial.print("Humidity = ");
Serial.print(h1);
Serial.println("% ");
Serial.print("\n");
Write_SDcard();
Serial.println();
Serial.println();
delay(8000);
}
void Initialize_SDcard()
{
// 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;
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println("Date,Time, LATITUDE, LONGITUDE,Temperature in Cel, Temperature in F, Air Humidity"); //Write the first row of the excel file
dataFile.close();
}
}
void Write_SDcard()
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(my_Str); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(my_SStr); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(LAT); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(LONG); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(t); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(t1); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(h1); //Store date on SD card
//dataFile.print(","); //Move to next column using a ","
dataFile.println(); //End of Row move to next row
dataFile.close(); //Close the file
}
else
Serial.println("OOPS!! SD card writing failed");
}```