Problem with pzem-017 dan Nodemcu

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
Here I can not display the voltage results that I read. But when using the pzem-03 master the voltage is read. help me to get the value i want.

this is my program.


#define BLYNK_TEMPLATE_ID "xxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxx"
#define BLYNK_FIRMWARE_VERSION        "1.1.0"

#define BLYNK_PRINT Serial

        #include <SoftwareSerial.h>                           
        SoftwareSerial PZEMSerial;                            

        #include <ESP8266WiFi.h>                              
        #include <BlynkSimpleEsp8266.h>                       
        char auth[] = "xxxxxxx";     
        char ssid[] = "xxxxxx";                  
        char pass[] = "xxxxxxx";                         

        
        
        #include <ModbusMaster.h>                             
        #define MAX485_DE  16                                 
        #define MAX485_RE  5                                  
                                                              
        static uint8_t pzemSlaveAddr = 0x01;                   
        static uint16_t NewshuntAddr = 0x0001;                
       
        ModbusMaster node;                                      
        
        float PZEMVoltage =0;                                 
        float PZEMCurrent =0;                                 
        float PZEMPower =0;                                   
        float PZEMEnergy=0;                                   
 
        unsigned long startMillisPZEM;                        
        unsigned long currentMillisPZEM;                      
        const unsigned long periodPZEM = 1000;                
        
        unsigned long startMillisReadData;                    
        unsigned long currentMillisReadData;                  
        const unsigned long periodReadData = 1000;            
        int ResetEnergy = 0;                                  
        int a = 1;
        unsigned long startMillis1;                           

void setup() 
{
        
       
        
        startMillis1 = millis();
        
        Serial.begin(9600);                                   
        PZEMSerial.begin(9600,SWSERIAL_8N2,4,0);               
        Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);    

        

        startMillisPZEM = millis();                           
        pinMode(MAX485_RE, OUTPUT);                           
        pinMode(MAX485_DE, OUTPUT);                           
        digitalWrite(MAX485_RE, 0);                           
        digitalWrite(MAX485_DE, 0);                           
                                                              
        node.preTransmission(preTransmission);                
        node.postTransmission(postTransmission);
        node.begin(pzemSlaveAddr,PZEMSerial);                          
        delay(1000);                                          

        

        startMillisReadData = millis();                       

}

void loop() 
{
        
        
        
        Blynk.run();
       
        if ((millis()- startMillis1 >= 10000) && (a ==1))
        {
          setShunt(pzemSlaveAddr);                                           
          changeAddress(0XF8, pzemSlaveAddr);                             
          a = 0;
        } 

        
        
        currentMillisPZEM = millis(); 
        
        if (currentMillisPZEM - startMillisPZEM >= periodPZEM)                                            
        {    
          uint8_t result;                                                                                   
          result = node.readInputRegisters(0x0000, 6);                                                    
          if (result == node.ku8MBSuccess)                                                                
            {
              uint32_t tempdouble = 0x00000000;                                                            
              PZEMVoltage = node.getResponseBuffer(0x0000) / 100.0;                                       
                                                                                                          
              PZEMCurrent = node.getResponseBuffer(0x0001) / 100.0;                                       
              
              tempdouble =  (node.getResponseBuffer(0x0003) << 16) + node.getResponseBuffer(0x0002);      
              PZEMPower = tempdouble / 10.0;                                                              
              
              tempdouble =  (node.getResponseBuffer(0x0005) << 16) + node.getResponseBuffer(0x0004);      
              PZEMEnergy = tempdouble;                                                                    
              
              if (pzemSlaveAddr==2)                                                                       
                {
                }
            } 
              else
                {
                }
              startMillisPZEM = currentMillisPZEM ;                                                       
        }
                                                                                                        
        
        currentMillisReadData = millis();                                                                 
        if (currentMillisReadData - startMillisReadData >= periodReadData)                                  
          {
            Serial.print("Vdc : "); Serial.print(PZEMVoltage); Serial.println(" V ");
            Serial.print("Idc : "); Serial.print(PZEMCurrent); Serial.println(" A ");
            Serial.print("Power : "); Serial.print(PZEMPower); Serial.println(" W ");
            Serial.print("Energy : "); Serial.print(PZEMEnergy); Serial.println(" kWh ");
            Blynk.virtualWrite(V0,PZEMVoltage);                                                           
            Blynk.virtualWrite(V1,PZEMCurrent);
            Blynk.virtualWrite(V2,PZEMPower);
            Blynk.virtualWrite(V3,PZEMEnergy);
            startMillisReadData = millis();
          }
          
}

void preTransmission()                                                                                    
{
        
        if(millis() - startMillis1 > 5000)                                                                
        {
          digitalWrite(MAX485_RE, 1);                                                                     
          digitalWrite(MAX485_DE, 1);                                                                     
          delay(1);                                                                                       
        }
}

void postTransmission()                                                                                   
{
        
        
        if(millis() - startMillis1 > 5000)                                                                
        {
          delay(3);                                                                                       
          digitalWrite(MAX485_RE, 0);                                                                     
          digitalWrite(MAX485_DE, 0);                                                                     
        }
}

void setShunt(uint8_t slaveAddr)                                                                          
{

        
        
        static uint8_t SlaveParameter = 0x06;                                                             
        static uint16_t registerAddress = 0x0003;                                                         
        
        uint16_t u16CRC = 0xFFFF;                                                                         
        u16CRC = crc16_update(u16CRC, slaveAddr);                                                         
        u16CRC = crc16_update(u16CRC, SlaveParameter);
        u16CRC = crc16_update(u16CRC, highByte(registerAddress));
        u16CRC = crc16_update(u16CRC, lowByte(registerAddress));
        u16CRC = crc16_update(u16CRC, highByte(NewshuntAddr));
        u16CRC = crc16_update(u16CRC, lowByte(NewshuntAddr));
      
        preTransmission();                                                                                
      
        PZEMSerial.write(slaveAddr);                                                                      
        PZEMSerial.write(SlaveParameter);
        PZEMSerial.write(highByte(registerAddress));
        PZEMSerial.write(lowByte(registerAddress));
        PZEMSerial.write(highByte(NewshuntAddr));
        PZEMSerial.write(lowByte(NewshuntAddr));
        PZEMSerial.write(lowByte(u16CRC));
        PZEMSerial.write(highByte(u16CRC));
        delay(10);
        postTransmission();                                                                               
        delay(100);
}

BLYNK_WRITE(V4)                                                
{
        if(param.asInt()==1)
          { 
            uint16_t u16CRC = 0xFFFF;                         
            static uint8_t resetCommand = 0x42;               
            uint8_t slaveAddr = pzemSlaveAddr;                 
            u16CRC = crc16_update(u16CRC, slaveAddr);
            u16CRC = crc16_update(u16CRC, resetCommand);
            preTransmission();                                                
            PZEMSerial.write(slaveAddr);                      
            PZEMSerial.write(resetCommand);                   
            PZEMSerial.write(lowByte(u16CRC));                
            PZEMSerial.write(highByte(u16CRC));                
            delay(10);
            postTransmission();                               
            delay(100);
          }
}

void changeAddress(uint8_t OldslaveAddr, uint8_t NewslaveAddr)                                            
{

        
    
        static uint8_t SlaveParameter = 0x06;                                                             
        static uint16_t registerAddress = 0x0002;                                                         
        uint16_t u16CRC = 0xFFFF;                                                                         
        u16CRC = crc16_update(u16CRC, OldslaveAddr);                                                      
        u16CRC = crc16_update(u16CRC, SlaveParameter);
        u16CRC = crc16_update(u16CRC, highByte(registerAddress));
        u16CRC = crc16_update(u16CRC, lowByte(registerAddress));
        u16CRC = crc16_update(u16CRC, highByte(NewslaveAddr));
        u16CRC = crc16_update(u16CRC, lowByte(NewslaveAddr));
        preTransmission();                                                                                
        PZEMSerial.write(OldslaveAddr);                                                                       
        PZEMSerial.write(SlaveParameter);
        PZEMSerial.write(highByte(registerAddress));
        PZEMSerial.write(lowByte(registerAddress));
        PZEMSerial.write(highByte(NewslaveAddr));
        PZEMSerial.write(lowByte(NewslaveAddr));
        PZEMSerial.write(lowByte(u16CRC));
        PZEMSerial.write(highByte(u16CRC));
        delay(10);
        postTransmission();                                                                                
        delay(100);
}

I have a problem with my design. previously my design worked fine. However on 08/15/2022 my design is not working. what is causing my design to not work? I have replaced all components with new ones. In the following, I will show a schematic of the design and program that previously worked.

#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
SoftwareSerial PZEMSerial;
LiquidCrystal_I2C LCD(0x27, 16, 2);


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "K-efohjF6lZ4I4QFhVaSTtOAH8he_HfZ";
char ssid[] = "iPhone";
char pass[] = "GODBLESS";


#include <ModbusMaster.h>
#define MAX485_DE  12
#define MAX485_RE  14

static uint8_t pzemSlaveAddr = 0x01;
static uint16_t NewshuntAddr = 0x0001;

ModbusMaster node;

float PZEMVoltage = 0;
float PZEMCurrent = 0;
float PZEMPower = 0;
float PZEMEnergy = 0;

unsigned long startMillisPZEM;
unsigned long currentMillisPZEM;
const unsigned long periodPZEM = 1000;



unsigned long startMillisReadData;
unsigned long currentMillisReadData;
const unsigned long periodReadData = 1000;
int ResetEnergy = 0;
int a = 1;
unsigned long startMillis1;

void setup()
{

  startMillis1 = millis();

  Serial.begin(9600);
  PZEMSerial.begin(9600, SWSERIAL_8N2, 13, 0);
  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  startMillisPZEM = millis();
  pinMode(MAX485_RE, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  digitalWrite(MAX485_RE, 0);
  digitalWrite(MAX485_DE, 0);

  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
  node.begin(pzemSlaveAddr, PZEMSerial);
  delay(1000);

  LCD.begin();
  LCD.setCursor(0, 0);
  startMillisReadData = millis();

}

void loop()
{


  Blynk.run();

  if ((millis() - startMillis1 >= 10000) && (a == 1))
  {
    setShunt(pzemSlaveAddr);
    changeAddress(0XF8, pzemSlaveAddr);
    a = 0;
  }

  currentMillisPZEM = millis();

  if (currentMillisPZEM - startMillisPZEM >= periodPZEM)
  {
    uint8_t result;
    result = node.readInputRegisters(0x0000, 6);
    if (result == node.ku8MBSuccess)
    {
      uint32_t tempdouble = 0x00000000;
      PZEMVoltage = node.getResponseBuffer(0x0000) / 100.0;

      PZEMCurrent = node.getResponseBuffer(0x0001) / 100.0;

      tempdouble =  (node.getResponseBuffer(0x0003) << 16) + node.getResponseBuffer(0x0002);
      PZEMPower = tempdouble / 10.0;

      tempdouble =  (node.getResponseBuffer(0x0005) << 16) + node.getResponseBuffer(0x0004);
      PZEMEnergy = tempdouble / 1000.0;

      if (pzemSlaveAddr == 2)
      {
      }
    }
    else
    {
    }
    startMillisPZEM = currentMillisPZEM ;
  }


  currentMillisReadData = millis();
  if (currentMillisReadData - startMillisReadData >= periodReadData)
  {
    Serial.print("Vdc : "); Serial.print(PZEMVoltage); Serial.println(" V ");
    Serial.print("Idc : "); Serial.print(PZEMCurrent); Serial.println(" A ");
    Serial.print("Power : "); Serial.print(PZEMPower); Serial.println(" W ");
    Serial.print("Energy : "); Serial.print(PZEMEnergy); Serial.println(" kWh ");

    LCD.setCursor(0, 0);                                                                          /* Set cursor to first colum 0 and second row 1  */
    LCD.print(PZEMVoltage);                                                                    /* Display Voltage on LCD Display with 1 decimal*/
    LCD.print("V         ");
    LCD.setCursor(9, 0);
    LCD.print(PZEMEnergy);
    LCD.print("kWh        ");
    LCD.setCursor(0, 1);
    LCD.print(PZEMCurrent);
    LCD.print("A          ");
    LCD.setCursor(9, 1);
    LCD.print(PZEMPower);
    LCD.print("W          ");

    Blynk.virtualWrite(V0, PZEMVoltage);
    Blynk.virtualWrite(V1, PZEMCurrent);
    Blynk.virtualWrite(V2, PZEMPower);
    Blynk.virtualWrite(V3, PZEMEnergy);
    startMillisReadData = millis();
  }

}

void preTransmission()
{
  /* 1- PZEM-017 DC Energy Meter */
  if (millis() - startMillis1 > 5000)
  {
    digitalWrite(MAX485_RE, 1);
    digitalWrite(MAX485_DE, 1);
    delay(1);
  }
}

void postTransmission()
{

  /* 1- PZEM-017 DC Energy Meter */
  if (millis() - startMillis1 > 5000)
  {
    delay(3);
    digitalWrite(MAX485_RE, 0);
    digitalWrite(MAX485_DE, 0);
  }
}

void setShunt(uint8_t slaveAddr)
{

  /* 1- PZEM-017 DC Energy Meter */

  static uint8_t SlaveParameter = 0x06;
  static uint16_t registerAddress = 0x0003;

  uint16_t u16CRC = 0xFFFF;                                                                         /* declare CRC check 16 bits*/
  u16CRC = crc16_update(u16CRC, slaveAddr);                                                         // Calculate the crc16 over the 6bytes to be send
  u16CRC = crc16_update(u16CRC, SlaveParameter);
  u16CRC = crc16_update(u16CRC, highByte(registerAddress));
  u16CRC = crc16_update(u16CRC, lowByte(registerAddress));
  u16CRC = crc16_update(u16CRC, highByte(NewshuntAddr));
  u16CRC = crc16_update(u16CRC, lowByte(NewshuntAddr));

  preTransmission();                                                                                /* trigger transmission mode*/

  PZEMSerial.write(slaveAddr);                                                                      /* these whole process code sequence refer to manual*/
  PZEMSerial.write(SlaveParameter);
  PZEMSerial.write(highByte(registerAddress));
  PZEMSerial.write(lowByte(registerAddress));
  PZEMSerial.write(highByte(NewshuntAddr));
  PZEMSerial.write(lowByte(NewshuntAddr));
  PZEMSerial.write(lowByte(u16CRC));
  PZEMSerial.write(highByte(u16CRC));
  delay(10);
  postTransmission();                                                                               /* trigger reception mode*/
  delay(100);
}

BLYNK_WRITE(V4)                                               // Virtual push button to reset energy for Meter 1
{
  if (param.asInt() == 1)
  {
    uint16_t u16CRC = 0xFFFF;                         /* declare CRC check 16 bits*/
    static uint8_t resetCommand = 0x42;               /* reset command code*/
    uint8_t slaveAddr = pzemSlaveAddr;                 // if you set different address, make sure this slaveAddr must change also
    u16CRC = crc16_update(u16CRC, slaveAddr);
    u16CRC = crc16_update(u16CRC, resetCommand);
    preTransmission();                                /* trigger transmission mode*/
    PZEMSerial.write(slaveAddr);                      /* send device address in 8 bit*/
    PZEMSerial.write(resetCommand);                   /* send reset command */
    PZEMSerial.write(lowByte(u16CRC));                /* send CRC check code low byte  (1st part) */
    PZEMSerial.write(highByte(u16CRC));               /* send CRC check code high byte (2nd part) */
    delay(10);
    postTransmission();                               /* trigger reception mode*/
    delay(100);
  }
}

void changeAddress(uint8_t OldslaveAddr, uint8_t NewslaveAddr)                                            //Change the slave address of a node
{

  /* 1- PZEM-017 DC Energy Meter */

  static uint8_t SlaveParameter = 0x06;                                                             /* Write command code to PZEM */
  static uint16_t registerAddress = 0x0002;                                                         /* Modbus RTU device address command code */
  uint16_t u16CRC = 0xFFFF;                                                                         /* declare CRC check 16 bits*/
  u16CRC = crc16_update(u16CRC, OldslaveAddr);                                                      // Calculate the crc16 over the 6bytes to be send
  u16CRC = crc16_update(u16CRC, SlaveParameter);
  u16CRC = crc16_update(u16CRC, highByte(registerAddress));
  u16CRC = crc16_update(u16CRC, lowByte(registerAddress));
  u16CRC = crc16_update(u16CRC, highByte(NewslaveAddr));
  u16CRC = crc16_update(u16CRC, lowByte(NewslaveAddr));
  preTransmission();                                                                                 /* trigger transmission mode*/
  PZEMSerial.write(OldslaveAddr);                                                                       /* these whole process code sequence refer to manual*/
  PZEMSerial.write(SlaveParameter);
  PZEMSerial.write(highByte(registerAddress));
  PZEMSerial.write(lowByte(registerAddress));
  PZEMSerial.write(highByte(NewslaveAddr));
  PZEMSerial.write(lowByte(NewslaveAddr));
  PZEMSerial.write(lowByte(u16CRC));
  PZEMSerial.write(highByte(u16CRC));
  delay(10);
  postTransmission();                                                                                /* trigger reception mode*/
  delay(100);
}

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

Copy and paste these if you can’t find the correct symbol on your keyboard.

Also, I’ve merged this with your previous post, as they appear to share the same code.

Pete.

Hi sir. Im sorry for that. Im done with that. Can you help me to fix it? I got no idea what happen

right sir. however, yesterday the design that I had made went well. whereas today it doesn’t work the way I want it to. Can sir help me and explain to me what happened to my design? because I don’t know what happened.

Arief