ESP32 weird http requests when using freertos tasks

schnabulator
Posts: 3
Joined: Mon Jan 22, 2024 8:54 pm

ESP32 weird http requests when using freertos tasks

Postby schnabulator » Sat Jan 27, 2024 11:16 pm

Hi,
This is my project on an esp32 programmed in arduino where i try the text of a google apps script in a seperate task. Everything works fine until i push the google caledar method in a seperate task, then it will always fail to do the request after the second time and later just stop requesting at all, but just create a new task every time. You can see this in the console:
.192.168.178.60
creating task
task created
this is the last thing that gets printed in the second run
this will not get printed in the second run

creating task
task created
this is the last thing that gets printed in the second run
this will not get printed in the second run

creating task
task created
this is the last thing that gets printed in the second run
failed google calendar http request: -1
creating task
creating task
creating task
creating task
creating task
creating task
creating task
this is my code:

Code: Select all

#include <WiFi.h>
#include <HTTPClient.h>
#include "time.h"

// Replace with your network credentials
const char* ssid = "FRITZ!Box 6490 Cable";
const char* password = "xxx";

// NTP server to request epoch time
const char* ntpServer = "fritz.box";

const String gooleAppsScriptUrl = "https://script.google.com/macros/s/xxx/exec";

RTC_DATA_ATTR int bootCount = 0;

//Multithreading
TaskHandle_t googleCalendar;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {             
    if(millis() < 12000){
      Serial.print('.');
      delay(1000);
    }
    else {
      ESP.restart();
    }
  }
  Serial.println(WiFi.localIP());
}

void loop() {
  
  // put your main code here, to run repeatedly:
  if (googleCalendar == NULL){
    Serial.println("creating task");
    xTaskCreate(
    setGoogleCalendar
    ,  "Google calendar request"
    ,  20000  // Stack size
    ,  NULL  // When no parameter is used, simply pass NULL
    ,  1  // Priority
    ,  &googleCalendar // With task handle we will be able to manipulate with this task.
    );
  }
  delay(500);
}

void setGoogleCalendar(void *pvParameters) {
  (void) pvParameters;
  Serial.println("task created");
  
  if(WiFi.status() != WL_CONNECTED){
    Serial.println("google Calendar error: no WiFi"); 
    googleCalendar = NULL;
    vTaskDelete(NULL);
  }
  HTTPClient http;
  http.begin(gooleAppsScriptUrl.c_str());
  http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
  String payload = "";
  int httpStatus = http.GET();

  Serial.println("this is the last thing that gets printed in the third run");
  if (httpStatus == 200){
    Serial.println("this will not get printed in the third run");
    payload = http.getString();
  }
  else {
    http.end();
    Serial.println("failed google calendar http request: " + String(httpStatus));
    googleCalendar = NULL;
    vTaskDelete(NULL);
  }
  
  Serial.println(payload);

  http.end();
  
  googleCalendar = NULL;
  vTaskDelete(NULL);
}
Thank you so much for your help

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 weird http requests when using freertos tasks

Postby lbernstone » Sun Jan 28, 2024 11:28 am

Check to make sure that your URL is still correct (I figure you have done this). Figure out what response you are getting that isn't == 200.
Some sites will respond poorly to abandoned connections. Adding a Connection: close header may help with this. I would recommend that instead of building/tearing down your task every time, you use vTaskSuspend. That way, you can use the same HTTPClient (and socket connection) every time you query, and save yourself some cycles.

schnabulator
Posts: 3
Joined: Mon Jan 22, 2024 8:54 pm

Re: ESP32 weird http requests when using freertos tasks

Postby schnabulator » Sun Jan 28, 2024 8:55 pm

Thank you so much for your response. Using the same http client for every connection actually solved the problem. Should i stil add an connection close header or is this unnecessary now? Thank you again, i was starting to despair

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 weird http requests when using freertos tasks

Postby lbernstone » Mon Jan 29, 2024 9:01 am

Good hygiene prevents future problems. If there will be a real gap between uses (> 60 secs), then close the session. Also, brush your teeth after every meal.

Who is online

Users browsing this forum: No registered users and 43 guests