#include <SoftwareSerial.h>
SoftwareSerial PZEMSerial;
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecureAxTLS.h>
char auth[] = "aWq6tXF4V8w4gCPk23dS8vzYsX46HjAs";
char ssid[] = "Deaun2";
char pass[] = "57238840";
#include <ModbusMaster.h>
#define MAX485_DE 16
#define MAX485_RE 5
#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT11 // DHT 11
#define ON_Board_LED 2
static uint8_t pzemSlaveAddr = 0x01;
static uint16_t NewshuntAddr = 0x0000;
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 = 295000;
int ResetEnergy = 0;
int a = 1;
unsigned long startMillis1;
WiFiClientSecure client;
void setup()
{
startMillis1 = millis();
Blynk.begin(auth, ssid, pass);
Serial.begin(9600);
PZEMSerial.begin(9600,SWSERIAL_8N2,4,0);
WiFi.begin(ssid, pass);
Serial.println("");
pinMode(ON_Board_LED,OUTPUT);
digitalWrite(ON_Board_LED, HIGH);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
digitalWrite(ON_Board_LED, LOW);
delay(250);
digitalWrite(ON_Board_LED, HIGH);
delay(250);
}
digitalWrite(ON_Board_LED, HIGH);
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
client.setInsecure();
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();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print("*");
delay(1000);
}
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(" Wh ");
Blynk.virtualWrite(V0, PZEMVoltage);
Blynk.virtualWrite(V1, PZEMCurrent);
Blynk.virtualWrite(V2, PZEMPower);
Blynk.virtualWrite(V3, PZEMEnergy);
GG_ScriptSheet();
startMillisReadData = millis();
}
}
void CheckWiFi(){
while (WiFi.status() != WL_CONNECTED) {
Serial.print("*");
delay(1000);
}
}
void GG_ScriptSheet(){
axTLS::WiFiClientSecure client;
const char* HOST = "script.google.com";
const char* GScriptId = "AKfycbyPl4hu242GL-idMzgAD4X8xDYMOYEWKEfT3n5DcYnKRcLXN7c";
String url32 = String("/macros/s/") + GScriptId + "/exec?&FIELD1="+(String)PZEMVoltage +
"&FIELD2="+(String)PZEMCurrent+
"&FIELD3="+(String)PZEMPower+
"&FIELD4="+(String)PZEMEnergy;
Serial.print("Client.Connecting...");
if (client.connect(HOST, 443)) {
client.println("GET " + String(url32) + " HTTP/1.0"); //HTTP/1.0 OK sent LINEnotify and GoogleSheet
client.println("Host: " + String(HOST));
client.println("User-Agent: ESP8266\r\n");
client.println("Connection: close\r\n\r\n");
//client.println(true ? "" : "Connection: close\r\n");
//client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Type: application/json");
//2client.println("Connection: close");
client.println("Content-Length: " + String(url32.length()));
client.println();
//client.println(postData);
client.print("\r\n\r\n");
Serial.print("Response...");
}
Serial.println("OK !!!");
}
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);
}
In file included from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/user_interface.h:31:0,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/glue.h:53,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/arch/cc.h:61,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwip/arch.h:48,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwip/debug.h:40,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwipopts.h:3608,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwip/opt.h:51,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwip/ip_addr.h:40,
from C:\Users\Admin\AppData\Local\Arduino15\libraries\blynk-library-master\src/Blynk/BlynkProtocolDefs.h:81,
from C:\Users\Admin\AppData\Local\Arduino15\libraries\blynk-library-master\src/Blynk/BlynkApi.h:39,
from C:\Users\Admin\AppData\Local\Arduino15\libraries\blynk-library-master\src/BlynkApiArduino.h:14,
from C:\Users\Admin\AppData\Local\Arduino15\libraries\blynk-library-master\src/BlynkSimpleEsp8266.h:24,
from E:\psb_v1\psb_v1.ino:3:
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/ipv4_addr.h:53:22: error: field 'ip' has incomplete type
struct ipv4_addr ip;
^
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/ipv4_addr.h:54:22: error: field 'netmask' has incomplete type
struct ipv4_addr netmask;
^
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/ipv4_addr.h:55:22: error: field 'gw' has incomplete type
struct ipv4_addr gw;
^
In file included from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/glue.h:53:0,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/arch/cc.h:61,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwip/arch.h:48,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwip/debug.h:40,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwipopts.h:3608,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwip/opt.h:51,
from C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/lwip2/include/lwip/ip_addr.h:40,
from C:\Users\Admin\AppData\Local\Arduino15\libraries\blynk-library-master\src/Blynk/BlynkProtocolDefs.h:81,
from C:\Users\Admin\AppData\Local\Arduino15\libraries\blynk-library-master\src/Blynk/BlynkApi.h:39,
from C:\Users\Admin\AppData\Local\Arduino15\libraries\blynk-library-master\src/BlynkApiArduino.h:14,
from C:\Users\Admin\AppData\Local\Arduino15\libraries\blynk-library-master\src/BlynkSimpleEsp8266.h:24,
from E:\psb_v1\psb_v1.ino:3:
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/user_interface.h:363:22: error: field 'ip' has incomplete type
struct ipv4_addr ip;
^
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/user_interface.h:368:22: error: field 'start_ip' has incomplete type
struct ipv4_addr start_ip;
^
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/user_interface.h:369:22: error: field 'end_ip' has incomplete type
struct ipv4_addr end_ip;
^
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/user_interface.h:532:22: error: field 'ip' has incomplete type
struct ipv4_addr ip;
^
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/user_interface.h:533:22: error: field 'mask' has incomplete type
struct ipv4_addr mask;
^
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/user_interface.h:534:22: error: field 'gw' has incomplete type
struct ipv4_addr gw;
^
C:\Users\Admin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/sdk/include/user_interface.h:544:22: error: field 'ip' has incomplete type
struct ipv4_addr ip;
^
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).