Blynk stop receive data after few seconds or minutes

I’m trying to to build the bidirectional energy meter per Solardunio blog page using NodeMCU, 16 channel Analog Multiplexer and split core current sensor HSTS016L. I was able to get the reading show up on the Blynk app but all the data stop update after few seconds for minutes (random time). I have tried to power cycle, replace and reprogram the NodeMCU but problems still exist.
Does anyone know what stop the Blynk to receive the data? (all my other Blynk project still dipslay the live data fine, running on local Blynk server)

Below is the code from Solarduino:

        /* 0.2- Blynk RTC */
        
        BlynkTimer blynkTimer;
        long interval = 5000;                                   // Duration for uploading values to Blynk server in milli seconds (upload every 5 seconds)
        int DCVoltageWait = 1;                                  /* Switching variable for DC Voltage */
        int DCCurrentWait = 1;                                  /* Switching variable for DC Current */
        int DCPowerWait = 1;                                    /* Switching variable for DC Power */
        int DCEnergyWait = 1;                                   /* Switching variable for DC Energy */
        int ACVoltageWait = 1;                                  /* Switching variable for AC Voltage */
        int ACCurrentWait = 1;                                  /* Switching variable for AC Current*/
        int ACPowerWait = 1;                                    /* Switching variable for AC Power */
        int ACEnergyWait = 1;                                   /* Switching variable for AC Energy */
        int ACFrequencyWait = 1;                                /* Switching variable for frequency */
        int multiplexerWait = 1;                                /* Switching variable for 16 channel multiplexer */
        
        /* 0.3- 16-Channel Multiplexer */
        
        #define S0 D0                                           /* Assign Multiplexer pin S0 connect to pin D0 of NodeMCU */
        #define S1 D1                                           /* Assign Multiplexer pin S1 connect to pin D1 of NodeMCU */
        #define S2 D2                                           /* Assign Multiplexer pin S2 connect to pin D2 of NodeMCU */
        #define S3 D3                                           /* Assign Multiplexer pin S3 connect to pin D3 of NodeMCU */
        #define SIG A0                                          /* Assign SIG pin as Analog output for all 16 channels of Multiplexer to pin A0 of NodeMCU */
        float DC_Voltage;                                       /* Assign the variable as analog output value from Channel C0 */
        float DC_Current;                                       /* Assign the variable as analog output value from Channel C1 */
        float AC_Voltage;                                       /* Assign the variable as analog output value from Channel C2 */
        float AC_Current;                                       /* Assign the variable as analog output value from Channel C3 */
        unsigned long channelLastSample  = 0;                   /* Time variable for multiplexer */

        /* 0.4 - Initial Offset Function */
        
        float initialAnalogOffset = 4;  //4                     // Analog Value displayed when sensor provides 0V output - use potentiometer to calibrate multiplexer
        float voltageAtAnalogMax = 3.153;  //3.16 - 3.289               // Voltage Value measured when sensor provides analog value of 1024 (max) - use potentiometer and multimeter
        float analogOffset = ((1024 - initialAnalogOffset) / 1024)*(voltageAtAnalogMax / 3.3) ;     /* Variable for offset*/
        int initialOffsetWait = 1;                              /* Switching variable for initial offset */   
        unsigned long initialOffsetLastSample  = 0;             /* Time variable for initial offset */

        /* 1- DC Voltage Measurement */
        
        float voltageSampleReadDC  = 0;                         /* to read the analog sensor of a sample*/
        float voltageSampleSumDC   = 0;                         /* accumulation of sample readings */
        float voltageSampleCountDC = 0;                         /* to count number of sample. */
        float voltageMeanDC ;                                   /* to calculate the average value from all samples*/ 
        float finalVoltageDC =0;                                /* shows the final voltage reading*/
        float supplyVoltageDC = 3.3;                            /* maximum measuring voltage of micro-controller , default 5V for UNO, 3.3V for NodeMCU*/
        float R1 = 20000;             //19890                          // Input resistance value for R1 (in ohm) based on Voltage Divider Method (Based on the wiring diagram)
        float R2 = 1000;               //987                         // Input resistance value for R2 (in ohm) based on Voltage Divider Method (Based on the wiring diagram)
        unsigned long DCVoltageLastSample = 0;                  /* Time variable for DC Voltage */

        /* 2- DC Current Measurement */

        float mVperAmpValueDC = 3.123;                          // For example, a 20A Hall-Effect Current Transformer rated at 20A, 2.5V +/- 0.625V, mVperAmp will be 625 mV / 20A = 31.25mV/A  
        float supplyVoltageCDC = 3300;  //3300 -3289                        // maximum measuring voltage of micro-controller , default 5000mV for UNO, 3300mV for NodeMCU*/
        float currentSampleReadDC  = 0;                         /* to read the analog sensor of a sample*/
        float currentSampleSumDC   = 0;                         /* accumulation of sample readings */
        float currentSampleCountDC = 0;                         /* to count number of sample. */
        float currentMeanDC ;                                   /* to calculate the average value from all samples*/ 
        float finalCurrentDC ;                                  /* the final current reading without adding offset value*/
        float DC_Current_Calibrate=0;                           /* variable for DC Current Offset */
        unsigned long DCCurrentLastSample = 0;                  /* Time variable for DC Current */
        
        /* 3- DC Power Calculation */
        
        float finalPowerDC;                                     /* Variable for DC Power Value */

        /* 4- DC Energy Calculation */
        
        float finalEnergyDC;                                    /* Variable for instantaneous energy unit */
        float netEnergyDC = 0;                                  /* Variable for accumulated net energy */
        float importEnergyDC = 0;                               /* Variable for accumulated import energy */
        float exportEnergyDC = 0;                               /* Variable for accumulated export energy*/
       
        /* 5- AC Voltage Measurement */
        
        float R3 = 200;                                         // Input resistance value for R3 (in ohm) based on Voltage Divider Method (Based on the wiring diagram)
        float R4 = 330;                                         // Input resistance value for R4 (in ohm) based on Voltage Divider Method (Based on the wiring diagram)
        float voltageSampleReadAC  = 0;                         /* to read the analog sensor of a sample including voltageOffset1 */
        float voltageSampleSumAC   = 0;                         /* accumulation of sample readings */
        float voltageSampleCountAC = 0;                         /* to count number of sample. */
        float voltageMeanAC ;                                   /* to calculate the average value from all samples, in analog values*/ 
        float RMSVoltageMeanAC ;                                /* square roof of voltageMean without offset value, in analog value*/
        float adjustRMSVoltageMeanAC;                           /* RMS including voltageOffset2 */
        float FinalRMSVoltageAC;                                /* final voltage value with offset value*/
        float voltageOffset1AC =0.00 ;                          /* variable for 1st offset value*/
        float voltageOffset2AC = 0.00;                          /* variable for 2nd offset value*/
        float VACResistor;                                      /* Multiplication or reduction based on voltage divider method*/
        float waveformMagnification = 1.5;                      // Vac waveform reduction factor to prevent waveform distortion near to limit. 2 means waveform reduced by 50%.
        unsigned long ACVoltageLastSample = 0;                  /* Time variable for AC Voltage */
        
        /* 6- AC Current Measurement */

        float mVperAmpValueAC = 31.25;                          // For example, a 50A Hall-Effect Current Transformer rated at 50A, 2.5V +/- 0.625V, mVperAmp will be 625 mV / 50A = 12.5 mV/A
        float supplyVoltageAC = 3300;                           /* maximum measuring voltage of micro-controller , default 5000mV for UNO, 3300mV for NodeMCU*/
        float currentSampleReadAC  = 0;                         /* to read the analog sensor of a sample including current Offset value*/
        float currentSampleSumAC   = 0;                         /* accumulation of sample readings */
        float currentSampleCountAC = 0;                         /* to count number of sample. */
        float currentMeanAC ;                                   /* to calculate the average value from all samples, in analog values*/ 
        float RMSCurrentMeanAC ;                                /* square roof of currentMean, in analog values */   
        float FinalRMSCurrentAC ;                               /* the final RMS current reading*/
        float AC_Current_Calibrate=0;                           /* variable for AC Current Offset */
        unsigned long ACCurrentLastSample = 0;                  /* Time variable for AC Current */
        
        /* 7- AC Power Calculation */

        float voltageAC ;                                       /* Variable for instantaneous AC Voltage Value */
        float currentAC1  ;                                     /* Variable for part of instantaneous AC Current Value */
        float currentAC2  ;                                     /* Variable for part of instantaneous AC Current Value */
        float currentAC3  ;                                     /* Variable for part of instantaneous AC Current Value */
        float powerSampleReadAC=0;                              /* Variable for analog sample of AC Power */
        float powerSampleSumAC   = 0;                           /* accumulation of sample readings */
        float powerSampleCountAC = 0;                           /* to count number of sample. */
        float realPowerAC;                                      /* final Real Power Value*/
        float apparentPowerAC;                                  /* Apparent Power Value*/
        float powerFactorAC;                                    /* Power Factor Value*/
        unsigned long ACPowerLastSample = 0;                    /* Time variable for AC Power */
        
        /* 8- AC Energy Calculation */
        
        float finalEnergyAC;                                    /* Variable for instantaneous energy unit */
        float netEnergyAC = 0;                                  /* Variable for accumulated net energy */
        float importEnergyAC = 0;                               /* Variable for accumulated import energy */
        float exportEnergyAC = 0;                               /* Variable for accumulated export energy */

        /* 9- AC Frequency Calculation */
        
        float frequencySampleReadAC;                            /* Variable for voltage analog waveform*/
        float frequencyAC;                                      /* Frequency Value */
        float cycleNumber = 0;                                  /* Variable to count cycle of waveform*/
        int ACFrequencyState = 0;                               /* Switching state based on waveform*/
        unsigned long startMillisAC = 0;                        /* Time variable for starting time*/
        unsigned long currentMillisAC = 0;                      /* Time variable for every waveform*/
        unsigned long ACFrequencyLastSample = 0;                /* Time variable for Frequency calculation*/

      

Continue with the code:

void setup() 
{
 
        /* 0- General */
        
        /* 0.1- Blynk Server and Wifi Connection */
       
//        WiFi.begin(ssid, pass);                                 /* Function to begin Wifi connection*/
//        Blynk.config(auth, server);                             /* Function to begin Blynk server connection*/

        // For WiFi OTA ==> by Key
        WiFi.mode(WIFI_STA);
        #if defined(USE_LOCAL_SERVER)
        WiFi.begin(ssid, pass);
        Blynk.config(AUTH, server, port);
        #else
        Blynk.begin(AUTH, ssid, pass);
        #endif
        while (Blynk.connect() == false) {}
        ArduinoOTA.setHostname(OTA_HOSTNAME);
        ArduinoOTA.begin();
       
        /* 0.2- Blynk RTC */
        
        blynkTimer.setInterval(interval,blynk);                 /* Bylnk timer for sending values to Blynk Server */
        
        /* 0.3- 16-Channel Multiplexer */
        
        pinMode(S0,OUTPUT);    pinMode(S1,OUTPUT);              /* Define digital signal pin as output to the Multiplexer pin SO & S1 */                                                                                      /* Define digital signal pin as output to the Multiplexer pin S1 */  
        pinMode(S2,OUTPUT);    pinMode(S3,OUTPUT);              /* Define digital signal pin as output to the Multiplexer pin S2 & S3 */ 
        pinMode(SIG, INPUT);                                    /* Define analog signal pin as input or receiver from the Multiplexer pin SIG */  

        /* 0.4- Initial Offset */
        
        initialOffsetLastSample = millis();                     /* start counting time for initial offset*/
}


 

     
void loop() 
{
        
        /* 0.1- Blynk Server & Timer */
        
        while(!Blynk.connected()){ Blynk.connect();}                      /* If Blynk suddently disconnect, try to reconnect again */
        Blynk.run();                                                      /* allow constant communication between Blynk server and Node MCU*/
        ArduinoOTA.handle();                                              /* for WiFi OTA */
        blynkTimer.run();                                                 /* allow cycle timer to send values to Blynk server

        /* 0.3- 16-Channel Multiplexer */
        
        if((multiplexerWait==1) && (millis()>= channelLastSample + 1))    /* If multiplexer is allow to calculate, for every 1 milli second, run the codes*/
          {
            digitalWrite(S0,LOW); digitalWrite(S1,LOW);                   /* Open Channel 0 (C0 pin - binary output 0,0,0,0)*/
            digitalWrite(S2,LOW); digitalWrite(S3,LOW);
            DC_Voltage = analogRead(SIG) - initialAnalogOffset ;          /* Record the value as DC Voltage, after minus off the offset value */
            
            digitalWrite(S0,HIGH); digitalWrite(S1,LOW);                  /* Open Channel 1 (C1 pin - binary output 1,0,0,0)*/
            digitalWrite(S2,LOW); digitalWrite(S3,LOW);  
            DC_Current = analogRead(SIG) - initialAnalogOffset ;          /* Record the value as DC Current, after minus off the offset value */ 
            
            digitalWrite(S0,LOW); digitalWrite(S1,HIGH);                  /* Open Channel 2 (C2 pin - binary output 0,1,0,0)*/
            digitalWrite(S2,LOW); digitalWrite(S3,LOW);  
            AC_Voltage = analogRead(SIG)- initialAnalogOffset;            /* Record the value as AC Voltage, after minus off the offset value */ 
           
            digitalWrite(S0,HIGH); digitalWrite(S1,HIGH);                 /* Channel 3 (C3 pin - binary output 1,1,0,0)*/
            digitalWrite(S2,LOW); digitalWrite(S3,LOW); 
            AC_Current = analogRead(SIG)- initialAnalogOffset;            /* Record the value as AC Current, after minus off the offset value */ 
        
            channelLastSample = millis();                                 /* Reset counting time for the next cycle*/
          }

        if((DCVoltageWait == 0) && (DCCurrentWait == 0) &&                /* If all the calculation have been done, stop the sensor readings*/
           (ACVoltageWait == 0) && (ACCurrentWait == 0) )
          { multiplexerWait = 0;  }

        /* 0.4 Initial Offset */

        if((initialOffsetWait ==1) && (millis() >= initialOffsetLastSample + 15000))            /* For the first 15 seconds during startup, it auto calculate for certain offset for some sensors*/
        {
          DC_Current_Calibrate = currentMeanDC;                                                 /* Offset for DC Current*/
          AC_Current_Calibrate = sqrt(currentMeanAC);                                           /* Offset for AC Current*/
          voltageOffset1AC = -sqrt(voltageMeanAC);                                              /* Offset for AC Voltage*/                         
          multiplexerWait = 1;                                                                  /* Switch Multiplexer sensor measurement code ON */
          DCCurrentWait = 1;                                                                    /* Switch DC Current calculation code ON */
          ACVoltageWait = 1;                                                                    /* Switch AC Voltage calculation code ON */
          ACCurrentWait = 1;                                                                    /* Switch AC Current calculation code ON */
          initialOffsetWait = 2;                                                                /* Switch to second initial offset function*/
          initialOffsetLastSample = millis();                                                   /* Start countng time for second initial offset function*/
        }

        if(initialOffsetWait ==2)                                                               /* If Second inital offset function is activated*/
        {
          if(millis() >= initialOffsetLastSample + 5000)                                        /* If after 5 more seconds, run the code*/
          {
            voltageOffset2AC = -RMSVoltageMeanAC;                                               /* Set voltage second offset value*/
            initialOffsetWait = 0;                                                              /* Stop the initial offset function until further notice*/
          }
        }
        
        /* 1 - DC Voltage Measurement */
        
        if((DCVoltageWait == 1) && (millis() >= DCVoltageLastSample + 1))                         /* If DC Voltage is allow to calculate, for every 1 milli second, run the codes*/           
          {
            voltageSampleReadDC = DC_Voltage;                                                     /* read the analog sensor sample value */
            voltageSampleSumDC = voltageSampleSumDC + voltageSampleReadDC ;                       /* accumulate value with older sample readings*/
            voltageSampleCountDC = voltageSampleCountDC + 1;                                      /* count the number of samples*/
            DCVoltageLastSample = millis();                                                       /* Reset counting time for the next cycle*/
          }
          
        if(voltageSampleCountDC == 1000)                                                          /* after 1000 count or 1 second, do the calculation*/
          {
            voltageMeanDC = voltageSampleSumDC/voltageSampleCountDC;                              /* calculate average value of all sample readings taken*/
            finalVoltageDC = ((voltageMeanDC*supplyVoltageDC)/1024) / (R2/(R1+R2))* analogOffset; /* Calculate the final voltage */
            voltageSampleSumDC =0;                                                                /* to reset accumulate sample values for the next cycle */
            voltageSampleCountDC = 0;                                                             /* to reset the counting for the next cycle */
            DCVoltageWait = 0;                                                                    /* Stop the calculation function until further notice*/
          }

        /* 2- DC Current Measurement */

        if((DCCurrentWait ==1) && (millis() >= DCVoltageLastSample + 1))                                        /* If DC Current is allow to calculate, for every 1 milli second, run the codes*/
          {
            currentSampleReadDC = DC_Current - DC_Current_Calibrate;                                            /* read the analog sample value including offset value*/
            currentSampleSumDC = currentSampleSumDC + currentSampleReadDC ;                                     /* accumulate total analog values for each sample readings*/
            currentSampleCountDC = currentSampleCountDC + 1;                                                    /* count the number of samples*/  
            DCVoltageLastSample = millis();                                                                     /* Reset counting time for the next cycle*/
          }
        
        if(currentSampleCountDC == 1000)                                                                        /* after 1000 count or 1 second, do the calculation*/
          { 
            currentMeanDC = currentSampleSumDC/currentSampleCountDC ;                                           /* average accumulated analog values*/
            finalCurrentDC = (((currentMeanDC /1024)*supplyVoltageCDC) /mVperAmpValueDC)* analogOffset;         /* calculate the final RMS current*/
            if((finalCurrentDC <= (625/mVperAmpValueDC/100)) && (finalCurrentDC >= (-625/mVperAmpValueDC/100))) /* if the current detected is less than 1% based on rating, set current value to 0A*/
            { finalCurrentDC =0; }
            currentSampleSumDC =0;                                                                              /* to reset accumulate sample values for the next cycle */
            currentSampleCountDC=0;                                                                             /* to reset number of sample for the next cycle */
            DCCurrentWait = 0;
          }

        /* 3- DC Power Calculation */
        
        if((DCPowerWait == 1) && (DCVoltageWait == 0) && (DCVoltageWait ==0))                                   /* If DC Power code is allow to calculate, while DC Voltage & DC Current have calculate finished, run the code*/
          {
            finalPowerDC = finalVoltageDC*finalCurrentDC;                                                       /* DC Power is Current x Voltage */
            DCPowerWait = 0;                                                                                    /* Stop the calculation function until further notice*/
          }
          
        /* 4- DC Energy Calculation */

        if((DCEnergyWait == 1) && (DCVoltageWait == 0) && (DCVoltageWait ==0) && (DCPowerWait == 0))            /* If DC Energy is allow to calculate, while DC Power, DC Voltage & DC Current have calculate finished, run the code*/
          {
            finalEnergyDC = ((finalPowerDC / 3600)* (interval/1000)) ;                                          /* Calculate Energy Sample value*/
            netEnergyDC = netEnergyDC + finalEnergyDC;                                                          /* Calculate accumulated Net Energy*/
            if( finalPowerDC >= 0)                                                                              /* If the power value is larger than 0*/
            { importEnergyDC = importEnergyDC + finalEnergyDC; }                                                /* Add the Energy Sample value to the import variable*/
            if(finalPowerDC < 0)                                                                                /* If the power value is smaller than 0*/
            { exportEnergyDC = exportEnergyDC - finalEnergyDC; }                                                /* Add the Energy Sample value to the export variable*/
            DCEnergyWait = 0;                                                                                   /* Stop the calculation function until further notice*/
          }
          
        /* 5- AC Voltage Measurement */
          
        if((ACVoltageWait == 1) && (millis() >= ACVoltageLastSample + 1))                                       /* If AC Voltage is allow to calculate, for every 1 milli second, run the codes*/
          {
            voltageSampleReadAC = AC_Voltage + voltageOffset1AC;                                                /* read the analog sample value including offset value*/
            voltageSampleSumAC = voltageSampleSumAC + sq(voltageSampleReadAC) ;                                 /* accumulate total analog values for each sample readings*/
            voltageSampleCountAC = voltageSampleCountAC + 1;                                                    /* count the number of samples*/
            ACVoltageLastSample = millis();                                                                     /* Reset counting time for the next cycle*/
          }
           
        if(voltageSampleCountAC == 1000)                                                                        /* after 1000 count or 1 second, do the calculation*/
          {
            voltageMeanAC = voltageSampleSumAC/voltageSampleCountAC;                                            /* calculate average value of all sample readings taken*/
            RMSVoltageMeanAC = (sqrt(voltageMeanAC))*waveformMagnification;                                     /* calculate the RMS voltage including the magnification ratio towards the waveform.*/      
            adjustRMSVoltageMeanAC = RMSVoltageMeanAC + voltageOffset2AC;                                       /* calculate the RMS voltage including second offset value */                                                                                                                                                       /* square root of the average value*/                                                                                                             
            VACResistor = R4 / (R3 + R4);                                                                       /* Voltage divider calculation*/
            FinalRMSVoltageAC = adjustRMSVoltageMeanAC*(0.66/VACResistor);                                      /* final RMS voltage with voltage divider offset*/
            if(FinalRMSVoltageAC <= 2.5)                                                                        /* to eliminate any possible ghost or fake values when no voltage measured*/
              {FinalRMSVoltageAC = 0;}
            voltageSampleSumAC =0;                                                                              /* to reset accumulate sample values for the next cycle */
            voltageSampleCountAC=0;                                                                             /* to reset sample count for the next cycle */
            ACVoltageWait = 0;                                                                                  /* Stop the calculation function until further notice*/
          }

        /* 6- AC Current Measurement */

        if((ACCurrentWait ==1) && (millis() >= ACVoltageLastSample + 1))                                        /* If AC Current is allow to calculate, for every 1 milli second, run the codes*/
          {
            currentSampleReadAC = AC_Current - AC_Current_Calibrate;                                            /* read the analog sample value including offset value*/
            currentSampleSumAC = currentSampleSumAC + sq(currentSampleReadAC) ;                                 /* accumulate total analog values for each sample readings*/
            currentSampleCountAC = currentSampleCountAC + 1;                                                    /* count the number of samples*/
            ACVoltageLastSample = millis();                                                                     /* Reset counting time for the next cycle*/
          }
        
        if(currentSampleCountAC == 1000)                                                                        /* after 1000 count or 1 second, do the calculation*/
          { 
            currentMeanAC = currentSampleSumAC/currentSampleCountAC;                                            /* average accumulated analog values*/
            RMSCurrentMeanAC = sqrt(currentMeanAC);                                                             /* square root of the average value*/
            FinalRMSCurrentAC = (((RMSCurrentMeanAC /1024) *supplyVoltageAC) /mVperAmpValueAC);                 /* calculate the final RMS current*/
            if(FinalRMSCurrentAC <= (625/mVperAmpValueAC/100))                                                  /* if the current detected is less than or up to 1% based on rating, set current value to 0A*/
            { FinalRMSCurrentAC =0; }
            currentSampleSumAC =0;                                                                              /* to reset accumulate sample values for the next cycle */
            currentSampleCountAC=0;                                                                             /* to reset sample count for the next cycle */
            ACCurrentWait = 0;                                                                                  /* Stop the calculation function until further notice*/
          }

        /* 7- AC Power Calculation */

        if((ACPowerWait ==1) && (millis() >= ACPowerLastSample + 1))                                            /* If AC Power is allow to calculate, for every 1 milli second, run the codes*/
          {     
            voltageAC = waveformMagnification*(AC_Voltage + voltageOffset1AC);                                  /* Calculate instantaneous Voltage Value*/
            currentAC1 = AC_Current - AC_Current_Calibrate;                                                     /* Calculate part of instantaneous Current Value*/
            currentAC2 = (currentAC1/1024)*supplyVoltageAC;                                                     /* Calculate part of instantaneous Current Value*/
            currentAC3 = currentAC2/mVperAmpValueAC;                                                            /* Calculate part of instantaneous Current Value*/
            powerSampleReadAC = voltageAC * currentAC3 ;                                                        /* Get instantaneous Real Power sample value*/
            powerSampleSumAC = powerSampleSumAC + powerSampleReadAC ;                                           /* accumulate total sample values*/
            powerSampleCountAC= powerSampleCountAC + 1;                                                         /* count the number of samples*/
            ACPowerLastSample = millis();                                                                       /* Reset counting time for the next cycle*/
          }

        if(powerSampleCountAC == 1000)                                                                          /* after 1000 count or 1 second, do the calculation*/
          {  
            realPowerAC = powerSampleSumAC / 1000;                                                              /* calculate the final Real Power*/
            if(realPowerAC < 0) 
            { FinalRMSCurrentAC = - FinalRMSCurrentAC;}                                                         /* Provide current direction */
            apparentPowerAC = FinalRMSVoltageAC * FinalRMSCurrentAC;                                            /* calculate the final Apparent Power*/
            powerFactorAC = realPowerAC / apparentPowerAC;                                                      /* calculate the final Power Factor*/    
            if(realPowerAC < 0)
            { powerFactorAC = -1* (realPowerAC / apparentPowerAC);}                                             /* Power Factor will be negative value if Real Power is negative*/
            if(powerFactorAC > 1 || powerFactorAC <-1)                                                          /* If power Factor larger than 1 or smaller than -1, turn the power factor to 0 value*/
            { powerFactorAC = 0; }
            powerSampleSumAC = 0;                                                                               /* to reset accumulate sample values for the next cycle */
            powerSampleCountAC = 0;                                                                             /* to reset sample count for the next cycle */
            ACPowerWait = 0;                                                                                    /* Stop the calculation function until further notice*/
          }

        /* 8- AC Energy Calculation */
        
        if((ACEnergyWait == 1) && (ACVoltageWait == 0) && (ACVoltageWait ==0) && (ACPowerWait == 0))            /* If AC Energy is allow to calculate, while AC Power, AC Voltage & AC Current have calculate finished, run the code*/
          {
            finalEnergyAC = ((realPowerAC / 3600)* (interval/1000));                                            /* Calculate Energy Sample value*/
            netEnergyAC = netEnergyAC + finalEnergyAC;                                                          /* Calculate accumulated Net Energy*/
            if( realPowerAC >= 0)                                                                               /* If real power is larger than 0*/
            { importEnergyAC = importEnergyAC + finalEnergyAC; }                                                /* Add the Energy Sample value to the import variable*/
            if(realPowerAC < 0)                                                                                 /* If real power is smaller than 0*/
            { exportEnergyAC = exportEnergyAC - finalEnergyAC; }                                                /* Add the Energy Sample value to the export variable*/
            ACEnergyWait = 0;                                                                                   /* Stop the calculation function until further notice*/
          }

        /* 9- AC Frequency Measurement */

        if((ACFrequencyWait == 1) && (millis() >= ACFrequencyLastSample + 1))                                   /* If Frequency is allow to calculate, for every 1 milli second, run the codes*/
          { 
            frequencySampleReadAC = AC_Voltage + voltageOffset1AC;                                              /* Read the AC Voltage Waveform*/
            
            if((ACFrequencyState == 0) && (frequencySampleReadAC < 0))                                          /* Beginning & Standby mode when waveform is negative*/
              { ACFrequencyState = 1; }                                                  

            if((ACFrequencyState == 1) && (frequencySampleReadAC >= 0))                                         /* Start countint time when signal is larger than 0 */
              { startMillisAC = millis();
                ACFrequencyState = 2; }
          
            if((ACFrequencyState == 2) && (frequencySampleReadAC < 0))                                          /* When the waveform dropped to negative, go standby mode*/
              { ACFrequencyState = 3; }    
        
            if((ACFrequencyState == 3) && (frequencySampleReadAC >= 0))                                         /* When waveform is positive again, it completes a cycle*/
              { cycleNumber = cycleNumber + 1;                                                                  /* Whenever it activates, add the cycle number value*/
                currentMillisAC = millis();                                                                     /* Record the current time whenever it activates*/
                ACFrequencyState = 2; }                                                                         /* go back to the previous stage for the next cycle*/
             
            if(cycleNumber == 45)                                                                               /* When cycle number reaches 45 (must be less than 47 for 50Hz or 55 for 60Hz*/
              { frequencyAC = 1000 / ((currentMillisAC - startMillisAC) / cycleNumber);                         /* Calculate the frequency value*/
                cycleNumber = 0;                                                                                /* Reset the cycle number for the next set*/
                ACFrequencyState = 0;                                                                           /* Reset the waveform state for the next set*/
                ACFrequencyWait = 0;                                                                            /* Stop the calculation function until further notice*/
                ACFrequencyLastSample = millis();}
          }
        

}


void blynk()                                                                                                    /* For every repeat cycle (5 seconds by default), run the codes*/
{
      if((initialOffsetWait == 0)&&(ACFrequencyWait==0))                                                        /* Make sure Initial Offset code & frequency codes are inactive before start uploading values*/
        {
          Blynk.virtualWrite(V1,finalVoltageDC); 
          Blynk.virtualWrite(V2,finalCurrentDC);
          Blynk.virtualWrite(V3,finalPowerDC);
          Blynk.virtualWrite(V4,netEnergyDC);
          Blynk.virtualWrite(V5,importEnergyDC);
          Blynk.virtualWrite(V6,exportEnergyDC);
          Blynk.virtualWrite(V7,FinalRMSVoltageAC);       
          Blynk.virtualWrite(V8,FinalRMSCurrentAC);
          Blynk.virtualWrite(V9,realPowerAC);
          Blynk.virtualWrite(V10,apparentPowerAC);
          Blynk.virtualWrite(V11,powerFactorAC);
          Blynk.virtualWrite(V12,netEnergyAC);
          Blynk.virtualWrite(V13,importEnergyAC);
          Blynk.virtualWrite(V14,exportEnergyAC);
          Blynk.virtualWrite(V15,frequencyAC);
          
          DCVoltageWait = 1;                                                                                    /* activates all calculation codes after all values uploaded to Blynk*/
          DCCurrentWait = 1;
          DCPowerWait = 1;
          DCEnergyWait = 1;
          ACVoltageWait = 1;
          ACCurrentWait = 1;
          ACPowerWait = 1;
          ACEnergyWait = 1;
          ACFrequencyWait = 1;
          multiplexerWait = 1;          
        }
}

:thinking:

Hmmm, could it possibly be that void loop?

Pete.

Can you please give little more detail for what need to be done ?

Pete.