112 lines
3.6 KiB
C++
112 lines
3.6 KiB
C++
/*
|
|
* Created by Laurent CLaude
|
|
*
|
|
* This code is in license GPL v3
|
|
*
|
|
* Horloge Nixie basée sur ESP + module RTC-DS1307, avec fonctionnalités wifi pour synchro NTP
|
|
*/
|
|
#include "hardware.h"
|
|
#include "secrets.h"
|
|
#include <ESP8266WiFi.h>
|
|
#include <WiFiUdp.h>
|
|
#include <RTClib.h> // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
|
|
#include <NTP.h> // The NTP library allows you to receive time information from the Internet. https://github.com/sstaub/NTP
|
|
#include "nixie.h" // Mes routines de pilotage d'affichage Nixie
|
|
|
|
const char *ssid = SECRET_WIFI_SSID;
|
|
const char *password = SECRET_WIFI_PASS;
|
|
char daysOfTheWeek[7][12] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
|
|
|
|
WiFiUDP wifiUdp;
|
|
NTP ntp(wifiUdp);
|
|
RTC_DS1307 rtc;
|
|
|
|
/////////////////////////////////////////////////////
|
|
//////////// FONCTIONS ////////////////
|
|
/////////////////////////////////////////////////////
|
|
//
|
|
void syncNTPtoRTC(){
|
|
|
|
//// Récupération du temps Internet par NTP
|
|
Serial.println ("Routine de synchro NTP vers RTC :");
|
|
Serial.print ("- récupération du temps Internet : ");
|
|
ntp.update(); // récupération du temps NTP
|
|
Serial.println(ntp.formattedTime("%T")); // hh:mm:ss
|
|
|
|
//// Mise à jour du temps RTC de l'horloge locale
|
|
Serial.print ( "- enregistrement du temps Internet dans l'horlore RTC" );
|
|
rtc.adjust(DateTime(ntp.year(), ntp.month(), ntp.day(), ntp.hours(), ntp.minutes(), ntp.seconds()));
|
|
Serial.println ( " : OK." );
|
|
|
|
//// Affichage du temps RTC en console série pour débug
|
|
DateTime now = rtc.now();
|
|
Serial.print ( "Maintenant l'horloge interne indique : " );
|
|
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
|
|
Serial.print(" ");
|
|
Serial.print(now.day(), DEC);
|
|
Serial.print('/');
|
|
Serial.print(now.month(), DEC);
|
|
Serial.print('/');
|
|
Serial.print(now.year(), DEC);
|
|
Serial.print(" ");
|
|
Serial.print(now.hour(), DEC);
|
|
Serial.print(':');
|
|
Serial.print(now.minute(), DEC);
|
|
Serial.print(':');
|
|
Serial.println(now.second(), DEC);
|
|
}
|
|
|
|
|
|
/////////////////////////////////////////////////////
|
|
//////////// setup et loop ////////////////
|
|
/////////////////////////////////////////////////////
|
|
//
|
|
void setup () {
|
|
//// Initialisation de la liaison série
|
|
Serial.begin(115200);
|
|
Serial.println ("");
|
|
Serial.println ("Liaison série OK");
|
|
|
|
//// Connexion au WIFI
|
|
WiFi.begin(ssid, password);
|
|
Serial.print ("Connexion au WiFi");
|
|
while ( WiFi.status() != WL_CONNECTED ) {
|
|
delay ( 500 );
|
|
Serial.print ( "." );
|
|
}
|
|
Serial.println ( " OK" );
|
|
|
|
// Récup du temps avec prise en compte de l'heure d'été pour la France
|
|
Serial.print("Initialisation NTP");
|
|
ntp.ruleDST("CEST", Last, Sun, Mar, 2, 120); // last sunday in march 2:00, timetone +120min (+1 GMT + 1h summertime offset)
|
|
ntp.ruleSTD("CET", Last, Sun, Oct, 3, 60); // last sunday in october 3:00, timezone +60min (+1 GMT)
|
|
ntp.begin();
|
|
Serial.println(" : OK");
|
|
Serial.print("- sur Internet, nous sommes le : ");
|
|
ntp.update();
|
|
Serial.println(ntp.formattedTime("%A %d/%m/%Y, il est : %T")); // www dd/mm/yyyy hh:mm:ss
|
|
|
|
//// Initialisation RTC
|
|
Serial.print("Initialisation RTC");
|
|
if (! rtc.begin()) {
|
|
Wire.begin(I2C_SDA,I2C_SCL); // Broches (SDA,SCL) de l'I2C pour la RTC
|
|
delay(1000);
|
|
if (! rtc.begin()) {
|
|
Serial.println(" --> RTC introuvable ! Fin.");
|
|
while (1);
|
|
}
|
|
}
|
|
Serial.println (" : OK");
|
|
|
|
syncNTPtoRTC(); // Mise à l'heure de l'horloge RTC locale avec l'heure Internet
|
|
}
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
void loop () {
|
|
|
|
Serial.print("Rien ");
|
|
delay(10000);
|
|
|
|
}
|