Check if there is a Serial-communication between Nextion and Esp32

Hello friends,

I try to solve a bug that i found in my code. I kinda figured it out what causes it but i am struggling to implement the solution.

I have a function in my loop called write_pH_And_Temperature_to_nextion()
This function writes every second values to my display.

Now i have a button on my Nextion where i can store new values. Sometimes when i push the button often after another i get Stack smashing protect failure!

I figured it out, when i comment my loop-function write_pH_And_Temperature_to_nextion() out, the error vanishes. It seems to me, that there is some kind of collision between these tow functions.

Here is the button function:

void b_Messintervall_SavePopCallback(void *ptr){

  
  //get current pageID
  getpageIDWithDP(&settingsTemporarily.CurrentPageID);

  //get value of measured-interval from nextion
  settings.MeasuringInterval = myNex.readNumber("n0.val");
  settings.tMeasuringIntervalUnit = myNex.readNumber("cb0.val");

  Serial.println(settings.MeasuringInterval);
  Serial.println(settings.tMeasuringIntervalUnit);

  //depening on which probe was selected..
  switch(settings.tMeasuringIntervalUnit)
  {
    case(0):            
    break;

    case(1):
    settings.MeasuringInterval = settings.MeasuringInterval*60;        //conversion into second
    break;

    case(2):                    
    settings.MeasuringInterval = settings.MeasuringInterval*60*60;     //conversion into second
    break;
  }

  settingsTemporarily.saveSettings();                                  //saves settings into EEPROM

  sendNotification(popups.popuptxt_changessaved);

}

My plan now is to implement a function at the begining of this function b_Messintervall_SavePopCallback(), that is listening if there is a Serial communication right now between my ESP32 and Nextion and only start the function b_Messintervall_SavePopCallback() when there isnt one anymore.

Has anyone an idea?

Thank you

Why not get one of the inexpensive china logic analyzers, they can be gotten to your door for under $10.00. There is a lot of software for them and they work very nicely. They will not only monitor the serial but decode it as well.

Note in advance:
I'm not familar with ESP norwith Nextion.

A possible solution might be to add a flag; set the flag on the first button press, clear it when the button press action is complete. If the flag is set and a button press is detected, simply ignore it.

void b_Messintervall_SavePopCallback(void *ptr)
{

  static bool saveInProgress = false;

  // if a save is in progress
  if(saveInProgress == true)
  {
    return;
  }

  // indicate save is in progress
  saveInProgress = true;

  //get current pageID
  getpageIDWithDP(&settingsTemporarily.CurrentPageID);

  //get value of measured-interval from nextion
  settings.MeasuringInterval = myNex.readNumber("n0.val");
  settings.tMeasuringIntervalUnit = myNex.readNumber("cb0.val");

  Serial.println(settings.MeasuringInterval);
  Serial.println(settings.tMeasuringIntervalUnit);

  //depening on which probe was selected..
  switch (settings.tMeasuringIntervalUnit)
  {
    case (0):
      break;

    case (1):
      settings.MeasuringInterval = settings.MeasuringInterval * 60;      //conversion into second
      break;

    case (2):
      settings.MeasuringInterval = settings.MeasuringInterval * 60 * 60; //conversion into second
      break;
  }

  settingsTemporarily.saveSettings();                                  //saves settings into EEPROM

  sendNotification(popups.popuptxt_changessaved);

  // clear the flag
  saveInProgress = false;
}

I'm not sure if it will work as the code will still react on the button press; but it will not go trough all the 'motions' that may consume time.

Thank you for the answer.

Never heard of that? Is the name "logic analyzer"?

Can you please post an example

Thank you

Thank you for the answer.

That was my first try. But unfortunly this did not work.

Cant figure out how to handle this problem=(

I kinda figured out what causes the problem. But i cant figure it out why.

This function runs every second (it display the measured values on my display)

void write_pH_And_Temperature_to_nextion(){


  static float plotValues[10];
  static int z=0;
  static int z1=0;
  static char timestampString[10][11];
  static bool literal = 1;

  DateTime time = rtc.now();


  plotValues[z] = settingsTemporarily.ProbeValue;                 //writes the measured values into the vector 

  //+++ Probe measurement +++ 
  Serial2.print(F("Front.t_messwert.txt="));              //writes Value into textfield              
  Serial2.print(F("\""));
  Serial2.print(String(settingsTemporarily.ProbeValue));
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff); 

  //+++ Temperature +++
  Serial2.print(F("Front.t_temp.txt="));              //writes Value into textfield              
  Serial2.print(F("\""));
  Serial2.print(String(settingsTemporarily.Temperature));
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff); 

  
  //+++ bars of the bar-plot
  for(int t=8; t>=0; t--)
  { 
    Serial2.print(F("j"));
    Serial2.print(t);
    Serial2.print(F(".val="));
    Serial2.print(int(mapf(plotValues[mapCircularBufferIndex3(t, z, 7)],settings.MeasuringMin,settings.MeasuringMax,0,100)));
    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);
  }

  //+++ x-Achse +++
  if(literal == 0)
  {
    sprintf(timestampString[z1], "%02d:%02d:%02d",time.hour(),time.minute(),time.second());
    for(int t=4; t>=0; t--) 
    {
      Serial2.print(F("x"));
      Serial2.print(t);
      Serial2.print(F(".txt="));
      Serial2.print(F("\""));
      Serial2.print(timestampString[mapCircularBufferIndex3(t, z1, 4)]);
      Serial2.print(F("\""));
      Serial2.write(0xFF);
      Serial2.write(0xFF);  
      Serial2.write(0xFF);
      literal = 1;
    }
    z1++;
  }
  else if(literal==1)
  {
    literal = 0;
  }

  z++;

  if(z1==5)
  {
    z1=0;
  }

  if(z > 7)
  {
    z=0;
  }

}


int mapCircularBufferIndex3(int index, int bufferBegin, int buffer)
{
  index += bufferBegin;
  if (index > buffer) index -= buffer+1;
  return index;
}

When i comment the for-loops out, the Stack smashing protect failure disapears. Anyone an idea why?

I don't know if it will help. From Instruction Set - Nextion it lloks like you can disable buttons; disable when you enter the callback, enable just before you leave the callback.

Yea, just create a task that runs periodically that checks the serial port for received info and puts that info into a queue that is sent to a parser.

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  char OneChar;
  char *str;
  str = (char *)ps_calloc(300, sizeof(char) ); // put str buffer into PSRAM
  // log_i("Free PSRAM before String: %d", ESP.getFreePsram());
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == '>')
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
               xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &str );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          strncat( str, &OneChar, 1 );
        }
        else
        {
          if ( OneChar == '<' )
          {
            strcpy( str, ""); // clear string buffer
            BeginSentence = true; // found beginning of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
    //        log_i( "fReceiveSerial_LIDAR " );
    //        log_i(uxTaskGetStackHighWaterMark( NULL ));
  }
  free(str);
  vTaskDelete( NULL );
} //void fParseSerial( void * parameters  )

ok. thank you. does this make sense, that when i comment the for-loops out of my function, the error disappears?

thank you

Yes, your for loop is exceeding the assigned stack space or you are exceeding the bounds of an array. I'd begin with commenting out those serial prints and paring down the code in the function till the error disappears, even if the function does not produce useful output. Then add back in code sections till the error crops back up.

Ok thank you for the help.

this code works without error, when i push a button:

void write_pH_And_Temperature_to_nextion(){


  static float plotValues[10];
  static int z=0;
  static int z1=0;
  static char timestampString[10][11];
  static bool literal = 1;

  DateTime time = rtc.now();


  plotValues[z] = settingsTemporarily.ProbeValue;                 //writes the measured values into the vector 

  //+++ Probe measurement +++ 
  Serial2.print(F("Front.t_messwert.txt="));                     //writes Value into textfield              
  Serial2.print(F("\""));
  Serial2.print(String(settingsTemporarily.ProbeValue));
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff); 

  //+++ Temperature +++
  Serial2.print(F("Front.t_temp.txt="));                          //writes Value into textfield              
  Serial2.print(F("\""));
  Serial2.print(String(settingsTemporarily.Temperature));
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff); 

  
  //+++ bars of the bar-plot
  /*for(int t=8; t>=0; t--)
  { 
    Serial2.print(F("j"));
    Serial2.print(t);
    Serial2.print(F(".val="));
    Serial2.print(int(mapf(plotValues[mapCircularBufferIndex3(t, z, 7)],settings.MeasuringMin,settings.MeasuringMax,0,100)));
    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);
  }*/

  //+++ x-Achse +++
  if(literal == 0)
  {
    sprintf(timestampString[z1], "%02d:%02d:%02d",time.hour(),time.minute(),time.second());
    /*for(int t=4; t>=0; t--) 
    {
      Serial2.print(F("x"));
      Serial2.print(t);
      Serial2.print(F(".txt="));
      Serial2.print(F("\""));
      Serial2.print(timestampString[mapCircularBufferIndex3(t, z1, 4)]);
      Serial2.print(F("\""));
      Serial2.write(0xFF);
      Serial2.write(0xFF);  
      Serial2.write(0xFF);
      literal = 1;
    }*/
    z1++;
  }
  else if(literal==1)
  {
    literal = 0;
  }

  z++;

  if(z1==5)
  {
    z1=0;
  }

  if(z > 7)
  {
    z=0;
  }
}

As soon as i include the first for-loop the error appears:

void write_pH_And_Temperature_to_nextion(){


  static float plotValues[10];
  static int z=0;
  static int z1=0;
  static char timestampString[10][11];
  static bool literal = 1;

  DateTime time = rtc.now();


  plotValues[z] = settingsTemporarily.ProbeValue;                 //writes the measured values into the vector 

  //+++ Probe measurement +++ 
  Serial2.print(F("Front.t_messwert.txt="));                     //writes Value into textfield              
  Serial2.print(F("\""));
  Serial2.print(String(settingsTemporarily.ProbeValue));
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff); 

  //+++ Temperature +++
  Serial2.print(F("Front.t_temp.txt="));                          //writes Value into textfield              
  Serial2.print(F("\""));
  Serial2.print(String(settingsTemporarily.Temperature));
  Serial2.print(F("\""));
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff); 

  
  //+++ bars of the bar-plot
  for(int t=8; t>=0; t--)
  { 
    Serial2.print(F("j"));
    Serial2.print(t);
    Serial2.print(F(".val="));
    Serial2.print(int(mapf(plotValues[mapCircularBufferIndex3(t, z, 7)],settings.MeasuringMin,settings.MeasuringMax,0,100)));
    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);
  }

  //+++ x-Achse +++
  if(literal == 0)
  {
    sprintf(timestampString[z1], "%02d:%02d:%02d",time.hour(),time.minute(),time.second());
    /*for(int t=4; t>=0; t--) 
    {
      Serial2.print(F("x"));
      Serial2.print(t);
      Serial2.print(F(".txt="));
      Serial2.print(F("\""));
      Serial2.print(timestampString[mapCircularBufferIndex3(t, z1, 4)]);
      Serial2.print(F("\""));
      Serial2.write(0xFF);
      Serial2.write(0xFF);  
      Serial2.write(0xFF);
      literal = 1;
    }*/
    z1++;
  }
  else if(literal==1)
  {
    literal = 0;
  }

  z++;

  if(z1==5)
  {
    z1=0;
  }

  if(z > 7)
  {
    z=0;
  }
}

Little bit lost what to do now. Or did u mean that i should comment the serial.prints in the for-loop out?

Thanks again

I unraveled the for loop.

No errors

  //+++ bars of the bar-plot
  for(int t=8; t>=0; t--)
  { 
    Serial2.print(F("j"));
    //Serial2.print(t);
    //Serial2.print(F(".val="));
    //Serial2.print(int(mapf(plotValues[mapCircularBufferIndex3(t, z, 7)],settings.MeasuringMin,settings.MeasuringMax,0,100)));
    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);
  }

Error appears

  //+++ bars of the bar-plot
  for(int t=8; t>=0; t--)
  { 
    Serial2.print(F("j"));
    Serial2.print(t);
    //Serial2.print(F(".val="));
    //Serial2.print(int(mapf(plotValues[mapCircularBufferIndex3(t, z, 7)],settings.MeasuringMin,settings.MeasuringMax,0,100)));
    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);
  }

for (byte t = 8; t >=0; t--)
?

I was suspecting this might be an issue. I tried finding a for loop I did that counted down. I know it looked different then how yours looks.
My for loop count down looked more like

for( i = n; i-- > 0; ) {
  // Use i as normal here
}

another count down for loop example

for(fahrenheit = 250; fahrenheit >= 0; fahrenheit -= 25)

Cool progress.

You can google "Logic Analyzer" and find them from a few dollars to thousands of dollars each. They are a common tool in most digital labs and becoming very popular is software labs as well. The inexpensive one is called: " USB Logic Analyzer Equipped with 8 channels 24M 8CH Microcontroller ARM FPGA Debug Tool 24MHz, 16MHz, 12MHz, 8MHz, 4MHz, 2MHz"


I have several of them and they work great. There is not a lot of protection in them but they are inexpensive and work very well.

Thanks for all the answers.

Hmm i still dont get it why my for-loop may cause this problem. Can someone explain to me why my for loop is wrong or at least could cause this problem?

Would love to understand it

Thanks again

Ps: will test tomorrow another for-loop and will see if the error disappears

Just a shot in the dark here --
Change that to Serial2.print("j");
[ ? ? ]

Unfortunly when i exlude F, the error still appears.

@Idahowalker When i use
for (int i=8; i-- >=0; )
{
Serial2.print(F("j"));
Serial2.print(t);
}

the error still appears. I am desperate, but eager to find the problem

Any ideas?

Thank you

No because a byte is unsigned so will roll over to 255 and you get an infinite loop. so int makes sense (and char would do)

Hey friends,

I do apologize, because u kinda posted this problem in another thread already:

(Check if there is a Serial-communication between Nextion and Esp32)

But through my research and the help from good people in here, i realized that the my initial-thread was kinda misleading and i found the root of my problem. Maybe someone fights or will fight with the same problem:

This function is in the main-loop and is called every second

void write_pH_And_Temperature_to_nextion(){

Serial.println("--------------------------");

for(int t=8; t>=0; t--){ 

    Serial2.print("j");
    Serial2.print(t);
    //Serial2.print(F(".val="));
    //Serial2.print(int(mapf(plotValues[mapCircularBufferIndex3(t, z, 7)],settings.MeasuringMin,settings.MeasuringMax,0,100)));

    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);
  }

when i now i push a button on my nextion, sometimes! i get this Stack smashing protect failure!

I figured it out that the for-loop is the cause of this. When i comment Serial2.print(t); out, the error disappears.

Can anyone explain to me why this is happening and how i can solve this issue. I guess a for-loop in the main loop is a common thing that happens in all projects.

Thank you very much