Hello,
I’m currently working on PZEM-017 to measure the voltage and energy usage of my solar system.
I had PZEM-017 connect to “TTL to RS485 Module” and ESP8266 D1 Mini
Here is the connection:
ESP D5 --> RS485 TxD
ESP D6 --> RS485 RxD
ESP 5V --> RS485 VCC --> PZEM017 5V
ESP G --> RS485 GND --> PZEM017 GND
RS485 A --> PZEM A
RS485 B --> PZEM B
After running the code as posted below, the serial display zezo even those there is a reading on actual device.
U_PR: 0.00
I_PR: 0.000
P_PR: 0.00
PPR: 0.000
Please help me so it can display the actual reading.
Here is the code:
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <ModbusMaster.h>
#include <ESP8266WiFi.h>
#include "settingsPZEM017.h"
#include <SoftwareSerial.h> // ( NODEMCU ESP8266 )
SoftwareSerial pzem(D5,D6); // (RX,TX) connect to TX,RX of PZEM for NodeMCU
#include <ModbusMaster.h>
ModbusMaster node;
SimpleTimer timer;
//WiFi data
char ssid[] = "xxxxxx"; //WiFi Credential
char pass[] = "xxxxx"; //WiFi Password
char server[] = "xxx.xxx.xxx.xxx"; //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 "xxxxxxxxxxxxxxxxxxx" //PZEM-017 Auth code for Blynk Local Server
int timerTask1;
double U_PR, I_PR, P_PR, PPR;
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();
}
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);
}
void loop(){
Blynk.run();
uint8_t result;
result = node.readInputRegisters(0x0000, 6);
if (result == node.ku8MBSuccess)
{
uint32_t tempdouble = 0x00000000;
U_PR = node.getResponseBuffer(0x0000) / 100.0;
I_PR = node.getResponseBuffer(0x0001) / 100.0;
tempdouble = (node.getResponseBuffer(0x0003) << 16) + node.getResponseBuffer(0x0002);
P_PR = tempdouble / 10.0;
tempdouble = (node.getResponseBuffer(0x0005) << 16) + node.getResponseBuffer(0x0004); /
PPR = tempdouble;
}
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.println("====================================================");
updateBlynk();
delay(1000);