first commit
This commit is contained in:
148
voltage_and_current/voltage_and_current.ino
Normal file
148
voltage_and_current/voltage_and_current.ino
Normal file
@@ -0,0 +1,148 @@
|
||||
// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
|
||||
|
||||
#include "EmonLib.h" // Include Emon Library
|
||||
EnergyMonitor emon1; // Create an instance
|
||||
EnergyMonitor emon2; // Create an instance
|
||||
|
||||
const int numReadings = 10;
|
||||
const int numIndex = 2;
|
||||
float last_values[numIndex][numReadings -1];
|
||||
int readIndex[numIndex];
|
||||
long total[numIndex];
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
//analogReference(EXTERNAL);
|
||||
|
||||
// float vcc = readVcc() / 1000.0;
|
||||
// Serial.print(vcc);
|
||||
emon1.voltage(A2, 640 , 2.6); // Voltage: input pin, calibration, phase_shift
|
||||
emon1.current(A1, 56); // Current: input pin, calibration.
|
||||
//emon2.voltage(A2, 320, 7); // Voltage: input pin, calibration, phase_shift
|
||||
emon2.current(A0, 5);
|
||||
|
||||
for (int i = 0; i<numIndex; i++) {
|
||||
readIndex[i] = 0;
|
||||
for (int j = 0; j<numReadings; j++) {
|
||||
last_values[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int bcl = 0;
|
||||
void loop()
|
||||
{
|
||||
bcl++;
|
||||
// Calculate all. No.of half wavelengths (crossings), time-out
|
||||
// delay(200);
|
||||
emon1.calcVI(20,200); // Calculate all. No.of half wavelengths (crossings), time-out
|
||||
emon2.calcVI(20,200);
|
||||
|
||||
//emon1.serialprint(); // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)
|
||||
|
||||
float puissance_reelle1 = emon1.realPower; // 1 creation de la variable flottante "puissance reelle" qui existe dans la librairie sous "emon1.realPower"
|
||||
|
||||
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("Conso ");
|
||||
|
||||
if (bcl > 5) {
|
||||
// float vcc = readVcc() / 1000.0;
|
||||
// Serial.print(vcc);
|
||||
// Serial.print(" vcc ");
|
||||
Serial.print(verif_voltage); // 1 envoyer vers l'ordinateur la valeur "verif_voltage (Vrms)"
|
||||
Serial.print(" Vc "); // 1 envoyer vers l'ordinateur le caractère "V"
|
||||
// Serial.print(verif_ampere); // 1 envoyer vers l'ordinateur la valeur "verif_voltage (Vrms)"
|
||||
// Serial.print(" Ac "); // 1 envoyer vers l'ordinateur le caractère "A"
|
||||
Serial.print(puissance_reelle1);
|
||||
Serial.print(" Wrc ");
|
||||
Serial.print(emon1.apparentPower);
|
||||
Serial.print(" Wac ");
|
||||
Serial.print(emon1.calcIrms(5440) * 230);
|
||||
Serial.print(" WI ");
|
||||
Serial.print(Cos_phi); // 1 envoyer vers l'ordinateur la valeur "verif_voltage (Vrms)"
|
||||
Serial.print(" cos ");
|
||||
|
||||
//Serial.print("Solaire ");
|
||||
// Serial.print(emon2.Vrms); // 1 envoyer vers l'ordinateur la valeur "verif_voltage (Vrms)"
|
||||
// Serial.print(" V "); // 1 envoyer vers l'ordinateur le caractère "V"
|
||||
// Serial.print(emon2.Irms); // 1 envoyer vers l'ordinateur la valeur "verif_voltage (Vrms)"
|
||||
// Serial.print(" A "); // 1 envoyer vers l'ordinateur le caractère "A"
|
||||
Serial.print(smooth(1,emon2.realPower));
|
||||
Serial.print(" Wr ");
|
||||
Serial.print(emon2.apparentPower);
|
||||
Serial.print(" Wa ");
|
||||
Serial.print(emon2.powerFactor); // 1 envoyer vers l'ordinateur la valeur "verif_voltage (Vrms)"
|
||||
Serial.println(" cos ");
|
||||
// Serial.print(emon1.calcIrms(5440) * 230); // Calculate Irms only
|
||||
// Serial.print(" Wc ");
|
||||
// Serial.print(emon2.calcIrms(5440) * 230); // Calculate Irms only
|
||||
// Serial.println(" Ws ");
|
||||
}
|
||||
else {
|
||||
//Serial.print(".");
|
||||
delay(200);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
float smooth(int index, float last_value) { /* function smooth */
|
||||
////Perform average on sensor last_values
|
||||
float average;
|
||||
// subtract the last reading:
|
||||
total[index] = total[index] - last_values[index][readIndex[index]];
|
||||
|
||||
// read the sensor:
|
||||
last_values[index][readIndex[index]] = last_value;
|
||||
// add value to total:
|
||||
total[index] = total[index] + last_values[index][readIndex[index]];
|
||||
// handle index
|
||||
readIndex[index] = readIndex[index] + 1;
|
||||
if (readIndex[index] >= numReadings) {
|
||||
readIndex[index] = 0;
|
||||
}
|
||||
float tot = 0;
|
||||
for (int j = 0; j< numReadings; j++) {
|
||||
// Serial.print(last_values[index][j]);
|
||||
// Serial.print(" ");
|
||||
tot +=last_values[index][j];
|
||||
}
|
||||
|
||||
//Serial.println("");
|
||||
// calculate the average:
|
||||
average = tot / numReadings; //al[index] / numReadings;
|
||||
|
||||
return average;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
// Read current supply voltage
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
// long readVcc() {
|
||||
// bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
|
||||
// long result;
|
||||
// // Read 1.1V reference against Vcc
|
||||
// #if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||||
// ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84
|
||||
// #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
||||
// ADMUX = _BV(MUX3) | _BV(MUX2);
|
||||
// #else
|
||||
// ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // For ATmega328
|
||||
// #endif
|
||||
// // ADCSRB = 0;
|
||||
//
|
||||
// delay(2); // Wait for Vref to settle
|
||||
// ADCSRA |= _BV(ADSC); // Convert
|
||||
// while (bit_is_set(ADCSRA,ADSC));
|
||||
// result = ADCL;
|
||||
// result |= ADCH<<8;
|
||||
// result = 1126400L / result; // Back-calculate Vcc in mV
|
||||
// // ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
|
||||
//
|
||||
// // analogReference(DEFAULT);
|
||||
//
|
||||
// return result; // Vcc in millivolts
|
||||
//}
|
||||
Reference in New Issue
Block a user