In this tutorial, we will learn how to make a simple, single-phase, IoT energy meter, where you can see the value in real-time in your android phone. If you want to know how to work with the measurement circuit, and how to acquire the electrical values, check the “ESP8266 NodeMCU – Simple energy meter using PZEM004T”
Blynk Android App
Blynk is a hardware-agnostic IoT platform with white-label mobile apps, private clouds, device management, data analytics, and machine learning. You should check their Website to know more about Blynk.
So to install and configure the app, you must follow the tutorial on their website. However, if you follow the blynk tutorial, you will end up using the blynk.io server. A blynk.io server is a good option because, is safe, continuous and fast. Although blynk.io server is currently providing 2.000 of energy, which limits your Dashboard. To solve this problem, you can use public free blynk servers, from this GitHub. Remember that these servers are unreliable and can have an intermittent shutdown. I will use the second server, but you can use any of them.
2º Server : blynk.iot-cm.com
App Port: 9443
Device Port: 8080
Status : Alive (seen 10 Nov 2019)
Energy : 1,000,000
Blynk Email Sending : unknown
Server location : Thailand (TH)
Admin : Krisana Meesuk Department of Electronic Science, Chiang Mai
To change the blynk.io server for the blynk.iot-cm.com, in the login in the page of the app, when choosing the server, you need to click on the “3 dots” button and copy the server name and App port, as shown in figure 2.
After making the instructions above, you will receive an email with a token. We will use that token in the code for access to the server. To begin, create a new project and name it as you desire. In this tutorial, it will be “Meter”.
Below the project name, you need to especify the hardware. Choose the ESP8266 option.
Since, this is an energy meter the dashboard will show the main variables of an electrical installation, like in the list below. It will also have a button if you want to switch the meter on and off. You can see the general layout in figure 5.
- Voltage (V);
- Corrent (A);
- Power (W);
- Power factor (Cos_fi);
- Reactive Power (VAr);
- Mouth Energy (kWh).
- Temporal graph, to see the past values.
To configure the widgets, press them and a menu will appear. The button options and the pin type can be seen in figure 6. The button is a way to turn the system on and off. Use the virtual pin V6
The values of the electrical parameters are shown in “Value Display Settings” and the configurations can be seen in figure 7. Since these values are obtained from the PZEM, they are not digital nor analogue. Thus we must connect them to the virtual pin V0 of blynk with a reading rate “Push”.
Last but not least, we can use the graph to display the electric variables in time. Give a title to the graph and introduce a “Datastream” per value that you want to display. Those are the values from the visual pin chosen according to the variables from PZEM004T. For example, in figure 8, you can see the configuration of the voltage “Datastream”. Choose the virtual pin V0 and specify the suffix and the decimals. For the current “Datastream” choose the virtual pin V1 and make the same. Continue to include “datastreams” as many variables you have in the code.
Coding
We will use a library from Git-Hub, that communicate with PZEM004T and manipulate it to send the values to our blynk app. We also need a library for a wifi connection with blynk.io. If you do not know how to instal a library or use the library manager in Arduino IDE, you can check Installing Additional Arduino Libraries. The Blynk library and EPS8266 library must also be included by you. Copy the main sketch below to your Arduino IDE project and save it. Have fun!!
Note: NodeMCU only has one Serial Port. Because of that, we need to use software serial in order to prevent collision while downloading the code.
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <PZEM004Tv30.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
float Volt = 0;
float Amp = 0;
float Watt = 0;
float W_h = 0;
float Hz = 0;
float pf = 0;
int On_Off = 0;
char flag = 0;
char estate_on = 0;
char estate_off = 0;
/* Use software serial for the PZEM
* Pin 11 Rx (Connects to the Tx pin on the PZEM)
* Pin 12 Tx (Connects to the Rx pin on the PZEM)
*/
PZEM004Tv30 pzem(19, 18);
BLYNK_WRITE (V6){ //On and Off Button
On_Off = param.asInt();
if(On_Off){
if(!estate_on){
Serial.println("Sistem ON");
Blynk.notify("Sistem ON");
estate_on = 1;
estate_off = 0;
flag = 1;
}
}else{
if(!estate_off){
Serial.println("Sistem OFF");
Blynk.notify("Sistem OFF");
estate_off = 1;
estate_on = 0;
flag = 0;
}
}
}
// This function sends the PZEM004 values to Virtual Pins.
// In the app, Widget's reading Hz should be set to PUSH.
void Send_Values_To_Blynk(){
// You can send any value at any time.
// Please don't send more that 10 values per second.
// Now, we are sending each time we call this function
Blynk.virtualWrite(V0, Volt);
Blynk.virtualWrite(V1, Amp);
Blynk.virtualWrite(V2, Watt);
Blynk.virtualWrite(V3, W_h);
Blynk.virtualWrite(V4, Hz);
Blynk.virtualWrite(V5, pf);
}
// This function get the values from PZEM004 via serial.
void Get_Values_From_PZEM(){
Volt = pzem.Volt();
if( !isnan(Volt) ){
Serial.print("Voltage: "); Serial.print(Volt); Serial.println("V");
} else {
Serial.println("Error reading Voltage");
}
Amp = pzem.Amp();
if( !isnan(Amp) ){
Serial.print("Current: "); Serial.print(Amp); Serial.println("A");
} else {
Serial.println("Error reading current");
}
Watt = pzem.Watt();
if( !isnan(Watt) ){
Serial.print("Instant Power: "); Serial.print(Watt); Serial.println("W");
} else {
Serial.println("Error reading Instant Power");
}
W_h = pzem.W_h();
if( !isnan(W_h) ){
Serial.print("Energy: "); Serial.print(W_h,3); Serial.println("kWh");
} else {
Serial.println("Error reading Energy");
}
Hz = pzem.Hz();
if( !isnan(Hz) ){
Serial.print("Frequency: "); Serial.print(Hz, 1); Serial.println("Hz");
} else {
Serial.println("Error reading frequency");
}
pf = pzem.pf();
if( !isnan(pf) ){
Serial.print("PF: "); Serial.println(pf);
} else {
Serial.println("Error reading PF factor");
}
Serial.println();
delay(1000);
}
void setup() {
// Debug console
Serial.begin(9600);
//Blynk.begin(auth, ssid, pass);
Blynk.begin(auth, ssid, pass, "blynk-iot-cm.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(128,199,173,118), 8080);
}
void loop() {
Blynk.run();
if(flag){
Get_Values_From_PZEM();
Send_Values_To_Blynk();
}
}
This is a tօpic that is close to my heart… Many thanks!
Where are your contact details though?