Connecting pzem004-t v3 with esp8266 and Blynk

same issue after updating the software serial library

1Capture4

if I put // before then the issue is gone, but iam not sure it will work this way

So the previous issue gone.
There is no Serial2 in the original code as well as your posted code.
If you add it, you have to declare it. We have no way to know if you don’t post the whole code.

Hi Khoih,
Thanks for help, the sketch uploads without any issue now, but the serial monitor shows the following data:



My setup schematic is at the top of the post.
i have modified only these 4 in the sketch:

//WiFi data
char ssid[] = "Put your WiFi SSID here";                    //WiFi Credential
char pass[] = "Put your WiFi password here";              //WiFi Password
char server[] = "Put your Blynk local server IP address here";         //Blynk local server IP address
int port = 8080;                        //Blynk local port
#define USE_LOCAL_SERVER                //Use local Blynk Server - comment-out if use Blynk hosted cloud service
#define AUTH    "put your Blynk App Authorization code here"    //PZEM-004v3 Auth code for Blynk Local Server

In blynk I have created second projecct with PZEM004T( first was for DHT22 sensor), but the app continuously shows PZEM disconnected and connects again,using the new generated key for this code.
For blink local server IP i have pinged blynk-cloud.com.
please let me know if some modification is required, I know i am close now to achieve this.

The code uploads now, can you please look into the following new issue

The stack dump that you’re seeing in your serial monitor is because the board is resetting.

I’m confused by a few things…

  1. are you using the SoftwareSerial port on D5/D6 to communicate with the PZEM, or the hardware serial port? If the latter then you can’t use this port for debugging as well.

  2. are you using your own local Blynk server, or the Blynk cloud servers?

Pete.

I am not sure, I have connected RX to the RX on the board coming from LV RX of the level shifter and TX to the TX on the board coming from LV side of the level shifter.
I believe it is Blynk cloud server.

Firstly, you alway connect Rx to Tx and vice versa.
Assuming that this is the code that you’re using:

then D5 is your software UART Rx pin, which connects to the PZEM Tx pin, and D6 is your software UART Tx pin which connects to the PZEM Rx pin (as is says in the comments).

You would certainly know if you’d built your own local server, and as you haven’t then you will be using the Blynk cloud servers. As a result, this line:

needs to be commented-out.

Pete.

Just a quick question, while using DHT22 I had to feed the blynk auth key only but here it needs to be connected to the cloud server, why it is required?
I commented out as you instructed but the issue is still there
sorry but google isnt of help or may be this thing is badly on my nerves.

With the DHT code you would have had to provide your Wi-FI SSID, Wi-Fi password and Blynk Auth code.
It would then have a line that says:

  Blynk.begin(AUTH, ssid, pass);

which connects the device to the Blynk cloud server.

This code is exactly the same, when not using the local server option, but has options that make it simpler to use a local server.

Did you make the wiring changes too?

Pete.

If you use the blocking Blynk.begin() in your setup() function and the board can’t connect to WiFi / Blynk server, the loop() function will never be executed. Therefore, anything you put there forever won’t run.
There have many discussions about this, you’d better do some more research on this blocking Blynk.begin() and non blocking Blynk.config() + Blynk.connect().

The following code, modified from original code, will show you how to use those functions, and will possibly fix your problems. It also cleans up the loop() and automatically connects / reconnects WiFi and Blynk whenever necessary, in setup() as well as in runtime loop().

#define USE_BLYNK_WM      0       // https://github.com/khoih-prog/Blynk_WM

#include <ArduinoOTA.h>

#if USE_BLYNK_WM
#include <BlynkSimpleEsp8266_WM.h>
#else
#include <BlynkSimpleEsp8266.h>
#endif

//#include <SimpleTimer.h>
#include <ModbusMaster.h>
#include <ESP8266WiFi.h>
#include "settingsPZEM.h"

#include <SoftwareSerial.h>  //  ( NODEMCU ESP8266 )
SoftwareSerial pzem(D5,D6);  // (RX,TX) connect to TX,RX of PZEM for NodeMCU
//SoftwareSerial pzem(D7,D8);  // (RX,TX) connect to TX,RX of PZEM
#include <ModbusMaster.h>
ModbusMaster node;
SimpleTimer timer;

#if !USE_BLYNK_WM
//WiFi data
char ssid[] = "****";          //WiFi Credential
char pass[] = "****";          //WiFi Password
char server[] = "****";        //Blynk local server IP address
int port = 8080;               //Blynk local port

#define AUTH    "****"    //PZEM-004v3 Auth code for Blynk Local Server
#endif

int timerTask1;
double U_PR, I_PR,  P_PR,  PPR, PR_F, PR_PF, PR_alarm;
uint8_t result;  uint16_t data[6];

#define UPDATE_INTERVAL_MS      5000

// Use 0 or 1 when you don't need to debug with many messages
#define DEBUG_LOOP              2

#define MODBUS_ID               1

void updateBlynk() 
{
  getPZEM();
 
  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,         PPR);
  Blynk.virtualWrite(vPIN_FREQUENCY,             PR_F);
  Blynk.virtualWrite(vPIN_POWER_FACTOR,          PR_PF);
  Blynk.virtualWrite(vPIN_OVER_POWER_ALARM,      PR_alarm);
}

uint8_t getPZEM()
{
  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);
    PPR       = (node.getResponseBuffer(0x05)/1000.0f);
    PR_F      = (node.getResponseBuffer(0x07)/10.0f);
    PR_PF     = (node.getResponseBuffer(0x08)/100.0f);
    PR_alarm  = (node.getResponseBuffer(0x09));

    #if (DEBUG_LOOP > 0)
    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("PPR:      "); Serial.println(PPR,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("====================================================");
    #endif   
  }
  #if (DEBUG_LOOP > 0)
  else
    Serial.println("PZEM: can't get data");
  #endif
  
  return result;
}

// Use this to avoid being blocked here if no WiFi
void connectWiFi(const char* ssid, const char* pass)
{
    #if (DEBUG_LOOP > 0)
    Serial.println("Connecting to " + String(ssid));
    #endif
    
    WiFi.mode(WIFI_STA);
    if (pass && strlen(pass)) 
    {
        WiFi.begin(ssid, pass);
    } else 
    {
        WiFi.begin(ssid);
    }
    int i = 0;
    while ((i++ < 30) && (WiFi.status() != WL_CONNECTED)) 
    {
        BlynkDelay(500);
    }

    #if (DEBUG_LOOP > 0)
    if (WiFi.status() == WL_CONNECTED)
      Serial.println("Connected to " + String(ssid));
    else
      Serial.println("Can't connect to " + String(ssid));
    #endif
}

void connectWiFiAndBlynk()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    connectWiFi(ssid, pass);  
  }
  else
  {
    #if (DEBUG_LOOP > 0)
    Serial.println("Trying connecting to BlynkServer " + String(server));
    #endif
    Blynk.connect();

    if (Blynk.connected())
    {
      #if (DEBUG_LOOP > 0)
      Serial.println("Connected to BlynkServer " + String(server));
      #endif
    }     
  }
}

void setup()
{
  Serial.begin(115200); 
  Serial.println("\nStart serial"); 
  pzem.begin(9600); 
  Serial.println("Start PZEM serial");
  node.begin(MODBUS_ID, pzem);  
  Serial.println("Start PZEM"); // 1 = ID MODBUS

  #if USE_BLYNK_WM
    Blynk.begin();
  #else
    // Use this to avoid being blocked here if no WiFi
    //connectWiFi(ssid, pass);
    Blynk.config(AUTH, server, port);

    connectWiFiAndBlynk();
    //Blynk.connect();
  #endif  
    
  ArduinoOTA.setHostname(OTA_HOSTNAME);
  ArduinoOTA.begin();
  timerTask1 = timer.setInterval(UPDATE_INTERVAL_MS, updateBlynk);
}

void loop()
{
  if (Blynk.connected())
  {
    Blynk.run();
  }
  else
  {
    connectWiFiAndBlynk();
  }
  
  ArduinoOTA.handle();
  timer.run();  
}

As a last resort I unplugged the level shifter and connected the Pzem with board directly with 5V to VV of ESP and ground to G ,RX & TX with D5 and D6 of ESP, applied your code and guess what, my dream come true finally.


A sign of relief for me at the moment but iam afraid this way the board wouldn’t get fried.

Thanks for the help otherwise the stuffs might ended up in garbage bin.
I can sleep well tonight.

1 Like

Glad it’s finally working.

Pete.

A post was split to a new topic: Using multiple PZEM sensors

hello
please i did all the steps you mentioned above but i am receiving an error message which says:
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

//here is my code

#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <ModbusMaster.h>
#include <ESP8266WiFi.h>
#include "settingsPZEM.h"
#include <SoftwareSerial.h> // ( NODEMCU ESP8266 )
SoftwareSerial pzem(D5,D6); // (RX,TX) connect to TX,RX of PZEM for NodeMCU
//SoftwareSerial pzem(D7,D8); // (RX,TX) connect to TX,RX of PZEM
#include <ModbusMaster.h>
ModbusMaster node;
SimpleTimer timer;
//WiFi data
char ssid[] = "Put your WiFi SSID here"; //WiFi Credential
char pass[] = "Put your WiFi password here"; //WiFi Password
char server[] = "Put your Blynk local server IP address here"; //Blynk local server IP address
int port = 8080; //Blynk local port
#define USE_LOCAL_SERVER //Use local Blynk Server – comment-out if use Blynk hosted cloud service
#define AUTH "put your Blynk App Authorization code here" //PZEM-004v3 Auth code for Blynk Local Server
int timerTask1;
double U_PR, I_PR, P_PR, PPR, PR_F, PR_PF, PR_alarm;
uint8_t result; uint16_t data[6];
void setup(){
Serial.begin(115200); Serial.println("Start serial"); pzem.begin(9600); Serial.println("Start PZEM serial");
node.begin(1, pzem); Serial.println("Start PZEM"); // 1 = ID MODBUS
WiFi.mode(WIFI_STA);
#if defined(USE_LOCAL_SERVER)
WiFi.begin(ssid, pass);
Blynk.config(AUTH, server, port);
#else
Blynk.begin(AUTH, ssid, pass);
#endif
while (Blynk.connect() == false) {}
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.begin();
// timerTask1 = timer.setInterval(1000, updateBlynk);
}
void updateBlynk() {
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, PPR);
Blynk.virtualWrite(vPIN_FREQUENCY, PR_F);
Blynk.virtualWrite(vPIN_POWER_FACTOR, PR_PF);
Blynk.virtualWrite(vPIN_OVER_POWER_ALARM, PR_alarm);
}
void loop(){
Blynk.run();
//ArduinoOTA.handle();
//timer.run();
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);
PPR = (node.getResponseBuffer(0x05)/1000.0f);
PR_F = (node.getResponseBuffer(0x07)/10.0f);
PR_PF = (node.getResponseBuffer(0x08)/100.0f);
PR_alarm = (node.getResponseBuffer(0x09));
}
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("PPR: "); Serial.println(PPR,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("====================================================");
updateBlynk();
delay(1000);
}

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

hello
please i need guidance on this code
here is part of the error code

In file included from C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkApi.h:37,
                 from C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\Blynk\src/BlynkApiArduino.h:14,
                 from C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\Blynk\src/BlynkSimpleEsp8266.h:24,
                 from C:\Users\GEMANDZE DONALD\Documents\Arduino\sketch_jun11a\sketch_jun11a.ino:2:
C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkTimer.h:36:21: error: redefinition of 'class BlynkTimer'
   36 | #define SimpleTimer BlynkTimer
      |                     ^~~~~~~~~~
C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\SimpleTimer/SimpleTimer.h:10:7: note: in expansion of macro 'SimpleTimer'
   10 | class SimpleTimer {
      |       ^~~~~~~~~~~
C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkTimer.h:36:21: note: previous definition of 'class BlynkTimer'
   36 | #define SimpleTimer BlynkTimer
      |                     ^~~~~~~~~~
C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkTimer.h:41:7: note: in expansion of macro 'SimpleTimer'
   41 | class SimpleTimer {
      |       ^~~~~~~~~~~
Multiple libraries were found for "SoftwareSerial.h"
 Used: C:\Users\GEMANDZE DONALD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.0\libraries\SoftwareSerial
 Not used: C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\EspSoftwareSerial
Multiple libraries were found for "ArduinoOTA.h"
 Used: C:\Users\GEMANDZE DONALD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.0\libraries\ArduinoOTA
 Not used: C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\ArduinoOTA
Using library ArduinoOTA at version 1.0 in folder: C:\Users\GEMANDZE DONALD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.0\libraries\ArduinoOTA 
Using library ESP8266WiFi at version 1.0 in folder: C:\Users\GEMANDZE DONALD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.0\libraries\ESP8266WiFi 
Using library Blynk at version 1.0.0 in folder: C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\Blynk 
Using library SimpleTimer at version 1.0.0 in folder: C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\SimpleTimer 
Using library ModbusMaster at version 2.0.1 in folder: C:\Users\GEMANDZE DONALD\Documents\Arduino\libraries\ModbusMaster 
Using library SoftwareSerial at version 6.12.3 in folder: C:\Users\GEMANDZE DONALD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.0\libraries\SoftwareSerial 
Using library ESP8266mDNS at version 1.2 in folder: C:\Users\GEMANDZE DONALD\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.0\libraries\ESP8266mDNS 
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

You either need to install the SimpleTimer library, or switch to using BlynkTimer instead.

Pete.


I already have the Simpletimer library installed but same error still

Pete.