/*
* Projet Mon Horloge
* Affichag sur matrix led 32x8
* Heures, Minutes, Secondes
* Date
* Temperature, Humidite, Pression en alternance
* Mise à lheure automatique par dcf77
* Pin : A0=Dht, D2=Dcf, A4,A4=DS1307, A3=Luminosit, D3=433Mhz, D7=Touch, D9,D10,D12,D13=Matrix Led
*/
#include <Wire.h>
#include <DS1307.h>
#include <DHT.h>
#include <DCF77.h>
#include <Time.h>
#include <HT1632.h>
#include <font_5x4.h>
#include <images.h>
#define DHTPIN A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
DS1307 clock;
#define DCF_PIN 2 // Connection DCF 77 device to pin D2
#define DCF_INTERRUPT 0 // Interrupt number associated with pin
time_t time;
DCF77 DCF = DCF77(DCF_PIN, DCF_INTERRUPT);
int compte = 0;
String heure;
int luminosite = 0;
int i = 0;
String jour;
char disp[] = "HELLO";
void setup()
{
HT1632.begin(12, 10, 9);
Serial.begin(9600);
dht.begin();
clock.begin();
DCF.Start();
// mise a heure valeurs passees
clock.fillByYMD(2015, 10, 12);
clock.fillByHMS(17, 9, 55);
clock.fillDayOfWeek(MON);
clock.setTime(); //write time to the RTC chip
HT1632.renderTarget(0);
HT1632.clear();
HT1632.drawText(disp, 6, 1, FONT_5X4, FONT_5X4_END, FONT_5X4_HEIGHT);
HT1632.render();
delay(2000);
pinMode(7, INPUT); // bouton poussoir
}
void loop()
{
eclairement();
time_t DCFtime = DCF.getTime();
if (DCFtime != 0)
{
setTime(DCFtime);
miseajourds1307();
HT1632.setPixel(31, 7);
HT1632.render();
delay(100);
HT1632.clearPixel(31, 7);
HT1632.render();
}
clock.getTime();
if (digitalRead(7) == LOW)
{
if (clock.second == 2)
{
efface();
printDate();
delay(3000);
alterne();
}
else
{
printTime();
delay(1000);
}
}
else
{
alterne();
}
}
// Fonctions
void printTime()
{
char heur[10];
clock.getTime();
Serial.print(clock.hour, DEC);
Serial.print(":");
if (clock.minute < 10)
{
Serial.print("0");
Serial.print(clock.minute, DEC);
sprintf(heur, "%d:0%d", clock.hour, clock.minute);
}
else
{
Serial.print(clock.minute, DEC);
sprintf(heur, "%d:%d", clock.hour, clock.minute);
}
Serial.print(":");
if (clock.second < 10)
{
Serial.print("0");
Serial.println(clock.second, DEC);
}
else
{
Serial.println(clock.second, DEC);
}
HT1632.clear();
HT1632.drawText(heur, 7, 1, FONT_5X4, FONT_5X4_END, FONT_5X4_HEIGHT);
HT1632.render();
delay(100);
HT1632.clearPixel(16, 2);
HT1632.clearPixel(16, 4);
HT1632.render();
}
void printDate()
{
clock.getTime();
switch (clock.dayOfWeek)// Friendly printout the weekday
{
case MON:
Serial.print("Lun ");
jour = ("Lundi");
break;
case TUE:
Serial.print("Mar ");
jour = ("Mardi");
break;
case WED:
Serial.print("Mer ");
jour = ("Mercredi");
break;
case THU:
Serial.print("Jeu ");
jour = ("Jeudi");
break;
case FRI:
Serial.print("Ven ");
jour = ("Vendredi");
break;
case SAT:
Serial.print("Sam ");
jour = ("Samedi");
break;
case SUN:
Serial.print("Dim ");
jour = ("Dimanche");
break;
}
Serial.print(clock.dayOfMonth, DEC);
Serial.print("/");
Serial.print(clock.month, DEC);
Serial.print("/");
Serial.println(clock.year + 2000, DEC);
char jourdate[8];
char journee[10];
jour.toCharArray(jourdate, 8);
// sprintf(jourdate, "%s", jourdate);
HT1632.clear();
HT1632.drawText(jourdate, 1, 1, FONT_5X4, FONT_5X4_END, FONT_5X4_HEIGHT);
HT1632.render();
delay(2000);
sprintf(journee, "%d.%d.%d", clock.dayOfMonth, clock.month, clock.year); // clock.year + 2000
HT1632.clear();
HT1632.drawText(journee, 2, 1, FONT_5X4, FONT_5X4_END, FONT_5X4_HEIGHT);
HT1632.render();
}
void printTemp()
{
float temperat = dht.readTemperature();
Serial.print("Temperature :");
Serial.print(temperat, 1);
Serial.println(" C");
char str_temp[3];
char temper[3];
dtostrf(temperat, 4, 1, str_temp);
sprintf(temper, "%s", str_temp);
HT1632.clear();
HT1632.drawText(temper, 2, 1, FONT_5X4, FONT_5X4_END, FONT_5X4_HEIGHT);
HT1632.setPixel(18, 1);
HT1632.setPixel(18, 2);
HT1632.setPixel(19, 1);
HT1632.setPixel(19, 2);
printIn();
HT1632.render();
}
void printHumi()
{
float humidite = dht.readHumidity();
char str_temp[3];
char humi[3];
dtostrf(humidite, 2, 1, str_temp);
sprintf(humi, "%s", str_temp);
HT1632.clear();
HT1632.drawText(humi, 2, 1, FONT_5X4, FONT_5X4_END, FONT_5X4_HEIGHT);
HT1632.setPixel(17, 1);
HT1632.setPixel(17, 2);
HT1632.setPixel(18, 1);
HT1632.setPixel(18, 2);
HT1632.setPixel(17, 5);
HT1632.setPixel(18, 4);
HT1632.setPixel(19, 3);
HT1632.setPixel(20, 2);
HT1632.setPixel(21, 1);
HT1632.setPixel(20, 4);
HT1632.setPixel(20, 5);
HT1632.setPixel(21, 4);
HT1632.setPixel(21, 5);
printIn();
HT1632.render();
Serial.print("Humidite : ");
Serial.print(humidite, 1);
Serial.println(" % ");
}
void printdcf()
{
Serial.print("Dcf: ");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println("");
switch (weekday())// Friendly printout the weekday
{
case 1:
Serial.print("Dim ");
break;
case 2:
Serial.print("Lun ");
break;
case 3:
Serial.print("Mar ");
break;
case 4:
Serial.print("Mer ");
break;
case 5:
Serial.print("Jeu ");
break;
case 6:
Serial.print("Ven ");
break;
case 7:
Serial.print("Sam ");
break;
}
Serial.print(" ");
Serial.print(day());
Serial.print("/");
Serial.print(month());
Serial.print("/");
Serial.println(year());
}
// ajout du zero devant les minutes et heures inférieures à 10
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
void efface()
{
HT1632.clear();
}
void eclairement()
{
luminosite = analogRead(A3);
if (luminosite <= 50)
{
HT1632.setBrightness(1);
}
if (luminosite >= 51 )
{
HT1632.setBrightness(16);
}
}
void alterne()
{
efface();
printTemp();
delay(4000);
efface();
printHumi();
delay(3000);
efface();
// pour debug
printdcf();
delay(2000);
efface();
}
void miseajourds1307()
{
clock.fillByYMD(year(), month(), day());
clock.fillByHMS(hour(), minute(), second());
switch (weekday()) // Friendly printout the weekday
{
case 1:
clock.fillDayOfWeek(SUN);
break;
case 2:
clock.fillDayOfWeek(MON);
break;
case 3:
clock.fillDayOfWeek(TUE);
break;
case 4:
clock.fillDayOfWeek(WED);
break;
case 5:
clock.fillDayOfWeek(THU);
break;
case 6:
clock.fillDayOfWeek(FRI);
break;
case 7:
clock.fillDayOfWeek(SAT);
break;
}
clock.setTime(); //write time to the RTC chip
}
void printIn()
{
HT1632.setPixel(27, 5);
HT1632.setPixel(27, 6);
HT1632.setPixel(27, 7);
HT1632.setPixel(29, 5);
HT1632.setPixel(29, 6);
HT1632.setPixel(29, 7);
HT1632.setPixel(30, 5);
HT1632.setPixel(31, 5);
HT1632.setPixel(31, 6);
HT1632.setPixel(31, 7);
}