Fonctionnalités ok : Wifi (basique), NTP, RTC.

TODO : gestion d'erreurs.
This commit is contained in:
Laurent Claude 2023-06-06 11:05:49 +02:00
parent 0f028e6757
commit 55ca79e431
5 changed files with 103 additions and 121 deletions

View File

@ -5,78 +5,103 @@
*
* Horloge Nixie basée sur ESP + module RTC-DS1307, avec fonctionnalités wifi pour synchro NTP
*/
#include "hardware.h"
#include "nixie.h"
#include "horlogerie.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
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <RTClib.h>
// Les affectations physiques
// event at to 14:45 (for tests)
uint8_t DAILY_EVENT_HH = 14; // event start time: hour
uint8_t DAILY_EVENT_MM = 45; // event start time: minute
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;
char daysOfTheWeek[7][12] = {
"Dimanche",
"Lundi",
"Mardi",
"Mercredi",
"Jeudi",
"Vendredi",
"Samedi"
};
/////////////////////////////////////////////////////
//////////// FONCTIONS ////////////////
/////////////////////////////////////////////////////
//
void syncNTPtoRTC(){
void setup () {
Serial.begin(115200);
Wire.begin(I2C_SDA,I2C_SCL); // Broches (SDA,SCL) de l'I2C pour la RTC
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
// SETUP RTC MODULE
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
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." );
Serial.print ( "Maintenant l'horloge interne indique : " );
// sets the RTC to the date & time on PC this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// sets the RTC with an explicit date & time, for example to set
// January 21, 2021 at 3am you would call:
// rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0));
initwifintp();
DateTime now = rtc.now();
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
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();
}
/////////////////////////////////////////////////////
void loop () {
DateTime now = rtc.now();
printTime(now);
if (now.hour() == DAILY_EVENT_HH &&
now.minute() == DAILY_EVENT_MM) {
Serial.println("It is on scheduled time");
// TODO: write your code"
} else {
Serial.println("It is NOT on scheduled time");
}
delay(1000);
}
void printTime(DateTime time) {
Serial.print("Date : ");
Serial.print(time.year(), DEC);
Serial.print('/');
Serial.print(time.month(), DEC);
Serial.print('/');
Serial.print(time.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[time.dayOfTheWeek()]);
Serial.print(") - Heure : ");
Serial.print(time.hour(), DEC);
Serial.print(':');
Serial.print(time.minute(), DEC);
Serial.print(':');
Serial.println(time.second(), DEC);
Serial.println("Rien");
delay(10000);
}

15
boutsdecode.txt Normal file
View File

@ -0,0 +1,15 @@
// event at to 14:45 (for tests)
uint8_t DAILY_EVENT_HH = 14; // event start time: hour
uint8_t DAILY_EVENT_MM = 45; // event start time: minute
DateTime now = rtc.now();
printTime(now);
if (now.hour() == DAILY_EVENT_HH &&
now.minute() == DAILY_EVENT_MM) {
Serial.println("It is on scheduled time");
// TODO: write your code"
} else {
Serial.println("It is NOT on scheduled time");
}

View File

@ -1,49 +0,0 @@
#include "horlogerie.h"
#include "Arduino.h"
#include "hardware.h"
#include "secrets.h"
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
void initwifintp() {
// Replace with your network credentials (see in 'arduino_secrets.h' file)
char ssid[] = SECRET_WIFI_SSID;
const char* password = SECRET_WIFI_PASS;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
// Initialize Serial Monitor
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(3600);
}

View File

@ -1,11 +0,0 @@
/* fichier nixie.h */
#include "Arduino.h"
#include "hardware.h"
#include "secrets.h"
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
void initwifintp();

View File

@ -1,2 +1,4 @@
## Change with your credentials and rename this file in 'secrets.h'
#define SECRET_WIFI_SSID "YOUR_SSID"
#define SECRET_WIFI_PASS "YOUR_PASS"