Multitasking... REAL multitasking with esp32


Hi fellows, I was digging on internet and fortunately found the way how to use both cores of esp32.

Why? because when the device doesn’t get connected… It keeps trying to, and doesn’t do anything else, of course it can be sorted modifying the library, but always it will loss control time when tries to communicate, due it have to wait network answers or timeouts.

So as this little asskicker has two cores, is perfect for this, because one can be dedicated to communicate (or try to from here to eternity), and the other will be measuring and controlling the process without caring about if the IoT server is running… and it is connected to wifi, and wifi’s router has internet access… and blah blah blah…

Just copy and paste this and code your sketch at void loop() as usual (this loop runs by default always at core1), the thinger process will run in the other core (core0), they share variables (be carefull reading and writing them if you do in both cores), but I guess core0 wont run anything else, just will read thinger’s “things” and report to cloud, so use global variables and declare it at setup as the “things” you need (as always, basically).

I used this with:
Arduino IDE version 1.8.9 version 2.13
Esp32 by Espressif System version 1.0.2

#define _DEBUG_
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"
//#include "ADC.h"

#include <ThingerESP32.h>

#define USERNAME "your name"
#define DEVICE_ID "your device"
#define DEVICE_CREDENTIAL "device pass"

#define SSID "wifi"
#define SSID_PASSWORD "wifi pass"
#define LED_BUILTIN 2


TaskHandle_t Task1;

void setup() {
  thing.add_wifi(SSID, SSID_PASSWORD);

  // resource output example (i.e. reading a sensor value)
  thing["millis"] >> outputValue(millis());

  //create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
                    Task1code,   /* Task function. */
                    "Task1",     /* name of task. */
                    10000,       /* Stack size of task */
                    NULL,        /* parameter of the task */
                    1,           /* priority of the task */
                    &Task1,      /* Task handle to keep track of created task */
                    0);          /* pin task to core 0 */                  

//Task1code: runs loop, 1 core dedicated to communications
void Task1code( void * pvParameters ){
  Serial.print(" running on core ");

void loop() 
  delay(one_year); // If you want >:)     

You can check with the command Serial.println(xPortGetCoreID()); to verify in which core is runnig that loop :wink:

Hope this helps (it helped me a LOT).