I am trying to get a 30 minutes repetitive task to send data over MQTT in a project of mine but I cannot get it to even reach 10 minutes (600000 milliseconds) without the device being reset and data never sent.
Hardware is an ESP32 WROOM dev board and I use Arduino IDE to program it.
Here is the code that gives me a headache as I cannot find an example for long delays/pauses on the internet so far.
void TaskIOADA(void *pvParameters) { // This is a task.
(void)pvParameters;
TickType_t xLastWakeTime;
const TickType_t xFrequency = 300000; //5 minutes cannot reach 10 minutes (600000) ==> reset and data never sent
// Initialise the xLastWakeTime variable with the current time.
xLastWakeTime = xTaskGetTickCount();
for (;;){
vTaskDelayUntil( &xLastWakeTime, xFrequency/ portTICK_PERIOD_MS );
io.run();
.... Other stuff being done there....
If someone knows how to get this task to run every 30 minutes, I would be grateful!
It has to be a task as I am running multiple different task on this device like blinking an LED + reading an analog sensor which could trigger the LED to get solid and stop readings for an extended period of time after closing a timed valve then reopening the timed valve + reading periodically 2 other sensors + sending data over MQTT every 30 minutes.
Understood, but you are using a task that 'never ends' you call it once and then it executes stay dormant and executes again...
You can simply call the task that executes an then exits, after the 30 minuts period you recall that task.... ( this way if you need to change the period... )
If I may add ( its a personal preference/consideration... ) the mqtt task has his reasons ( you could bind the task to a core different from the one used by sensor and so on ), as it may you may need to rejoing the net, the server, something unexpected...
...but a task for a 'simple task' as flashing a led or operating a valve it seems an unnecessary complication ( simply wondering how do these task communicate each other ) this can be simply done in the main task ( the loop ) using as many finite machine as needed each state machine do a simple task an then returns immediately, advancing to a different state when necessary ( you could add as many as needed of states machines and of states )
Believe me I have tried millis and it is just not feasible with my project, all examples you find everywhere are blinking LEDs only which is not helping at all. Operating a valve in a task makes a lot of sense since it will have to be actuated one way (open or closed) for a specific amount of time then switched off, then triggered according to external events, the valve status will be indicated by the LED.
Why a task to blink an LED? To not interfere with the other tasks as this LED will change state according to different status.
I agree with the task recall but I have yet to grasp the concept, all examples are too vague for me.
That is simply not true. There are literally thousands of examples in threads on this site that do all sorts of things.
If you understand how that code gets the blinking function to be called at intervals, then you can just replace that led line with anything you want as long as it is non-blocking.
I've been around this forum for 13 years now. I've seen this sentiment so many times. Believe me when I tell you that trying to use tasks and RTOS to get around learning how to write code that doesn't block is actually the harder thing to do. Because even with the RTOS you still need to understand the idea behind the other thing. And skipping that step and jumping straight to RTOS just becomes confusing. You're left with the options of copying working code and being satisfied or trying to change it and being frustrated because you don't understand it.
Believe me when I tell you from experience, this is easier the other way. It doesn't sound like it would be. But in reality the easier path for you lies in that direction. It's still a little hard, but not nearly as rough as the path you have chosen.
My point exactly. It's because you still haven't learned the vital lessons that come along with writing non-blocking that things like this still confuse you. Once you learn the non-blocking way, then if you still want to use RTOS you will at least understand how it works behind the scenes and you will better be able to take advantage of it.
So I do understand that everything lies in the number of ticks that seems to overflow something but I don't understand how to get around. I have tried your example with success using 5 minutes but any value above 5 minutes crashes the device and resets.
OP has been asked many times and has not yet seemed to notice. I think it may be time to mute this thread and let this one go. Who knows without the code and OP is not getting the hint that the problem isn't where they think it is.
The delay() function blocks everything else when called. All my libraries and boards up to date. This is the portion of the code which is the problem. Any value above 5 minutes resets the device.
No, not when Arduino is on top of FreeRTOS or Mbed. The multitasking system keeps on running.
In my post #11 I gave a link the the source code of delay() on a ESP32 and even the code itself.
We have a saying on this forum: "The problem is in the part that you are not showing".
I made up "Koepel's law": "Fifty percent chance that the problem is in the section that was ignored from the beginning, because you thought that the problem could not be caused by that section"
There is a website for snippets: https://snippets-r-us.com/
Then some of us look at the code and your results and that is an indication that the problem is probably somewhere else.
I just follow my feeling, my feeling says that you have to prove that there is problem with a sketch that we can try.
It has been answered now but here is an ESP32 RTOS traffic light application using delay() statements where a counter in one task is running during delay() statements in another task.