ESP32 Guru Meditation Error: Code 1 panic'ed

Hello,
I am writing a sketch that is a bit complex. It includes two I2C sensors, Wifi, Web client, BlueTooth, constatns in flash, and a timer. I realized that the program sometimes crashes with Guru Meditation Error message in the Output when printing to the web client.
I investigated a bit, stripped the code off so it is only Web client, sensors and timer now, and it still crashes at client.println().

Here is my code

#include <WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>



#define OUT1  17
#define OUT2  5
#define OUT3  18
#define ONEWIRE_AIR  4
#define ONEWIRE_WATER  16

#define PROCESSING_PERIOD_SEC 20
#define COMMUNICATION_PERIOD_SEC  2
#define READ_ERROR_TIMEOUT_SEC  60


#define PROCESSING_PERIOD_TICKS  PROCESSING_PERIOD_SEC
#define COMMUNICATION_PERIOD_TICKS  COMMUNICATION_PERIOD_SEC
#define READ_ERROR_TIMEOUT_TICKS READ_ERROR_TIMEOUT_SEC


const uint16_t port = 77;

unsigned char WirelessIncoming, TCPRxString[144];
char TCPTxMessage[256];

String ssid = "******";
String password = "******";
String host = "192.168.0.3";
float setpointF = 25.0;
float threshold_up[3] = {1, 3,5};
float threshold_down[3] = {2,4,6};
unsigned short int id = 0;
unsigned char control_enable = 0;


float temperatureAir;
float temperatureWater;      

OneWire oneWireAir(ONEWIRE_AIR);
OneWire oneWireWater(ONEWIRE_WATER);
DallasTemperature sensorAir(&oneWireAir);
DallasTemperature sensorWater(&oneWireWater);

hw_timer_t *Timer0_Cfg = NULL;
unsigned short int communication_ticks = 0;
bool communication_period = true;

WiFiClient client;

volatile SemaphoreHandle_t timerSemaphore;

void IRAM_ATTR Timer0_ISR()
   {
   xSemaphoreGiveFromISR(timerSemaphore, NULL);
   }


void setup()
   {
   Serial.begin(230400); 
   pinMode(OUT1, OUTPUT); digitalWrite(OUT1,LOW);
   pinMode(OUT2, OUTPUT); digitalWrite(OUT2,LOW);
   pinMode(OUT3, OUTPUT); digitalWrite(OUT3,LOW);
 
   sensorAir.begin();
   sensorWater.begin();
   WiFi.begin( ssid.c_str(), password.c_str());
   timerSemaphore = xSemaphoreCreateBinary();
   Timer0_Cfg = timerBegin(1000000);
   timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR);
   timerAlarm(Timer0_Cfg, 1000000, true,0);
   }
 
void loop()
   {
   sensorAir.requestTemperatures();   temperatureAir   = sensorAir.getTempCByIndex(0);
   sensorWater.requestTemperatures(); temperatureWater = sensorWater.getTempCByIndex(0);
   if (xSemaphoreTake(timerSemaphore, 0) == pdTRUE)
      {
      communication_ticks++;
      if (communication_ticks == COMMUNICATION_PERIOD_TICKS) //2 sec
         {
         communication_period = true;
         communication_ticks = 0;
         }
      }
   if (communication_period == true)
      {
      Serial.println("communication");
      communication_period = false;    
      if (WiFi.status() == WL_CONNECTED)
         {   
         Serial.println("WiFi connected");
         if (client.connected()==false)
            {       
            client.stop();  
            Serial.println("client not connected");       
            if (client.connect(host.c_str(), port))
               {
               Serial.println("connecting to sever");
               }
            }
         else //povezani na server
            {
            Serial.println("client connected, sending");
            sprintf(TCPTxMessage,"%u,%u,%.2f,%.2f,%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f", control_enable, id,temperatureAir,temperatureWater,digitalRead(OUT1),digitalRead(OUT2),digitalRead(OUT3),
                                                                                             setpointF, threshold_up[0],threshold_down[0],threshold_up[1],threshold_down[1],threshold_up[2],threshold_down[2] );
            Serial.println("client message ready");
            Serial.println(strlen(TCPTxMessage));
            client.println(TCPTxMessage);
            Serial.println("client message sent");
            }
         }         
      else Serial.println("WIFI not connected");  
      }
   }
   

and here is the Output:

  communication
WiFi connected
client connected, sending
client message ready
57
Guru Meditation Error: Core  1 panic'ed (InstrFetchProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x00000001  PS      : 0x00060130  A0      : 0x8008e3ae  A1      : 0x3ffbcbb0  
A2      : 0x3ffc3df8  A3      : 0x00000018  A4      : 0x00000001  A5      : 0x00000000  
A6      : 0x80000000  A7      : 0x3ffbdc80  A8      : 0x800e2aa5  A9      : 0x3ffbcb90  
A10     : 0x00000001  A11     : 0xb33fffff  A12     : 0x8008ff77  A13     : 0x3ffbcaa0  
A14     : 0x00000003  A15     : 0x00060023  SAR     : 0x00000000  EXCCAUSE: 0x00000014  
EXCVADDR: 0x00000000  LBEG    : 0x00000010  LEND    : 0x00000000  LCOUNT  : 0x00000000  



Core  0 register dump:
PC      : 0x00000004  PS      : 0x00000000  A0      : 0x00000000  A1      : 0x000f4240  
A2      : 0x00000000  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00000000  
A6      : 0x60002000  A7      : 0x00000000  A8      : 0x00000000  A9      : 0x00000000  
A10     : 0x00000000  A11     : 0x00000000  A12     : 0x00000000  A13     : 0x00000000  
A14     : 0x00000000  A15     : 0xe01f8000  SAR     : 0x9c400000  EXCCAUSE: 0x000007d0  
EXCVADDR: 0x00004e20  LBEG    : 0x000fffff  LEND    : 0x000fffff  LCOUNT  : 0x00000000  


Backtrace: 0x00000001:0x000f4240 |<-CORRUPTED




ELF file SHA256: 1454c1bb29a24f65

According to your print outs, you're attempting to println() to the client without ever connecting to the server.

Thank you for taking a look at this, but this is not the case. In the line

if (client.connected()==false)

I check if there is a valid connection, and then in the else branc I print to the server

else //povezani na server
            {
            Serial.println("client connected, sending");
            sprintf(TCPTxMessage,"%u,%u,%.2f,%.2f,%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f", control_enable, id,temperatureAir,temperatureWater,digitalRead(OUT1),digitalRead(OUT2),digitalRead(OUT3),
                                                                                             setpointF, threshold_up[0],threshold_down[0],threshold_up[1],threshold_down[1],threshold_up[2],threshold_down[2] );
            Serial.println("client message ready");
            Serial.println(strlen(TCPTxMessage));
            client.println(TCPTxMessage);
            Serial.println("client message sent");
            }

I doubt if it has something to do with timer firing during client.println().
Let me stress also that the communication may work for several minutes before this happens

I saw but don't trust the result of that check on an uninitialized client object. How can it be connected if you never attempt to connect with the server?

OK, but let me know if this part of code connect to the server:

         if (client.connected()==false)
            {       
            client.stop();  
            Serial.println("client not connected");       
            if (client.connect(host.c_str(), port))
               {
               Serial.println("connecting to sever");
               }
            }

Yes but it never executes, your printout doesn't show "connecting to sever".

Sorry, I could not add the entire printout, since the program runs for many cycles without problem, I mean I never close connection to the server, and if it happens on server side I check client.connected() and reconnect.
If you think it is important I could provide entire, long if we are not lucky, printout

I still have no idea what is going on, here is the printout of the beginning of the program execution where it can be seen that the connection with the server has been established

'[ 781][V][esp32-hal-uart.c:560] uartBegin(): UART0 initialization done.
[ 788][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40169188
[ 794][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 17 successfully set to type GPIO (1) with bus 0x12
[ 799][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40169188
[ 804][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 5 successfully set to type GPIO (1) with bus 0x6
[ 809][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40169188
[ 815][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 18 successfully set to type GPIO (1) with bus 0x13
[ 902][V][NetworkEvents.cpp:119] checkForEvent(): Network Event: 9 - WIFI_READY
[ 974][V][STA.cpp:184] _onStaEvent(): STA Started
[ 976][V][NetworkEvents.cpp:119] checkForEvent(): Network Event: 11 - STA_START
[ 980][V][STA.cpp:110] _onStaArduinoEvent(): Arduino STA Event: 11 - STA_START
=========== After Setup Start ============
INTERNAL Memory Info:

Total Size : 334620 B ( 326.8 KB)
Free Bytes : 249296 B ( 243.5 KB)
Allocated Bytes : 75396 B ( 73.6 KB)
Minimum Free Bytes: 249064 B ( 243.2 KB)
Largest Free Block: 110580 B ( 108.0 KB)

GPIO Info:

GPIO : BUS_TYPE[bus/unit][chan]

 1 : UART_TX[0]
 3 : UART_RX[0]
 4 : GPIO
 5 : GPIO
16 : GPIO
17 : GPIO
18 : GPIO

============ After Setup End =============
[ 1035][V][STA.cpp:204] _onStaEvent(): STA Connected: SSID: ******, BSSID: 88:9e:68:d0:18:55, Channel: 1, Auth: WPA_WPA2_PSK
[ 1041][V][NetworkEvents.cpp:119] checkForEvent(): Network Event: 13 - STA_CONNECTED
[ 1045][V][STA.cpp:110] _onStaArduinoEvent(): Arduino STA Event: 13 - STA_CONNECTED
communication
WIFI not connected
communication
WIFI not connected
[ 3628][V][NetworkInterface.cpp:78] _onIpEvent(): sta Got New IP: 192.168.0.14 MASK: 255.255.255.0 GW: 192.168.0.1
[ 3634][V][NetworkEvents.cpp:119] checkForEvent(): Network Event: 16 - STA_GOT_IP
[ 3637][V][STA.cpp:110] _onStaArduinoEvent(): Arduino STA Event: 16 - STA_GOT_IP
[ 3641][V][STA.cpp:169] _onStaArduinoEvent(): STA IP: 192.168.0.14, MASK: 255.255.255.0, GW: 192.168.0.1
communication
WiFi connected
client not connected
connecting to sever
communication
WiFi connected
client connected, sending
client message ready
57
client message sent
communication
WiFi connected
client connected, sending
client message ready
57
client message sent
communication
WiFi connected
client connected, sending
client message ready
57
client message sent
communication
WiFi connected
client connected, sending
client message ready
57
client message sent
communication
WiFi connected
client connected, sending
client message ready
57
client message sent'

Then, as per the initial Outpu I provided, everything goes on nicely for minutes, sometimes hours, and finally crashes.

could be a memory leakage problem - worth monitoring the size of the free heap memory

Thank you for pointing this out. Would checking esp_get_free_heap_size() be good method for investigation?

OK, I installed ESP exception decoder, and this is what I get

ESP Exception Decoder
Sketch: sketch_oct24a FQBN: esp32:esp32:mhetesp32devkit

Guru Meditation Error: Core  1 panic'ed (InstrFetchProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x00000001  PS      : 0x00060130  A0      : 0x8008e3ae  A1      : 0x3ffbcbb0  
A2      : 0x3ffc3df8  A3      : 0x00000018  A4      : 0x00000001  A5      : 0x00000000  
A6      : 0x80000000  A7      : 0x3ffbdc80  A8      : 0x800e2ad5  A9      : 0x3ffbcb90  
A10     : 0x00000001  A11     : 0xb33fffff  A12     : 0x8008ff77  A13     : 0x3ffbcaa0  
A14     : 0x00000003  A15     : 0x00060023  SAR     : 0x00000000  EXCCAUSE: 0x00000014  
EXCVADDR: 0x00000000  LBEG    : 0x00000010  LEND    : 0x00000000  LCOUNT  : 0x00000000  


Backtrace: 0xfffffffe:0x3ffbcbb0 0x4008e3ab:0x3ffbcbd0 0x4008fcf6:0x3ffbcbf0


Core  0 register dump:
PC      : 0x0000000d  PS      : 0x00000000  A0      : 0x00000000  A1      : 0x000f4240  
A2      : 0x00000000  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00000000  
A6      : 0x60002000  A7      : 0x00000000  A8      : 0x00000000  A9      : 0x00000000  
A10     : 0x00000000  A11     : 0x00000000  A12     : 0x00000000  A13     : 0x00000000  
A14     : 0x00000000  A15     : 0xe01f8000  SAR     : 0x9c400000  EXCCAUSE: 0x000007d0  
EXCVADDR: 0x00004e20  LBEG    : 0x000fffff  LEND    : 0x000fffff  LCOUNT  : 0x00000000  


Backtrace: 0x0000000a:0x000f4240 |<-CORRUPTED




ELF file SHA256: c74ecf6ccd0361d3

PC: 0x00000001
EXCVADDR: 0x00000000

Decoding stack results
0x4008e3ab: prvIdleTask at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/FreeRTOS-Kernel\tasks.c:4439
0x4008fcf6: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa\port.c:162

Any thoughts? I am really lost here, this was supposed to be an easy task...

Please post that using Code Tags (just like your code). It will be much easier to read.

try printing the value at regular intervals during program execution
it could indicate you are running out of heap memory which could cause the program to crash

I changed the original post so the Output is now under Code Tags

I print it every two seconds, and the heap size is about 200000 consistently:

communication
199800
WiFi connected
client connected, sending
client message ready
57
client message sent
communication
199800
WiFi connected
client connected, sending
client message ready
57
client message sent

I would try closing the connection after every print to the server and then reopening when it's time for the next transaction.

Still no progress on my side.

Closing the connection after every print to the server is not good for me since I need to asynchronously send data from server to the client, the feature I removed just for this "experiment".

I am making the sketch even more simple, removing the timer altogether, removing I2C communication, trying to pinpoint the cause of the crash. Will report here

Perhaps not, but I'd still try it as a test. I could provide additional information.

Replacing the ESP32 module solved all the problems :melting_face:
Thank you all for your support!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.