FreeRTOS http only working once (IllegalInstruction)

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

FreeRTOS http only working once (IllegalInstruction)

Postby schnabulator » Mon Jan 22, 2024 10:30 pm

Hello
this is my arduino code for an esp32 which just setups wifi and then runs the setGoogleCalendar() function forever:
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3. #include "time.h"
  4.  
  5. // Replace with your network credentials
  6. const char* ssid = "FRITZ!Box 6490 Cable";
  7. const char* password = "xxxx";
  8.  
  9. // NTP server to request epoch time
  10. const char* ntpServer = "fritz.box";
  11.  
  12. const String gooleAppsScriptUrl = "https://script.google.com/macros/s/xxxxxxxxxxxx/exec";
  13.  
  14. RTC_DATA_ATTR int bootCount = 0;
  15.  
  16. //Multithreading
  17. TaskHandle_t googleCalendar;
  18.  
  19. void setup() {
  20.   // put your setup code here, to run once:
  21.   Serial.begin(115200);
  22.   WiFi.begin(ssid, password);
  23.   Serial.print("Connecting to WiFi ..");
  24.   while (WiFi.status() != WL_CONNECTED) {            
  25.     if(millis() < 12000){
  26.       Serial.print('.');
  27.       delay(1000);
  28.     }
  29.     else {
  30.       ESP.restart();
  31.     }
  32.   }
  33.   Serial.println(WiFi.localIP());
  34. }
  35.  
  36. void loop() {
  37.   // put your main code here, to run repeatedly:
  38.   if (googleCalendar == NULL){
  39.     xTaskCreate(
  40.     setGoogleCalendar
  41.     ,  "Google calendar request"
  42.     ,  20000  // Stack size
  43.     ,  NULL  // When no parameter is used, simply pass NULL
  44.     ,  1  // Priority
  45.     ,  &googleCalendar // With task handle we will be able to manipulate with this task.
  46.     );
  47.   }
  48. }
  49.  
  50. void setGoogleCalendar(void *pvParameters) {
  51.   (void) pvParameters;
  52.  
  53.   HTTPClient http;
  54.   http.begin(gooleAppsScriptUrl.c_str());
  55.   http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
  56.   String payload = "";
  57.   int httpStatus = http.GET();
  58.  
  59.   Serial.println("this is the last thing that gets printed in the second run");
  60.  
  61.   if (httpStatus == 200){
  62.     Serial.println("this will not get printed in the second run");
  63.     delay(500);
  64.     payload = http.getString();
  65.   }
  66.   else {
  67.     Serial.println("failed google calendar http request: " + httpStatus);
  68.     return;
  69.   }
  70.  
  71.   Serial.println(payload);
  72.  
  73.   http.end();
  74.   googleCalendar = NULL;
  75.   vTaskDelete(NULL);
  76. }
when i run the setGoogleCalendar function once everything works as it should. However the second time it only runs until the first Serial.println and spits out this error:
Guru Meditation Error: Core 0 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d29ec: 00250602 afa2f01d f0a0223c
Core 0 register dump:
PC : 0x400d29f0 PS : 0x00060130 A0 : 0x00000000 A1 : 0x3fffc790
A2 : 0xa38d7f39 A3 : 0x3fffc880 A4 : 0x00000000 A5 : 0x00000000
A6 : 0x00000000 A7 : 0x00000000 A8 : 0xa38d7f39 A9 : 0x3fffc770
A10 : 0x3fffc7bc A11 : 0x400d4d78 A12 : 0xa38d7f39 A13 : 0x00000000
A14 : 0x3fffc828 A15 : 0x3fffc838 SAR : 0x00000017 EXCCAUSE: 0x00000000
EXCVADDR: 0x00000000 LBEG : 0x400899bc LEND : 0x400899c7 LCOUNT : 0x00000000


Backtrace: 0x400d29ed:0x3fffc790




ELF file SHA256: a313233634d34ef3
when i put it in the stack trace decoder i get this result:
0x400d29ec: setGoogleCalendar(void*) at C:\Users\johan\OneDrive\Dokumente\Arduino\Freertostestwithtask_copy_20240122224400\Freertostestwithtask_copy_20240122224400.ino:76
0x400d29f0: setGoogleCalendar(void*) at C:\Users\johan\OneDrive\Dokumente\Arduino\Freertostestwithtask_copy_20240122224400\Freertostestwithtask_copy_20240122224400.ino:76
0x400d4d78: TLSTraits::~TLSTraits() at C:\Users\johan\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.11\libraries\HTTPClient\src\HTTPClient.cpp:64
0x400d29ed: setGoogleCalendar(void*) at C:\Users\johan\OneDrive\Dokumente\Arduino\Freertostestwithtask_copy_20240122224400\Freertostestwithtask_copy_20240122224400.ino:76
Due to the error occuring in the last line with only a bracket i expected it to have something to do with rtos tasks not being closed but according to espressif documentation this error should not occur.
Now i have noticed that core 0 panics which runs the wifi functions? and occurs in the http library. However this error only happens when i set the function as a seperate task and does not happen when i let it run normaly.
How is this behaviour possible?

Thank you for your help in advance :)

ESP_Sprite
Posts: 9715
Joined: Thu Nov 26, 2015 4:08 am

Re: FreeRTOS http only working once (IllegalInstruction)

Postby ESP_Sprite » Tue Jan 23, 2024 4:03 am

Not sure if it's the issue, but there's a 'return' in your task (when httpStatus is not 200). You cannot do that; that'll panic; you need to do the vTaskDelete(NULL) there as well.

Who is online

Users browsing this forum: iamloudness, noweare and 88 guests