Blynk and PZEM-004T V3

Hello, I am having this issue with my project, whenever i run my Peacefair PZEM-004T V3 Energy Monitor along side my blynk, it doesnt read values , but if i end up commenting out or removing the " Blynk.begin(auth, ssid, pass);" line it works fine. any help or assistance would be appreciated.

This is my code below.

#include <SoftwareSerial.h>
#include <PZEM004Tv30.h>

unsigned long previousMillis = 0;    // stores the last time the method was called
const long interval = 5000;          // the interval (in milliseconds) at which to call the method

float sample = 10;

//#define BLYNK_TEMPLATE_ID   "TMPL2AYCIphoL"

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

PZEM004Tv30 pzem(D2, D1);
bool notifiedUser = false;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth_key";

char ssid[] = "WiFI";
char pass[] = "password";

int ledPin = D4;

BLYNK_WRITE(V1) // This is the virtual pin number of the button widget
{
  int buttonState = param.asInt();
  if (buttonState == 1) {
    digitalWrite(ledPin, HIGH); // turn the LED on
  } else {
    digitalWrite(ledPin, LOW); // turn the LED off
  }
}


void setup()
{
  // Debug console
 Serial.begin(115200);
 pinMode(ledPin, OUTPUT);
 Blynk.begin(auth, ssid, pass);
 Blynk.virtualWrite(V2, sample);
}


void loop()
{
    unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    pzSensor();
  }
  Blynk.run();
}


void pzSensor(){
   float voltage = pzem.voltage();
   if(voltage != NAN){
    if(String(voltage) == "nan"){
        digitalWrite(ledPin, HIGH);
    }
    else{
        digitalWrite(ledPin, LOW);
    }
       Serial.print("Voltage: ");
       Serial.print(voltage);
       Serial.println("V");
   } else {
       Serial.println("Error reading voltage");
   }
   

   float current = pzem.current();
   if(current != NAN){
       Serial.print("Current: ");
       Serial.print(current);
       Serial.println("A");
   } else {
       Serial.println("Error reading current");
   }

   Serial.println();
}

> Blockquote

This is a Blynk Legacy sketch. Are you running a Blynk Legacy local server on your network, or are you using Blynk IoT?

If you are using Blynk IoT then have you deliberately excluded these three lines from your sketch for security reasons…

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "REDACTED"
#define BLYNK_TEMPLATE_NAME         "REDACTED"
#define BLYNK_AUTH_TOKEN            "REDACTED"

What version of the Blynk library do you have installed?

Pete.

I am using Blynk IoT, and i have Blynk 1.2.0 installed, i think i must have omitted this.

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "REDACTED"
#define BLYNK_TEMPLATE_NAME         "REDACTED"

I’d suggest that you post your full sketch with sensitive information redacted, along with the output from your serial monitor (copy/paste the text and use triple backticks as if you were posting code).

Pete.

Here is the Code

#include <PZEM004Tv30.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

SimpleTimer timer;

int ledPin = D4;

// SoftwareSerial pins
#define RX_PIN D6
#define TX_PIN D7

PZEM004Tv30 pzem(D2, D1);

#define BLYNK_TEMPLATE_ID "**********"
#define BLYNK_TEMPLATE_NAME "***********"

SoftwareSerial EspSerial(RX_PIN, TX_PIN); // Create a SoftwareSerial object for serial communication


// Go to the Project Settings (nut icon).
char auth[] = "***********************************";

char ssid[] = "*************";
char pass[] = "*********";

void setup() {
  Serial.begin(9600); // Initialize serial communication with the PC


  Blynk.begin(auth, ssid, pass); // Connect to Blynk using the WiFi object

  // Set a timed task to send a value to the Blynk app every 5 seconds
  timer.setInterval(5000, sendValue);
    pinMode(ledPin, HIGH);
}

void loop() {
  Blynk.run(); // Run the Blynk library
  timer.run(); // Run the BlynkTimer object
}

void sendValue() {
  int value = random(0, 100); // Generate a random value between 0 and 100
  pzSensor();
  //Serial.print("Sending value: ");
  float voltage = pzem.voltage();
  //Serial.println(voltage);
  Blynk.virtualWrite(V2, value); // Send the value to the Blynk app
}


void pzSensor(){
   float voltage = pzem.voltage();
   if(voltage != NAN){
    if(String(voltage) == "nan"){
        digitalWrite(ledPin, HIGH);
    }
    else{
        digitalWrite(ledPin, LOW);
    }
       Serial.print("Voltage: ");
       Serial.print(voltage);
       Serial.println("V");
   } else {
       Serial.println("Error reading voltage");
   }

   float current = pzem.current();
   if(current != NAN){
       Serial.print("Current: ");
       Serial.print(current);
       Serial.println("A");
   } else {
       Serial.println("Error reading current");
   }
}
#define BLYNK_TEMPLATE_ID "**********"
#define BLYNK_TEMPLATE_NAME "***********"

It must be at the very top of your sketch.

It doesn’t need to be at the top with library version 1.2.0 because it now defaults to connecting to the IoT servers rather than the decommissioned Legacy servers.

It’s still a good idea to have have this, along with the auth token, at the top of the sketch, because it makes pasting the copied values easier.

Pete.

1 Like

and what about the serial monitor output?

Where are you getting the auth token from?
Blynk provides you with three lines of firmware configuration code, which includes the auth token, assigned to a variable called BLYNK_AUTH_TOKEN, in the Device Info tab of the web console.
The idea is that you copy/paste these to the top of your sketch, then change this line of code…

to this…

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Connect to Blynk using the WiFi object

which makes this redundant…

Pete.