Dear all,
I am reading data from a sensor and sending the data to readings.csv file on the SD card.
For the clock I am using the library DS3231 from this website:
http://www.rinkydinkelectronics.com/library.php?id=73
For the SD card I am using the standard SD.h library in the IDE.
I read the documentation of the RTC and I have 2 difficulties:
- I am not able to get separately the value of year, month, day, hour, minutes, sec but I can get them all together like 08:43:40 or 11.01.20
- The character ("\t") is not recognised. I mean, I am trying to use that in order to write to the next cell on CSV file horizontally.
I am able to write to the following line trough println instruction and it works fine.
Any help or suggestions will be really appreciated.
The part of the code of interest is:
// If the file opened OK, write to it on SD card
lcd.clear();
if (readings)
{
lcd.print("file OPEN");
delay(1000);
readings.print(rtc.getMonthStr());
readings.print("\t");
readings.print(rtc.getTimeStr());
readings.print("\t");
readings.print(rtc.getDateStr());
//readings.print(\t);
//readings.print(h);
//readings.print(\t);
//readings.print(m);
//readings.print(\t);
//readings.print(s);
//readings.print(\t);
readings.print(t);
readings.print("\t"); // Prints a TAB
readings.println(h); // Prints the value specified and RETURN
// close the file:
readings.close();
lcd.clear();
lcd.println("file CLOSE");
delay(1000);
}
else
{
// if the file didn't open, print an error:
lcd.println("file not open");
delay(4000);
}
The full code is:
// Libraries
#include <dht.h> // Includes the libraries for the sensor
#include <LiquidCrystal.h> // Includes the libraries for the LCD
#include <DS3231.h>; // Includes the libraries for the clock
#include <SPI.h>; // Includes the libraries for the Serial Peripheral Interface that allows the Master (Arduino) to control a slave device
#include <SD.h>; // Includes the libraries for the SD card
// Interface and Objects definitions
DS3231 rtc(SDA, SCL); // Initialises the DS3231 using the hardware interface
#define dataPin 2 // Defines pin number to which the sensor is connected (avoid D4-9 and A0 used by LCD)
dht DHT; // Creates a DHT object
LiquidCrystal lcd(8,9,4,5,6,7); // Creates a LCD LiquidCrystal object
const int chipSelect = 10; // Assigns the pin for the SPI-SD chip selection
void setup()
{
pinMode(chipSelect, OUTPUT); // Ensures that the SPI-SD selection pin is an output
rtc.begin(); // Initialises the rtc object
// rtc.setDOW(TUESDAY); // Sets Day-of-Week to TUESDAY
// rtc.setTime(15, 46, 05); // Sets the time to 12:00:00 (24hr format)
// rtc.setDate(10, 1, 2017); // Sets the date to January 1st, 2017
lcd.begin(16, 2); // Initialises the interface to the LCD screen, and specifies the dimensions
lcd.print("Initialising SD");
delay (2000);
lcd.clear();
if (!SD.begin(chipSelect))
{
lcd.println("SD failed!");
return;
}
lcd.println("SD OK");
delay(2000);
lcd.clear();
lcd.print("Reading sensor"); // Prints to the LCD
delay (4000); // Waits 4 seconds
}
void loop()
{
// Open the file on the SD card
delay(2000);
File readings = SD.open("readings.txt", FILE_WRITE);
// Reading the data
int readData = DHT.read22(dataPin); // Reads the data from the sensor
float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t))
{
lcd.println("DHT FAIL!");
return;
}
// If the file opened OK, write to it on SD card
lcd.clear();
if (readings)
{
lcd.print("file OPEN");
delay(1000);
readings.print(rtc.getMonthStr());
readings.print("\t");
readings.print(rtc.getTimeStr());
readings.print("\t");
readings.print(rtc.getDateStr());
//readings.print(\t);
//readings.print(h);
//readings.print(\t);
//readings.print(m);
//readings.print(\t);
//readings.print(s);
//readings.print(\t);
readings.print(t);
readings.print("\t"); // Prints a TAB
readings.println(h); // Prints the value specified and RETURN
// close the file:
readings.close();
lcd.clear();
lcd.println("file CLOSE");
delay(1000);
}
else
{
// if the file didn't open, print an error:
lcd.println("file not open");
delay(4000);
}
// Printing the results on the LCD screen with time and date
delay(2000);
lcd.clear(); // Clears the LCD screen and positions the cursor in the upper-left corner
lcd.print(rtc.getDateStr()); // Prints date
lcd.setCursor(8,1); // Moves the cursor on the second line of the LCD
lcd.println(rtc.getTimeStr()); // Prints time
delay (4000); // Waits 4 seconds
lcd.clear(); // Clears the LCD screen and positions the cursor in the upper-left corner
char tempF[6]; // Defines a character variable
char humF[6];
dtostrf(t, 5, 1, tempF); // Converts a float to a char array so it can then be printed easily
dtostrf(h, 2, 0, humF);
lcd.print("T:"); lcd.print(tempF); lcd.print((char)223); lcd.print("C ");
// NOTE: char(223) identifies the degree symbol
lcd.print("H: "); lcd.print(humF); lcd.print("%");
delay(2000); // Delays 2 seconds, as the DHT22 minimum sampling rate is 0.5Hz
}
Thanks a lot.
M.