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();
}