sntp_set_time_sync_notification_cb

Xinonix
Posts: 1
Joined: Fri Jun 28, 2024 7:42 pm

sntp_set_time_sync_notification_cb

Postby Xinonix » Fri Jun 28, 2024 7:56 pm

I have an application that uses ntp to set the esp32-s3 time and internal rtc. I use sntp_set_time_sync_notification_cb callback to sync ntp and internal time to rtc. The callback function get executed but stops working after a while. I have a loop that executes on xSemaphoreTake set by timerinterrupt. outside that xSemaphoreTake I have put vTaskDelay(10 / portTICK_PERIOD_MS); to yield execution to whatever runs in the background.
The callback is as follows:

Code: Select all

void timeSyncCallback(struct timeval* tv) {
  struct tm timeinfo;
  Serial.printf("Time dif: tv->tv_sec - rtc.getEpoch() : %ld\n", tv->tv_sec - rtc.getEpoch());
  if (tv->tv_sec - rtc.getEpoch() && getLocalTime(&timeinfo)) {
    rtc.setTimeStruct(timeinfo);
  }
}
The loop function is as follows:

Code: Select all

void loop() {
    static theSensor sensor = theSensor(false);  //false = test data
  static int looping = 0;
  static unsigned long now = 0;
  if (xSemaphoreTake(minuteSemaphore, 0) == pdTRUE) {
    now = (rtc.getEpoch());
    vector<float> sensorData = sensor.getSensorData();
    Observation m = Observation(now, sensorData);
    HourCollection.push(m);
    if (rtc.getMinute() == 0) {  //on the hour
      Observation h = HourCollection.getAverage(now);
      DayCollection.push(h);
      Serial.println(DayCollection.toString());
      if (rtc.getHour() == 0) {  //on midnight
        Observation d = DayCollection.getAverage(now);
        YearCollection.push(d);
        Serial.println(YearCollection.toString());
      }
    }
    if (++looping == 5) {
      looping = 0;
      Serial.println("");
      Serial.println(HourCollection.toString());
      Serial.println(HourCollection.JsonString());
    } else {
      Serial.print(".");
    }
    vTaskDelay(10 / portTICK_PERIOD_MS);
  }
}
The timer function:

Code: Select all

void ARDUINO_ISR_ATTR onMinute() {
  // portENTER_CRITICAL_ISR(&timerMux);
  // trigger = millis();  //rtc.getEpoch();
  // portEXIT_CRITICAL_ISR(&timerMux);
  xSemaphoreGiveFromISR(minuteSemaphore, NULL);
}
and timer setup:

Code: Select all

minuteSemaphore = xSemaphoreCreateBinary();
  // Set timer frequency to 1Mhz
  mTimer = timerBegin(1000000);
  // Attach onMinute function to timer.
  timerAttachInterrupt(mTimer, &onMinute);
  // Set alarm to call onMinute function every second (value in microseconds).
   timerAlarm(mTimer, 1000000 * 60, true, 0);
ANy idea why the call back does not respond after the inital responses?

Who is online

Users browsing this forum: No registered users and 32 guests