#include "DHT.h"
#include <DHT_U.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <RTClib.h>
#include <SPI.h>
#define DHTPIN 4
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define dWAITING 0 //tiempo de espera inicializado
#define dPRINCIPAL 1 //principal inicializado
const int pluviometer = 2;
volatile int Pulsoplv = 0;
const int encdLCD = 3;
volatile int Button = 0;
int intervalW=5000;
byte displayStatus = dWAITING;
int TEMPERATURA;
int HUMEDAD;
int MAXIT = -40;
int MINIT = 100;
int MAXIH = -40;
int MINIH = 100;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27,20,4);
RTC_DS1307 rtc;
DateTime now;
void setup() {
lcd.backlight();
lcd.init();
rtc.begin();
//rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
lcd.begin(20,4);
lcd.print("EST METEOROLGC");
lcd.setCursor(0,1);
lcd.print("Download data");
delay (1500);
lcd.setCursor(1,1);
lcd.print("Data Complete");
delay (1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Press Button");
delay (1500);
lcd.setCursor(0,1);
lcd.print("To see the info");
delay (2500);
pinMode(pluviometer, INPUT);
attachInterrupt(digitalPinToInterrupt(pluviometer), interruptionCall, FALLING);
pinMode(encdLCD, INPUT);
attachInterrupt(digitalPinToInterrupt(encdLCD), showlcd, FALLING);
Wire.begin();
intervalW;
dht.begin();
TEMPERATURA = dht.readTemperature();
HUMEDAD = dht.readHumidity();
}
void loop() {
TEMPERATURA = dht.readTemperature();
HUMEDAD = dht.readHumidity();
int DETEMP = TEMPERATURA;
int DEHUME = HUMEDAD;
switch (displayStatus)
{
case dWAITING:
displayWaiting();
break;
case dPRINCIPAL:
displayPrincipal();
default:
break;
}
}
void showlcd() {
Button++;
displayStatus=Button/Button;
}
void interruptionCall() {
Pulsoplv++;
}
float getRain() {
const float mm=0.2794;
float accumRain = Pulsoplv*mm;
return accumRain;
}
void displayInfo(){
lcd.clear();
lcd.display();
lcd.setBacklight (150);
lcd.setCursor(1,0);
lcd.print("Temp");
lcd.setCursor(7,0);
int Tmp = analogRead( A0 );
lcd.print( Tmp );
lcd.print(" C");
lcd.setCursor(1,1);
lcd.print("Prcpt");
float accumRain = getRain ();
lcd.setCursor(8,1);
lcd.print(accumRain);
lcd.setCursor(12,1);
lcd.print(" mm");
lcd.setCursor(0,2);
lcd.print("Fecha");
lcd.setCursor(8,2);
now = rtc.now();
char bufferDate [12]="00/00/0000";
formatoFecha(bufferDate, now.day(),now.month(),now.year());
lcd.print(bufferDate);
intervalW;
intervalW;
intervalW;
intervalW;
lcd.setBacklight (100);
delay (2500);
lcd.setBacklight (50);
delay (2500);
lcd.setBacklight (0);
displayStatus=0;
}
void formatoFecha(char bufferFecha[12], int numDia, int numMes, int numA) {
sprintf(bufferFecha, "%02d/%02d/%04d", numDia, numMes, numA);
}
void formatoHora(char bufferHora[10], int hora, int min, int seg) {
sprintf(bufferHora, "%02d:%02d:%02d", hora, min, seg);
}
void DateTime() {
now=rtc.now();
char bufferDate[12]="00/00/0000";
formatoFecha(bufferDate, now.day(), now.month(), now.year());
char bufferTime[10]="00:00:00";
formatoHora(bufferTime, now.hour(), now.minute(), now.second());
}
void displayWaiting(){
lcd.clear();
lcd.setBacklight (0);
lcd.noDisplay();
intervalW;
intervalW;
}
void displayPrincipal(){
displayInfo();
}