Skip to content

Instantly share code, notes, and snippets.

@vladakru
Last active April 23, 2020 17:48
Show Gist options
  • Save vladakru/736acc32e3dff1af675e65c9eed41766 to your computer and use it in GitHub Desktop.
Save vladakru/736acc32e3dff1af675e65c9eed41766 to your computer and use it in GitHub Desktop.
Sketch za Arduino - Home Energy Monitor
#include <Esp8266.h>
#include "EmonLib.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(3, 2); /* RX:D3, TX:D2 */
// Kalibracija Senzora
#define SAMPLE_COUNT 10
#define VOLT_CAL 258.7 //voltage calibration
#define CURRENT_CAL1 61.5 //sensor 1 calibration -- R
#define CURRENT_CAL2 61.5 //sensor 2 calibration -- S
#define CURRENT_CAL3 61.5 //sensor 3 calibration -- T
//create 2 instances of the energy monitor lib
EnergyMonitor emon1;
EnergyMonitor emon2;
EnergyMonitor emon3;
//arrays to hold the sample data
double volts1[SAMPLE_COUNT];
double amps1[SAMPLE_COUNT];
double watts1[SAMPLE_COUNT];
double volts2[SAMPLE_COUNT];
double amps2[SAMPLE_COUNT];
double watts2[SAMPLE_COUNT];
double volts3[SAMPLE_COUNT];
double amps3[SAMPLE_COUNT];
double watts3[SAMPLE_COUNT];
const int currentPin1 = 1;
const int currentPin2 = 2;
const int currentPin3 = 3;
const int voltagePin = 0;
//counter to keep track of the current sample location
int counter = 0;
void setup()
{
Serial.begin(115200);
mySerial1.begin(9600);
emon1.voltage(voltagePin, VOLT_CAL, 1.7); // Voltage: input pin, calibration, phase_shift
emon1.current(currentPin1, CURRENT_CAL1); // Current: input pin, calibration.
emon2.voltage(voltagePin, VOLT_CAL, 1.7); // Voltage: input pin, calibration, phase_shift
emon2.current(currentPin2, CURRENT_CAL2); // Current: input pin, calibration.
emon3.voltage(voltagePin, VOLT_CAL, 1.7); // Voltage: input pin, calibration, phase_shift
emon3.current(currentPin3, CURRENT_CAL3); // Current: input pin, calibration.
}
void loop()
{
//reset the var that keeps track of the number of samples taken
//(loop back around to 0 on the array for our running total)
if(counter >= SAMPLE_COUNT){
counter = 0;
}
//calculate the most recent readings
emon1.calcVI(20,5000);
emon2.calcVI(20,5000);
emon3.calcVI(20,5000);
//save the voltage, current, watts to the array for later averaging
amps1[counter] = emon1.Irms;
volts1[counter] = emon1.Vrms;
watts1[counter] = emon1.Vrms * emon1.Irms;
amps2[counter] = emon2.Irms;
volts2[counter] = emon2.Vrms;
watts2[counter] = emon2.Vrms * emon2.Irms;
amps3[counter] = emon3.Irms;
volts3[counter] = emon3.Vrms;
watts3[counter] = emon3.Vrms * emon3.Irms;
counter++;
//setup the vars to be averaged
double wattAvg1 = 0;
double voltAvg1 = 0;
double ampAvg1 = 0;
double wattAvg2 = 0;
double voltAvg2 = 0;
double ampAvg2 = 0;
double wattAvg3 = 0;
double voltAvg3 = 0;
double ampAvg3 = 0;
//add em up for averaging
for(int i = 0; i < SAMPLE_COUNT; i++){
wattAvg1 += watts1[i];
voltAvg1 += volts1[i];
ampAvg1 += amps1[i];
wattAvg2 += watts2[i];
voltAvg2 += volts2[i];
ampAvg2 += amps2[i];
wattAvg3 += watts3[i];
voltAvg3 += volts3[i];
ampAvg3 += amps3[i];
}
//get the final average by dividing by the # of samples
wattAvg1 /= SAMPLE_COUNT;
ampAvg1 /= SAMPLE_COUNT;
voltAvg1 /= SAMPLE_COUNT;
wattAvg2 /= SAMPLE_COUNT;
ampAvg2 /= SAMPLE_COUNT;
voltAvg2 /= SAMPLE_COUNT;
wattAvg3 /= SAMPLE_COUNT;
ampAvg3 /= SAMPLE_COUNT;
voltAvg3 /= SAMPLE_COUNT;
//calculate the total amps and watts
double totalAmp = ampAvg1 + ampAvg2 + ampAvg3;
double totalWatt = wattAvg1 + wattAvg2 + wattAvg3;
if (voltAvg1 <= 30){
voltAvg1 = 0;
}
if(totalAmp <= 0.35){
totalAmp = 0;
}
if(totalWatt <= 80){
totalWatt = 0;
}
Serial.println("Voltage:");
Serial.println(voltAvg1);
Serial.println("Current:");
Serial.println(totalAmp);
Serial.println("Watt:");
Serial.println(totalWatt);
Serial.println(" ");
//send the power info to the ESP module through Serial1
sendPowerInfo (voltAvg1, totalAmp, totalWatt);
}
//send the power info to the ESP module through Serial1 (comma separated and starting with *)
void sendPowerInfo(double Volts, double Amps, double Watts){
mySerial1.print("*");
mySerial1.print(Volts);
mySerial1.print(",");
mySerial1.print(Amps);
mySerial1.print(",");
mySerial1.println(Watts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment