Esp32 Stop working after a random time with a new code .

soleyj
Posts: 4
Joined: Mon May 08, 2023 11:21 am

Esp32 Stop working after a random time with a new code .

Postby soleyj » Thu Dec 21, 2023 9:51 am

Hi,

I have servals esp32 connected between them with 2 uarts, one for input and the other for output, like ws2812b communication. Also each esp32 has 3 hx711 attached. The total esp32 in serial that I have is 10.

The esp32 needs to detect if there is a human on the load cell.

I have one code that is running correctly with no problem but the detection of the hx711 is not accurate enough.

In order to improve this reading I have made a new algorithm that seems to work much better but the esp32, some of them stop working after random time. Maybe minutes or hours. If one esp32 stops working all esp32 stop working because they need the input signal from the one that is behind.

It seems to be a software problem because with the other code non esp32 is stopped working.

The esp32 is always printing to serial port some data. This printing rate is over 200ms and the other code does exactly the same.

I have observed the following things, when the system stops.
Sometimes the serial port is empty, no printing.
Sometimes the serial port is printing charters like this ��;{��?�O?}��������������V��^:��z�i�[^?\�x5�����\�/���=۸�yڿ��j�w?�ϴ~���M�}}�:���~/
The esp32 blink led to show if all the readings of hx711 are ok. So sometimes the port is empty or printing random characters and this led is blinking.

The code that I have added is the following one.

Eeprom to save config value. Is only read at start and update on demand. ( never update when the reset happens)
Some math algorithms are attached .

Code: Select all

long lastValue[3];
long long first_100[3];
int derivate10[3][10];
int sum_derivate10[3];
int sum_derivate5[3];
int lastCalValue[3];
int errorCounter[3];
int lastRead[3];

int lastStableList[3][10];
int lastStableRead[3];

void pushLastStable(int id, int value)
{
  for(int i = 9; i > 0 ; i--)
  {
    lastStableList[id][i] = lastStableList[id][i-1];
  }
  lastStableList[id][0] = value;
  lastStableRead[id] = 0;
  for(int i = 0; i < 10; i ++)
  {
    lastStableRead[id] += lastStableList[id][i];
  }
  lastStableRead[id] = lastStableRead[id] / 10;

}



void save_derviate_10(int id)
{
  static bool firstTime[3];
  if( firstTime[id] ==  false)
  {
    lastRead[id] = calibrationValue[id];
    firstTime[id]  = true;
    lastCalValue[id]= lastRead[id];
    lastStableRead[id]  = calibrationValue[id];;
    for(int i = 0; i < 10; i ++)
    {
      lastStableList[id][i] =  calibrationValue[id];
    }
  }

  if ( abs(temp_read_small[id][(counter[id]-1) % 100 ]) > 8200)
  {
    // floor is getting error
    errorCounter[id]++;
    if( errorCounter[id] > 5)
    {
      errorCounter[id] = 5;
      pressFloor[id] = 10;
      return;
    }
  }
  else
  {
    errorCounter[id] = 0;
    if( pressFloor[id] == 10)
    {
      pressFloor[id] = 0;
    }
  }
  // moving all the array
  for(int i = 9; i > 0 ; i--)
  {
    derivate10[id][i] = derivate10[id][i-1];
  }
  int diff =  temp_read_small[id][(counter[id]-1) % 100 ] - lastRead[id] ;
  static int noiseFilter[3];
  if( abs(diff)> 50)
  {
    derivate10[id][0] = 0 ;
    noiseFilter[id]++;
    if( noiseFilter[id] > 5)
    {
      noiseFilter[id] =0;
      lastRead[id] = temp_read_small[id][(counter[id]-1) % 100 ] ;
    }

  }
  else
  {
    noiseFilter[id] =0;
    lastRead[id] = temp_read_small[id][(counter[id]-1) % 100 ] ;
    derivate10[id][0] = diff;
  }

  sum_derivate10[id] = 0;
  sum_derivate5[id] = 0;
  for(int i = 0; i < 10; i ++)
  {
    sum_derivate10[id] += derivate10[id][i];
    if( i < 5)
    {
      sum_derivate5[id] += derivate10[id][i];
    }
  }
  
}

#define SUMDER 20

void check_state_derivate(int id)
{
  static int ignore[3];
  static int counterB[3];
  static int counterA[3];
  
  save_derviate_10(id);
  if( pressFloor[id]  == 10)
  {
    return;
  }
  int status = 0; // 1 press 2 off 0 no change 

  if( ignore[id] == 0)
  {
    if( sum_derivate10[id] <  - SUMDER )
    {
      counterA[id]++;
      if( counterA[id] > 3)
      {
        if( direction[id] == 0)
        {
          direction[id] = 1;  
          WriteEEPROM(id,1);
        }
        if( direction[id] == 1)
        {
          status = 1;
        }
        else
        {
          status = 2;
        }
      }
    }
    else if( sum_derivate10[id] > SUMDER )
    {
      counterB[id]++;
      if( counterB[id] > 3)
      {
        if( direction[id] == 0)
        {
          direction[id] = 2;
          WriteEEPROM(id,2); 
        }
        if( direction[id] == 2)
        {
          status = 1;
        }
        else
        {
          status = 2;
        }
      }
    }
    else{
      counterB[id] = 0;
      counterA[id] = 0;
    }

    if( status == 1)
    {
      // Serial.println(id);
      pressFloor[id] = 1;
    }
    else if( status == 2 && abs(lastRead[id] -  lastStableRead[id]) < 30)
    {
      pressFloor[id] = 0;
      // lastCalValue[id]= lastRead[id];
      pushLastStable(id ,lastRead[id] );
      if( abs(sum_derivate5[id]) < 5)
      {
        ignore[id] = 3;
      }
    }
    static int counter0Stable[3];
    if( pressFloor[id] == 1 &&   abs(lastRead[id] -  lastStableRead[id]) < 30 )
    {
      counter0Stable[id] ++;
      if( counter0Stable[id]  > 3)
      {
        pressFloor[id] = 0;
      }
    }
    else
    {
      counter0Stable[id] = 0;
    }
  }
  else
  {
    ignore[id] --;
  }  
}
Any ideas on how to find the problem? Is really difficult to debug because the failure can happen after hours.

Maybe delete some code and test part by part and find where the failure is?

Thanks.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Esp32 Stop working after a random time with a new code .

Postby lbernstone » Thu Dec 21, 2023 3:56 pm

The error is in your serial writes, so maybe focus your efforts on the part where it is writing to serial?

soleyj
Posts: 4
Joined: Mon May 08, 2023 11:21 am

Re: Esp32 Stop working after a random time with a new code .

Postby soleyj » Thu Dec 21, 2023 6:47 pm

The serial writer is wired into the main loop trigger by a variable from the main timer.

This may not be the error because the code remains the same as the code that is working.

Maybe the error is related to the EEPROM?

I have this code to update the EEPROM.

Code: Select all

void WriteEEPROM(int direction, int value)
{
  timerAlarmDisable(Timer0_Cfg);
  delay(20);
  EEPROM.write(direction, value);
  EEPROM.commit();
  
  timerAlarmEnable(Timer0_Cfg);
}
Also trigger in the main loop.

But it is only updated when they receive an order from the main pcb, which in theory is never sent in the period that the esp32 stops.

Any other idea. Thanks

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Esp32 Stop working after a random time with a new code .

Postby lbernstone » Fri Dec 22, 2023 7:30 pm

The serial garbling happens either because you are printing non-printable characters (extremely likely) or because your baud rate has changed/drifted (quite unlikely). Take a look at your serial writes.

Who is online

Users browsing this forum: daniel_3in and 70 guests