/*
SD CARD DATA LOGGER FOR MULTIPLES TEMPERATURE SENSORS
We will be using two temperature ds1820 sensors and
two humidity sensors. to log data on to a SD.
*/
//===============================LYBRAIES ================================//
#include <OneWire.h> //for the ds01820 always on the top
#include <SPI.h> //WARNING IN ORDER THIS WORKS PROPERLY
#include <SD.h> //THIS LIB MUST BE IN THIS ORDER.
#include "DHT.h"
#include <avr/wdt.h>
#include <MemoryFree.h>
//-------------for the ds 1820
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 10
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress probe1 = { 0x28, 0xFF, 0x08, 0x3B, 0x4A, 0x04, 0x00, 0xCA };
DeviceAddress probe2 = { 0x28, 0xFF, 0x16, 0x3C, 0x4A, 0x04, 0x00, 0x92 };
//-------------for the dht
#define DHT1PIN 4 // what pin we're connected to
#define DHT2PIN 5 // what pin we're connected to
#define DHT1TYPE DHT11 // DHT 11
#define DHT2TYPE DHT11 // DHT 11
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
//----------for the rtc1302 handling
#include <virtuabotixRTC.h>
//----------for the sd handling
const int chipSelect = 4; //FOR THE SD LIB
//==============================GLOBAL VARIABLES===========================//
//------------for the ds1820
float p1tempC = 0;
float p2tempC = 0;
boolean toprint = false;
char data[70]; // make a string for assembling the data to log:
char temp1S[6]; //variable to store the temperature in to char format
char temp2S[6];
byte second = 1;
byte pasSecond = 0;
char dthData[30]; // line for assembly the data of sensors
//------------for the rtc
unsigned int id = 0;
boolean save = false;
int dayofmonth;
int month;
int year;
int hour;
int minutes;
int seconds;
int dataROW = 0;
//=========================== CONSTRUCTORS ==============================//
// Creation of the Real Time Clock Object
virtuabotixRTC myRTC(6, 7, 8);
//===============================VOID SETUP===============================//
void setup()
{
delay(2000);
Serial.begin(9600); // Open serial communications and wait for port to open:
delay(2000);
// for the dth handling
//----------construct separate sensors
dht1.begin();
dht2.begin();
pinMode(10, OUTPUT); // change this to 53 on a mega // don't follow this!!
digitalWrite(10, HIGH); // Add this line
Serial.print(F("Initializing SD card...")); // see if the card is present and can be initialized:
if (!SD.begin(chipSelect))
{
Serial.println(F("Card failed, or not present")); // don't do anything more:
//return;
}
Serial.println(F("card initialized."));
sensors.begin(); // Start up the library ds1820
sensors.setResolution(probe1, TEMPERATURE_PRECISION); // set the resolution to 10 bit
sensors.setResolution(probe2, TEMPERATURE_PRECISION);
//-----print the header on sd and serial
printheader();
//watch dog timmer to 8 seconds.
wdt_enable(WDTO_8S);
}
//===============================VOID LOOP===============================//
void loop()
{
wdt_reset(); //------watch dog timmer reset
myRTC.updateTime(); //----------keep ticking the clock
if(myRTC.seconds == 1 ) save = true; //Serial.println(F("save is true"));
if(myRTC.seconds == 59 && save == true ) second++; //Take a measurement each minute
if(second > pasSecond )
{
dataROW++; //var to create an index
getTime(); //----------For get the complete date time
sensors.requestTemperatures(); //-----For the temperatures readings.
p1tempC = sensors.getTempC(probe1);
p2tempC = sensors.getTempC(probe2);
readDTH(); //------read the two sensors dth
// printDTHinfo(); //------print the data on a single line
assemblyData(); //----------For assembly the data on one string
Serial.println( data ); //----------Serial print of the data to save
sdwrite(); //----------we write to the sd
pasSecond = second; //----------booleand guard to print just once per minute
save = false;
Serial.print(F("freeMemory()="));
Serial.println(freeMemory());
// Serial.println("save is false");
}
}
//===================== PRINT HEADER OF FILE AT STARTUP ==============//
void printheader()
{
Serial.println(F("Inciando recabo de datos"));
Serial.println(F(" DATE - HOUR T1 - T2 - H1 - T1 - H2 - T2 "));
// 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("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
dataFile.println(F("Inciando recabo de datos"));
dataFile.println(F("DATE - HOUR - MINUTE - SECOND - T1 - T2 - H1 - T1 - H2 - T2 "));
//dayofmonth, month, year, hour, minutes, seconds, temp1S, temp2S
dataFile.close();
}
// if the file isn't open, pop up an error:
else
{
Serial.println("error opening datalog.txt");
}
}
//=============================== GET DATE TIME ======================//
void getTime(){
dayofmonth = myRTC.dayofmonth;
month = myRTC.month;
year = myRTC.year;
hour = myRTC.hours;
minutes= myRTC.minutes;
seconds = myRTC.seconds;
}
//=============================== DS1820 TEMP ==========================//
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
Serial.print(F("Error getting temperature "));
}
else
{
Serial.print(F("C: "));
Serial.print(tempC);
}
}
//=============================== DTH SENSOR READ==========================//
void readDTH()
{
//this subfunctions call the sensors, read the values and
//chek it for sessor data correct. Prints the values on
//the global variables to be assembly in another function.
//------------for the dht
float h1 = 0;
float t1 = 0;
float h2 = 0;
float t2 = 0;
char h1txt[6];
char t1txt[6];
char h2txt[6];
char t2txt[6];
h1 = dht1.readHumidity();
t1 = dht1.readTemperature();
h2 = dht2.readHumidity();
t2 = dht2.readTemperature();
dtostrf( h1, 5, 2, h1txt);
dtostrf( t1, 5, 2, t1txt);
dtostrf( h2, 5, 2, h2txt);
dtostrf( t2, 5, 2, t2txt);
sprintf( dthData, "%s %s %s %s\0", h1txt, t1txt, h2txt, t2txt);
}
//=============================== SD WRITE ===============================//
void sdwrite(){
// 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("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
dataFile.println(data);
dataFile.close();
// print to the serial port too:
Serial.println(data);
}
// if the file isn't open, pop up an error:
else
{
Serial.println(F("error opening datalog.txt"));
}
}//--------end of sd write
//============================ ASSEMBLY DATA ===============================//
void assemblyData(){
//dtostrf( [doubleVar] , [sizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
//dtostrf(float, width, precision, buffer);
dtostrf(p1tempC,5,2, temp1S ); //converts the temprature of the 1 proble from float to string
dtostrf(p2tempC,5,2, temp2S ); //converts the temperature 2 from float to string
//Serial.println("Assembly convert data " );
//Serial.println(temp1S);
//Serial.println(temp2S);
sprintf(data, "%d %d/%d/%d %d:%d:%d %s %s %s\0", dataROW, dayofmonth, month, year, hour, minutes, seconds, temp1S, temp2S, dthData ); //Assembly the text on a single string
}