Running TCP server and WebServer concurrently on ESP32 CAM

Ilan_espresiff
Posts: 1
Joined: Thu Feb 29, 2024 6:30 pm

Running TCP server and WebServer concurrently on ESP32 CAM

Postby Ilan_espresiff » Thu Feb 29, 2024 6:45 pm

Hello everyone.

I am trying to run a TCP server to recieve joystick coordinates in JSON format, and a WebServer that captures frames from the ESP32 camera, which can be accessed by sending HTTP requests to the url of the ESP32 CAM. When running the TCP server on its own on an ESP32 and WebServer on its own on an ESP32 CAM board, everything works fine. I am trying to write code to allow these tasks to run concurrently on the ESP32 CAM using freeRTOS.

Below is the code I have written (Note that the undefined functions here are defined in some other files that I import):

Code: Select all

#include "webserver_setup.h"
#include "tcpserver_setup.h"
 
const char* WIFI_SSID = "********";
const char* WIFI_PASS = "********";

TaskHandle_t Task1;
TaskHandle_t Task2;

SemaphoreHandle_t mutex;

#define TASK1_STACK_SIZE 5000
#define TASK2_STACK_SIZE 5000

void setup() {
  Serial.begin(115200);  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

  // connect to wifi
  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi");
    delay(500);
  }

  mutex = xSemaphoreCreateMutex();

  xTaskCreatePinnedToCore(
                    Task1code,   
                    "Task1",     
                    TASK1_STACK_SIZE,       
                    NULL,        
                    1,           
                    &Task1,      
                    0);                          
  delay(500); 

  xTaskCreatePinnedToCore(
                    Task2code,   
                    "Task2",    
                    TASK2_STACK_SIZE,     
                    NULL,       
                    1,          
                    &Task2,    
                    1);   
  delay(500);

  //Serial.println(uxTaskGetStackHighWaterMark(Task1));
  //Serial.println(uxTaskGetStackHighWaterMark(Task2));
}

void Task1code( void * pvParameters ){
  Serial.print("Task1 running on core ");
  Serial.println(xPortGetCoreID());

  // get high water mark once you enter the task
  UBaseType_t uxHighWaterMark;

  uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );  
  ws_setup();

  while(true){
    xSemaphoreTake(mutex, portMAX_DELAY);
    server.handleClient();
    uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
    xSemaphoreGive(mutex);
  } 
}

void Task2code( void * pvParameters ){
  Serial.print("Task2 running on core ");
  Serial.println(xPortGetCoreID());

  UBaseType_t uxHighWaterMark;
  uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
  
  tcp_setup();
  
  while(true){
    xSemaphoreTake(mutex, portMAX_DELAY);
    tcp_loop();
    uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
    xSemaphoreGive(mutex);
  }
}

void loop() {}


I keep getting a GuruMeditation error.

Code: Select all

Guru Meditation Error: Core  0 panic'ed (StoreProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x4008a22c  PS      : 0x00060830  A0      : 0x80086721  A1      : 0x3ffd89f0  
A2      : 0x00000400  A3      : 0x3ffe46c4  A4      : 0x3ffe56c4  A5      : 0x00060a23  
A6      : 0x00060a20  A7      : 0x00000001  A8      : 0x00000000  A9      : 0x000000ff  
A10     : 0x00000003  A11     : 0x00060a23  A12     : 0x00060a20  A13     : 0x3ffd8a58  
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x0000000c  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x00000000  LBEG    : 0x4008afd8  LEND    : 0x4008aff4  LCOUNT  : 0xffffffff  


Backtrace: 0x4008a229:0x3ffd89f0 0x4008671e:0x3ffd8a10 0x4010782d:0x3ffd8a30
Looking online, I found that if EXCVADDR is 0, then we are dereferencing a NULL pointer. When I view the stack trace, it always points back to some function in the esp32 cam library.

Code: Select all

PC: 0x4008a22c: ll_cam_dma_filter_jpeg at /Users/ficeto/Desktop/ESP32/ESP32S2/esp32-arduino-lib-builder/components/esp32-camera/target/esp32/ll_cam.c line 103
EXCVADDR: 0x00000000

Decoding stack results
0x4008a229: ll_cam_dma_filter_jpeg at /Users/ficeto/Desktop/ESP32/ESP32S2/esp32-arduino-lib-builder/components/esp32-camera/target/esp32/ll_cam.c line 103
0x4008671e: ll_cam_memcpy at /Users/ficeto/Desktop/ESP32/ESP32S2/esp32-arduino-lib-builder/components/esp32-camera/target/esp32/ll_cam.c line 482
0x4010782d: cam_task at /Users/ficeto/Desktop/ESP32/ESP32S2/esp32-arduino-lib-builder/components/esp32-camera/driver/cam_hal.c line 152
I'd like to kindly request for any insights into how I can write my code in such a way that this issue can be avoided if possible. Thank you very much.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 123 guests