Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By jaydippatelkalol
#92503 whole EEPROM reset and filled with random values when press reset button at specific process in function.

Code: Select all#include <SoftwareSerial.h>

typedef struct
{
  uint32_t uint0_8_bit  : 8;
  uint32_t uint8_16_bit : 8;
  uint32_t uint16_24_bit: 8;
  uint32_t uint24_32_bit: 8;

} uint32_data_st;

float VF = 0;
float CF = 0;
float Vol = 0;
float Current = 0;
float Power = 0;
uint32_t PFData = 0;
uint8_t last_state_of_PF_bit = 0;

#define Button_reset_eeprom 0

uint32_t VolPar;
uint32_t VolData;
uint32_t PowerPar;
uint32_t PowerData;
uint16_t PF;
uint32_t CurrentPar;
uint32_t CurrentData;


float last_kwh_reading = 0.0;


#include <EEPROM.h>

//#define HL Serial
SoftwareSerial HL(D1, D2); // RX, TX

void setup() {
  // put your setup code here, to run once:

  pinMode(Button_reset_eeprom, INPUT_PULLUP);


  EEPROM.begin(512);
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(4800);
  VF = 1.88;
  CF = 1.0;
  Serial.println("--------last_kwh_reading start -------");
  last_kwh_reading = read_data_total_KWH()   ;
  Serial.println( last_kwh_reading, 4);

  Serial.println("--------last_kwh_reading end -------");
  Serial.println("======EE========");
  Serial.println((char)EEPROM.read(511));
  Serial.println("========EEE======");


  //delay(10000);
  HL.begin(4800, SWSERIAL_8E1);
  // HL.enableIntTx(true);



}

void loop() {

  if (digitalRead(Button_reset_eeprom) == 0)
  {
    Serial.println("============== Reset EEPROM =========== \n============== reset mcu ==============");
    store_data_total_KWH(00.000000000);
    delay(1000);
    ESP.reset();
  }


  digitalWrite(LED_BUILTIN, 0);
  HLW8032_Work();
  digitalWrite(LED_BUILTIN, 1);

  delay(3000);

}// END void loop()



bool Checksum(uint8_t serial_data[24])
{
  byte check = 0;
  for (byte a = 0; a <= 20; a++)
  {
    check = check + serial_data[a];
  }
  if (check  == serial_data[21])
  {

    return true;
  }
  else
  {
    return false;
  }
}





float GetKWh()
{
  float InspectingPower = GetInspectingPower();
  float PFcnt_pls = (1.0 / (float)PowerPar) * (1.0 / (float)VF * (float)CF ) * 1000000000.0 * 3600.0;
  //float KWh = ((float)PFData * (float)PF) / (float)PFcnt;
  float KWh = (((float)PFData * 65536.0) + (float)PF) / (float)PFcnt_pls;
  return KWh;

}



float GetInspectingPower()
{
  return (float)Vol * (float)Current * 0.001;
}





void  store_data_total_KWH( float data_ft)
{
  uint32_t data_32 = FloatToUint (data_ft);
  uint32_data_st  *data_32_data_pt  ;
  data_32_data_pt =  (uint32_data_st*) &data_32;

  uint8_t  data_byte_0  = data_32_data_pt->uint0_8_bit;
  uint8_t  data_byte_8  = data_32_data_pt->uint8_16_bit;
  uint8_t  data_byte_16 = data_32_data_pt->uint16_24_bit;
  uint8_t  data_byte_24 = data_32_data_pt->uint24_32_bit;

  EEPROM.write(5, data_byte_0);
  EEPROM.commit();
  EEPROM.write(6, data_byte_8);
  EEPROM.commit();
  EEPROM.write(7, data_byte_16);
  EEPROM.commit();
  EEPROM.write(8, data_byte_24);
  EEPROM.commit();
  EEPROM.write(511, 'A');
  if (EEPROM.commit())
  {
    Serial.println("------Store Done Total KWH------");
  }
}




float read_data_total_KWH(void)
{
  return   UintToFloat ((    EEPROM.read(8) << 24 | (EEPROM.read(7) << 16 | EEPROM.read(6) << 8) | (EEPROM.read(5))   )) ;
}



uint32_t FloatToUint(float n)
{
  return (uint32_t)(*(uint32_t*)&n);
}

float UintToFloat(uint32_t n)
{
  return (float)(*(float*)&n);
}

void HLW8032_Work(void)
{

  serial_flush();
  delay(200);

  if (HL.available() > 0)
  {

    for (uint8_t HL_Serial_loop = 0; HL_Serial_loop <= 24 ; HL_Serial_loop++)
    {

      uint8_t status_reg = HL.read();
      //Serial.println(status_reg,HEX);
      if (status_reg == 0x55)
      {
        //delay(5); // no need due to delay before "if (HL.available() > 0)"
        uint8_t chk_reg = HL.read();

        if (chk_reg == 0x5A)
        {

          // Serial.println("------ Serial Data Hex ------");

          //for (uint8_t serial_index = 0; serial_index <= 21 ; serial_index++)

          uint8_t serial_data[24] = {0};
          uint8_t serial_index = 0;
          while (HL.available() > 0)
          {
            serial_data[serial_index] = HL.read();

            //delay(2); // no need due to delay before "if (HL.available() > 0)"
            if (serial_index >= 21)
            {
              break;
            }
            serial_index++;
          }


          Serial.println("============== serial_index length ================");
          Serial.println(serial_index);
          Serial.println("===================================================");

          if (serial_index != 21)
          {
            Serial.println("======================Return=============================");
            return;

          }






          //Serial.println("---------------");
          if (Checksum(serial_data) == true)
          {

            VolPar = ((uint32_t)serial_data[0]  << 16) + ((uint32_t)serial_data[1] << 8) + serial_data[2];
            if (bitRead(serial_data[18], 6) == 1)
            {

              VolData = ((uint32_t)serial_data[3]  << 16) + ((uint32_t)serial_data[4] << 8) + serial_data[5];


              Vol = ((float)VolPar / (float)VolData) * (float)VF;


            }


            CurrentPar = ((uint32_t)serial_data[6]  << 16) + ((uint32_t)serial_data[7] << 8) + serial_data[8];
            if (bitRead(serial_data[18], 5) == 1)
            {

              CurrentData = ((uint32_t)serial_data[9]  << 16) + ((uint32_t)serial_data[10] << 8) + serial_data[11];

              Current = (((float)CurrentPar / (float)CurrentData) * (float)CF) * 1000.0;


            }



            if (bitRead(serial_data[18], 4) == 1  )
            {
              PowerPar = ((uint32_t)serial_data[12]  << 16) + ((uint32_t)serial_data[13] << 8) + (uint32_t)serial_data[14];
              PowerData = ((uint32_t)serial_data[15]  << 16) + ((uint32_t)serial_data[16] << 8) + (uint32_t)serial_data[17];

              Power = (((float)PowerPar / (float)PowerData) * (float)VF * (float)CF);


            }

            PF = ((uint16_t)serial_data[19] << 8) + serial_data[20];
            //store_data_KWH(PF);



            if (bitRead(serial_data[18], 7) == 1 && last_state_of_PF_bit == 0)
            {
              PFData++;
              last_state_of_PF_bit = 1;
            }
            else if (bitRead(serial_data[18], 7) == 0  && last_state_of_PF_bit == 1)
            {
              PFData++;
              last_state_of_PF_bit = 0;
            }

            Serial.println("++++++ Test++++++");
            Serial.println(bitRead(serial_data[18], 7));
            Serial.println("+++++++++++++++++");


            Serial.println("\n====================Readings====================");
            Serial.println("Voltage= " + (String) Vol);
            Serial.println("CurrentData= " + (String)Current );

            Serial.println("Power Active = " + (String)Power);
            Serial.println("Power Inspected = " + (String) GetInspectingPower());

            Serial.println("Power Data= " + (String)PFData);
            Serial.println("PF resisror = " + (String)PF);

            float KWH = GetKWh();
            Serial.print("Power Kwh= " );
            Serial.println(KWH, 4);

            //store_data_KWH( KWH );
            float total_power = last_kwh_reading + KWH ;

            store_data_total_KWH(total_power);

            Serial.print("Total Power Kwh= " );
            Serial.println(total_power, 4);

            Serial.println("\n==============================================");

            return;



          }




        }//END if (chk_reg == 0x5A)
      }// END if (status_reg == 0x55)

      else if (status_reg >> 4 == 0x0F)
      {
        //Serial.println(status_reg,HEX);
        bool pwr_ovf = status_reg & 0x02;
        bool cur_ovf = status_reg & 0x04;
        bool vol_ovf = status_reg & 0x08;
        if (pwr_ovf == 1)
        {
          //Serial.println("----> pwr_ovf <----");
        }
        if (cur_ovf == 1)
        {
          // Serial.println("----> cur_ovf <----");
        }
        if (vol_ovf == 1)
        {
          //Serial.println("----> vol_ovf <----");
        }



      }



    }


  } // END if (HL.available() > 0)


}


void serial_flush()
{
  for (uint16_t i = 0; i <= 2400 ; i++)
  {
    HL.read();
    HL.flush();
  }
}
You do not have the required permissions to view the files attached to this post.