Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ESP32 #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions EmonLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
modified to use up to 12 bits ADC resolution (ex. Arduino Due)
by [email protected] 26.12.2013
Low Pass filter for offset removal replaces HP filter 1/1/2015 - RW

Modified to use more than 10 bits ADC resolution in more places than was fixed earlier.
by Jm Casler ([email protected]) 2020 05 11
*/

// Proboscide99 10/08/2016 - Added ADMUX settings for ATmega1284 e 1284P (644 / 644P also, but not tested) in readVcc function
Expand Down Expand Up @@ -106,9 +109,9 @@ void EnergyMonitor::calcVI(unsigned int crossings, unsigned int timeout)
// B) Apply digital low pass filters to extract the 2.5 V or 1.65 V dc offset,
// then subtract this - signal is now centred on 0 counts.
//-----------------------------------------------------------------------------
offsetV = offsetV + ((sampleV-offsetV)/1024);
offsetV = offsetV + ((sampleV-offsetV)/ADC_COUNTS);
filteredV = sampleV - offsetV;
offsetI = offsetI + ((sampleI-offsetI)/1024);
offsetI = offsetI + ((sampleI-offsetI)/ADC_COUNTS);
filteredI = sampleI - offsetI;

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -188,7 +191,7 @@ double EnergyMonitor::calcIrms(unsigned int Number_of_Samples)

// Digital low pass filter extracts the 2.5 V or 1.65 V dc offset,
// then subtract this - signal is now centered on 0 counts.
offsetI = (offsetI + (sampleI-offsetI)/1024);
offsetI = (offsetI + (sampleI-offsetI)/ADC_COUNTS);
filteredI = sampleI - offsetI;

// Root-mean-square method current
Expand Down
17 changes: 11 additions & 6 deletions EmonLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
modified to use up to 12 bits ADC resolution (ex. Arduino Due)
by [email protected] 26.12.2013
Low Pass filter for offset removal replaces HP filter 1/1/2015 - RW

Modified to use more than 10 bits ADC resolution in more places than was fixed earlier.
by Jm Casler ([email protected]) 2020 05 11
*/

#ifndef EmonLib_h
Expand Down Expand Up @@ -35,6 +38,8 @@
// otherwise will default to 10 bits, as in regular Arduino-based boards.
#if defined(__arm__)
#define ADC_BITS 12
#elif defined(ESP32)
#define ADC_BITS 12
#else
#define ADC_BITS 10
#endif
Expand All @@ -59,10 +64,10 @@ class EnergyMonitor
long readVcc();
//Useful value variables
double realPower,
apparentPower,
powerFactor,
Vrms,
Irms;
apparentPower,
powerFactor,
Vrms,
Irms;

private:

Expand All @@ -81,14 +86,14 @@ class EnergyMonitor
int sampleV; //sample_ holds the raw analog read value
int sampleI;

double lastFilteredV,filteredV; //Filtered_ is the raw analog value minus the DC offset
double lastFilteredV, filteredV; //Filtered_ is the raw analog value minus the DC offset
double filteredI;
double offsetV; //Low-pass filter output
double offsetI; //Low-pass filter output

double phaseShiftedV; //Holds the calibrated phase shifted voltage.

double sqV,sumV,sqI,sumI,instP,sumP; //sq = squared, sum = Sum, inst = instantaneous
double sqV, sumV, sqI, sumI, instP, sumP; //sq = squared, sum = Sum, inst = instantaneous

int startV; //Instantaneous voltage at start of sample window.

Expand Down