Multitasking... REAL multitasking with esp32


#1

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
Thinger.io 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

ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

TaskHandle_t Task1;

void setup() {
  Serial.begin(115200); 
  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
  xTaskCreatePinnedToCore(
                    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 */                  
  delay(500); 
}

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

  for(;;){
    TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
    TIMERG0.wdt_feed=1;
    TIMERG0.wdt_wprotect=0;
    thing.handle();
  } 
}
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).