Home > Enterprise >  Core0 blocking code from Core1 to run on ESP32 (isn't supposed to run both code at same time?)
Core0 blocking code from Core1 to run on ESP32 (isn't supposed to run both code at same time?)

Time:05-19

I'm writing a code where, in the CORE1 (default core), a LED should blink every 300ms. On the CORE0, the wifi management shall occur.

When Blynk.connected() is true , everything runs as supposed. LED blinking happily on CORE1 and CORE0 waiting a disconnection in order to reconnect. However, if !Blynk.connected(), the core 0 will try to reconnect the internet, and while this happens my LED stops blinking (which should continue be blinking, since it's running on CORE1. After connection is established, the LED returns to blinking every 300ms. Looks like wm.autoConnect() function blocks not only the CORE0, but also CORE1.

Here's my code for you, guys:

// I won't hide these informations bellow because this is just a test. You can compile on your esp32
#define BLYNK_TEMPLATE_ID "TMPLc9VK-ym3"
#define BLYNK_DEVICE_NAME "Wifi Bluetooth ESP32 DHT Station"
#define BLYNK_AUTH_TOKEN "IzH-vvhVf0uLJaV54Ziero7kjUiFeq5g"


#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <BlynkSimpleEsp32.h>


#define TRIGGER_PIN 22
#define LED_PIN 23

TaskHandle_t Task1;

int timeout = 120;
unsigned long blink_timer;
unsigned long wifi_reconnect_timer;

char auth[] = BLYNK_AUTH_TOKEN;

WiFiManager wm;

void wifi_stuff( void * pvParameters ){
  for(;;){
          if(!Blynk.connected()){
      if(millis()-wifi_reconnect_timer >= 2000){
    wm.autoConnect();
    vTaskDelay(10);
    wifi_reconnect_timer = millis();
    Serial.println("trying to reconnect to blynk");
      }
  }
vTaskDelay(10);
  if ( digitalRead(TRIGGER_PIN) == LOW) {
    WiFiManager wm;    
    wm.resetSettings();
    wm.setConfigPortalTimeout(timeout);
    wm.setEnableConfigPortal(false);
  }

  } 
}

void setup() {
  WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA AP  
  Serial.begin(115200);
  Serial.println("\n Starting");
  pinMode(TRIGGER_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
   wm.setEnableConfigPortal(false);
   wm.autoConnect();
  Blynk.config(auth);
  Blynk.connect(1000); //blynk connect timeout
    xTaskCreatePinnedToCore(
                    wifi_stuff,   /* Task function. */
                    "Task1",     /* name of task. */
                    10000,       /* Stack size of task */
                    NULL,        /* parameter of the task */
                    0,           /* priority of the task */
                    &Task1,      /* Task handle to keep track of created task */
                    0); 
}

void loop() {
Blynk.run();
vTaskDelay(50);
//bellow is for debug
if (millis() - blink_timer > 300){
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));
    blink_timer = millis();
 Serial.println(Blynk.connected());
}
}

BTW, I'm beginner: Both in ESP32 Multithreading, also in Stack Overflow.

Thank you very much!

CodePudding user response:

Usually networking and other I/O-intensive code running on separate threads does not interfere with other catastrophically, even if running on the same core. That's because any one thread is likely spending its time blocked, waiting for some IO operation to complete, while other threads are free to execute and do something useful. In this sense your careful division of threads among cores does not have much effect. You wouldn't notice any irregularities in the LED's blinking even if running on the same core as the WiFi driver. Sure, there might be timing issues which could cause problems with very sensitive real time processes - maybe a ~10 ms delay somewhere but you wouldn't see this from blinking LEDs with a naked eye.

It seems that your specific problem here is that Blynk.run() blocks when there's no Internet connection: https://community.particle.io/t/blynk-blocking-code-if-no-connection-to-wifi-or-cloud/36039. This in turn causes the LED to be blocked until the connection is restored and Blynk.run() exits. That's not related to multithreading, that's just bad design of the library.

  • Related