Blynk with RTOS

Hello,

I am currently working on a IoT controller for some automatic windows in my roof. Therefore, it is very important, that the ESP32 I use automatically connects on powerup and reconnects if it looses the WiFi connection. While researching, I found that using the ESPs RTOS (Real Time Operating System) to pin a task to core 0 of the ESP to work very well. It automatically connects and tries to reconnect if it looses its connection.

TaskHandle_t task1;
#include <WiFi.h>

const char* ssid     = "mySSID";
const char* password = "myPass";

#define timeout 20000
#define recovery 30000

void setup() {
  Serial.begin(115200);
  xTaskCreatePinnedToCore(codeForTask1, "Task 1", 6000, NULL, 1, &task1, 0);
}

void loop() {
}

void codeForTask1(void * parameter){
  for(;;){
    if(WiFi.status() == WL_CONNECTED){
      vTaskDelay(10000 / portTICK_PERIOD_MS);
      continue;
    }
    
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);

    unsigned long startAttemptTime = millis();
    
    while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < timeout){      
    }

    if(WiFi.status() != WL_CONNECTED){
      Serial.println("Failed to establish connection!");
      vTaskDelay(recovery / portTICK_PERIOD_MS);
      continue;
    }
    Serial.println("Connected! Hooray!");
  } 
}

However, I cant figure out how to use this routine with Blynk, eventhough the WiFi.begin() command is part of the Blynk.begin() statement. My results all ended up crashing the ESP or resulting in bootloops.

So if any of you knows how to do this, please let me now :slight_smile:

Thank you!

If you’re managing your own network connection then I think you need to switch to using Blynk.config and Blynk.connect rather than Blynk.begin…

You’ll also need to include the Blynk ESP32 library and add Blynk.run to your void loop of course.

Pete.

Yeah I tried this:

    TaskHandle_t task1;

    #define BLYNK_PRINT Serial

    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <BlynkSimpleEsp32.h>

    const char* ssid     = "mySSID";
    const char* password = "myPass";
    char auth[] = "myAuth";

    #define timeout 20000
    #define recovery 30000

    void setup() {
      Serial.begin(115200);
      xTaskCreatePinnedToCore(codeForTask1, "Task 1", 6000, NULL, 1, &task1, 0);

    }

    void loop() {
      Blynk.run();
    }

    void codeForTask1(void * parameter){
      for(;;){
        if(WiFi.status() == WL_CONNECTED){
          vTaskDelay(10000 / portTICK_PERIOD_MS);
          continue;
        }
        
        Serial.print("Connecting to ");
        Serial.println(ssid);
        WiFi.begin(ssid, password);

        unsigned long startAttemptTime = millis();
        
        while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < timeout){      
        }

        if(WiFi.status() != WL_CONNECTED){
          Serial.println("Failed to establish connection!");
          vTaskDelay(recovery / portTICK_PERIOD_MS);
          continue;
        }
        Serial.println("Connected! Hooray!");
        Blynk.config(auth);
        Blynk.connect(3333);
      } 
    }

But it results in Bootloops. Sometime ist says “Connecting to 0.0.0.0” or something similar, but it randomly disconnects and reboots.

Backtrace: 0x4008c7f4:0x3ffbe160 0x4008ca25:0x3ffbe180 0x400db43f:0x3ffbe1a0 0x40081b51:0x3ffbe1c0 0x400837c1:0x3ffb3710 0x400d24c2:0x3ffb3730 0x40088799:0x3ffb3750

Where do I have to place the Blynk statements to get this to work?

The Serial monitor looks like this:

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:0x3fff0018,len:4
load:0x3fff001c,len:1100
load:0x40078000,len:9564
ho 0 tail 12 room 4
load:0x40080400,len:6320
entry 0x400806a8
Connecting to xxxxxxx
[5001] Connecting to 0.0.0.0
E (5100) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (5100) task_wdt:  - IDLE0 (CPU 0)
E (5100) task_wdt: Tasks currently running:
E (5100) task_wdt: CPU 0: wifi
E (5100) task_wdt: CPU 1: IDLE1
E (5100) task_wdt: Aborting.
abort() was called at PC 0x400db43f on core 0

Backtrace: 0x4008c7f4:0x3ffbe160 0x4008ca25:0x3ffbe180 0x400db43f:0x3ffbe1a0 0x40081b51:0x3ffbe1c0 0x4000c2e1:0x3ffaee90 0x401374e2:0x3ffaeea0 0x4013877e:0x3ffaef60 0x401388e8:0x3ffaef80 0x40138ba9:0x3ffaf000 0x40138bea:0x3ffaf0c0 0x4013899d:0x3ffaf0f0 0x40138a33:0x3ffaf160 0x4012e105:0x3ffaf1b0 0x4012f1fa:0x3ffaf1d0 0x4012f209:0x3ffaf210 0x40119a01:0x3ffaf230 0x40119e10:0x3ffaf270 0x4011aafd:0x3ffaf2e0 0x4011abb6:0x3ffaf330 0x4011610d:0x3ffaf380 0x40116428:0x3ffaf3d0 0x401192e1:0x3ffaf3f0 0x40119302:0x3ffaf410 0x40118ebc:0x3ffaf430 0x40119186:0x3ffaf450 0x4008f943:0x3ffaf470 0x40088799:0x3ffaf4b0

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:0x3fff0018,len:4
load:0x3fff001c,len:1100
load:0x40078000,len:9564
ho 0 tail 12 room 4
load:0x40080400,len:6320
entry 0x400806a8
Connecting to xxxxx
Connected! Hooray!
[1653] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP32

[[1656] Connecting to blynk-cloud.com:80
1656] Connecting to 0.0.0.0
[19921] Connecting to blynk-cloud.com:80

The convention place for the Blynk.begin and config statements are in the void setup, after the WiFi connection is established.
Have you tried Blynk.config(); without the timeout?

Pete.

I think I found the Problem. While Blynk.connect(); is running on Core 0, Blynk.run(); is running on core 1 simultaneously. That means, that sometimes Blynk.run(); runs, before a connection is established, which results in Problems.

Furthermore, I added while(Blynk.connected() == false){} below Blynk.connect();