1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include <SPI.h>
#include <Ethernet.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(35, 37, 39);
byte mac[] = {0x8A, 0x1E, 0xD5, 0xAC, 0x32, 0x82};
int erreur = 0;
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Ethernet Error");
erreur = 1;
} else {
serveur.begin();
NTP.onNTPSyncEvent ([](NTPSyncEvent_t error) {
if (error) {
erreur = 1;
}
});
NTP.setNTPTimeout (60);
NTP.setInterval (60);
NTP.begin("1.fr.pool.ntp.org", 1, true);
myRTC.setDS1302Time(second(), minute(), hour(), weekday(), day(), month(), year());
Serial.println(Ethernet.localIP());
}
}
void loop() {
Serial.println(dateTimeBD());
delay(1000);
}
String dateTimeBD(){
String resultat = "";
if (Ethernet.linkStatus() == LinkON && erreur == 0) {
resultat = String(year())+"-";
if (month() < 10) {resultat += "0"+String(month());}else {resultat += String(month());}
resultat += "-";
if (day() < 10) {resultat += "0"+String(day());}else {resultat += String(day());}
resultat += " ";
if (hour() < 10) {resultat += "0"+String(hour());}else {resultat += String(hour());}
resultat += ":";
if (minute() < 10) {resultat += "0"+String(minute());}else {resultat += String(minute());}
resultat += ":";
if (second() < 10) {resultat += "0"+String(second());}else {resultat += String(second());}
return resultat;
} else {
myRTC.updateTime();
resultat = String(myRTC.year)+"-";
if (myRTC.month < 10) {resultat += "0"+String(myRTC.month);}else {resultat += String(myRTC.month);}
resultat += "-";
if (myRTC.dayofmonth < 10) {resultat += "0"+String(myRTC.dayofmonth);}else {resultat += String(myRTC.dayofmonth);}
resultat += " ";
if (myRTC.hours < 10) {resultat += "0"+String(myRTC.hours);}else {resultat += String(myRTC.hours);}
resultat += ":";
if (myRTC.minutes < 10) {resultat += "0"+String(myRTC.minutes);}else {resultat += String(myRTC.minutes);}
resultat += ":";
if (myRTC.seconds < 10) {resultat += "0"+String(myRTC.seconds);}else {resultat += String(myRTC.seconds);}
return resultat+" RTC";
}
} |
Partager