Hi, I have an SD card, and the GPS (NEO 6M GPS module)data gets stored in it when GPS is used as a standalone sensor. But when I try to insert DHT 11 sensor ( for temperature and humidity) it shows "error opening GPS_data.txt" and nothing gets stored in the SD card. Only a blank txt file named GPS_data.txt gets created. How to solve this issue? I am attaching the entire code for reference.
#include "SD.h"
#include <SPI.h>
#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
File myFile;
int RXPin = 3;
int TXPin = 2;
TinyGPSPlus gps;
SoftwareSerial SerialGPS(RXPin, TXPin);
String Latitude, Longitude, Altitude, day, month, year, hour, minute, second, Date, Time,Temperaturee,Temperatureee ,Humidity,my_Str,Data;
float t = DHT.temperature; // Read temperature
float t1; // read temperature in Fahrenheit
float h1 = DHT.humidity; // Read humidity
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
Serial.println("Creating GPS_data.txt...");
myFile = SD.open("GPS_data.txt", FILE_WRITE);
if (myFile) {
myFile.println( "Latitude, Longitude, Altitude, Date , Time (UTC), Temperature in C, Temperature in F , Humidity in % \r\n"); //Write the first row of the excel file
myFile.close();
} else {
Serial.println("error opening GPS_data.txt");
}
SerialGPS.begin(9600);
}
void loop() {
while (SerialGPS.available() > 0)
if (gps.encode(SerialGPS.read()))
obtain_data();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("GPS NOT DETECTED!");
while(true);
}
}
void obtain_data()
{
my_Str=String(",");
if (gps.location.isValid())
{
Latitude = gps.location.lat();
Longitude = gps.location.lng();
Altitude = gps.altitude.meters();
}
else
{
Serial.println("Location is not available");
}
if (gps.date.isValid())
{
month = gps.date.month();
day = gps.date.day();
year = gps.date.year();
Date = day + "-" + month + "-" + year;
Serial.println(Date);
}
else
{
Serial.print("Date Not Available");
}
Serial.print("Time (UTC): ");
if (gps.time.isValid())
{
if (gps.time.hour() < 10) ;
hour = gps.time.hour();
if (gps.time.minute() < 10);
minute = gps.time.minute();
if (gps.time.second() < 10) ;
second = gps.time.second();
Time = hour + ":" + minute + ":" + second;
Serial.println(Time);
}
else
{
Serial.print("Time Not Available");
}
{
Serial.print("Tttemperature = ");
Temperaturee = String(t);
Serial.print(Temperaturee);
Serial.print("°C | ");
t1=((t*9.0)/5.0+32.0); // Convert celsius to fahrenheit
Temperatureee = String(t1);
Serial.print(Temperatureee);
Serial.println("°F ");
Serial.print("humidity = ");
Humidity= String(h1);
Serial.print(Humidity);
Serial.println("% ");
Serial.print("\n");
}
Data = Latitude + my_Str + Longitude + my_Str + Altitude + my_Str + Date + my_Str + Time + my_Str +Temperaturee + my_Str +Temperatureee + my_Str + Humidity;
//Data = Latitude + "," + Longitude + "," + Altitude + "," + Date + "," + Time;
Serial.print("Save data: ");
Serial.println(Data);
myFile = SD.open("GPS_data.txt", FILE_WRITE);
if (myFile) {
Serial.print("GPS logging to GPS_data.txt...");
myFile.println(Data);
myFile.close();
Serial.println("done.");
} else {
Serial.println("error opening GPS_data.txt");
}
Serial.println();
delay(3000);
}
Verify your DHT is on pin 7 (and SD CS is on pin 4).
[edit]
I wonder if the SD is having power issues... either too little or too much... with the DHT in the circuit. Have you tried commenting-out all the DHT code (but DHT still getting power, but not connected to the Arduino pin 7)?
[/edit]
DHT is on Pin 7 and SD CS is at pin 4. But the problem arises when GPS has been connected then everything goes for a toss regarding SD card. Otherwise without GPS , I have collected and recorded data to SD card from three sensors ( DHT11, Soil moisture sensor, and another waterproof temperature sensor) simultaneously. Everything has been done withut any hassle. But this GPS becomes headache. In the serial monitor everything is fine ( GPS along with DHT sensor gives output ). I will implement your suggestion in the morning in my lab
so, you say now that DHT was first and GPS messed everything up.
now i understand, in sketch every variable about DHT is casted properly,
String x = String (float)
lcd.print(x)
but no one single variable is casted by obtaining from GPS,
String Latitude = gps.location.lat();
i think any of this strings is empty and
Data = Latitude + my_Str + Longitude + my_Str + Altitude + my_Str + Date + my_Str + Time + my_Str +Temperaturee + my_Str +Temperatureee + my_Str + Humidity;
Serial.println(Data);
myFile.println(Data);
stores nothing
if i am right then sd contains many empty lines
i changed my_Str variable from space to comma, now empty lines would be ",,,,,,,," and visible
The format is a direct copy/paste, so yes, as I described. The commas will need to be worked. No comma at the record start, but past the start, the next record will need a comma... or the CSV will be a mess.
i misplaced data and date
whatever. for me is only important what is in the SD Card file. what was and what is now.
if there ",,,,,,," then casting is mandatory.
I formatted the SD card and modified the code a little bit but it now says "initialization failed!". I don't get what happened now. I am attaching the modified code for reference.
#include "SD.h"
#include <SPI.h>
#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
File myFile;
int RXPin = 3;
int TXPin = 2;
TinyGPSPlus gps;
SoftwareSerial SerialGPS(RXPin, TXPin);
String Latitude, Longitude, Altitude, day, month, year, hour, minute, second, Date, Time, Temperaturee, Temperatureee , Humidity, my_Str, Data;
float LAT, LONG, ALT;
float t = DHT.temperature; // Read temperature
float t1; // read temperature in Fahrenheit
float h1 = DHT.humidity; // Read humidity
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
Serial.println("Creating GPS_data.txt...");
myFile = SD.open("GPS_data.txt", FILE_WRITE);
if (myFile) {
myFile.println( "Latitude, Longitude, Altitude, Date , Time (UTC), Temperature in C, Temperature in F , Humidity in % \r\n"); //Write the first row of the excel file
myFile.close();
} else {
Serial.println("error opening GPS_data.txt");
}
SerialGPS.begin(9600);
}
void loop() {
while (SerialGPS.available() > 0)
if (gps.encode(SerialGPS.read()))
obtain_data();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("GPS NOT DETECTED!");
while (true);
}
}
void obtain_data() {
my_Str = String(",");
if (gps.location.isValid()) {
LAT = gps.location.lat();
Latitude = String(LAT);
LONG = gps.location.lng();
Longitude = String(LONG);
ALT = gps.altitude.meters();
Altitude = String(ALT);
}
else Serial.println("Location is not available");
if (gps.date.isValid()) {
month = gps.date.month();
day = gps.date.day();
year = gps.date.year();
Date = my_Str + day + "-" + month + "-" + year;
Serial.println(Date);
}
else Serial.print("Date Not Available");
Serial.print("Time (UTC): ");
if (gps.time.isValid()) {
if (gps.time.hour() < 10) ;
hour = gps.time.hour();
if (gps.time.minute() < 10);
minute = gps.time.minute();
if (gps.time.second() < 10) ;
second = gps.time.second();
Time = hour + ":" + minute + ":" + second;
Serial.println(Time);
}
else Serial.print("Time Not Available");
Serial.print("Tttemperature = ");
Temperaturee = String(t);
Serial.print(Temperaturee);
Serial.print("°C | ");
t1 = ((t * 9.0) / 5.0 + 32.0); // Convert celsius to fahrenheit
Temperatureee = String(t1);
Serial.print(Temperatureee);
Serial.println("°F ");
Serial.print("humidity = ");
Humidity = String(h1);
Serial.print(Humidity);
Serial.println("% ");
Serial.print("\n");
Data = Latitude + my_Str + Longitude + my_Str + Altitude + my_Str + Date + my_Str + Time + my_Str + Temperaturee + my_Str + Temperatureee + my_Str + Humidity;
//Data = Latitude + "," + Longitude + "," + Altitude + "," + Date + "," + Time;
Serial.print("Save data: ");
Serial.println(Data);
myFile = SD.open("GPS_data.txt", FILE_WRITE);
if (myFile) {
Serial.print("GPS logging to GPS_data.txt...");
myFile.println(Data);
myFile.close();
Serial.println("done.");
} else {
Serial.println("error opening GPS_data.txt");
}
Serial.println();
delay(3000);
}
Can you guide me that" how to use secure disconnect function of operating system"? SD card is alright and it is storing the data ( like music files and other stuffs). But not working in my progam.
This process I always follow while ejecting external storage devices. But here the problem is with the SD card things are all right ( in context of my program writing) before formatting. But after formatting SD card initialization failed. But SD card can be used for other purposes like storing music videos , movies etc.
In the generated file another .txt has been stored as one of my colleagues used that SD card so before using that I formatted the SD card as I am seeing in some internet articles that it is better to format SD card before a new use. So I did accordingly and after that, the problem arises.
As far as code modification is concerned, I tried to modify this ( incontext to my previous code)
if (gps.location.isValid()) {
LAT = gps.location.lat();
Latitude = String(LAT);
LONG = gps.location.lng();
Longitude = String(LONG);
ALT = gps.altitude.meters();
Altitude = String(ALT);
}