Code: Select all
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <atomic>
#include <WebServer.h>
std::atomic<bool> taskRun(true);
TaskHandle_t taskHandle = NULL;
WebServer webServer(80);
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("==== START ====");
WiFi.mode(WIFI_STA);
WiFi.begin("ssid", "password");
while(WiFi.status() != WL_CONNECTED);
Serial.println(WiFi.localIP().toString());
webServer.on("/reset", []() {
webServer.send(200, "text/html", "ok");
resetTask();
});
webServer.begin();
}
void loop() {
// Watch the task and start it if necessary
if (!taskHandle || eTaskGetState(taskHandle) == eDeleted) { // sometimes the task finished but is still in eReady state, so it does not restart
Serial.println(millis());
taskRun.store(true);
xTaskCreatePinnedToCore(taskFunction, "task", 10000, NULL, 1, &taskHandle, 1);
Serial.println("task created");
}
webServer.handleClient(); // allow the user to stop and restart the task through the web server
if (Serial.available()){ // allow the user to stop and restart the task through the serial monitor
Serial.read();
//delay(500);
resetTask();
//delay(500);
}
}
void resetTask() {
taskRun.store(false);
Serial.println("reset task");
}
void taskFunction(void* pvParameters) {
while(taskRun.load()) {
// do work
taskYIELD(); // give cpu time to other tasks
}
Serial.println("task finished");
vTaskDelete(NULL);
}
What is happening, why does it sometimes work and sometimes not? Maybe I'm doing something weird or wrong, what would be a better way to acheive what I want?