Multithread Issue on ESP32

Hi everyone, I’m experiencing an issue with multithreading programming on Node MCU ESP-32-S.
In my main thread I start 5 new threads, one of which starts another one that is almost always asleep.
The problem is the following: if I let the main thread die, everything works just fine.
But if i make it run an endless loop after launching all the other threads, doing:

while True:

the situation changes, and the board gives me this error:

ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
mode:DIO, clock div:2
ho 0 tail 12 room 4
ho 0 tail 12 room 4
entry 0x400806c0
E (32) boot: OTA: -1/-1 -1/-1 -1/-4
E (33) boot: Invalid ota, returning default vm 0
E (33) boot: Starting vm 0

The only pins I am using are A2, A7, D14.PWM, D27 and the I2C0. Nothing is connected to 3V3, 5V or any GND.

Does anyone know how to solve this?

Also, is there a way to extract more information about the error?


It’s a bit confusing! What kind of error do you mean? I think the message you have posted is the standard boot message of the ESP32! :slight_smile:

Could you post the whole code?
I will try to reproduce the problem and debug it.

Here is the entire project. I have added some comments at the beginning of to explain you the situation.
I have found a strange solution: adding a sleep(2000) before everything else in the main thread. It is commented to let you experience the error mentioned before (22.2 KB) .

I am having hardware initialization errors since your code is using some hardware modules that are not available for me now.
So It is hard for me to trace the code.
Since the code works with adding a sleep(2000), these timing runtime errors might be due to alot of reasons, like hardware initialization that needs to be executed without interruption.

In general, I would suggest that you try to minimize the number of threads used, to reduce the complexity of the code and because threads are resource-intensive.
So for instance, do you need a separate thread for the LCD driver, can you update the LCD when there new data available from the sensor?

I think each thread should be tested as a blackbox then integrated carefully, because as you know with each thread created there is run-time memory created (along with other things), and as the number of threads increases on a resource-limited uC as ESP32 this can lead to problems, one of which is unpredictable timing.