Not getting data from pzem004tv3.0

hello everyone,
i have this code, and the compiling and everything goes as it should but i just dont get the right data on my dashboard. can anyone help me with it?


// tamplate id, device name and author token is listed here. 
#define BLYNK_AUTH_TOKEN ""
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME "power meter"

// wifi and the sensor libraries 
#include <WiFi.h>
#include <WiFiClient.h> 
#include <BlynkSimpleEsp32.h> 
#include <PZEM004Tv30.h> // 
#include <Arduino.h> 
//#include <HardwareSerial.h>
#include <esp_int_wdt.h>
#include <esp_task_wdt.h>

// Commnet this out to disable print and save spaces
//#define ARDUINO_ARCH_ESP32
#define BLYNK_PRINT Serial

//soft restart when needed 
//#if defined (ARDUINO_ARCH_ESP32)
//#define SOF_RESET() ESP.restart()
//#endif

/*#if defined(ESP32)
  *************************
 *  ESP32 initialization
 * ---------------------
 * The ESP32 HW Serial interface can be routed to any GPIO pin 
 * Here we initialize the PZEM on Serial2 with RX/TX pins 16 and 17
 */
#if !defined(PZEM_RX_PIN) && !defined(PZEM_TX_PIN)
#define PZEM_RX_PIN 16
#define PZEM_TX_PIN 17
#endif

#define PZEM_SERIAL Serial2
PZEM004Tv30 pzem(PZEM_SERIAL, PZEM_RX_PIN, PZEM_TX_PIN);
IPAddress ip(192,168,1,1);

//Wifi cridentials 
char ssid[] = ".";
char password[] =  "1";
char auth[] = BLYNK_AUTH_TOKEN;

BlynkTimer timer; 
//esp_wifi_connect(); 

// i dont understand this fully. 
//unsigned long startMillis; 
//unsigned long currentMillis; 
//unsigned long duration;
//const unsigned long period = duration;
  
 
//invoque watchdog+infinite loop
void hard_restart(){
  esp_task_wdt_init(1,true);
  esp_task_wdt_add(NULL);
  while(true);
} 



// this function is used to save all the data in blynk cloud. 
//BLYNK_CONNECTED();
//Blynk.syncAll();

void mySensor() {
 
  float voltage = pzem.voltage();
  float current = pzem.current();
  //float power = pzem.power();
 // float energy = pzem.energy();

  if (isnan(voltage) || isnan(current)){
    Serial.println("Fialed to read the sensor");
    return; 
  }
  Blynk.virtualWrite(V0, voltage);
  Blynk.virtualWrite(V1, current);
 // Blynk.virtualWrite(V2,power);
 // Blynk.virtualWrite(V3, energy );
}

void setup() {
  
  Serial.begin(115200);
  delay(200); 
  
  Blynk.begin(auth, ssid, password); 
  
  // uncomment in order to reset the internal energy counter
  //pzem.resetEnergy();

  timer.setInterval(1000L, myTimerEvent);// starting a timer 
}

void myTimerEvent() 
{
  
}
void loop(){
    Blynk.run();
    timer.run(); 
}

@projectbmtec 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.

Your function is being called by a timer. But there is nothing inside. How do you expect to get values ?

You are not using this function anywhere in the code.

yes, i got that from some code and i just didnt know what to code inside it.

would it work if i just skip the timer?

No, you need to add code into that function which will send your data to Blynk.
But, I’d start by adding code that will print the values to your serial monitor to ensure that you understand the process and the values make sense.

Pete.

i added this and it does work but i still get very deffirent value on my dashboard. is it because of miscalculation in the code?

void setup() {
  
  Serial.begin(115200);
  delay(200); 
  
  Blynk.begin(auth, ssid, password); 
  
  // uncomment in order to reset the internal energy counter
  //pzem.resetEnergy();

  timer.setInterval(1000L, myTimerEvent);// starting a timer 
}
void myTimerEvent() 
{
   Blynk.virtualWrite(V5, millis() / 1000); 
}

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

This tells the sketch to report how many milliseconds have elapsed since the device has booted, then divide that number by 1000 to give the number of seconds since the device has booted, then send the result to pin V5 in the app.

I’m guessing that this isn’t the data that you actually want, which I shy I said…

but you’ve clearly chosen to ignore that advice.

Pete.

yes, i did use the serial.println and i got some value’s on my serial monitor. i also made a widget for that pin number 5 and i get some value’s there too. so basically this function is used to show how long the microcontroler is on?

  
  Serial.begin(115200);
  delay(200); 
  
  Blynk.begin(auth, ssid, password); 
  
  // uncomment in order to reset the internal energy counter
  //pzem.resetEnergy();

  timer.setInterval(1000L, myTimerEvent);// starting a timer 
}
void myTimerEvent() 
{
 Serial.println ( millis()/1000 ); 
 return;
}

void loop(){
     Blynk.run();
     timer.run(); 
}
:18.868 -> 46
16:27:19.831 -> 47
16:27:20.853 -> 48
16:27:21.840 -> 49
16:27:22.861 -> 50
16:27:23.839 -> 51

Yes, that’s what I explained earlier.

As @Madhukesh explained earlier, you have a function called mySensor which is never being called. This is the code that takes the readings and sends them to Blynk, but you don’t have a timer set-up to execute this function.

If you’d have listened to this advice, and also added some serial print commands to that function, you would either realise that this function isn’t being executed, or have seen voltage and current readings in the serial monitor and maybe in Blynk too - if you’ve configured your datastreams and widgets correctly.

Pete.

Try this.

Messy!

Pete.

2 Likes

it works, and thaks a lot. i also wanted to ask how i can use millis() ( or any other way) to trace for how long a mchine has been running. it is for the down time of the laser machine. i have searched on the internet and i got this code. but the problem is that i dont have a fixed given time period. i wanted to code in a way that if the value of the current goes above 2 amps the timer should begin until the current is below 2 amps. and that would be my current time.

int ledState = LOW;             // used to set the LED state
unsigned long previousMillis = 0;  //will store last time LED was blinked
const long period = 1000;         // period at which to blink in ms

void setup() {
  pinMode(ledPin, OUTPUT); // set ledpin as output
}

void loop() {
 unsigned long currentMillis = millis(); // store the current time
  if (currentMillis - previousMillis >= period) { // check if 1000ms passed
   previousMillis = currentMillis;   // save the last time you blinked the LED
   if (ledState == LOW) { // if the LED is off turn it on and vice-versa
     ledState = HIGH;
   } else {
ledState = LOW;
}
   digitalWrite(ledPin, ledState);//set LED with ledState to blink again
 }
}