254 lines
6.8 KiB
C++
254 lines
6.8 KiB
C++
// ------------------------------
|
|
// LED
|
|
// ------------------------------
|
|
const byte LED_PIN = 13;
|
|
|
|
#define DEBUG TRUE
|
|
// ------------------------------
|
|
// Energie
|
|
// ------------------------------
|
|
#include "EmonLib.h" // Include Emon Library
|
|
EnergyMonitor emon1; // Create an instance
|
|
|
|
int boucle = 0;
|
|
|
|
#include <SoftwareSerial.h>
|
|
SoftwareSerial ESPserial(9, 8); // RX | TX
|
|
|
|
// ----------------
|
|
// PZEM for arduino
|
|
// -----------------
|
|
#include <PZEM004Tv30.h>
|
|
PZEM004Tv30 pzem(11, 12);
|
|
|
|
// ----------------------------
|
|
// Dimmer
|
|
// ----------------------------
|
|
#include <RBDdimmer.h>
|
|
#define outputPin 6
|
|
#define zerocross D2 // for boards with CHANGEBLE input pins
|
|
#define pas 5
|
|
dimmerLamp dimmer(outputPin); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
|
|
// To set dimmer off ==> dimmer.setPower(128);
|
|
// value 0 = On
|
|
|
|
// ----------------------------
|
|
// Interruption
|
|
// ----------------------------
|
|
const byte interruptPin = 3;
|
|
//volatile byte backlight_status = LOW;
|
|
|
|
// ----------------------------
|
|
// Screen LCD
|
|
// ----------------------------
|
|
#include <LiquidCrystal_I2C.h>
|
|
#include "LowPower.h"
|
|
|
|
#define I2C_SLAVE_ADDRESS 0x0B // 12 pour l'esclave 2 et ainsi de suite
|
|
#define PAYLOAD_SIZE 2
|
|
|
|
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
|
|
|
|
/******************************************************************/
|
|
/******************************************************************/
|
|
/******************************************************************/
|
|
|
|
#define SolaireProduction "1087"
|
|
#define Consommation_Apparente "1123"
|
|
#define CONSOMMATION_GENERALE "1115"
|
|
|
|
|
|
void setup()
|
|
{
|
|
lcd.init(); // initialize the lcd
|
|
delay(200);
|
|
lcd.noBacklight();
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Screen Ok");
|
|
Serial.begin(9600);
|
|
ESPserial.begin(9600);
|
|
|
|
// Screen
|
|
Serial.println("Screen init...");
|
|
delay(1000);
|
|
|
|
pinMode(LED_PIN, OUTPUT);
|
|
pinMode(interruptPin, INPUT);
|
|
|
|
lcd.clear();
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Serial communication & wifi");
|
|
emon1.voltage(A1, 320 , 2.6); // Voltage: input pin, calibration, phase_shift
|
|
emon1.current(A2, 30); // Current: input pin, calibration.
|
|
delay(1000);
|
|
|
|
// Dimmer
|
|
Serial.println("Dimmer Program is starting...");
|
|
delay(1000);
|
|
dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
|
|
Serial.println("Set value");
|
|
dimmer.setPower(50); // setPower(0-100%);
|
|
lcd.clear();
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Dimmer set to 0");
|
|
delay(1000);
|
|
|
|
lcd.print("-------PZEM-----------"); // Start Print Test to Line 2
|
|
pzem.resetEnergy();
|
|
delay(1000);
|
|
digitalWrite(interruptPin, LOW);
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
|
|
// if (digitalRead(interruptPin) == HIGH) {
|
|
// lcd.backlight();
|
|
// }
|
|
// else {
|
|
// lcd.noBacklight();
|
|
// }
|
|
double Irms[2];
|
|
boucle ++;
|
|
|
|
emon1.calcVI(20, 200); // 1 Demande a Emonlib de tout calculer, (puissance relle, volts moyen, ampère moyen et facteur de puissance)
|
|
// Irms[0] = emon1.calcIrms(5440) * 230; //emon1.apparentPower);
|
|
Irms[0] = emon1.apparentPower;
|
|
float verif_voltage = emon1.Vrms; // 1 creation de la variable "volts moyen" (mesurable avec un voltmètre pour l'etalonnage)
|
|
float verif_ampere = emon1.Irms; // 1 creation de la variable "Ampères Moyen" (mesurable avec une pince ampèremétrique pour l'etalonnage))
|
|
float Cos_phi = emon1.powerFactor;
|
|
|
|
Serial.print(verif_voltage);
|
|
Serial.print(" V ");
|
|
Serial.print(verif_ampere);
|
|
Serial.print(" A ");
|
|
Serial.print(emon1.realPower);
|
|
Serial.print(" Wr ");
|
|
Serial.print(emon1.apparentPower); // Calculate Irms only
|
|
Serial.print(" Wcap ");
|
|
Serial.print(Irms[0]); // Calculate Irms only
|
|
Serial.print(" Wc ");
|
|
if (boucle < 10) {
|
|
lcd.clear();
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Calibration :" + String(boucle));
|
|
delay (500);
|
|
}
|
|
else {
|
|
double value = pzemRead();
|
|
|
|
ESPserial.print(getJson(String(SolaireProduction), value));
|
|
delay(500);
|
|
Serial.print(value); // Calculate Irms only
|
|
Serial.print(" Wr ");
|
|
|
|
ESPserial.print(getJson(String(Consommation_Apparente), emon1.realPower));
|
|
delay(500);
|
|
|
|
ESPserial.print(getJson(String(CONSOMMATION_GENERALE), emon1.apparentPower));
|
|
|
|
if (boucle % 2 == 0) {
|
|
lcd.clear();
|
|
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Cso:" + String(emon1.apparentPower, 0) + " R " + String(emon1.realPower, 0));
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("Sol:" + String(value, 0) + " V " + String(emon1.Vrms, 1));
|
|
}
|
|
delay (2000);
|
|
|
|
if (boucle > 1000) {
|
|
boucle = 11;
|
|
}
|
|
}
|
|
|
|
while ( ESPserial.available()) {
|
|
Serial.write( ESPserial.read() );
|
|
}
|
|
Serial.println();
|
|
}
|
|
|
|
double pzemRead() {
|
|
lcd.setCursor(0, 0);
|
|
double voltage = pzem.voltage();
|
|
double current = pzem.current();
|
|
double pf = pzem.pf();
|
|
double power = pzem.power();
|
|
double energy = pzem.energy();
|
|
double frequency = pzem.frequency();
|
|
// if ( !isnan(voltage) ) {
|
|
// Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
|
|
// //lcd.print(String(voltage,1) + " V ");
|
|
//
|
|
// } else {
|
|
// Serial.println("Error reading voltage");
|
|
// }
|
|
|
|
// if ( !isnan(current) ) {
|
|
// Serial.print("Current: "); Serial.print(current); Serial.println("A");
|
|
// // lcd.print(String(current,1) + "A ");
|
|
//
|
|
// } else {
|
|
// Serial.println("Error reading current");
|
|
// }
|
|
|
|
|
|
if (boucle % 2 == 1) {
|
|
lcd.clear();
|
|
|
|
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");
|
|
}
|
|
|
|
} else {
|
|
//Serial.println("Error reading power");
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
// if ( !isnan(frequency) ) {
|
|
// Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
|
|
// } else {
|
|
// Serial.println("Error reading frequency");
|
|
// }
|
|
|
|
}
|
|
return power;
|
|
}
|
|
|
|
String getJson(String idx, double value)
|
|
{
|
|
String json = "/json.htm?type=command¶m=udevice&idx=" + idx + "&svalue=" + String(value) + ";0&nvalue=0&nvalue=0&nvalue=0";
|
|
Serial.println(json);
|
|
return json;
|
|
}
|