2023-06-02 19:53:53 +02:00
|
|
|
/*
|
|
|
|
* Created by Laurent CLaude
|
2023-07-10 22:17:09 +02:00
|
|
|
* https://www.laurentclaude.fr/
|
2023-06-02 19:53:53 +02:00
|
|
|
*
|
2023-07-10 22:17:09 +02:00
|
|
|
* My code is under license GPL v3
|
2023-06-02 19:53:53 +02:00
|
|
|
*
|
|
|
|
* Horloge Nixie basée sur ESP + module RTC-DS1307, avec fonctionnalités wifi pour synchro NTP
|
|
|
|
*/
|
|
|
|
#include "hardware.h"
|
2023-06-05 09:57:54 +02:00
|
|
|
#include "secrets.h"
|
2023-07-10 22:17:09 +02:00
|
|
|
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
2023-06-06 11:05:49 +02:00
|
|
|
#include <WiFiUdp.h>
|
2023-07-10 22:17:09 +02:00
|
|
|
#include <RTClib.h> // Date and time functions using a DS1307 RTC connected via I2C and Wire lib. https://github.com/adafruit/RTClib
|
|
|
|
#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
|
2023-06-02 19:53:53 +02:00
|
|
|
|
2023-07-10 22:17:09 +02:00
|
|
|
RTC_DS1307 rtc;
|
2023-06-06 11:05:49 +02:00
|
|
|
char daysOfTheWeek[7][12] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
|
2023-07-10 22:17:09 +02:00
|
|
|
int timeout = 120; // seconds to run for
|
|
|
|
bool wifiOK, ntpOK, rtcOK;
|
|
|
|
unsigned long LastRTCUpdate; // le temps de dernière MAJ de l'horloge interne RTC
|
|
|
|
unsigned long LastNixieUpdate; // le temps de dernière MAJ affichage Nixie
|
|
|
|
|
|
|
|
const long intervalRTCUpdate = 17000; // 86400000 = 24 heures
|
|
|
|
const long intervalNixieUpdate = 1000; // 1000 = 1 seconde
|
2023-06-02 19:53:53 +02:00
|
|
|
|
2023-06-06 11:05:49 +02:00
|
|
|
WiFiUDP wifiUdp;
|
|
|
|
NTP ntp(wifiUdp);
|
2023-06-02 19:53:53 +02:00
|
|
|
|
2023-06-06 11:05:49 +02:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//////////// FONCTIONS ////////////////
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
2023-07-10 22:17:09 +02:00
|
|
|
bool initWIFI(){
|
|
|
|
// is configuration portal requested?
|
|
|
|
WiFiManager wm;
|
|
|
|
wm.setConfigPortalTimeout(60);
|
|
|
|
wm.setHostname("Horloge Nixie");
|
2023-06-02 19:53:53 +02:00
|
|
|
|
2023-07-10 22:17:09 +02:00
|
|
|
bool res;
|
|
|
|
//reset settings on startup if switch pressed
|
|
|
|
if (! digitalRead(Rotary_SW)) {
|
|
|
|
Serial.println("RAZ");
|
|
|
|
wm.resetSettings();
|
|
|
|
}
|
|
|
|
res = wm.autoConnect("NixieClockAP"); // Création d'un AP ou connexion mémorisée
|
2023-06-06 11:05:49 +02:00
|
|
|
|
2023-07-10 22:17:09 +02:00
|
|
|
if(!res) {
|
|
|
|
Serial.println("Failed to connect");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//if you get here you have connected to the WiFi
|
|
|
|
Serial.println("connected...yeey :)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return(res);
|
|
|
|
}
|
2023-06-06 11:05:49 +02:00
|
|
|
|
2023-07-10 22:17:09 +02:00
|
|
|
bool initRTC(){
|
|
|
|
//// Initialisation RTC
|
|
|
|
Serial.print("Initialisation de l'horloge interne RTC");
|
|
|
|
rtcOK = rtc.begin();
|
|
|
|
if (! rtcOK) {
|
|
|
|
Wire.begin(I2C_SDA,I2C_SCL); // Broches (SDA,SCL) de l'I2C pour la RTC
|
|
|
|
delay(1000);
|
|
|
|
if (! rtc.begin()) {
|
|
|
|
Serial.println(" --> RTC introuvable !");
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Serial.println (" : OK");
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Serial.println(" : déjà démarrée !");
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printRTC(){
|
|
|
|
//// Affichage du temps RTC en console série pour débug
|
2023-06-06 11:05:49 +02:00
|
|
|
DateTime now = rtc.now();
|
2023-07-10 22:17:09 +02:00
|
|
|
Serial.print ( " Heure de l'horloge interne (RTC) : " );
|
2023-06-06 11:05:49 +02:00
|
|
|
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);
|
|
|
|
}
|
2023-06-02 19:53:53 +02:00
|
|
|
|
2023-07-10 22:17:09 +02:00
|
|
|
void initNTP(){
|
|
|
|
|
|
|
|
// Paramétrage NTP 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");
|
|
|
|
//ntp.updateInterval(1000); // update every second
|
|
|
|
Serial.print("Le temps Internet (NTP) indique : ");
|
|
|
|
ntp.update();
|
|
|
|
Serial.println(ntp.formattedTime("%A %d/%m/%Y %T")); // www dd/mm/yyyy hh:mm:ss
|
|
|
|
}
|
|
|
|
|
|
|
|
void syncNTPtoRTC(){
|
|
|
|
//// Récupération du temps Internet par NTP
|
|
|
|
Serial.println ("Synchro temps NTP vers RTC :");
|
|
|
|
Serial.print ("- récupération du temps Internet : ");
|
|
|
|
ntp.update(); // récupération du temps NTP
|
|
|
|
Serial.println(ntp.formattedTime("%A %d/%m/%Y %T")); // www dd/mm/yyyy 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." );
|
|
|
|
LastRTCUpdate = millis();
|
|
|
|
}
|
2023-06-02 19:53:53 +02:00
|
|
|
|
2023-06-06 11:05:49 +02:00
|
|
|
/////////////////////////////////////////////////////
|
2023-07-10 22:17:09 +02:00
|
|
|
/////////////////// setup ////////////////////
|
2023-06-06 11:05:49 +02:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
2023-06-02 19:53:53 +02:00
|
|
|
void setup () {
|
2023-07-10 22:17:09 +02:00
|
|
|
//// Initialisation hardware
|
|
|
|
pinMode(Rotary_SW, INPUT_PULLUP); // Encodeur rotatif : switch
|
|
|
|
pinMode(Rotary_A, INPUT_PULLUP); // Encodeur rotatif : voie A
|
|
|
|
pinMode(Rotary_B, INPUT_PULLUP); // Encodeur rotatif : voie B
|
|
|
|
|
|
|
|
pinMode(BCD_D, OUTPUT);// D Pour digits afficheurs nixie 1
|
|
|
|
pinMode(BCD_C, OUTPUT);// C
|
|
|
|
pinMode(BCD_B, OUTPUT);// B
|
|
|
|
pinMode(BCD_A, OUTPUT);// A
|
|
|
|
|
|
|
|
pinMode(BCD_D2, OUTPUT);// D Pour digits afficheurs nixie 2
|
|
|
|
pinMode(BCD_C2, OUTPUT);// C
|
|
|
|
pinMode(BCD_B2, OUTPUT);// B
|
|
|
|
pinMode(BCD_A2, OUTPUT);// A
|
|
|
|
|
|
|
|
// Démarrage de l'I2C :
|
|
|
|
Wire.begin(I2C_SDA,I2C_SCL); // Broches (SDA,SCL) de l'I2C pour la RTC
|
|
|
|
|
2023-06-06 11:05:49 +02:00
|
|
|
//// Initialisation de la liaison série
|
2023-06-02 19:53:53 +02:00
|
|
|
Serial.begin(115200);
|
2023-06-06 11:05:49 +02:00
|
|
|
Serial.println ("");
|
|
|
|
Serial.println ("Liaison série OK");
|
2023-06-02 19:53:53 +02:00
|
|
|
|
2023-07-10 22:17:09 +02:00
|
|
|
wifiOK = initWIFI(); // initialisation du wifi
|
|
|
|
initNTP(); // récupération du temps Internet
|
|
|
|
rtcOK = initRTC(); // initialisation de l'horloge interne
|
|
|
|
|
|
|
|
if (wifiOK && rtcOK) {
|
|
|
|
printRTC(); // Affichage du temps RTC en console série
|
|
|
|
syncNTPtoRTC(); // Mise à l'heure de l'horloge RTC locale avec l'heure Internet
|
|
|
|
printRTC(); // Affichage du temps RTC en console série
|
2023-06-02 19:53:53 +02:00
|
|
|
}
|
2023-07-10 22:17:09 +02:00
|
|
|
Serial.print("Pour info, le temps de compil : ");
|
|
|
|
Serial.print(__DATE__);
|
|
|
|
Serial.println(__TIME__);
|
|
|
|
Serial.println("Fin des initialisations.");
|
|
|
|
printRTC(); // Affichage du temps RTC en console série
|
|
|
|
Serial.println("------------------------");
|
2023-06-02 19:53:53 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-06 11:05:49 +02:00
|
|
|
/////////////////////////////////////////////////////
|
2023-07-10 22:17:09 +02:00
|
|
|
///////////////// loop //////////////////
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
2023-06-02 19:53:53 +02:00
|
|
|
void loop () {
|
2023-07-10 22:17:09 +02:00
|
|
|
unsigned long currentMillis = millis();
|
|
|
|
|
|
|
|
// Mise à jour de l'affichage Nixie
|
|
|
|
if ((currentMillis - LastNixieUpdate >= intervalNixieUpdate) || (currentMillis < LastNixieUpdate)) {
|
|
|
|
LastNixieUpdate = currentMillis;
|
2023-06-02 19:53:53 +02:00
|
|
|
|
2023-07-10 22:17:09 +02:00
|
|
|
DateTime now = rtc.now();
|
|
|
|
int heu_d = (now.hour())/10;
|
|
|
|
int heu_u = (now.hour())%10;
|
|
|
|
int min_d = (now.minute())/10;
|
|
|
|
int min_u = (now.minute())%10;
|
|
|
|
int sec_d = (now.second())/10;
|
|
|
|
int sec_u = (now.second())%10;
|
2023-06-06 11:05:49 +02:00
|
|
|
|
2023-07-10 22:17:09 +02:00
|
|
|
printNixie2(heu_d);
|
|
|
|
printNixie(heu_u);
|
|
|
|
Serial.print(heu_d);
|
|
|
|
Serial.print(heu_u);
|
|
|
|
delay(1200);
|
|
|
|
|
|
|
|
printNixie2(99);
|
|
|
|
printNixie(99);
|
|
|
|
delay(000);
|
|
|
|
|
|
|
|
printNixie2(min_d);
|
|
|
|
printNixie(min_u);
|
|
|
|
Serial.print(min_d);
|
|
|
|
Serial.print(min_u);
|
|
|
|
delay(1200);
|
|
|
|
|
|
|
|
printNixie2(99);
|
|
|
|
printNixie(99);
|
|
|
|
delay(1200);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mise à jour de l'horloge interne RTC. Une fois par 24H
|
|
|
|
if ((currentMillis - LastRTCUpdate >= intervalRTCUpdate) || (currentMillis < LastRTCUpdate)) {
|
|
|
|
LastRTCUpdate = currentMillis;
|
|
|
|
|
|
|
|
syncNTPtoRTC(); // Mise à l'heure de l'horloge RTC locale avec l'heure Internet
|
|
|
|
printRTC(); // Affichage du temps RTC en console série
|
|
|
|
}
|
2023-06-02 19:53:53 +02:00
|
|
|
}
|