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);
}