Compréhension fonctionnement bibliotheque NTP

Bonjour,

Je suis en train de faire un data Logger de T° avec un ESP32. je recupere l'heure grace à la connexion sur un serveur NPT.
Tout fonctionne bien, je recupere bien l'heure et la température en message serie.
Par contre, je ne vois pas l'ESP sur mon reseau et j'ai l'impression qu'il ne se connecte au wifi que quelques secondes pendant le setup.
Comment fait donc la bibliotheque pour afficher la bonne heure toutes les secondes. Est-ce qu'elle utilise le timer interne de l'esp?

il y'a cette partie à la fin du setup
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);

Voici mon code:

//*
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h> 
#include <time.h>

#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define SENSOR_PIN 14 // The ESP32 pin GPIO14 connected to DS18B20 sensor's data pin

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library

String temperature;
////////////////////////////////////////////// PARTIE WIFI+NPT

const char* ssid       = "xxxxx";
const char* password   = "xxxxx";

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

/////////////////////////////////////////

void printLocalTime() // Fonction affichage date
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}



void setup() {
  Serial.begin(115200);

  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }

  delay(2000);         // wait for initializing
  oled.clearDisplay(); // clear display

  oled.setTextSize(2);          // text size
  oled.setTextColor(WHITE);     // text color
  oled.setCursor(0, 10);        // position to display

  DS18B20.begin();     // initialize the sensor
  temperature.reserve(10); // to avoid fragmenting memory when using String

  Serial.println(temperature); // print the temperature in Celsius to Serial Monitor
  oledDisplayCenter(temperature);

  ////////////////////////////////
    //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  Serial.print("Adresse IP: ");
  Serial.println(WiFi.localIP()); 

  
  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);

}

void loop() {
  DS18B20.requestTemperatures();             // send the command to get temperatures
  float tempCelsius = DS18B20.getTempCByIndex(0);  // read temperature in Celsius

  temperature  = String(tempCelsius, 2); // two decimal places
  temperature += "°C";
  

  Serial.println(temperature); // print the temperature in Celsius to Serial Monitor
  oledDisplayCenter(temperature);
  printLocalTime();

}

///////////////////////////////////////////////////////////////////////////
void oledDisplayCenter(String text) {
  int16_t x1;
  int16_t y1;
  uint16_t width;
  uint16_t height;

  oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);

  // display on horizontal and vertical center
  oled.clearDisplay(); // clear display
  oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
  oled.println(text); // text to display
  oled.display();
}

Salut.

Oui. Lorsque le serveur NTP est disponible, la mise à jour de l'heure interne de l'ESP32 a lieu toutes les heures.

L'offset par rapport à GMT n'est pas constant en France. Si tu désires obtenir l'heure locale en tenant compte de la timezone :

  configTzTime("CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", ntpServer);

Ainsi, les changements d'heure été / hiver auront bien lieu.

Merci beaucoup pour cette reponse.
Mais si l'ESP se deconnecte du wifi en fin de setup, il n'y a pas de re-synchronisation automatique possible?
Dans les faits, je pense que je ne serai pas embété car j'ai enlevé ces lignes de deconnexion car je vais avoir besoin de la connexion wifi pour transferer des données mais j'étais surpris du fonctionnement.

Forcément.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.