Inconsistent behavior of xTimerCreate() timer

dwrice0
Posts: 6
Joined: Mon Sep 09, 2024 4:37 pm

Inconsistent behavior of xTimerCreate() timer

Postby dwrice0 » Mon Sep 09, 2024 4:59 pm

I'm relatively new to ESP-IDF and programming an ESP32 but not programming in general. In my particular case, I'm trying to create a timer that will fire after 5 minutes on an ESP32-S3 (Seeed Studio Xiao S3 Sense). My specific timer is defined and started as follows:

Code: Select all

        wakeTimerHandle = xTimerCreate("Wake Timer", 300000 / portTICK_PERIOD_MS, pdFALSE, 0, onTimer);
        if(wakeTimerHandle == NULL) {
            ESP_LOGE(TAG, "Wake Timer Creation Failed");
        }
        else {
            xTimerStart(wakeTimerHandle, portMAX_DELAY);
            timerEnabled = true;
            ESP_LOGI(TAG, "Wake Timer Started");
        }
Of course, 300000 milliseconds is 300 seconds which is 5 minutes. The timer is not firing at 5 minutes. If left running, it eventually fires but this timing is not consistent. I feel like maybe I'm hitting some overflow scenario.

What suggests to me an overflow issue is that if I set it for 30000 ms (30 seconds) it works fine and same for 3000 ms.

MicroController
Posts: 1749
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Inconsistent behavior of xTimerCreate() timer

Postby MicroController » Tue Sep 10, 2024 1:37 pm

Are you putting the ESP to sleep while the timer is running?

dwrice0
Posts: 6
Joined: Mon Sep 09, 2024 4:37 pm

Re: Inconsistent behavior of xTimerCreate() timer

Postby dwrice0 » Tue Sep 10, 2024 2:45 pm

Not for this particular timer. The "wake" in the variable names are reflecting that it is the timer to determine how long it stays awake before going back to sleep.

What I am doing is putting the ESP to sleep with both esp_sleep_enable_ext0_wakeup() and esp_sleep_enable_timer_wakeup() so that I can either wake up on a trigger or at a regular time interval. After waking up by time interval, I start the timer in question to put the device back to sleep after 5 minutes. Basically, my goal eventually is to have this device wake up every hour, stay awake for period of time with the softAP, webserver and SD card enabled so I can retrieve data from it wirelessly and then go back to sleep. During testing, I was just using 5 minutes and couldn't get that work.

MicroController
Posts: 1749
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Inconsistent behavior of xTimerCreate() timer

Postby MicroController » Wed Sep 11, 2024 9:38 pm

Could you be inadvertently modifying or recreating the timer at some point during its runtime?

Need to see more code :)

dwrice0
Posts: 6
Joined: Mon Sep 09, 2024 4:37 pm

Re: Inconsistent behavior of xTimerCreate() timer

Postby dwrice0 » Thu Sep 12, 2024 1:49 pm

So, nothing obviously wrong with the basic timer code above?

Rather than post a wall of code, I'll do what I should have already done and create a simple program to just start a 5 minute timer and print something out when it fires. If that behaves the same, I'll post that code.

dwrice0
Posts: 6
Joined: Mon Sep 09, 2024 4:37 pm

Re: Inconsistent behavior of xTimerCreate() timer

Postby dwrice0 » Thu Sep 12, 2024 10:31 pm

This simple program seems to work just fine:

Code: Select all

#include <stdio.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"

static const char* TAG = "timertesting";
bool timerEnabled = false;
TimerHandle_t wakeTimerHandle = NULL;

static void onTimer(TimerHandle_t timer){
    timerEnabled = false;
    xTimerStop(timer, 0);
}

static void enableTimer() {
    wakeTimerHandle = xTimerCreate("Wake Timer", 300000 / portTICK_PERIOD_MS, pdFALSE, 0, onTimer);
    if(wakeTimerHandle == NULL) {
        ESP_LOGE(TAG, "Wake Timer Creation Failed");
    }
    else {
        xTimerStart(wakeTimerHandle, portMAX_DELAY);
        timerEnabled = true;
        ESP_LOGI(TAG, "Wake Timer Started");
    }
}

void app_main(void) {
    enableTimer();
    while (1) {
        if (timerEnabled==false) {
            printf("TimerFired!\n");
            enableTimer();
        }
        vTaskDelay(5000 / portTICK_PERIOD_MS);
        printf("Looping!\n");
        printf("Current ticks: %ld\n", xTaskGetTickCount());
    }
}

MicroController
Posts: 1749
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Inconsistent behavior of xTimerCreate() timer

Postby MicroController » Fri Sep 13, 2024 7:10 am

When sharing a (global) variable between different tasks (timerEnabled), that variable should at least be declared volatile. Otherwise, one task may miss the change another task made to its value.
Could there be a problem with inter-task communication like this in your original code?

dwrice0
Posts: 6
Joined: Mon Sep 09, 2024 4:37 pm

Re: Inconsistent behavior of xTimerCreate() timer

Postby dwrice0 » Fri Sep 13, 2024 2:35 pm

Thanks for that info. I will test and report back. I'm definitely weak in my knowledge of how some of the additional keywords for function and variable definitions that deal with how they are handled/placed in RAM or handled differently in a RTOS.

dwrice0
Posts: 6
Joined: Mon Sep 09, 2024 4:37 pm

Re: Inconsistent behavior of xTimerCreate() timer

Postby dwrice0 » Sun Sep 15, 2024 2:56 pm

OK. I figured out my issue and I'm pretty embarrassed to say what the issue was. It basically stems from the fact I let the code sit for awhile and came back to it not remembering my entire plan. What it came down to was that fact my strategy was that after the device woke up and started the timer and other things including wifi in AP mode, I had code to basically pause the timer if there was an active wifi connection so that if someone was in the middle of gathering data from the device it did not kick them off.

So, basically, in the wifi event handler on the connect event, I stopped the timer. And on the disconnect event, if the num connected was 0, then I'd restart the timer. I just happened to have devices that I had previously used to test the wifi portion with that still had the SSID remembered and they were randomly connecting and disconnecting causing random timer behavior.

I located the issue using basic tshooting skills of commenting out particular features one at a time to see which one caused the issue to show up.

Thanks @MicroController for being willing to assist.

MicroController
Posts: 1749
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Inconsistent behavior of xTimerCreate() timer

Postby MicroController » Sun Sep 15, 2024 4:49 pm

Good to hear you got it working :)

Who is online

Users browsing this forum: No registered users and 55 guests