ESP32 Dual Core, multitask not running

Hi Blynkers,

Following a @PeteKnight suggestion, I’m trying to use the dual core capabilty of ESP32 to avoid the blocking interference of BlynkEdgent on the normal processes of my devices.

Following the example of @Lichtsignaal on this post

I made a test with my device. It’s a very long code but I’ll post what I think are the relevant parts:

My setup:

  BlynkEdgent.begin();

  xTaskCreatePinnedToCore(loop2, "loop2", 4096, NULL, 1, NULL, 1);
  xTaskCreatePinnedToCore(loop1, "loop1", 4096, NULL, 2, NULL, 0);

And My loops:

void loop2(void *pvParameters) //loop para procesos BLYNK en Core1
{
  while (1) {
     BlynkEdgent.run();
     timer.run();
     delay(1);
  }
}

void loop1(void *pvParameters) //loop para procesos del dosificador en Core0
{
  while (1)
  {
    millis_actual = millis();
    if ( (millis_actual - millis_anterior) >= Intervalo_dosis)
     {
       dosificar = true;
       millis_anterior = millis_actual;
     }
    delay(1);
  }
}

void loop() {
  vTaskDelay(100);
}

But the device is in a endless loop of rebooting, showing this messages on the console:

Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered (loop2) 
Core  1 register dump:
PC      : 0x40130ed6  PS      : 0x00060536  A0      : 0x80131e49  A1      : 0x3ffcf9b0  
A2      : 0x3ffcfd70  A3      : 0x3ffcfeec  A4      : 0x3ffcfa70  A5      : 0x3ffcfa70  
A6      : 0x00000010  A7      : 0x00000018  A8      : 0x80133e8c  A9      : 0x3ffcfcd0  
A10     : 0x00000000  A11     : 0x00000000  A12     : 0x00000030  A13     : 0x3ffbaa2c  
A14     : 0x3ffc56ac  A15     : 0x3ffb6c68  SAR     : 0x0000000f  EXCCAUSE: 0x00000001  
EXCVADDR: 0x00000000  LBEG    : 0x40089a20  LEND    : 0x40089a2b  LCOUNT  : 0x00000000  


Backtrace:0x40130ed3:0x3ffcf9b00x40131e46:0x3ffcfd10 0x40131ebb:0x3ffcfd30 0x40131ef5:0x3ffcfd50 0x4012dfe4:0x3ffcfd70 0x401285f6:0x3ffcfe70 0x401286ea:0x3ffcfee0 0x40128736:0x3ffcff90 0x401288b5:0x3ffd0010 0x401245f1:0x3ffd0030 0x401246ce:0x3ffd01e0 0x401247ad:0x3ffd0200 0x4012487d:0x3ffd0270 0x40121f56:0x3ffd0290 0x40122f5f:0x3ffd02c0 0x401258f6:0x3ffd0310 0x40126d2d:0x3ffd0340 0x40124ddd:0x3ffd04a0 0x40124fdc:0x3ffd04d0 0x4011fc10:0x3ffd04f0 0x4011d6d5:0x3ffd0590 0x4011d70f:0x3ffd05b0 0x400da819:0x3ffd05d0 0x400da11e:0x3ffd0860 0x400da226:0x3ffd08a0 0x400d61c2:0x3ffd08c0 0x400d63b5:0x3ffd08f0 0x400d64a7:0x3ffd0920 0x400d9858:0x3ffd0940 




ELF file SHA256: 0000000000000000

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1184
load:0x40078000,len:13160
load:0x40080400,len:3036
entry 0x400805e4

I added the vTaskDelay(100); on the void loop() following a suggestion from a forum but did’n worked.

Any ideas of what is going on?

Pete.

Thanks @PeteKnight , before reading your repply, I made the following change:

In my setup I just created one task for my device processes:

xTaskCreatePinnedToCore(loop1, "loop1", 4096, NULL, 2, NULL, 0);

And I created just a new loop for my device processes and keeped the original loop for the Blynk processes:

void loop1(void *pvParameters) //loop para procesos del dosificador en Core0
{
  while (1)
  {
    millis_actual = millis();
    if ( (millis_actual - millis_anterior) >= Intervalo_dosis)
     {
       dosificar = true;
       millis_anterior = millis_actual;
     }
    delay(1);
  }
}

void loop() {
  BlynkEdgent.run();
  timer.run();
}

And it worked!

Now I’ll made some testing to confirm if the Edgent routines when the wifi goes off doesn’t block my devices process.

1 Like

Excuse me sir, Could you reconnect your ESP32 after it goes offline?

Hi @rickaves. Yes, the device reconects as soon as the wifi goes online again.

But by the moment I still notice a long blocking time when WiFi is off, of about 50 seconds. I think this is related to the Edgent attemps for reconecting.

This timeouts in Settings.h have to be the responsible:

#define WIFI_NET_CONNECT_TIMEOUT      50000
#define WIFI_CLOUD_CONNECT_TIMEOUT    50000

Do you mind if I see the part of the code where you achieve to reconnect your device?
I’m struggling with it :frowning:

OK, first, to answer @rickaves question, I’m using BlynkEdgent, Edgent add to your sketch a lot of processes that manages all the wifi, comissioning, led indicator a lot more. Please, Edgent authors forgive me.

Well. I have good news. I modified the edgent code in order to check if moving BlynkEdgent processes to the second core (Core 0), and keeping my own processes ins the main core (Core 1) will assure that my devices processes will keep running wthout being blocked by the Blynk Wifi reconection.

And it worked!

This is the Edgent example modified. Thanks @PeteKnight for the hint for solving the Stack overflow issue.

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID           "XXXXX"
#define BLYNK_DEVICE_NAME           "XXXXX"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
#define USE_ESP32S2_DEV_KIT

#include "BlynkEdgent.h"

unsigned long millis_anterior;
unsigned long millis_actual;
unsigned long Intervalo_dosis = 0;
unsigned long Ciclo_dosis;
bool dosificar = false;

void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();
  xTaskCreatePinnedToCore(loop1, "loop1", 8192, NULL, 1, NULL, 0);
}

void loop1(void *pvParameters) //loop para procesos de BLYNK en Core0
{
  while (1)
  {
    BlynkEdgent.run();
    delay(1);
//    timer.run();    
  }
}

void loop() {
  millis_actual = millis();
    if ( (millis_actual - millis_anterior) >= 30000)
    {
       dosificar = true;
       millis_anterior = millis_actual;
       Serial.println("proceso Dispositivo");
    }
}

My device have to do a task every 30 seconds, even if the device is offline. (this code still assume that at the power ON the wifi will available to connect Blynk Cloud…

This is the result of the test (serial port):

18:48:35.360 -> [221] 
18:48:35.360 ->     ___  __          __
18:48:35.360 ->    / _ )/ /_ _____  / /__
18:48:35.360 ->   / _  / / // / _ \/  '_/
18:48:35.360 ->  /____/_/\_, /_//_/_/\_\
18:48:35.360 ->         /___/ v1.1.0 on ESP32
18:48:35.360 -> 
18:48:35.360 ->  #StandWithUkraine    https://bit.ly/swua
18:48:35.360 -> 
18:48:35.360 -> 
18:48:35.360 -> [231] --------------------------
18:48:35.360 -> [231] Product:  Dosif Continuo 4Ch
18:48:35.407 -> [241] Firmware: 0.1.18 (build Aug 25 2022 17:47:45)
18:48:35.407 -> [242] Token:    XjFX - •••• - •••• - ••••
18:48:35.407 -> [252] Device:   ESP32 @ 240MHz
18:48:35.407 -> [252] MAC:      30:C6:F7:30:6B:C4
18:48:35.407 -> [252] Flash:    4096K
18:48:35.407 -> [253] ESP sdk:  v4.4.1-472-gc9140caf8c
18:48:35.407 -> [263] Chip rev: 1
18:48:35.407 -> [263] Free mem: 243864
18:48:35.407 -> [263] --------------------------
18:48:35.407 -> 
18:48:35.407 -> >[264] INIT => CONNECTING_NET
18:48:35.407 -> [267] Connecting to WiFi: Wifi-Test
18:48:36.815 -> [1655] Using Dynamic IP: 192.168.72.76
18:48:36.815 -> [1658] CONNECTING_NET => CONNECTING_CLOUD
18:48:36.815 -> [1668] Connecting to blynk.cloud:443
18:48:38.030 -> [2877] Certificate OK
18:48:38.217 -> [3088] Ready (ping: 205ms).
18:48:38.311 -> [3158] CONNECTING_CLOUD => RUNNING
18:49:05.118 -> proceso Dispositivo
18:49:35.150 -> proceso Dispositivo
18:50:05.144 -> proceso Dispositivo
18:50:08.130 -> [92991] RUNNING => CONNECTING_NET
18:50:08.130 -> [92995] Connecting to WiFi: Wifi-Test
18:50:35.148 -> proceso Dispositivo
18:50:58.165 -> [143006] Connecting to WiFi: Wifi-Test
18:51:05.147 -> proceso Dispositivo
18:51:17.922 -> [162767] Using Dynamic IP: 192.168.72.76
18:51:17.922 -> [162767] CONNECTING_NET => CONNECTING_CLOUD
18:51:17.922 -> [162779] Connecting to blynk.cloud:443
18:51:19.179 -> [164030] Certificate OK
18:51:19.603 -> [164450] Ready (ping: 416ms).
18:51:19.650 -> [164521] CONNECTING_CLOUD => RUNNING
18:51:35.139 -> proceso Dispositivo
18:52:05.122 -> proceso Dispositivo
18:52:35.144 -> proceso Dispositivo
18:53:05.147 -> proceso Dispositivo
18:53:11.194 -> [276058] RUNNING => CONNECTING_NET
18:53:11.194 -> [276063] Connecting to WiFi: Wifi-Test
18:53:35.127 -> proceso Dispositivo
18:54:01.217 -> [326079] Connecting to WiFi: Wifi-Test
18:54:05.140 -> proceso Dispositivo
18:54:35.145 -> proceso Dispositivo
18:54:51.211 -> [376086] Connecting to WiFi: Wifi-Test
18:55:05.107 -> proceso Dispositivo
18:55:35.120 -> proceso Dispositivo
18:55:41.247 -> [426100] Connecting to WiFi: Wifi-Test
18:56:05.129 -> proceso Dispositivo
18:56:31.243 -> [476117] Connecting to WiFi: Wifi-Test
18:56:35.126 -> proceso Dispositivo
18:57:05.128 -> proceso Dispositivo
18:57:21.255 -> [526131] Connecting to WiFi: Wifi-Test
18:57:35.120 -> proceso Dispositivo
18:58:05.099 -> proceso Dispositivo
18:58:11.282 -> [576140] Connecting to WiFi: Wifi-Test
18:58:35.104 -> proceso Dispositivo
18:59:01.299 -> [626159] Connecting to WiFi: Wifi-Test
18:59:05.131 -> proceso Dispositivo
18:59:35.103 -> proceso Dispositivo
18:59:51.284 -> [676177] Connecting to WiFi: Wifi-Test
19:00:05.111 -> proceso Dispositivo
19:00:35.107 -> proceso Dispositivo
19:00:41.301 -> [726195] Connecting to WiFi: Wifi-Test
19:01:05.126 -> proceso Dispositivo
19:01:31.334 -> [776209] Connecting to WiFi: Wifi-Test
19:01:35.137 -> proceso Dispositivo
19:02:05.109 -> proceso Dispositivo
19:02:21.320 -> [826219] Connecting to WiFi: Wifi-Test
19:02:35.114 -> proceso Dispositivo
19:03:05.110 -> proceso Dispositivo
19:03:11.359 -> [876233] Connecting to WiFi: Wifi-Test
19:03:20.392 -> [885279] Using Dynamic IP: 192.168.72.76
19:03:20.392 -> [885279] CONNECTING_NET => CONNECTING_CLOUD
19:03:20.392 -> [885292] Connecting to blynk.cloud:443
19:03:21.936 -> [886832] Certificate OK
19:03:22.169 -> [887038] Ready (ping: 201ms).
19:03:22.216 -> [887108] CONNECTING_CLOUD => RUNNING
19:03:35.132 -> proceso Dispositivo

As you can see, every 30 seconds, with or without Wifi, my own task will de done by the device, despite the edgent status.

As soon as the wifi is recovered, the Blynk connection will be.

Just a trick, in order to avoid the hardware reset Edgent will do if the wifi doesn’t recover in a long time, I modified the ConfigMode.h in Edgente as was suggested in some post i don’t remember (Be patient @PeteKnight).

void enterError() {
  BlynkState::set(MODE_ERROR);

  unsigned long timeoutMs = millis() + 10000;
  while (timeoutMs > millis() || g_buttonPressed)
  {
    delay(10);
    app_loop();
    if (!BlynkState::is(MODE_ERROR)) {
      return;
    }
  }
  BlynkState::set(MODE_CONNECTING_NET); //Add this line and comment the original to just try to connect again without HW reset.
  /*DEBUG_PRINT("Restarting after error.");
  delay(10);

  restartMCU();*/
}
4 Likes

a new test. If you start this code withou having wifi, the device own task will be done at time too:

19:18:24.682 -> [1789614] RUNNING => CONNECTING_NET
19:18:24.682 -> [1789619] Connecting to WiFi: Wifi-Test
19:18:28.332 -> ets Jun  8 2016 00:22:57
19:18:28.332 -> 
19:18:28.332 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
19:18:28.332 -> configsip: 0, SPIWP:0xee
19:18:28.332 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
19:18:28.332 -> mode:DIO, clock div:1
19:18:28.332 -> load:0x3fff0030,len:1184
19:18:28.332 -> load:0x40078000,len:13160
19:18:28.332 -> load:0x40080400,len:3036
19:18:28.332 -> entry 0x400805e4
19:18:28.895 -> [220] 
19:18:28.895 ->     ___  __          __
19:18:28.895 ->    / _ )/ /_ _____  / /__
19:18:28.895 ->   / _  / / // / _ \/  '_/
19:18:28.895 ->  /____/_/\_, /_//_/_/\_\
19:18:28.895 ->         /___/ v1.1.0 on ESP32
19:18:28.895 -> 
19:18:28.895 ->  #StandWithUkraine    https://bit.ly/swua
19:18:28.895 -> 
19:18:28.895 -> 
19:18:28.895 -> [230] --------------------------
19:18:28.895 -> [230] Product:  Dosif Continuo 4Ch
19:18:28.895 -> [241] Firmware: 0.1.18 (build Aug 25 2022 17:47:45)
19:18:28.895 -> [241] Token:    XjFX - •••• - •••• - ••••
19:18:28.895 -> [251] Device:   ESP32 @ 240MHz
19:18:28.895 -> [251] MAC:      30:C6:F7:30:6B:C4
19:18:28.895 -> [252] Flash:    4096K
19:18:28.895 -> [252] ESP sdk:  v4.4.1-472-gc9140caf8c
19:18:28.895 -> [262] Chip rev: 1
19:18:28.942 -> [262] Free mem: 243864
19:18:28.942 -> [263] --------------------------
19:18:28.942 -> 
19:18:28.942 -> >[263] INIT => CONNECTING_NET
19:18:28.942 -> [265] Connecting to WiFi: Wifi-Test
19:18:58.659 -> proceso Dispositivo
19:19:18.941 -> [50274] Connecting to WiFi: Wifi-Test
19:19:28.660 -> proceso Dispositivo
19:19:58.641 -> proceso Dispositivo
19:20:08.928 -> [100289] Connecting to WiFi: Wifi-Test
19:20:28.648 -> proceso Dispositivo
19:20:58.650 -> proceso Dispositivo
19:20:58.928 -> [150299] Connecting to WiFi: Wifi-Test
19:21:28.653 -> proceso Dispositivo
19:21:48.970 -> [200313] Connecting to WiFi: Wifi-Test
19:21:58.643 -> proceso Dispositivo
19:22:28.633 -> proceso Dispositivo
19:22:38.966 -> [250323] Connecting to WiFi: Wifi-Test
4 Likes

Hi sunnyref

This code work if wifi in never active?
let me explain better i am creating a device that has to work with blynk or without blynk

Thanks

Best Regards

Yes, but I suggest to change the Edgent process to Core1 (Main) and Device processes to Core0 (secondary)

Thank i try this

Best Regards

ok work for me
but i don’t understand how to move from core 0 to core 1
who can help me?

Best regards