Esp32 - NTP
Afficher la date et heure - ESP32 - Moniteur série
Assurez-vous que les URL ci-dessous soient bien renseignées dans
- Fichier / Préférences / URL de gestionnaire de cartes supplémentaires
- http://arduino.esp8266.com/stable/package_esp8266com_index.json
- https://dl.espressif.com/dl/package_esp32_index.json
- https://espressif.github.io/arduino-esp32/package_esp32_index.json
- https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
Afficher la date et l'heure - ESP32 - OLED 0.96"
Nous allons modifier le code afin d'afficher la date et l'heure sur un afficheur Oled 0.96"
Câbler l'afficheur Oled 0.96" I2C à votre Esp32 (SDA/SCL)
Voir la page "ESP32 - DHT22 - Oled 0.96" - Afficher la température"
Le code:
// Librairies
#include <WiFi.h> // Connexion Wifi
#include <Wire.h> // Gestion I2C pour Oled SDA / SCL
#include <Adafruit_GFX.h> // Affichage graphique
#include <Adafruit_SSD1306.h> // Driver Oled 0.96" - SSD1306
#include <NTPClient.h> // Client NTP - Obtenir Année, Jour, Heure, Minute, Seconde, etc ....
#include <WiFiUdp.h> // Utilisation du protocole UDP
// Configuration WiFi
const char* ssid = "Votre_SSID"; // Votre_SSID
const char* password = "Votre_Mot_de_Passe"; // Votre_Mot_de_Passe
// Configuration OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Configuration NTP - Serveur NTP pool.ntp.org
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600, 60000); // UTC+1 - Recupération des données Date & Heure - Paris
void setup() {
Serial.begin(115200);
// Connexion WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connecté.");
// Initialisation de l'écran OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("Échec de l'initialisation de l'écran OLED !");
for (;;);
}
// Configuration de l'affichage
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
// Démarrer le client NTP
timeClient.begin();
}
void loop() {
timeClient.update(); // Update de la date & heure
// Récupérer et formater l'heure et la date
unsigned long epochTime = timeClient.getEpochTime();
struct tm *timeInfo;
time_t rawTime = epochTime;
timeInfo = localtime(&rawTime);
int day = timeInfo->tm_mday;
int month = timeInfo->tm_mon + 1; // Janvier = 0
int year = timeInfo->tm_year + 1900;
int hours = timeInfo->tm_hour;
int minutes = timeInfo->tm_min;
int seconds = timeInfo->tm_sec;
// Création des variables dateBuffer & timeBuffer
char dateBuffer[20];
sprintf(dateBuffer, "%02d/%02d/%04d", day, month, year); // Format jj/mm/aaaa
char timeBuffer[10];
sprintf(timeBuffer, "%02d:%02d:%02d", hours, minutes, seconds); //Format hh:mm:ss
// Effacer l'écran et afficher la date & l'heure
display.clearDisplay();
//display.setTextSize(1); // Cette ligne n'est pas utile
// Date
display.setCursor(15, 0); // Colonne 15 Ligne 0
display.print("Date: ");
display.print(dateBuffer); // Affichage de la date au format jj/mm/aaaa
// Heure
display.setCursor(18, 10); // Colonne 18 Ligne 10
display.print("Heure: ");
display.print(timeBuffer); // Affichage de l'heure au format hh:mm:ss
display.display();
delay(1000); // Mise à jour chaque seconde
}
// Fin du code ******************************************************************************
Télécharger le code ESP32_OLED_Wifi_NTP.ino
(Fichier zipper)