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:
this is my code:.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
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);
}