PZEM-004T v3.0 and NodeMCU / Wemos Mini running on Blynk - How To procedure

Change the decimal point in Blynk app, like /.##W then the result will look like this 44.99W

Can the energy reading be reset to zero programmatically?

Yes, you can.
I just looked up the data sheet and command set for the V3 and it’s done by writing to address 0x42
Section 2.56 of this document:

You’re probably going to need to use a node.writeSingleCoil(0x42,0000) command to do this, although life gets much easier if you use a library to so all the heavy lifting for you.

Pete.

2 Likes

Hi

I get this error
Executable segment sizes:

IROM : 289936 - code in flash (default or ICACHE_FLASH_ATTR)

IRAM : 30004 / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs…)

DATA : 1276 ) - initialized variables (global, static) in RAM/HEAP

RODATA : 2456 ) / 81920 - constants (global, static) in RAM/HEAP

BSS : 27144 ) - zeroed variables (global, static) in RAM/HEAP

esptool.py v2.8
Serial port COM9
Connecting……____Traceback (most recent call last):
File “C:\Users\joaqu\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3/tools/upload.py”, line 65, in
esptool.main(cmdline)
File “C:/Users/joaqu/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/tools/esptool\esptool.py”, line 2890, in main
esp.connect(args.before)
File “C:/Users/joaqu/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/tools/esptool\esptool.py”, line 483, in connect
raise FatalError(‘Failed to connect to %s: %s’ % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
_
serial port _
selected doesn´t exist or connected.

It seems that your PC can’t communicate with your NodeMCU on COM port 9
Is this the correct COM port?
Are you using a hub, or directly connected to a USB port on your PC?
What upload speed are you using?

Pete.

A post was split to a new topic: Using 2x PZEM-004T for solar monitoring

help me measure 380v voltage.
thanks.

Thank you Pete for this tutorial.
I am going to buy two of these modules to monitore my PV system.
I would like to know what happened if the system is power off.
The energy count go to zero or keep in memory the total?

It’s also possible to see the direction of the energy (import/export)? Theoretically I think yes as this device measure current and voltage at the same time.

Thank you

Hi I have nodemcu ESP8266 with pzem004t V3, I did everything same as above, I got all parameters, but one problem I faced the energy got reset i.e “0” after every 65.5KW/H. Can any one guide me how to retain energy or reset energy on every cycle?

1 Like

I got same problem !?
I think we need a Accumulate Function to store Active Energy Value and save it to EEPROM.
I’m still trying to do that :slight_smile:

For storing files or even just variables look into LittleFS. I have been using a Json lib that make it a snap to parse back and forth. EEPROM is either not supported or total phasing out.

1 Like

Oh, how can we do that in this case ?! Can you give more details ? Thanks !

Whoohoo !
I think i have a solution for you. I was try PZEM004Tv30 lib to Read Value from PZEM004Tv3 and the Energy value readout from PZEM is Accumulated from all previous reset ! :smiley:
So maybe problems came from the way use Modbus to readout these values.

Can you share the code that do Accumulated reading?

You can search PZEM004Tv30 Library.

Wow, that’s not the answer that I am looking for. I need accumulated piece of code so I can add it to my original code (first post) so other PZEM users can have a complete code.

If I just acting like your response then I would not create this original thread and list all my code here for you to used.

So, please share your code

I just switched to use PZEM004Tv30 Lib instead of your code.
Previous, I had tried wrote a Accumulate Function but It has didn’t work exactly yet. I still try to improve it but maybe there are problems when store Accumulate variable to EEPROM, i still don’t know ?!?! :thinking:

My Idea is:
:bulb: 1. Add 1 variable for Accumulate Active Energy (AAE), this variable will store in EEPROM. Every time function pzemupdate() work, it readout AAE and accumulate new value to AAE then store AAE back to EEPROM.
:bulb: 2. Add 1 data[] array to compare the Value read from PZEM004T module if the reset happen AAE > AE (AE = your PPR) then AAE = (( AE - d[i] ) + AAE), for normal AAE = AE.

///add EEPROM.h
#include <EEPROM.h>

double U_PR, I_PR, P_PR, PR_F, PR_PF, PR_alarm, AE;
double AAE = 0; //Variables for Accumutale Active Energy
double data[]={0,0,0};  //Variables for Electric Measuring
int i=0;                //index for data[];
int addr = 0; //Address in EEPROM where to store value of AAE - ACCUMULATE_ACTIVE_ENERGY


//PZEM DATA UPLOAD TO BLYNK SERVER

void pzemupdate() 
{
  result = node.readInputRegisters(0x0000, 10);
  if (result == node.ku8MBSuccess)  
  {
    U_PR      = (node.getResponseBuffer(0x00)/10.0f);
    I_PR      = (node.getResponseBuffer(0x01)/1000.000f);
    P_PR      = (node.getResponseBuffer(0x03)/10.0f);
    PR_F      = (node.getResponseBuffer(0x07)/10.0f);
    PR_PF     = (node.getResponseBuffer(0x08)/100.0f);
    PR_alarm  = (node.getResponseBuffer(0x09));  
    AE        = (node.getResponseBuffer(0x05)/1000.0f);
  }
  //Read AAE from EEPROM
    EEPROM.begin(512);         //Initialize EEPROM
    EEPROM.get(addr,AAE);    //Read AAE from EEPROM
    addr += sizeof(double);    //Move address to the next byte after AAE
    EEPROM.get(addr,data); //Read data[] from EEPROM
    addr += sizeof(data);    //Move address to the next byte after data[]
    EEPROM.get(addr,addr);   //Read addr from EEPROM
    addr = 0;
    Serial.println("Read previous AAE value !"); 
    
    if(AAE > AE)
    {
      AAE = (AAE + (AE - data[i]));
    }
      else
      {
        AAE = AE;
      } 
    i++;
    if(i == 3)
    { 
      i = 0;
    }
    data[i] = AE;

    EEPROM.put(addr,AAE);      //Write AAE in EEPROM Flash Address
    addr += sizeof(double);    //Move address to the next byte after AAE
    EEPROM.put(addr,data);   //Write data[] in EEPROM Flash Address
    addr += sizeof(data);    //Move address to the next byte after data[]
    EEPROM.put(addr,addr);     //Write data[] in EEPROM Flash Address
    addr += sizeof(addr);      //Move address to the next byte after data[]
    EEPROM.end();              //Execute Write EEPROM & release the RAM copy of EEPROM contents
    Serial.println("Store AAE to EEPROM !"); 

  Serial.print("U_PR:     "); Serial.println(U_PR);   // V
  Serial.print("I_PR:     "); Serial.println(I_PR,3);   //  A
  Serial.print("P_PR:     "); Serial.println(P_PR);   //  W 
  Serial.print("AE:      "); Serial.println(AE,3);   // kWh
  Serial.print("AAE:      "); Serial.println(AAE,3);   // kWh
  Serial.print("PR_F:     "); Serial.println(PR_F);    // Hz
  Serial.print("PR_PF:    "); Serial.println(PR_PF);  
  Serial.print("PR_alarm: "); Serial.println(PR_alarm,0);
  Serial.println("====================================================");
  
  Blynk.virtualWrite(vPIN_VOLTAGE,                  U_PR);
  Blynk.virtualWrite(vPIN_CURRENT_USAGE,            I_PR);
  Blynk.virtualWrite(vPIN_ACTIVE_POWER,             P_PR);
  Blynk.virtualWrite(vPIN_ACTIVE_ENERGY,            AE);
  Blynk.virtualWrite(vPIN_ACCUMULATE_ACTIVE_ENERGY, AAE);
  Blynk.virtualWrite(vPIN_FREQUENCY,                PR_F);
  Blynk.virtualWrite(vPIN_POWER_FACTOR,             PR_PF);
  Blynk.virtualWrite(vPIN_OVER_POWER_ALARM,         PR_alarm);
  
  delay(1000);
}

@4eyes please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Ok, Thanks ! :sweat_smile:

At what time interval is your pzemupdate() function being called?

Pete.