Bonjour,
Je suis sur mon projet de "thermomètre - horloge" et je me heurte à un problème. tout fonctionne sauf que l'heure affiché est le décalage du fuseau horaire (02:00 au début et pas l'heure réelle). Lorsque je test la "partie" NTP seule, tout fonctionne mais si je copie tout dans le même programme, il n'affiche pas la bonne heure. Dans le programme totale il y a :
- Le paramétrage d'une IP fixe, avec un nom DNS précis
- La gestion d'une sonde DHT22
- La gestion du NTP
- La gestion de l'écran ssd1360 (qui affiche bien les 2 paramètres ( Temp et Humidité - Heure)
/*
Définition d'un nom d'hôte et d'une adresse IP fixe
Thermomètre et Hydrométrie
Affichage sur OLED SSD1306
NTP
Copié sur :
https://RandomNerdTutorials.com/esp8266-nodemcu-set-custom-hostname-arduino/
*/
//Librairies
#include <ESP8266WiFi.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
//****************Pour NTP**********
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
//**********************************
#define DHTPIN 14 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//****************Pour NTP**********
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
//Week Days
String weekDays[7]={"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
//Month names
String months[12]={"Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Decembre"};
//**********************************
// Définition de l'écran
#define nombreDePixelsEnLargeur 128 // Taille de l'écran OLED, en pixel, au niveau de sa largeur
#define nombreDePixelsEnHauteur 64 // Taille de l'écran OLED, en pixel, au niveau de sa hauteur
#define brocheResetOLED -1 // Reset de l'OLED partagé avec l'Arduino (d'où la valeur à -1, et non un numéro de pin)
#define adresseI2CecranOLED 0x3C // Adresse de "mon" écran OLED sur le bus i2c (généralement égal à 0x3C ou 0x3D)
Adafruit_SSD1306 ecranOLED(nombreDePixelsEnLargeur, nombreDePixelsEnHauteur, &Wire, brocheResetOLED);
// Définition du réseau
// Paramètre connexion Box (STATION)
const char* ssid = "Nom SSID";
const char* password = "mon PWD";
// Nouveau nom d'hôte
String newHostname = "Therm_Bureau";
// Mettre une adresse IP fixe
IPAddress local_IP(192, 168, 1, 201);
// Gateway IP address
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
// Initialisation DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
float Temperature;
float Humidity;
void setup() {
// Pour l'écran de débogage
Serial.begin(115200);
// Réseau
// WiFi.mode(WIFI_STA);
//Obtenir le nom d'hôte
Serial.printf("Default hostname: %s\n", WiFi.hostname().c_str());
//Définir un nouveau nom d'hôte
WiFi.hostname(newHostname.c_str());
//Afficher le nouveau nom d'hôte
Serial.printf("New hostname: %s\n", WiFi.hostname().c_str());
// Configure l'IP fixe
if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println("STA Failed to configure");
}
//Initialise le Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
Serial.println(WiFi.hostname());
// Initialisation de l'écran OLED
if(!ecranOLED.begin(SSD1306_SWITCHCAPVCC, adresseI2CecranOLED))
while(1); // Arrêt du programme (boucle infinie) si échec d'initialisation
// Thermomètre
pinMode(DHTPIN, INPUT);
dht.begin();
//****************Pour NTP**********
// 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 = 7200
timeClient.setTimeOffset(7200);
//**********************************
}
void loop() {
ecranOLED.clearDisplay(); // Effaçage de l'intégralité du buffer
//****************Pour NTP**********
timeClient.update();
time_t epochTime = timeClient.getEpochTime();
// Serial.print("Epoch Time: ");
// Serial.println(epochTime);
String formattedTime = timeClient.getFormattedTime();
// Serial.print("Formatted Time: ");
// Serial.println(formattedTime);
int currentHour = timeClient.getHours();
// Serial.print("Hour: ");
// Serial.println(currentHour);
int currentMinute = timeClient.getMinutes();
// Serial.print("Minutes: ");
// Serial.println(currentMinute);
int currentSecond = timeClient.getSeconds();
// Serial.print("Seconds: ");
// Serial.println(currentSecond);
String weekDay = weekDays[timeClient.getDay()];
// Serial.print("Week Day: ");
// Serial.println(weekDay);
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
// Serial.print("Month day: ");
// Serial.println(monthDay);
int currentMonth = ptm->tm_mon+1;
// Serial.print("Month: ");
// Serial.println(currentMonth);
String currentMonthName = months[currentMonth-1];
// Serial.print("Month name: ");
// Serial.println(currentMonthName);
int currentYear = ptm->tm_year+1900;
// Serial.print("Year: ");
// Serial.println(currentYear);
//Print complete date:
String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
// Serial.print("Current date: ");
// Serial.println(currentDate);
//**********************************
// Lecture de la température et de l'humidité Totes les 250 milliseconds!
// Les lectures du capteur peuvent également être "anciennes" jusqu'à 10 secondes (c'est un capteur très lent)
float h = dht.readHumidity();
// Lire la température en degrés Celsius (valeur par défaut)
float t = dht.readTemperature();
// Vérifiez si des lectures ont échoué et quittez plus tôt (pour réessayer).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Calculer l'indice de chaleur en Celsius (Fahrenheit = faux)
float hic = dht.computeHeatIndex(t, h, false);
String affichage1 = "T:" + String (t,1) + "C";
String affichage2 = "H:" + String (h,1) + "%";
Serial.println(affichage1);
Serial.println(affichage2);
// *************************************************************************
//for(byte tailleDeCaractere=1; tailleDeCaractere <=3; tailleDeCaractere++) {
// boolean bCouleurInverse = false;
ecranOLED.clearDisplay(); // Effaçage de l'intégralité du buffer
byte tailleDeCaractere=3;
ecranOLED.setTextSize(tailleDeCaractere); // Taille des caractères (3:1)
ecranOLED.setCursor(0, 0); // Déplacement du curseur en position (0,0), c'est à dire dans l'angle supérieur gauche
ecranOLED.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
ecranOLED.setTextColor(SSD1306_WHITE); // Affichage du texte en "blanc" (avec la couleur principale, en fait, car l'écran monochrome peut être coloré)
ecranOLED.setCursor(0, 5);
ecranOLED.println(affichage1) ;
ecranOLED.print(affichage2) ;
ecranOLED.display(); // Transfert le buffer à l'écran
delay(10000);
//****************Pour NTP**********
// Serial.println("");
ecranOLED.clearDisplay(); // Effaçage de l'intégralité du buffer
ecranOLED.setTextSize(4);
ecranOLED.setTextColor(WHITE);
ecranOLED.setCursor(0, 10);
// Display static text au format HH:MM
String formattedTime2 = formattedTime.substring(0, 5);
ecranOLED.println(formattedTime2);
ecranOLED.display();
delay(10000);
//**********************************
}
void handle_OnConnect() {
Temperature = dht.readTemperature(); // Gets the values of the temperature
Humidity = dht.readHumidity(); // Gets the values of the humidity
}
Je pense que le problème viendrait de mon paramétrage IP fixe ...
Merci pour vos lumières.