167 lines
4.2 KiB
C++
167 lines
4.2 KiB
C++
#include <PZEM004Tv30.h>
|
|
|
|
/////////////////////
|
|
// Domoticz Classe
|
|
/////////////////////
|
|
|
|
#include "Domoticz.h"
|
|
|
|
Domoticz domo("192.168.1.3", "81", "Livebox-37cc", "8A6060920A8A86896F770F2C47");
|
|
|
|
|
|
/* 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)
|
|
*/
|
|
// ARduino
|
|
//PZEM004Tv30 pzem(11, 12);
|
|
|
|
// ESP
|
|
PZEM004Tv30 pzem(D5, D6);
|
|
|
|
#include <Wire.h> // This library is already built in to the Arduino IDE
|
|
#include <LiquidCrystal_I2C.h> //This library you can add via Include Library > Manage Library >
|
|
//LiquidCrystal_I2C lcd(0x3F, 20, 4);
|
|
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
|
|
|
|
const int PUISSANCE_RADIATEUR = 750;
|
|
|
|
const char* ssid = "Livebox-37cc";
|
|
const char* password = "8A6060920A8A86896F770F2C47";
|
|
|
|
IPAddress gateway(192, 168, 1, 1);
|
|
IPAddress subnet(255, 255, 0, 0);
|
|
IPAddress DNS(192, 168, 1, 1);
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
lcd.init(); // initializing the LCD
|
|
lcd.noBacklight(); // Enable or Turn On the backlight
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Init Wifi");
|
|
String macId = domo.generateKey();
|
|
IPAddress ip = domo.getIP(macId);
|
|
|
|
domo.initWifiStatic(ip, gateway, subnet, DNS);
|
|
|
|
delay(300);
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("-----------------------"); // Start Print Test to Line 2
|
|
pzem.resetEnergy();
|
|
delay(300);
|
|
lcd.clear();
|
|
|
|
}
|
|
int boucle = 0;
|
|
void loop() {
|
|
boucle ++;
|
|
lcd.setCursor(0, 0);
|
|
float voltage = pzem.voltage();
|
|
if( !isnan(voltage) ){
|
|
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
|
|
//lcd.print(String(voltage,1) + " V ");
|
|
|
|
} else {
|
|
Serial.println("Error reading voltage");
|
|
}
|
|
|
|
float current = pzem.current();
|
|
if( !isnan(current) ){
|
|
Serial.print("Current: "); Serial.print(current); Serial.println("A");
|
|
// lcd.print(String(current,1) + "A ");
|
|
|
|
} else {
|
|
Serial.println("Error reading current");
|
|
}
|
|
float pf = pzem.pf();
|
|
float power = pzem.power();
|
|
|
|
if( !isnan(pf) ){
|
|
Serial.print("PF: "); Serial.println(pf);
|
|
lcd.print(String(pf,3) + "pf ");
|
|
if (pf != 0) {
|
|
lcd.print(String(power / pf,0) + "Wa");
|
|
}
|
|
} else {
|
|
Serial.println("Error reading power factor");
|
|
}
|
|
|
|
lcd.setCursor(0, 1);
|
|
if( !isnan(power) ){
|
|
Serial.print("Power: "); Serial.print(power); Serial.println("W");
|
|
|
|
if (pf > 0) {
|
|
lcd.print("+" + String(power,1) + "W ");
|
|
}
|
|
else {
|
|
lcd.print("-" + String(power,1) + "Wa");
|
|
}
|
|
if (boucle > 10) {
|
|
printInfo(power);
|
|
boucle = 0;
|
|
}
|
|
|
|
|
|
} else {
|
|
Serial.println("Error reading power");
|
|
if (boucle > 10) {
|
|
printInfo(0);
|
|
boucle = 0;
|
|
}
|
|
}
|
|
|
|
|
|
float energy = pzem.energy();
|
|
if( !isnan(energy) ){
|
|
if (energy < 1000) {
|
|
lcd.print(String(energy * 1000,0) + "Wh ");
|
|
}
|
|
else {
|
|
lcd.print(String(energy,1) + "kWh ");
|
|
}
|
|
Serial.print("Energy: ");
|
|
Serial.print(energy,3);
|
|
Serial.println("kWh");
|
|
} else {
|
|
Serial.println("Error reading energy");
|
|
}
|
|
|
|
float frequency = pzem.frequency();
|
|
if( !isnan(frequency) ){
|
|
Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
|
|
} else {
|
|
Serial.println("Error reading frequency");
|
|
}
|
|
|
|
// pf très proche ou égal à 0 ==> début d'injection
|
|
// if(!isnan(pf) && pf < 0.01 && power > 10){
|
|
// int level = power / (PUISSANCE_RADIATEUR / 100);
|
|
//
|
|
// Serial.println("début injection " + level);
|
|
// }
|
|
|
|
|
|
Serial.println();
|
|
delay(210);
|
|
}
|
|
|
|
void printInfo(double value)
|
|
{
|
|
// Domoticz format /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT;BAR;BAR_FOR
|
|
|
|
boolean connected = domo.connect();
|
|
|
|
if (connected) {
|
|
String svalue = String(value);
|
|
Serial.println(svalue);
|
|
domo.executeJson("/json.htm?type=command¶m=udevice&idx=1138", svalue, "0");
|
|
domo._client.stop();
|
|
delay(200);
|
|
}
|
|
|
|
Serial.print("Time = ");
|
|
Serial.println(millis());
|
|
|
|
delay(200);
|
|
}
|