/*************************************************************
* includes
**************************************************************/
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <TimerOne.h>
/*************************************************************
* defines
**************************************************************/
#define CS_PIN 10
#define LED_PIN 2
#define DHT22_PIN 9
/*************************************************************
* variables globales & structs
**************************************************************/
typedef struct
{
unsigned char time;
unsigned char second;
unsigned char minute;
unsigned char hour;
unsigned char day;
unsigned char month;
unsigned char year;
}DATETIME_STR;
typedef struct
{
unsigned int saveData;
unsigned int readData;
float humidity;
float temperature;
}DTH22_STR;
LiquidCrystal_I2C lcd(0x27, 20, 4);
File fil;
DATETIME_STR dateTime;
DTH22_STR dht22;
/*************************************************************
* main
**************************************************************/
void setup() {
LCD_Init();
SDCard_Init();
TIMER1_Init();
RTC_Init();
GPIO_Init();
// put your setup code here, to run once:
}
void loop() {
if(dateTime.time)
dateTime.time = showDateTime();
// put your main code here, to run repeatedly:
}
/*************************************************************
* funciones globales
**************************************************************/
/*funcion que inicia GPIO´s*/
void GPIO_Init(void)
{
pinMode(LED_PIN, OUTPUT);
}
/*funcion que inicia el lcd*/
void LCD_Init(void)
{
lcd.init();
lcd.backlight();
}
/*funcion que inicia la tarjeta de memoria*/
void SDCard_Init(void)
{
if(!SD.begin(CS_PIN))
{
char buffer[20];
sprintf(buffer,"%s","Error: SD CARD");
lcdPrint(3,1,buffer);
while(1);
}
}
/*funcion que inicia el rtc*/
void RTC_Init(void)
{
dateTime.second = 0;
dateTime.minute = 0;
dateTime.hour = 12;
dateTime.day = 28;
dateTime.month = 11;
dateTime.year = 23;
}
/*funcion que inicia el timer 1*/
void TIMER1_Init(void)
{
Timer1.initialize(1000000);
Timer1.attachInterrupt(timerCallback);
Timer1.start();
}
/*funcion de interrupcion para timer 1*/
void timerCallback(void)
{
dateTime.time = 1;
if(++dateTime.second >= 60)
{
dateTime.second = 0;
if(++dateTime.minute >= 60)
{
dateTime.minute = 0;
if(++dateTime.hour >= 24)
{
dateTime.hour = 0;
}
}
}
}
/*funcion que controla el pritf del lcd*/
void lcdPrint(int x , int y , String buffer)
{
lcd.setCursor(x,y);
lcd.print(buffer);
}
/*funcio que alterna una salida*/
void outputToggle(int pin)
{
int status = digitalRead(pin);
digitalWrite(pin,!status);
}
/*funcion que lee el sensor DHT22*/
unsigned int readDHT22(int pin, float* Hum, float* Tem){
unsigned int status = 0;
pinMode(pin,OUTPUT);
digitalWrite(pin,LOW);
delay(2);
digitalWrite(pin,HIGH);
delayMicroseconds(30);
pinMode(pin,INPUT);
while(digitalRead(pin))
{
//add timeout
}
while(!digitalRead(pin))
{
//add timeout
}
while(digitalRead(pin))
{
//add timeout
}
int i = 32;
long int HT = 0;
do{
while(!digitalRead(pin));
delayMicroseconds(30);
if(digitalRead(pin)){
HT = ((HT<<1) | 1);
}else{
HT = (HT<<1);
}
while(digitalRead(pin));
i--;
}while(i != 0);
unsigned char cSum = 0;
i = 8;
do{
while(!digitalRead(pin));
delayMicroseconds(30);
if(digitalRead(pin)){
cSum = ((cSum<<1) | 1);
}else{
cSum = (cSum<<1);
}
while(digitalRead(pin));
i--;
}while(i != 0);
int H = ((0xFFFF0000 & HT)>>16);
int T = ((0xFFFF & HT));
unsigned char HH = ((0xFF00 & H)>>8);
unsigned char HL = (0xFF & H);
unsigned char TH = ((0xFF00 & T)>>8);
unsigned char TL = (0xFF & T);
unsigned char checkSum = (HH+HL+TH+TL);
if(checkSum && cSum){
*Hum = H;
*Hum /= 10;
*Tem = T;
*Tem /= 10;
}else{
status = 1;
}
return status;
}
/*funcion que imprime en la pantalla fecha y hora*/
unsigned int showDateTime(void)
{
char time[16];
char date[16];
char hum[16];
char temp[16];
float h , t;
sprintf(time,"%02d:%02d:%02d",dateTime.hour,dateTime.minute,dateTime.second);
lcdPrint(0,0,time);
sprintf(date,"%02d/%02d/%02d",dateTime.day,dateTime.month,dateTime.year);
lcdPrint(0,1,date);
if(readDHT22(DHT22_PIN,&t,&h) != 0)
{
lcdPrint(10,0,"H: ERROR");
lcdPrint(10,1,"T: ERROR");
}
else
{
sprintf(hum,"H: %.2d %%",(int)h);
lcdPrint(10,0,hum);
sprintf(temp,"T: %d C",(int)t);
lcdPrint(10,1,temp);
}
return 0;
}
/*funcion que escribe en la memoria sd card*/
unsigned int SDCard_Write(char* name,char*data)
{
unsigned int status = 0;
return status;
}