0
votes

I'm a little bit new to ESP32 and Arduino ID. I've been working on a project in which I want to switch the wifi network from one to another at any given time. But whenever I'm trying to close the existing connection (network) and reconnect to the new one, ESP32 crashes and gets rebooted. A bit peculiar behaviour I would say. Additionally, I even tried to use the EEPROM for the storage purpose, so, even when my ESP32 gets rebooted, it will take the updated values from the storage. Unfortunately, sometimes ESP32 crashes even when I update the variable which was being passed earlier to WiFi.begin(). Lastly, In my application, I'll be providing the new SSID and Password through the serial (COM) port. Can anyone help in achieving this task?

2
Please share a minimal, viable example of the code that demonstrates this problem instead of describing it. Also please share the messages that are output at the crash.romkey

2 Answers

0
votes

I'm posting a small portion of my code. As you can see, I'm taking some data from the serial and performing some parsing on it. After sending the frame on serial, for eg: # ESP32 52 SSID_PASS $\n\r(in the FcSerialCommandParse) I'm trying to store the SSID and PASS word, but as mentioned whenever I'm trying to modify the data, ESP32 Crashes.

//Includes
  #include <WiFi.h>
  #include <AsyncUDP.h>
  #include <WiFiUdp.h>
  #include <PubSubClient.h>
  #include <EEPROM.h>

//STRUCTURE VARIABLES
//union conversion 8 byte
  typedef union
  {
    uint8_t   uc_8 [(uint8_t)8]; int8_t    sc_8 [(uint8_t)8]; 
    uint16_t  ui_16[(uint8_t)4]; int16_t   si_16[(uint8_t)4]; 
    uint32_t  ui_32[(uint8_t)2]; int32_t   si_32[(uint8_t)2];   
    uint64_t  ui_64            ; int64_t   si_64   ;
    float   fl_32  [(uint8_t)2]; double    dd_64   ;
  }unConvo8;

//union conversion 4 byte
  typedef union
  {
    uint8_t   uc_8 [(uint8_t)4]; int8_t    sc_8 [(uint8_t)4];   
    uint16_t  ui_16[(uint8_t)2]; int16_t   si_16[(uint8_t)2];   
    uint32_t  ui_32            ; int32_t   si_32   ;
    float     fl_32            ; 
  }unConvo4;

//union conversion 2 byte 
  typedef union
  {
    
    uint8_t   uc_8 [(uint8_t)2]; int8_t    sc_8 [(uint8_t)2];
    uint16_t  ui_16            ; int16_t   si_16   ;
    
  }unConvo2;
    
//timer structure 
  typedef struct
  {
   
   uint8_t  ucScanInput_TimerFlag_100msec; // This flag is used to perfrom the task with the frequncy of 100 ms  
   uint8_t  ucScanInput_TimerFlag_1sec ;   // This flag is used to perfrom the task with the frequncy of 1 sec  
   uint8_t  ucScanInput_TimerFlag_1min ;   // This flag is used to perfrom the task with the frequncy of 1 min
     
   uint32_t uiVar1ms_Counter    ;  // Counter used to Count the 1   ms
   uint32_t uiVar10ms_Counter   ;  // Counter used to Count the 10  ms
   uint32_t uiVar100ms_Counter  ;  // Counter used to Count the 100 ms  
   
   uint32_t uiVar1sec_counter   ;  // Counter used to Count the 1    s
   uint32_t uiVar1min_counter   ;  // Counter used to Count the 1    m
   uint32_t uiVar1hour_counter  ;  // Counter used to Count the 1    h    
 
   uint8_t  uiVar1day_counter   ;
   uint8_t  uiVar1month_counter ;      
   uint8_t  uiVar1year_counter  ;
   
   uint64_t uiVar1SecUniversalCNT ;
   char     ucLocalDateNTime[30]  ;
   
  }stTimerCounter_ALL;


//COM PORT Structure
  typedef struct
  {
   
       uint8_t  ucStartTimerCnt   ;
       uint8_t  ucRXINTRFlag      ;
       uint16_t uiCOMRXCNT        ; 
       uint8_t  ucRxProcessFlag   ;
         
       uint8_t  ucCOMRXBuff[(uint16_t)250];
  
  
  }stCOMPort_ALL;

//UDP/ MQTT Structure
typedef struct 
{ 
   
   uint16_t uiMQTT_UDP_RXCNT          ;   
   uint8_t  ucMQTT_UDP_ProcessFlag    ;
   uint8_t  ucMQTT_UDP_InitDone       ;
   uint8_t  ucMQTT_UDP_ReconnectFlag  ;           
   uint8_t  ucMQTT_UDP_RXBuff[(uint16_t)1000];

   char     ucMQTT_TOPIC [_DEFAULT_STRLEN];
   char     ucMQTT_UDP_ServerIP[_DEFAULT_STRLEN];   
   uint16_t ucMQTT_UDP_ServerPort     ;      
   uint8_t  ucMQTTStatusFlag          ;  
   
  
}stMQTT_UDP_All;

//UDP/ MQTT Structure 
typedef struct 
{   
    
    uint8_t  ucWifiConnnectivityFlag ;
    char     ucWifiIMEINumber[_DEFAULT_STRLEN];
    uint8_t  ucWifiIMEINumberUpdate  ; 
    char     ucWifiSSID    [_DEFAULT_STRLEN] ;
    char     ucWifiPassword[_DEFAULT_STRLEN] ;
    uint8_t  ucWifiConnectingStart   ;
    uint8_t  ucWifiConnectTime       ;
    
    
}stWIFIPara_All;

//TCP Structure 
typedef struct 
{   
    char     ucTCPServerIP[_DEFAULT_STRLEN];   
    uint16_t uiTCPServerPort ;
    
}stTCPPara_All;    




  /***WIFI CLINET Variable***/
      WiFiClient RTKClient;


  /***WIFI CLINET Variable***/
      WiFiClient TCPClient;


  /***UPD CLINET Variable***/
      AsyncUDP Udp;

  /***Timer Variable***/
      stTimerCounter_ALL stVarTimerCounter_ALL  ;


  /***Com Port Variable***/
      stCOMPort_ALL  stVarCOMPort_ALL;


  /***pub/sun Variable***/
    //  PubSubClient   PunSubclient(RTKClient); 


  /***debugg Variable***/
      char  ucDebuggBuff[(uint8_t)200]; 
    

  /***MQTT/ UDP Variable***/
      stMQTT_UDP_All  stVarMQTT_All;


  /***WIFI Variable***/
      stWIFIPara_All  stVarWIFIPara_All;


  /***TCP Variable***/
      stTCPPara_All  stVarTCPPara_All;
   
/*---------------------------------------------------------------------------
 *                           MACROS
 *---------------------------------------------------------------------------
*/
  

    /**********************************************/
    #define _WIFI_SSID                 (char  *)"tw"       
    #define _WIFI_PASSWORD             (char  *)"saharaindia"  

    /**********************************************/
    #define _MQTT_CLINET_ID            (uint8_t) 29
    #define _MQTT_SERVER_ADDRESS       (char  *)"58.146.103.207"
    #define _MQTT_SERVER_PORTNO        (uint16_t)1883 
    #define _MQTT_SERVER_TOPIC_NAME    (char  *)"RTK_TEST" 

    /**********************************************/
    #define _UDP_BROADCAST_PORTNO     (uint16_t)15151
    
    /**********************************************/
    #define _TCP_SERVER_ADDRESS       (char   *)"58.146.103.207"
    #define _TCP_SERVER_PORTNO        (uint16_t)2020    

    /**********************************************/
    #define _EEPROM_SIGNATURE         (uint32_t)0x133456
    
    #define _EEPROM_ADD_SIGNATURE     (uint8_t )0  
    #define _EEPROM_ADD_WIFI_SSID     (uint8_t)10
    #define _EEPROM_ADD_WIFI_PASS     (uint8_t)31
    #define _EEPROM_ADD_MQTT_IP       (uint8_t)53
    #define _EEPROM_ADD_MQTT_TOPIC    (uint8_t)75
    #define _EEPROM_ADD_TCP_SERVER    (uint8_t)97
    #define _EEPROM_ADD_TCP_PORTNO    (uint8_t)119
    

/*---------------------------------------------------------------------------
*                           FUNCTIONS DECLARATION
*----------------------------------------------------------------------------
*/

  void IRAM_ATTR FnTimerISR     (void);
  void FnFlashReadConfig        (void);
  void FnWIFINetworkReconnect   (void);
  void FnSerialCheckProccessINTR(void);
  void FnSerialReceiveData      (void);
  uint8_t FcSerialCommandParse  (void);




/*****************************************************************************
 **@Function        :   FnCheckProccessINTR
 **@Descriptions    :  
 **@parameters      :   None
 **@return          :   None
*****************************************************************************/
void FnSerialCheckProccessINTR(void)
{
      static uint8_t ucMessageCnt = RESET ;
  if( stVarCOMPort_ALL.ucStartTimerCnt == SET )
    {
        ucMessageCnt++ ;    
    if( stVarCOMPort_ALL.ucRXINTRFlag  == SET )
      {
        ucMessageCnt  = RESET ;
        stVarCOMPort_ALL.ucRXINTRFlag  = RESET ;
      }
     
    /******** If the Time Duration Between two bytes icreases 3ms
              set the process flag for further process on the frame *********/        
    if( ucMessageCnt >= (uint8_t)0x03 )
      {
        ucMessageCnt  = RESET ;
        stVarCOMPort_ALL.ucStartTimerCnt = RESET ;
        stVarCOMPort_ALL.ucRxProcessFlag = SET   ;
      }
    }
}

/*****************************************************************************
 **@Function        :   CheckSerial
 **@Descriptions    :  
 **@parameters      :   None
 **@return          :   None
*****************************************************************************/
void FnSerialReceiveData(void)
{
     if ( Serial.available() > RESET )
           {
             char inChar = Serial.read();
             stVarCOMPort_ALL.ucCOMRXBuff[stVarCOMPort_ALL.uiCOMRXCNT++]=inChar;      

             /***************** FRAME TERMINATION CHECKING **************************/
             stVarCOMPort_ALL.ucRXINTRFlag      = SET   ;
        if(  stVarCOMPort_ALL.ucStartTimerCnt  != SET   )
             stVarCOMPort_ALL.ucStartTimerCnt   = SET   ;
           }
}

/*****************************************************************************
 **@Function        :   SerialProcess
 **@Descriptions    :  
 **@parameters      :   None
 **@return          :   None
*****************************************************************************/
void FnSerialProcess (void)
{

if (  stVarCOMPort_ALL.ucRxProcessFlag == SET   )
   {  stVarCOMPort_ALL.ucRxProcessFlag  = RESET ;
   
   /*****************************************************************************/
    if(FcSerialCommandParse() != RESET )
      {
          
          
      }
   /*****************************************************************************/      
    else
      {
          Serial.print("# ESP32 99 CMD-UNKNOWN $");
          memset(stVarCOMPort_ALL.ucCOMRXBuff, RESET ,
                 stVarCOMPort_ALL.uiCOMRXCNT);
                 stVarCOMPort_ALL.uiCOMRXCNT = RESET ;
      }
   }
}

/*****************************************************************************
 **@Function        :   FcSerialCommandParse
 **@Descriptions    :  
 **@parameters      :   None
 **@return          :   None
*****************************************************************************/
uint8_t FcSerialCommandParse (void)
{
  uint8_t ucRet = RESET ;
  
   if( strstr((char *)stVarCOMPort_ALL.ucCOMRXBuff,"# ESP32 ") )
     {
        
        char *ucChar    = strstr((char *)stVarCOMPort_ALL.ucCOMRXBuff,"ESP32");
              ucChar    = strchr( ucChar , ' ');     ucChar++;
        char *ucChar1   = strchr( ucChar , ' ');    *ucChar1 = NULL  ; ucChar1++;
        uint8_t ucSelec = atoi  ( ucChar ); ucChar = ucChar1         ;   
              ucChar1   = strchr( ucChar , ' ');    *ucChar1 = NULL  ;
        char ucTempBuff [(uint8_t)30]; strcpy ( ucTempBuff , ucChar );      
        char *ucTempBuff1;

        
        switch (ucSelec)
        {
                      
         case(uint8_t)6:
             {
                  
              if(( strcmp(ucTempBuff, "RESET") != NULL)
               ||( strstr(ucTempBuff, "RESET") != NULL))
                 {
                  if(  WiFi.status() == WL_CONNECTED )   
                        {
                             WiFi.mode(WIFI_STA);
                             WiFi.disconnect(true);
                             delay(2000); 
                        }
                    ESP.restart(); 
                 }
                  
              memset(stVarCOMPort_ALL.ucCOMRXBuff, RESET ,
                   stVarCOMPort_ALL.uiCOMRXCNT);
                   stVarCOMPort_ALL.uiCOMRXCNT = RESET ;   
                   ucRet = SET ;
             }break;

         
         case(uint8_t)52:
             {  
                sprintf( ucDebuggBuff,"RX *%s* CURR ^%s_%s^\n\r",
                ucTempBuff, stVarWIFIPara_All.ucWifiSSID, 
                stVarWIFIPara_All.ucWifiPassword );
                Serial.print(ucDebuggBuff);
                      
                      ucTempBuff1  = strchr (ucTempBuff,'_');
                  if( ucTempBuff1 == NULL )
                 goto skipWIFICred ;
                     *ucTempBuff1  = NULL ;  ucTempBuff1++;                      

                  if(  WiFi.status() == WL_CONNECTED )   
                    {
                         WiFi.mode(WIFI_STA);
                         WiFi.disconnect(true);
                         delay(2500); 
                         WiFi.softAP("WIFIESP32", "");
                         delay(2500); 
                    }
                      FnFlashWriteString( _EEPROM_ADD_WIFI_PASS, ucTempBuff1, _DEFAULT_STRLEN );                         
                      FnFlashWriteString( _EEPROM_ADD_WIFI_SSID, ucTempBuff , _DEFAULT_STRLEN ); 
                      Serial.print("WIFI PARA CHANGED!\n\r");
                      stVarWIFIPara_All.ucWifiConnnectivityFlag =  RESET ;
                      delay(1000);        
                    

                     skipWIFICred:
                     memset( stVarCOMPort_ALL.ucCOMRXBuff, RESET ,
                     stVarCOMPort_ALL.uiCOMRXCNT );
                     stVarCOMPort_ALL.uiCOMRXCNT = RESET ;   
                     ucRet = SET ;  
             }break; 

                   
        default:       
              {        
                Serial.print("# ESP32 99 CMD-IMPROPER_PARSE $\n");   
                memset( stVarCOMPort_ALL.ucCOMRXBuff , RESET ,
                        stVarCOMPort_ALL.uiCOMRXCNT );
                        stVarCOMPort_ALL.uiCOMRXCNT  = RESET ;   
                        ucRet = SET ;
              }
              break;        
        }    
     }
  return ucRet;
}
/*****************************************************************************
 **@Function        :   IRAM_ATTR onTimer
 **@Descriptions    :  
 **@parameters      :   None
 **@return          :   None
*****************************************************************************/
void IRAM_ATTR FnTimerISR(void)
{
      portENTER_CRITICAL_ISR(&timerMux);
      FnSerialCheckProccessINTR();

        stVarTimerCounter_ALL.uiVar1ms_Counter ++;
   if(  stVarTimerCounter_ALL.uiVar1ms_Counter >= 10 )
     {
        stVarTimerCounter_ALL.uiVar1ms_Counter = RESET  ;
        stVarTimerCounter_ALL.uiVar10ms_Counter++       ;          
     }
   
    if( stVarTimerCounter_ALL.uiVar10ms_Counter>= 10 )
    {
        stVarTimerCounter_ALL.uiVar10ms_Counter  = RESET ;
        stVarTimerCounter_ALL.uiVar100ms_Counter++       ;  
     
      /******************* SET THE SCAN FLAG *******************/
        stVarTimerCounter_ALL.ucScanInput_TimerFlag_100msec = SET ;
    }
   
    if( stVarTimerCounter_ALL.uiVar100ms_Counter>= 10 )
    {
        stVarTimerCounter_ALL.uiVar100ms_Counter = RESET ;
        stVarTimerCounter_ALL.uiVar1sec_counter++   ;
     
        /******************* SET THE SCAN FLAG *******************/
        stVarTimerCounter_ALL.ucScanInput_TimerFlag_1sec = SET ;
        stVarTimerCounter_ALL.uiVar1SecUniversalCNT++;

        if (stVarWIFIPara_All.ucWifiConnectingStart == SET )
           {
                 stVarWIFIPara_All.ucWifiConnectTime ++ ;
             if( stVarWIFIPara_All.ucWifiConnectTime == (uint8_t)3)
               {
                 stVarWIFIPara_All.ucWifiConnectingStart =
                 stVarWIFIPara_All.ucWifiConnectTime     = RESET ;
               }
           }
       
    }
   
    if(stVarTimerCounter_ALL.uiVar1sec_counter>= 60)
    {
      stVarTimerCounter_ALL.uiVar1sec_counter = RESET ;
      stVarTimerCounter_ALL.uiVar1min_counter++  ;
     
      /******************* SET THE SCAN FLAG *******************/
      stVarTimerCounter_ALL.ucScanInput_TimerFlag_1min = SET ;      
    }
   
    if(stVarTimerCounter_ALL.uiVar1min_counter >= 60 )
    {
      stVarTimerCounter_ALL.uiVar1min_counter   = RESET ;
      stVarTimerCounter_ALL.uiVar1hour_counter++    ;
    }

  if( stVarTimerCounter_ALL.uiVar1hour_counter >= 24 )
    {
     
      stVarTimerCounter_ALL.uiVar1hour_counter  =
      stVarTimerCounter_ALL.uiVar1min_counter   =
      stVarTimerCounter_ALL.uiVar1sec_counter   =
      stVarTimerCounter_ALL.uiVar100ms_Counter  =
      stVarTimerCounter_ALL.uiVar1ms_Counter    = RESET ;
   
    }
 
  portEXIT_CRITICAL_ISR (&timerMux);
}  



/*****************************************************************************/
void FnFlashReadConfig ( void )
{

   if( FnFlashReadUint32( _EEPROM_ADD_SIGNATURE ) == _EEPROM_SIGNATURE )
     {    
         Serial.print("FLASH SIGNATURE MATCHED!\n\r");
         FnFlashReadString(_EEPROM_ADD_WIFI_SSID , stVarWIFIPara_All.ucWifiSSID     , _DEFAULT_STRLEN );
         FnFlashReadString(_EEPROM_ADD_WIFI_PASS , stVarWIFIPara_All.ucWifiPassword , _DEFAULT_STRLEN );

         FnFlashReadString(_EEPROM_ADD_MQTT_IP   , stVarMQTT_All.ucMQTT_UDP_ServerIP, _DEFAULT_STRLEN );
         FnFlashReadString(_EEPROM_ADD_MQTT_TOPIC, stVarMQTT_All.ucMQTT_TOPIC       , _DEFAULT_STRLEN );
         stVarMQTT_All.ucMQTT_UDP_ServerPort = _MQTT_SERVER_PORTNO ;  
         
         FnFlashReadString(_EEPROM_ADD_TCP_SERVER, stVarTCPPara_All.ucTCPServerIP   , _DEFAULT_STRLEN );
         stVarTCPPara_All.uiTCPServerPort    =  FnFlashReadUint16( _EEPROM_ADD_TCP_PORTNO ) ;
        
         sprintf(ucDebuggBuff,"M.SSID=%s_%d\n\rPASS=%s_%d\n\rMQTT IP=%s_%d\n\rMQTT TOPIC=%s_%d\n\rTCP SERVER=%s_%d\n\rTCP PORT=%d\n\n\r",
         stVarWIFIPara_All.ucWifiSSID     , strlen(stVarWIFIPara_All.ucWifiSSID)     , stVarWIFIPara_All.ucWifiPassword  , strlen(stVarWIFIPara_All.ucWifiPassword) , 
         stVarMQTT_All.ucMQTT_UDP_ServerIP, strlen(stVarMQTT_All.ucMQTT_UDP_ServerIP), stVarMQTT_All.ucMQTT_TOPIC        , strlen(stVarMQTT_All.ucMQTT_TOPIC),
         stVarTCPPara_All.ucTCPServerIP   , strlen(stVarTCPPara_All.ucTCPServerIP)   , stVarTCPPara_All.uiTCPServerPort );
         Serial.print(ucDebuggBuff)       ;

     }
   else
     {    
          
         sprintf(ucDebuggBuff,"FLASH SIGNATURE DOESN'T MATCHED! = %x \n\r",
                 FnFlashReadUint32( _EEPROM_ADD_SIGNATURE ));
         Serial.print( ucDebuggBuff );
                
         FnFlashWriteUint32( _EEPROM_ADD_SIGNATURE , _EEPROM_SIGNATURE );
         FnFlashWriteString( _EEPROM_ADD_WIFI_SSID , _WIFI_SSID             , strlen( _WIFI_SSID ));
         FnFlashWriteString( _EEPROM_ADD_WIFI_PASS , _WIFI_PASSWORD         , strlen( _WIFI_PASSWORD ));
         FnFlashWriteString( _EEPROM_ADD_MQTT_IP   , _MQTT_SERVER_ADDRESS   , strlen( _MQTT_SERVER_ADDRESS ));          
         FnFlashWriteString( _EEPROM_ADD_MQTT_TOPIC, _MQTT_SERVER_TOPIC_NAME, strlen( _MQTT_SERVER_TOPIC_NAME));    
         FnFlashWriteString( _EEPROM_ADD_TCP_SERVER, _TCP_SERVER_ADDRESS    , strlen( _TCP_SERVER_ADDRESS ));   
         FnFlashWriteUint16( _EEPROM_ADD_TCP_PORTNO, _TCP_SERVER_PORTNO    );
         delay (1000); 
      
         strcpy( stVarWIFIPara_All.ucWifiSSID     , _WIFI_SSID );
         strcpy( stVarWIFIPara_All.ucWifiPassword , _WIFI_PASSWORD );  
         strcpy( stVarMQTT_All.ucMQTT_TOPIC       , _MQTT_SERVER_TOPIC_NAME );
         strcpy( stVarMQTT_All.ucMQTT_UDP_ServerIP, _MQTT_SERVER_ADDRESS    );
         stVarMQTT_All.ucMQTT_UDP_ServerPort      = _MQTT_SERVER_PORTNO      ;                               
         strcpy( stVarTCPPara_All.ucTCPServerIP   , _TCP_SERVER_ADDRESS     );  
         stVarTCPPara_All.uiTCPServerPort         = _TCP_SERVER_PORTNO       ;
       
         sprintf(ucDebuggBuff,"M.SSID=%s_%d\n\rPASS=%s_%d\n\rMQTT IP=%s_%d\n\rMQTT TOPIC=%s_%d\n\rTCP SERVER=%s_%d\n\rTCP PORT=%d\n\n\r",
         stVarWIFIPara_All.ucWifiSSID     , strlen(stVarWIFIPara_All.ucWifiSSID)     , stVarWIFIPara_All.ucWifiPassword , strlen(stVarWIFIPara_All.ucWifiPassword) , 
         stVarMQTT_All.ucMQTT_UDP_ServerIP, strlen(stVarMQTT_All.ucMQTT_UDP_ServerIP), stVarMQTT_All.ucMQTT_TOPIC       , strlen(stVarMQTT_All.ucMQTT_TOPIC),
         stVarTCPPara_All.ucTCPServerIP   , strlen(stVarTCPPara_All.ucTCPServerIP)   , stVarTCPPara_All.uiTCPServerPort );
         Serial.print(ucDebuggBuff)  ;
    
     }         
}   

/*****************************************************************************
 **@Function        :   setup
 **@Descriptions    :  
 **@parameters      :   None
 **@return          :   None
*****************************************************************************/
void setup() 
{
 /***************************************************/
  Serial.begin ((uint32_t)230400);
  
 /***************************************************/
  EEPROM.begin((uint8_t)127);
  FnFlashReadConfig();
  
 /***************************************************/  
 /********WIFI INIT*******/ 
    WiFi.disconnect(true);
    delay((uint16_t)2000);
     
    WiFi.begin(stVarWIFIPara_All.ucWifiSSID     , 
               stVarWIFIPara_All.ucWifiPassword);
  
    for ( uint8_t ucWifiCheck = RESET  ; 
                ucWifiCheck < (uint8_t)10 ; ucWifiCheck++ )
      {  
          if( WiFi.status() == WL_CONNECTED )     
            {
              stVarWIFIPara_All.ucWifiConnnectivityFlag = SET ;
              IPAddress SelfIPAddress = WiFi.localIP();  
              Serial.print("WIFI: CONNECTED!\n");
              Serial.println(SelfIPAddress);
              break;
            }
          else
            {
              stVarWIFIPara_All.ucWifiConnnectivityFlag = RESET ;
              Serial.print("WIFI: UNABLE TO CONNECT!\n");
            } 
            delay((uint16_t)1000); 
      }  



  /******** SETTING THE TIMMER TO 1MS INTR *******/  
  timer = timerBegin  (RESET    ,(uint8_t)80   , true);
  timerAttachInterrupt( timer   , &FnTimerISR  , true);
  timerAlarmWrite     ( timer   ,(uint16_t)1000, true);
  timerAlarmEnable    ( timer  );

  
}
  
/*****************************************************************************
 **@Function        :   setup
 **@Descriptions    :  
 **@parameters      :   None
 **@return          :   None
*****************************************************************************/
void FnWIFINetworkReconnect(void)
{
   if(WiFi.status() == WL_CONNECTED )     
     {
      if ( stVarWIFIPara_All.ucWifiConnnectivityFlag == RESET  )
         { stVarWIFIPara_All.ucWifiConnnectivityFlag  = SET    ;
            Serial.print("# ESP32 53 1 $\n");
         }
     }
   else
     {
      if ( stVarWIFIPara_All.ucWifiConnnectivityFlag == SET   )
         { stVarWIFIPara_All.ucWifiConnnectivityFlag =  RESET ;
            Serial.print("# ESP32 53 0 $\n");
            WiFi.disconnect(); delay(100);     
         }
         
          static uint8_t  
                 ucWIFIConnectivitycheck = RESET ;
                 ucWIFIConnectivitycheck ++ ;
            if ( ucWIFIConnectivitycheck == _WIFI_STATUS_CHECK_TIME )
               { ucWIFIConnectivitycheck = RESET ; 

                uint8_t ucWifiSSIDScan = WiFi.scanNetworks();
                if ( ucWifiSSIDScan != RESET )      
                    {
                     for ( uint8_t ucLoopCnt = RESET ; ucLoopCnt < ucWifiSSIDScan++ ; ucLoopCnt++ )     
                         {
                              
                          if(( strcmp ( WiFi.SSID(ucLoopCnt).c_str(),stVarWIFIPara_All.ucWifiSSID ) == RESET )
                           ||( strstr ( WiFi.SSID(ucLoopCnt).c_str(),stVarWIFIPara_All.ucWifiSSID ))!= NULL  )
                             {
                              
                               sprintf(ucDebuggBuff,"# ESP32 52 %s $\n",WiFi.SSID(ucLoopCnt));
                               Serial.print(ucDebuggBuff);
                               WiFi.disconnect(); delay(100);
                               stVarWIFIPara_All.ucWifiConnectingStart= SET ;
                               WiFi.begin(stVarWIFIPara_All.ucWifiSSID      , 
                                          stVarWIFIPara_All.ucWifiPassword ); 
                             
                             }                  
                         } 
                    }
               }
     }   

}

/*****************************************************************************
 **@Function        :   loop
 **@Descriptions    :  
 **@parameters      :   None
 **@return          :   None
*****************************************************************************/
void loop() 
{


/**************************************************************************/
  FnWIFINetworkReconnect();

/**************************************************************************/
  FnSerialReceiveData();
  FnSerialProcess();


}
0
votes

I tried a similar task with persisting values in memory. The ESP32 lacks an eeprom so you need to use some nvs (non volatile storage) functions as they are called for ESP32. See https://github.com/rpolitex/ArduinoNvs for example.