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

Connecting ESP8266-01 and PZEM 004t to Arduino Uno Blynk

Good afternoon Everyone.
i have recently become a “blynker”. if someone wouldn’t mind helping me out a bit.
i am near the end of producing a working project to control 3 water pumps via reed switches to turn them on and off as i please. An issue that i’m having is the power meter i’m using (PZEM004T) does not relay information to my arduino board. I have been able to get it working over Software Serial to Blynk from the Arduino Uno via USB/ internet connection through the PC. My issue arises when i connected the arduino to Blynk via ESP8266-01 .My Esp8266 now connects the board Blynk through WiFi, but i have now spent a couple hours trying to get the PZEM004T to relay some information to blynk, or even the UNO itself.

#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
#include <PZEM004Tv30.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "AJGVFSF8e6NIl4A2YZq_ngHi6xE1SO";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";



PZEM004Tv30 pzem(9,10); //pzem softserial pins


// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>

SoftwareSerial EspSerial(3, 2 ); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600


ESP8266 wifi(&EspSerial);


 
float voltage_blynk=0;
float current_blynk=0;
float power_blynk=0;
float energy_blynk=0;


int Pump1 = 4;
int Pump2 = 5;
int Pump3 = 6;
int CTState;
int SwitchCT = 7;
unsigned long lastMillis = 0;

BlynkTimer timer;
void READ_CT()
{ float v = pzem.voltage();          
 if(v >= 0.0){   voltage_blynk =v; } //V

  float i = pzem.current();
  if(i >= 0.0){ current_blynk=i;    }  //A                                                                                                                      
  
  float p = pzem.power();
  if(p >= 0.0){power_blynk=p;       } //kW
  
  float e = pzem.energy();          
  if(e >= 0.0){  energy_blynk =e;  } ///kWh

delay(1000);

    //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
        if (millis() - lastMillis > 1000) {
          lastMillis = millis();
          
          Blynk.virtualWrite(V1, voltage_blynk);
          Blynk.virtualWrite(V2, current_blynk  );            
          Blynk.virtualWrite(V3, power_blynk);
          Blynk.virtualWrite(V4, energy_blynk  );
          Blynk.virtualWrite(V5, lastMillis  );      

        }        

}
void setup()
{
// Debug console
 Serial.begin(9600);

digitalWrite (Pump1, HIGH);
digitalWrite (Pump2, HIGH);
digitalWrite (Pump3, HIGH);
digitalWrite (SwitchCT, HIGH);
pinMode (Pump1, OUTPUT);                                                              
pinMode (Pump2, OUTPUT);
pinMode (Pump3, OUTPUT);
pinMode (SwitchCT, OUTPUT);
// Set ESP8266 baud rate
 EspSerial.begin(9600);
delay(10);
 //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:

//Blynk.begin(auth, wifi, ssid, pass, Address(192,168,1,100), 8080);




}

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

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

I’d also suggest that you review your description of your hardware and software setups, and the issues you are encountering, as I am unable to make any sense of your description.

Pete.

@PeteKnight Hi. I have made some edits to the post and have tried to explain it better. the code is now in the right format (I HOPE).
thanks for your reply.

Well, your code is a bit of a disaster area…

You are including the same library twice.

You have a Blynk timer initialised and being fed in the void loop…

but you don’t have any timers declared in your void setup,

Your READ_CT function is never being called by your code, and it contains delays and millis() comparisons that are totally unnecessary if you were using timers correctly.

I’d suggest that you set-up a timer to call READ_CT and strip out the delays etc.
There may be other things that need to be done, but I’m not familiar with the PZEM004Tv30 library, so I can’t tell if there are other things that are needed to make that work.

Pete.

@PeteKnight. Good morning. Thanks for the heads up. I am in the midst of ironing out the issues now. I will get back to you.

Aright.
so i have tried and tried again. but im finally able to get a Blynk timer to function and read voltage from the PZEM004T but now as soon as the timer is executed and has read the data. i get a CMD ERROR in the serial monitor and the device disconnects from the server. the following is the code i am using. I even changed the ESP baud rate to 19200 as i thought the devices were interuppting each other.

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG
#define BLYNK_EXPERIMENTAL
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include <SoftwareSerial.h>
#include <PZEM004Tv30.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "0B00RpanRfz3sfbsfbfsbfbvfeX8rW9bnTlgIL1i22ETag";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

 unsigned long lastMillis = 0;

PZEM004Tv30 pzem(11, 12); //pzem softserial pins



// or Software Serial on Uno, Nano...


SoftwareSerial EspSerial(3, 2 ); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 19200

  
ESP8266 wifi(&EspSerial);


  
float voltage_blynk=0;
float current_blynk=0;
float power_blynk=0;
float energy_blynk=0;


int Pump1 = 4;
int Pump2 = 5;
int Pump3 = 6;
int CTState;
int SwitchCT = 7;

void Blynk_Delay(int milli){
   int end_time = millis() + milli;
   while(millis() < end_time){
       Blynk.run();
       yield();
   }}
   
BlynkTimer timer;

void READ_CT()

{
 
    float v = pzem.voltage();
             
   if(v >= 0.0){   voltage_blynk =v; }
        if (millis() - lastMillis > 10000) {
            lastMillis = millis();     
        Blynk.virtualWrite(V1, v);
         Blynk.virtualWrite(V5, lastMillis  );
    }

   

          }        


void setup()
{
  // Debug console
   Serial.begin(9600);
           
                   
           
            


  digitalWrite (Pump1, HIGH);
digitalWrite (Pump2, HIGH);
digitalWrite (Pump3, HIGH);
 digitalWrite (SwitchCT, HIGH);
pinMode (Pump1, OUTPUT);                                                              
pinMode (Pump2, OUTPUT);
pinMode (Pump3, OUTPUT);
pinMode (SwitchCT, OUTPUT);
// Set ESP8266 baud rate
   EspSerial.begin(19200);
  delay(10);

   Blynk.begin(auth, wifi, ssid, pass, "139.59.206.133", 8080);
 // Blynk.begin(auth, wifi, ssid, pass);
  
    timer.setInterval(1000L, READ_CT);  

  
   }



 


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

   
}

AND this is the error i am recieving right after the data is read from the sensor.

17:33:37.253 -> [598] Connecting to Sherriff
17:33:50.301 -> [13645] AT version:1.7.4.0(May 11 2020 19:13:04)
17:33:50.348 -> SDK version:3.0.4(9532ceb)
17:33:50.394 -> compile time:May 27 2020 10:11
17:33:50.441 ->  roWo21.O
17:33:50.441 -> 
17:33:51.379 -> [14720] Failed to enable MUX
17:33:57.488 -> [20833] +CIFSR:STAIP,"192.168.1.192"
17:33:57.534 -> +CIFSR:STAMAC,"bc:dd:c2:17:6f:bd"
17:33:57.581 -> [20842] Connected to WiFi
17:34:08.154 -> [31467] <[1D|00|01|00] 0B00RpanRfz3X8rW9sfgjfbnTlgIL1i22ETag
17:34:08.800 -> [32118] >[00|00|01|00|C8]
17:34:08.834 -> [32118] Ready (ping: 35ms).
17:34:08.868 -> [32119] Free RAM: 731
17:34:08.868 -> [32143] >[14|00|01|00]&
17:34:08.902 -> [32159] >pm[00]7[00]out[00]7[00]out[00]7[00]out[00]4[00]out[00]5[00]out[00]6[00]out
17:34:09.004 -> [32257] <[11|00|02|00]fver[00]0.6.1[00]h-beat[00]10[00]buff-in[00]256[00]dev
17:34:09.668 -> [32996] <[00]Arduino Uno[00]cpu[00]ATmega328P[00]con[00]ESP8266[00]
17:34:10.310 -> [33618] <build[00]Aug  3 2020 17:33:25[00]
17:34:11.023 -> [34344] >[00|00|02|00|C8]
17:34:16.114 -> [39424] <[14|00|03|00|0C]vw[00]1[00]245.200
17:34:21.159 -> [44443] Cmd error

Help Much appreciated.
Adam

That’s quite a low number, and may be causing your problems, it’s difficult to say.

I’d start by streamlining your code a bit, taking out the memory hungry bits that aren’t needed and the unused variables (which all use memory).

and this unused function:

should be removed.

These four variables:

aren’t used (voltage_blynk is referenced once, but it serves no purpose).

You define your ESP8266 baud rate as a variable:

but then instead of saying EspSerial.begin(ESP8266_BAUD); you hard-code the baud rate:

You’re calling your READ_CT function once every seconds.
It takes a reading from your PZEM, then does nothing with this reading unless 10 seconds has elapsed since the reading was last uploaded to Blynk.
This means that you’re throwing away 9 out of 10 readings.
The millis comparison in this function is a total waste of time. If you want to take a reading every 10 seconds and upload it to Blynk then set your Blynk timer to 10 seconds rather than 1 second.

Your READ_CT function could look like this:

void READ_CT()
{
  float v = pzem.voltage();
  Blynk.virtualWrite(V1, v);
  Blynk.virtualWrite(V5, millis());
}

and this may also work (you need to compile it to see if it does)…

void READ_CT()
{
  Blynk.virtualWrite(V1, pzem.voltage());
  Blynk.virtualWrite(V5, millis());
}

You may also want to comment-out the #define BLYNK_DEBUG as this has quite a big impact on execution speed and possibly even #define BLYNK_PRINT Serial if you’re still struggling with memory problems.

It’s also worth noting that you are writing values to your Pump1, Pump2, Pump3 and SwitchCT GPIO pins before you’ve initialised them as outputs. The pinMode commands should come before any digitalWrite commands.

Is there any reason why you’ve chosen the Uno (with very limited memory, a slow processor and no WiFi) rather than something like a NodeMCU?

Pete.

@PeteKnight these seem like some very good points. as i have tried a lot of different ways to get this thing working i seem to have been forgetting to remove or adjust certain code as i try different things from the community, (MY BAD) but i am learning as we do. thankyou.
the reason i am using Uno is because i just had no idea what was actually out there when i ordered my parts. And i cant just quickly order some new ones as i live in Zambia which took them 4 months to deliver my last ones from aliexpress. i did get very fraustrated with my current setup just trying to get the ESP8266 to flash and connect to the UNO without a dedicated esp flasher. I then ordered some uno WiFi boards last week and wish i had seen your board recommendations earlier. would have been so much simpler.
but my current setup is not far from finished.

Many Thanks
Adam

Generally speaking, when the ESP-01s are delivered the firmware that they ship with is fine and doesn’t need to be updated to get them working correctly.
On the rare occasions when I’ve use an Arduino/ESP-01 combo for testing purposes, the firmware version of the ESP-01 I use says this:

AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20

Pete.

@PeteKnight. Yep… this i know. after reading on the internet basically all the first installation manuals show me that the have to flash the ESP before use, so what do i do? i flash, and flash, and flash and not a single piece of software would work for me. until it did, it costed me a couple weeks of testing and hair pulling.
i have just tested my sketch again. but to no avail, the same error occurs.


#define BLYNK_PRINT Serial
#define BLYNK_DEBUG

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include <SoftwareSerial.h>
#include <PZEM004Tv30.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

 unsigned long lastMillis = 0;

PZEM004Tv30 pzem(11, 12); //pzem softserial pins



// or Software Serial on Uno, Nano...


SoftwareSerial EspSerial(3, 2 ); // RX, TX

// Your ESP8266 baud rate:

#define ESP8266_BAUD 19200
  
ESP8266 wifi(&EspSerial);


  



int Pump1 = 4;
int Pump2 = 5;
int Pump3 = 6;
int CTState;
int SwitchCT = 7;


   
BlynkTimer timer;

void READ_CT()

{
 

             
   
  Blynk.virtualWrite(V1, pzem.voltage());
  Blynk.virtualWrite(V5, millis());
    }

   

          


void setup()
{
  // Debug console
   Serial.begin(9600);
           
                   
           
            

pinMode (Pump1, OUTPUT);                                                              
pinMode (Pump2, OUTPUT);
pinMode (Pump3, OUTPUT);
pinMode (SwitchCT, OUTPUT);
  digitalWrite (Pump1, HIGH);
digitalWrite (Pump2, HIGH);
digitalWrite (Pump3, HIGH);
 digitalWrite (SwitchCT, HIGH);

// Set ESP8266 baud rate
    EspSerial.begin(ESP8266_BAUD);
  delay(10);

   Blynk.begin(auth, wifi, ssid, pass, "139.59.206.133", 8080);
 // Blynk.begin(auth, wifi, ssid, pass);
  
    timer.setInterval(1000L, READ_CT);  

  
   }



 


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

   
}

I DO HOPE I HAVE changed what you told me about…
the much simpler READ_CT function compiles fine. and is reading from the PZEM. but straight after it reads the data it seems to get stuck
Adam

Pete.

@PeteKnight thanks for your help. i will keep experimenting.
Adam.