So I'm using GPRS module with ESP32 to do https request to google maps API. It works fine with TinyGsmClient library. I get the result that I want. But then, I migrate it to FreeRTOS so I can multitask, but it doesn't work as fine. My FreeRtos code look like:
void httpsGETTask(void *pvParameters) {
while(1){
int timer=esp_timer_get_time(); //I put timer here and at the end of the code to see how long this task executed
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println(" fail");
vTaskDelay(pdMS_TO_TICKS(10000));
return;
}
SerialMon.print(F("Performing HTTPS GET request... "));
http.connectionKeepAlive(); // Currently, this is needed for HTTPS
int err = http.get(resource);
if (err != 0) {
SerialMon.println(F("failed to connect"));
vTaskDelay(pdMS_TO_TICKS(10000));
return;
}
int status = http.responseStatusCode();
SerialMon.print(F("Response status code: "));
SerialMon.println(status);
if (!status) {
vTaskDelay(pdMS_TO_TICKS(10000));
return;
}
String body = http.responseBody();
String instruction= findHtmlInstructions(body);
SerialMon.println(F("Response:"));
SerialMon.println(instruction);
int duration=esp_timer_get_time()-timer;
SerialMon.print("duration: ");
SerialMon.println(duration);
}
}
// Task handles
TaskHandle_t httpsTaskHandle = NULL;
void setup() {
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// !!!!!!!!!!!
// Set your reset, enable, power pins here
// !!!!!!!!!!!
SerialMon.println("Wait...");
setupGSM();
SerialMon.println("Connecting: ");
xTaskCreate(httpsGETTask, "HTTPTask", 10*1024, NULL, 1, &httpsTaskHandle);
delay(10);
SerialMon.println("Connecting done ");
}
void loop() {
// The loop function is not used in this example.
vTaskDelete(nullptr);
}
At first try I got
E (22228) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (22228) task_wdt: - IDLE (CPU 0)
E (22228) task_wdt: Tasks currently running:
E (22228) task_wdt: CPU 0: HTTPTask
E (22228) task_wdt: CPU 1: IDLE
E (22228) task_wdt: Aborting.
That mean the task took longer then watchdog timer. Well, I have done https request before using Wifi but this never happened, so I guess GPRS is much slower so it wait too long for the response.
I searched for the solution online, and found that I should put this task at lower priority. So I change it to:
xTaskCreate(httpsGETTask, "HTTPTask", 50*1024, NULL, 0, &httpsTaskHandle);
and the error was solved, but I found that it took so long for the request to finish. I put some timer mechanism, and found that the normal code took around 15s to executed while task with freeRTOS took around 37s to executed.
Edit:
I look at it again, and found that it took 37s for the task, but only 10s for the https request, and the rest is to get the string answer:
String body = http.responseBody();
This one line took 27 s
Is it normal that the RTOS task took so long?