BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Pzem dont show data

Hello… I have a code that worked on the old blink … on blink 2.0 it does not want to show data … help maybe I do not understand something … tried different options .
blynk library vesion 1.0.0
PZEM-004
Esp32

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
char auth[] = "";
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#include <PZEM004T.h>
#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"

PZEM004T pzem(&Serial);                                        /// use Serial
IPAddress ip(192,168,1,4);
float voltage_blynk=0;
float current_blynk=0;
float power_blynk=0;
float energy_blynk=0;

unsigned long lastMillis = 0;







void setup()
{
  Serial.begin(115200);
  delay(100);
  Blynk.begin(auth, "dd-wrt", "11111111");
  BlynkEdgent.begin();
  pzem.setAddress(ip);

}

void loop() {
  BlynkEdgent.run();

  float v = pzem.voltage(ip);
     if(v >= 0.0){   voltage_blynk =v; } //V

      float i = pzem.current(ip);
      if(i >= 0.0){ current_blynk=i;    }  //A

      float p = pzem.power(ip);
      if(p >= 0.0){power_blynk=p;       } //kW

      float e = pzem.energy(ip);
      if(e >= 0.0){  energy_blynk =e;  } ///kWh

  delay(1000);
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();

    Blynk.virtualWrite(V21, voltage_blynk);
    Blynk.virtualWrite(V22, current_blynk  );
    Blynk.virtualWrite(V23, power_blynk);
    Blynk.virtualWrite(V24, energy_blynk  );
    Blynk.virtualWrite(V25, lastMillis  );

  }
}

@Taras_Yurychko 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:
```

You also need to provide the requested information about library version, app version, hardware etc.

Pete.

sorry i’m new …

tried on esp8266 … does not work//
help ////

How have you defined the datastreams for V21 to V25, especially the Data Types and the Min/Max values?

Also, you really need to sort-out your void loop and replace the delay with a timer…

Pete.

thanks corrected but still does not work … to check installed the old version and it works …
Data Types in double

#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"

PZEM004T pzem(&Serial);                                        /// use Serial
IPAddress ip(192,168,1,49);
int voltage_blynk=0;
float current_blynk=0;
float power_blynk=0;
float energy_blynk=0;
unsigned long lastMillis = 0;

void setup()
{
  Serial.begin(115200);
  delay(100);
  Blynk.begin(auth, "dd-wrt", "11111111");
  BlynkEdgent.begin();
  pzem.setAddress(ip);
timer.setInterval(1000, pz);
}

void pz(){
  float v = pzem.voltage(ip);
     if(v >= 0.0){   voltage_blynk =v; } //V

      float i = pzem.current(ip);
      if(i >= 0.0){ current_blynk=i;    }  //A

      float p = pzem.power(ip);
      if(p >= 0.0){power_blynk=p;       } //kW

      float e = pzem.energy(ip);
      if(e >= 0.0){  energy_blynk =e;  } ///kWh

    

  if (millis() - lastMillis > 10000) {
    lastMillis = millis();

    Blynk.virtualWrite(V21, voltage_blynk);
    Blynk.virtualWrite(V22, current_blynk  );
    Blynk.virtualWrite(V23, power_blynk);
    Blynk.virtualWrite(V24, energy_blynk  );
    Blynk.virtualWrite(V25, lastMillis  );

  }
}
void loop() {
  BlynkEdgent.run();
timer.run();

}

Why do you have this code still?

What about your datastream min/max settings?

Pete.

hello… now running thanks …

#define BLYNK_DEVICE_NAME ""
char auth[] = "";
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#include <PZEM004T.h>
#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"
#include <HardwareSerial.h>
    //  hwserial UART2 at pins IO-16 (RX2) and IO-17 (TX2)
    HardwareSerial PzemSerial2(2);     // Use hwserial UART2 at pins IO-16 (RX2) and IO-17 (TX2)
    PZEM004T pzem(&PzemSerial2);                                    /// use Serial
IPAddress ip(192,168,1,1);
float voltage_blynk=0;
float current_blynk=0;
float power_blynk=0;
float energy_blynk=0;


void setup()
{
  Serial.begin(115200);
  delay(100);
  Blynk.begin(auth, "dd-wrt", "11111111");
  BlynkEdgent.begin();
  pzem.setAddress(ip);

}

void bacaPZEM(){
   //PZEM
   float v = pzem.voltage(ip);
      if(v >= 0.0){   voltage_blynk =v; } //V

       float i = pzem.current(ip);
       if(i >= 0.0){ current_blynk=i;    }  //A

       float p = pzem.power(ip);
       if(p >= 0.0){power_blynk=p;       } //kW

       float e = pzem.energy(ip);
       if(e >= 0.0){  energy_blynk =e;  } ///kWh

   delay(1000);

}

void sendtoblynk(){
  Blynk.virtualWrite(V21, voltage_blynk);
  Blynk.virtualWrite(V22, current_blynk  );
  Blynk.virtualWrite(V23, power_blynk);
  Blynk.virtualWrite(V24, energy_blynk  );
}

void loop() {
bacaPZEM();
sendtoblynk();

BlynkEdgent.run();


}

So you’ve abandoned the clean void loop approach and gone back to blocking delays.

At some point in future you’ll wonder why you’re getting disconnections from the Blynk server, or your device is locking-up. This will be the reason why.

Pete.

hello sorry forgot … now good ?

#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#include <PZEM004T.h>
#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"
#include <HardwareSerial.h>
#include <pzem.h>

    //  hwserial UART2 at pins IO-16 (RX2) and IO-17 (TX2)
    HardwareSerial PzemSerial2(2);     // Use hwserial UART2 at pins IO-16 (RX2) and IO-17 (TX2)
    PZEM004T pzem(&PzemSerial2);                                    /// use Serial
    IPAddress ip(192,168,1,1);
    float voltage_blynk=0;
    float current_blynk=0;
    float power_blynk=0;
    float energy_blynk=0;

    void bacaPZEM(){
    //PZEM
    float v = pzem.voltage(ip);
      if(v >= 0.0){   voltage_blynk =v; } //V
    
       float i = pzem.current(ip);
       if(i >= 0.0){ current_blynk=i;    }  //A
    
       float p = pzem.power(ip);
       if(p >= 0.0){power_blynk=p;       } //kW
    
       float e = pzem.energy(ip);
       if(e >= 0.0){  energy_blynk =e;  } ///kWh
    
    
    
    }
    
    void sendtoblynk(){
      Blynk.virtualWrite(V21, voltage_blynk);
      Blynk.virtualWrite(V22, current_blynk  );
      Blynk.virtualWrite(V23, power_blynk);
      Blynk.virtualWrite(V24, energy_blynk  );
    }

void setup()
{
  Serial.begin(115200);
  delay(100);
  Blynk.begin(auth, "dd-wrt", "11111111");
  BlynkEdgent.begin();
  pzem.setAddress(ip);
  timer.setInterval(10000L, sendtoblynk);
}


void loop() {
bacaPZEM();

timer.run();

BlynkEdgent.run();


}

Even worse!

Pete.

sorry look again i’m just learning

I’d suggest that you re-read the “keep your void loop clean” document.

Pete.

1 Like

If your intention is to take a PZEM reading every x seconds, then upload the results to Blynk then you have two options about how to structure your code…

  1. Put the code that reads the PZEM into a function called with a timer, and at the end of that function, call the function that sends the data to Blynk.

  2. Put both the PZEM reading code, and the Blynk upload code into the same function and call it with a timer.

If you want different frequencies for taking PZEM readings and uploading the results to Blynk then have two separate functions, and call them with two different timers.

Pete.

1 Like