BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Three LEDs with Dirty Delay (ESP32)


#1

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! :wink:

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!


Tutorial: ESP32 -- Non-Blocking and Concurrent Function Execution
Critical control timing with Blynk (1mS repeatable requirement)? WiFi + Sparkfun ESP8266 Blynk Board?
ESP32 Blynk and Multicore
Blynk interfering with Bluetooth Serial on ESP32
Esp32 not reconnecting to server
NRF24 to blynk
Device hangs/drops connection - ESP32 - Wifi
#2

very interesting. i didn’t know about this. where can i read more detailed docs about it?


#3

Well, this is totally unsupported I think. The ESP32 is still in beta stage. I’ve found the info https://github.com/copercini/esp32-iot-examples/blob/master/multiloop/multiloop.ino here and added Blynk in the loop, sort of speak … :smiley:


#4

but in the reality these 2 loops indeed run completely isolated? i mean no matter what is happening in loop1, loop2 can do blynk and wifi stuff?

that whould be a great help for time sensitive functions and stepper motor control for example!


#5

Yes. They run on physically different CPU cores of the ESP32 because it has two Cores.


#6

very cool! thanks for shareing :wink:


#7

That is awesome!


#8

One thing missing though, I haven’t got any Blynk debug output on the Serial port. So that would require some attention. Not sure why not. Other Serial output is fine.


#9

That’s only because your rebel trio of LEDs is no match for the Blinky firepower of my fully operational battlestation of 72 LEDs :stuck_out_tongue:

However, it may have more practical applications… :blush:

That worked OK on Arduino IDE… perhaps a PlatformIO issue?


#10

On that “spare” void loop, which will also run stuff… which core does it use?


#11

That is user-specified, it seems! Last argument as per comment

  // 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);

#12

I use CoolTerm for serial monitoring. Guess it could be me. I’m just glad I can separate all the task for now. This opens up a whole new range of possibilities (mostly dirty programming, lol).


#13

@Fettkeewl I guess then that since it is the “default” loop and otherwise unspecified, it probably runs on the default core… 0? EDIT - Nope, Core 1, based on one of those documents @Lichtsignaal posted below.

I knew the ESP had dual cores, but I thought the 2nd one was dedicated to the WiFi interface… this calls for more investigation… Meanwhile I must rethink which ESP to put in my rover as this might greatly improve its functionality without a massive timer investment.


#14

http://esp32.info/docs/esp_idf/html/db/da4/task_8h.html#adf67e7cd0bfd1eda9e8afd048206f7c2

More info here on the specific functions you can use with xTaskCreate stuff.

And even mroe here: https://techtutorialsx.com/2017/05/09/esp32-running-code-on-a-specific-core/ (this is a good one I think).

@Gunner I think it also depends on the Priority settings, from what I can gather when creating the task


#15

A side question, did you try the ADC On esp32?

I just read that it has 2 ADCs supporting 9 channels each, I was wondering if that meant that
each ADC could handle 9 different adc readings in sequence?
Or is it limited to 1 channel per adc but you can choose which one?


#16

No idea, haven’t ADC yet, but I’ll try and see if I can add some analog stuff to it. If I can find some sensors… lol.

-edit

If I look at the pictures it just has 9 analog ports, or so it sems.


#17

Crap, it even has 10(!!!) integrated touch sensors. This means you can use a simple copperclad board to create a keypad for example :smiley:


#18

Pretty cool eh… This will be a sweet Blynk board once all the teething issues are worked out… OTA, BLE, WiFi, Touch, Multiple UARTS, Dual Core, Fast, lots of IO, etc.

Unfortunately the latest Update IDF libs broke something in ArduinoOTA… so that doesn’t work, and I have no idea how to revert to the “prior” update :blush: So much new too fast.

EDIT - It has been fixed now :smiley: Now I just have to figure out how to get it to work more than once… something is different in the required commands then with the ESP8266.


#19

Looks fun :smile:
https://youtu.be/iusvNh6VfmE


#20

ADC works here is demo: