141 lines
3.4 KiB
C++
141 lines
3.4 KiB
C++
// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
|
|
|
|
#include "EmonLib.h" // Include Emon Library
|
|
EnergyMonitor emon_conso; // Create an instance
|
|
EnergyMonitor emon_solar;
|
|
|
|
#define NB_MESURES 50
|
|
|
|
const uint16_t port = 81;
|
|
const char * host = "192.168.1.3"; // ip or dns
|
|
|
|
int lectures = -10;
|
|
double sum_conso = 0;
|
|
double sum_solar = 0;
|
|
double last_conso_value = 0;
|
|
double last_solar_value = 0;
|
|
|
|
/*
|
|
* Displays text sent over the serial port (e.g. from the Serial Monitor) on
|
|
* an attached LCD.
|
|
* YWROBOT
|
|
*Compatible with the Arduino IDE 1.0
|
|
*Library version:1.1
|
|
*/
|
|
#include <Wire.h>
|
|
#include <LiquidCrystal_I2C.h>
|
|
|
|
#define I2C_SLAVE_ADDRESS 0x0B // 12 pour l'esclave 2 et ainsi de suite
|
|
#define PAYLOAD_SIZE 2
|
|
int n = 0;
|
|
|
|
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
|
|
|
|
void setup()
|
|
{
|
|
lcd.init(); // initialize the lcd
|
|
lcd.backlight();
|
|
Serial.begin(9600);
|
|
|
|
emon_conso.current(0, 60); // Current: input pin, calibration.
|
|
emon_solar.current(1, 60);
|
|
|
|
Wire.begin(I2C_SLAVE_ADDRESS);
|
|
Serial.begin(9600);
|
|
Serial.println("-------------------------------------I am Slave1");
|
|
delay(1000);
|
|
Wire.onRequest(requestEvents);
|
|
Wire.onReceive(receiveEvents);
|
|
|
|
delay(500);
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
double Irms_conso = emon_conso.calcIrms(1480); // Calculate Irms_conso only
|
|
double Irms_solar = emon_solar.calcIrms(1480); // Calculate Irms_conso only
|
|
|
|
lectures ++;
|
|
//
|
|
// Serial.print(Irms_conso*230.0); // Apparent power
|
|
// Serial.print(" ");
|
|
// Serial.println(Irms_conso); // Irms_conso
|
|
//
|
|
// Serial.print(Irms_solar*230.0); // Apparent power
|
|
// Serial.print(" ");
|
|
// Serial.println(Irms_solar);
|
|
|
|
delay(10);
|
|
if (lectures > 0) {
|
|
sum_conso += Irms_conso;
|
|
sum_solar += Irms_solar;
|
|
|
|
if (lectures == NB_MESURES) {
|
|
last_conso_value = sum_conso / lectures;
|
|
last_solar_value = sum_solar / lectures;
|
|
|
|
// envoieDomoticz("1053",String(sum_conso / lectures)); // intensite
|
|
// delay(500);
|
|
// envoieDomoticz("1086",String(sum_solar / lectures)); // intensite
|
|
|
|
lectures = -10;
|
|
sum_conso = 0;
|
|
sum_solar = 0;
|
|
|
|
lcd.clear();
|
|
lcd.setCursor(0,0);
|
|
lcd.print("Conso :" + String(last_conso_value));
|
|
lcd.setCursor(0,1);
|
|
lcd.print("Solaire:" + String(last_solar_value));
|
|
delay(2000);
|
|
}
|
|
}
|
|
if (Serial.available() > 0) {
|
|
// read the incoming byte:
|
|
int incomingByte = Serial.read();
|
|
// say what you got:
|
|
Serial.print("I received: ");
|
|
Serial.println(incomingByte, DEC);
|
|
}
|
|
|
|
while(Serial.available () > 0){
|
|
Serial.read();
|
|
}
|
|
Serial.flush();
|
|
// read all the available characters
|
|
// display each character to the LCD
|
|
|
|
}
|
|
|
|
|
|
void requestEvents()
|
|
{
|
|
Serial.println(F("---> received request"));
|
|
Serial.print(F("sending value : "));
|
|
char TempString[10]; // Hold The Convert Data
|
|
dtostrf(last_conso_value,2,2,TempString);
|
|
Serial.println(TempString);
|
|
|
|
Wire.write(TempString);
|
|
Wire.write(";");
|
|
|
|
dtostrf(last_solar_value,2,2,TempString);
|
|
Serial.println(TempString);
|
|
|
|
Wire.write(TempString);
|
|
Wire.write("\n");
|
|
|
|
|
|
}
|
|
|
|
void receiveEvents(int numBytes)
|
|
{
|
|
Serial.println(F("---> received events"));
|
|
n = Wire.read();
|
|
Serial.print(numBytes);
|
|
Serial.println(F("bytes received"));
|
|
Serial.print(F("received value : "));
|
|
Serial.println(n);
|
|
}
|
|
|