Page 1 of 1

NTP Error

Posted: Wed Nov 06, 2024 11:29 am
by BeertoMA
Hi everyone!

I'm using Arduino IDE with one Lilygo T-ETH-Lite ESP32S3 and four ESP32S3.
Giving an static IP to Lilygo, I want to connect to a private NTP server and then, I want to read the time and send it trhough I2C protocol to other ESP32S3.
One week ago, my code worked perfectly. I could see the data transmission to the ESP32S3 slaves, but now I get an error. I think this error is about the NTP Server, but I can't see what is the error.

My code:

Code: Select all

// INCLUIR LIBRERÍAS NECESARIAS
#include <ETH.h>
#include <NTPClient.h>
#include <Wire.h>

// DATOS DIRECCIÓN IP 
IPAddress ip(10, 82, 103, 216);         // IP 
IPAddress gateway(10, 82, 103, 209);    // Puerta de enlace
IPAddress subnet(255, 255, 255, 240);   // Máscara de subred

// DATOS SERVIDOR NTP
const char* ntpServer = "172.24.147.139";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

// DIRECCIONES I2C
const int I2C_SDA = 19;
const int I2C_SCL = 20;
#define DIRECCION_ESCLAVO_8 8
#define DIRECCION_ESCLAVO_9 9
#define DIRECCION_ESCLAVO_10 10
#define DIRECCION_ESCLAVO_11 11

// DECLARAR VARIABLES, I2C
uint32_t i = 0;

// VARIABLES FECHA Y HORA
char horaAct[3];
uint8_t horaActEnv;
char minutosAct[3];
uint8_t minutosActEnv;
char segundosAct[3];
uint8_t segundosActEnv;
char diaAct[3];
uint8_t diaActEnv;
char mesAct[3];
uint8_t mesActEnv;
char anoAct[5];
uint8_t anoActEnv;
bool cambioHorario = true;
char diaActLet[4];
char diaActSem[2];

void setup() {
  // INCIALIZAR MONITOR SERIE
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  
  // INICIALIZAR I2C
  Wire.begin(I2C_SDA, I2C_SCL);

  // INICIALIZAR ETHERNET
  ETH.begin();
  ETH.config(IPAddress(ip), IPAddress(gateway), IPAddress(subnet));

  // INICIALIZAR CLIENTE NTP
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();
}

void loop() {
  // COMPROBAR SERVIDOR NTP
  if ( !ETH.begin() ){
    horaActEnv = -5;
  }

  // MOSTRAR HORA
  printLocalTime();

  // ENVIAR MENSAJE A ESCLAVO 8
    Wire.beginTransmission(DIRECCION_ESCLAVO_8);
    Wire.write(horaActEnv);
    Wire.write(minutosActEnv);
    Wire.write(segundosActEnv);
    Wire.write(diaActEnv);
    Wire.write(mesActEnv);
    Wire.write(anoActEnv);
    Wire.endTransmission();

  // ENVIAR MENSAJE A ESCLAVO 9
    Wire.beginTransmission(DIRECCION_ESCLAVO_9);
    Wire.write(horaActEnv);
    Wire.write(minutosActEnv);
    Wire.write(segundosActEnv);
    Wire.write(diaActEnv);
    Wire.write(mesActEnv);
    Wire.write(anoActEnv);
    Wire.endTransmission();

  // ENVIAR MENSAJE A ESCLAVO 10
    Wire.beginTransmission(DIRECCION_ESCLAVO_10);
    Wire.write(horaActEnv);
    Wire.write(minutosActEnv);
    Wire.write(segundosActEnv);
    Wire.write(diaActEnv);
    Wire.write(mesActEnv);
    Wire.write(anoActEnv);
    Wire.endTransmission();

  // ENVIAR MENSAJE A ESCLAVO 11
    Wire.beginTransmission(DIRECCION_ESCLAVO_11);
    Wire.write(horaActEnv);
    Wire.write(minutosActEnv);
    Wire.write(segundosActEnv);
    Wire.write(diaActEnv);
    Wire.write(mesActEnv);
    Wire.write(anoActEnv);
    Wire.endTransmission();     
  
  // EVITAR REBOTES
  delay(1000);
}

void printLocalTime(){
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  
  

  if ( ((mesAct[1] - 48) == 1) && ((mesAct[0] - 48) == 0) && ((diaActSem[0] - 48) == 7) && ((horaAct[1] - 48) == 2) && ( ((diaAct[1] - 48) == 3) || ( ((diaAct[1] - 48) == 2) && ( ((diaAct[0] - 48) == 6) || ((diaAct[0] - 48) == 7) || ((diaAct[0] - 48) == 8) || ((diaAct[0] - 48) == 9) ) ) ) ){
    cambioHorario = true;
  }
  if ( ((mesAct[1] - 48) == 0) && ((mesAct[0] - 48) == 3) && ((diaActSem[0] - 48) == 7) && ((horaAct[1] - 48) == 2) && ( ((diaAct[1] - 48) == 3) || ( ((diaAct[1] - 48) == 2) && ( ((diaAct[0] - 48) == 6) || ((diaAct[0] - 48) == 7) || ((diaAct[0] - 48) == 8) || ((diaAct[0] - 48) == 9) ) ) ) ){
    cambioHorario = false;
  }

  Serial.println(&timeinfo);

  strftime(horaAct,3, "%H", &timeinfo);
  
  if (cambioHorario = true){
    if ( (horaAct[0] == 2) && (horaAct[1] == 0) ){
      horaActEnv = ( (horaAct[0] - 49) * 10 ) + ( (horaAct[1]* 1) - 49);
    }else{
      horaActEnv = ( (horaAct[0] - 48) * 10 ) + ( (horaAct[1]* 1) - 49);
    }
  }else{
    horaActEnv = ( (horaAct[0] - 48) * 10 ) + ( (horaAct[1]* 1) - 48);
  }
  Serial.print("Hora actual: "); 
  Serial.println(horaActEnv);

  strftime(minutosAct,3, "%M", &timeinfo);
  minutosActEnv = ( (minutosAct[0] - 48) * 10 ) + ( (minutosAct[1]* 1) - 48);
  Serial.print("Minutos actuales: ");  
  Serial.println(minutosActEnv);

  strftime(segundosAct,3, "%S", &timeinfo);
  segundosActEnv = ( (segundosAct[0] - 48) * 10 ) + ( (segundosAct[1]* 1) - 48);
  Serial.print("Segundos actuales: ");  
  Serial.println(segundosActEnv);

  strftime(diaActLet, 4, "%a", &timeinfo);
  Serial.print("Día semana: ");
  Serial.println(diaActLet);

  strftime(diaActSem, 3, "%u", &timeinfo);
  Serial.print("Día de la semana: ");
  Serial.println(diaActSem[0]);

  strftime(diaAct,3, "%d", &timeinfo);
  diaActEnv = ( (diaAct[0] - 48) * 10 ) + ( (diaAct[1]* 1) - 48);
  Serial.print("Dia actual: ");  
  Serial.println(diaActEnv);

  strftime(mesAct,3, "%m", &timeinfo);
  mesActEnv = ( (mesAct[0] - 48) * 10 ) + ( (mesAct[1]* 1) - 48);
  Serial.print("Mes actual: ");  
  Serial.println(mesActEnv);

  strftime(anoAct,5, "%Y", &timeinfo);
  anoActEnv = ((anoAct[2] - 48 ) * 10) + ( (anoAct[3] - 48 ) * 1);
  Serial.print("Año actual: ");  
  Serial.print("20");
  Serial.println(anoActEnv);

  Serial.println("---------------------");
}
Serial monitor:
Rebooting...
���ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x29 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4037962e
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce2820,len:0x10f0
load:0x403c8700,len:0x4
load:0x403c8704,len:0xbec
load:0x403cb700,len:0x2fcc
entry 0x403c88ac

assert failed: sntp_setoperatingmode /IDF/components/lwip/lwip/src/apps/sntp/sntp.c:728 (Required to lock TCPIP core functionality!)


Backtrace: 0x403766ce:0x3fca3740 0x4037c569:0x3fca3760 0x40382d9d:0x3fca3780 0x4201a4e4:0x3fca38b0 0x42009242:0x3fca38d0 0x42003b14:0x3fca3940 0x4200a2d6:0x3fca3a20 0x4037d09a:0x3fca3a40




ELF file SHA256: 3f01dafe2

Thank you all.

Re: NTP Error

Posted: Thu Nov 07, 2024 8:33 am
by aliarifat794
Instead of calling configTime() immediately in setup(), wait until the Ethernet connection is established.

Code: Select all

 
 void setup() {
  // INCIALIZAR MONITOR SERIE
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  
  // INICIALIZAR I2C
  Wire.begin(I2C_SDA, I2C_SCL);

  // INICIALIZAR ETHERNET
  ETH.begin();
  ETH.config(ip, gateway, subnet);

  // Wait for Ethernet connection before initializing NTP
  while (!ETH.connected()) {
    delay(100);
    Serial.println("Waiting for Ethernet connection...");
  }

  // Now initialize NTP once Ethernet is connected
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();
}