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!