So, I received my ESP32 recently and I wanted to show you guys how to set it up so you can actually use he very dirty Delay function without messing up the wonderful world of Blynk.
Note: this project is NOT affiliated with @Gunner his flashy light led blynky thing!
So, ESP32 has actually got two cores. If you setup the board in the IDE so you can program it, you can also address the two different cores and run your sketch in a multithreaded fashion. As demonstrated by the below code. It flashes three LEDās on V1, V2 and V3 with a big dirty loop full of delays!
#define BLYNK_PRINT Serial
#include <Arduino.h> // This include is only needed if you build on PlatformIO
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "charstuff";
char ssid[] = "wifistuff";
char pass[] = "morewifistuff";
WidgetLED led1(V1);
WidgetLED led2(V2);
WidgetLED led3(V3);
// Dirty loop with delays, YUCK!
void loop1(void *pvParameters)
{
while (1)
{
if (led1.getValue()) { led1.off();
} else { led1.on(); }
Serial.println("1s");
delay(1000);
if (led2.getValue()) { led2.off();
} else { led2.on(); }
Serial.println("2s");
delay(2000);
if (led3.getValue()) { led3.off();
} else { led3.on(); }
Serial.println("3s");
delay(3000);
}
}
// And running blynk stuff on other loop. Note the 1ms delay, this is need so the watchdog doesn't get confused
void loop2(void *pvParameters) {
while (1) {
Blynk.run();
delay(1);
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass, "serverstuffhere or cloud", 8442);
// Here we create what is normally a timer thing
// Note the last argument, this is the Core of the ESP it will run on!
xTaskCreatePinnedToCore(loop1, "loop1", 4096, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(loop2, "loop2", 4096, NULL, 1, NULL, 1);
}
void loop()
{
// And an emtpy loop!
}
Enjoy!