Hey,
I am using the following components to make a datalogging weather station:-
- DHT11 (humidity and temperature) sensor
- LDR (a standard light intensity measurement) sensor
- DS3231 RTC module (for time keeping purpose)
- A standard SD Card module for data storage purpose.
This is the code I've written for it :-
#include<Wire.h>
#include<SD.h>
#include<dht.h>
dht DHT;
const int flux=A0;
const int temp_humid=6;
const int CS=10;
const int power=2;
#define address 0x68
int decToBcd(int val)
{
return( (val/10*16) + (val%10) );
}
int bcdToDec(int val)
{
return( (val/16*10) + (val%16) );
}
void setup()
{
Wire.begin();
pinMode(flux,INPUT_PULLUP); //for light
pinMode(CS,OUTPUT); //chip select
pinMode(power,OUTPUT); //power for SD card
pinMode(temp_humid,INPUT); //DHT sensor
digitalWrite(power,HIGH); //setting power pin to high for SD card
Serial.begin(9600); //initializing Serial communication
if(!SD.begin(CS))
{
Serial.println("Card failed");
}
else
{
Serial.println("Card loaded !");
}
File datafile=SD.open("log.csv",FILE_WRITE);
if(datafile)
{
datafile.println(",,,,,,,,,,");
String header="#,Temperature,Humidity,Light,Date,Month,Year,Hour,Minute,Second,Day";
datafile.println(header);
datafile.close();
}
// setDS3231time(30,42,21,4,26,11,14);
}
void setDS3231time(int second, int minute, int hour, int dayOfWeek, int dayOfMonth, int month, int year)
{
// sets time and date data to DS3231
Wire.beginTransmission(address);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(int *second, int *minute, int *hour, int *dayOfWeek, int *dayOfMonth, int *month, int *year)
{
Wire.beginTransmission(address);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(address, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void Weekday(int l,char s[10]) //for converting number to Day of Week
{
if(l==1)
strcpy(s,"Sunday");
else if(l==2)
strcpy(s,"Monday");
else if(l==3)
strcpy(s,"Tuesday");
else if(l==4)
strcpy(s,"Wednesday");
else if(l==5)
strcpy(s,"Thursday");
else if(l==6)
strcpy(s,"Friday");
else if(l==7)
strcpy(s,"Saturday");
}
float ldrsensor() //function for retrieving light percentage
{
float val;
val=analogRead(flux);
val=map(val,15,1005,100,0);
return val;
}
long id=1; //for ID purpose
void loop()
{
float light; //variable for retrieving light value
light=ldrsensor();
char s[10];
DHT.read11(temp_humid); //reading DHT sensor data
int second, minute, hour, dayOfWeek, dayOfMonth, month, year; //variables for retrieving RTC data
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); //reading RTC data
File datafile=SD.open("log.csv",FILE_WRITE);
Weekday(dayOfWeek,s[10]);
String rawdata=String(id)+", "+String(DHT.temperature)+", "+String(DHT.humidity)+", "+String(light)+", "+String(dayOfMonth)+", "+String(month)+", "+String(year)+", "+String(hour)+", "+String(minute)+", "+String(second)+", "+String(dayOfWeek);
String disdata=String(id)+", "+String(DHT.temperature)+" C "+String(DHT.humidity)+"% "+String(light)+"% "+String(dayOfMonth)+"/"+String(month)+"/"+String(year)+" "+String(hour)+":"+String(minute)+":"+String(second)+" "+String(dayOfWeek);
if(datafile)
{
Serial.println(disdata);
datafile.println(rawdata);
id++;
datafile.close();
}
else
{
Serial.println("Can't save to card");
}
delay(1000);
}
Since the DS3231 returns the day of the week (variable-dayOfWeek) in form of a number ranging from 1-7, I had to make a new function called Weekday to convert that number into corresponding days (i.e. Sunday,Monday, et al). For debugging purposes, I've displayed the data onto the Serial Monitor.
The problem that I'm encountering is that the Weekday function is not working. When I pass in the parameters and load the code, the day of week part disappears altogether. When I comment out that part, the code works perfectly, yet I'm stuck with a raw number (1-7) instead of the name of the day.
Can anyone resolve this anomaly ?