Page 1 of 6

Integrate EmonLib (energy monitor) library to ESP

PostPosted: Sun May 17, 2015 6:28 pm
by juanpintom
Hi all. I started to integrate this library on ESP:


Im using this example:

And just 2 functions need to be adapted:
Code: Select alldouble EnergyMonitor::calcIrms(unsigned int Number_of_Samples)
   #if defined emonTxV3
   int SupplyVoltage=3300;
   int SupplyVoltage = readVcc();

  for (unsigned int n = 0; n < Number_of_Samples; n++)
    sampleI = analogRead(inPinI);

    // 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);
   filteredI = sampleI - offsetI;

    // Root-mean-square method current
    // 1) square current values
    sqI = filteredI * filteredI;
    // 2) sum
    sumI += sqI;

  double I_RATIO = ICAL *((SupplyVoltage/1000.0) / (ADC_COUNTS));
  Irms = I_RATIO * sqrt(sumI / Number_of_Samples);

  //Reset accumulators
  sumI = 0;
  return Irms;

long EnergyMonitor::readVcc() {
  long result;
  //not used on emonTx V3 - as Vcc is always 3.3V - eliminates bandgap error and need for calibration http://harizanov.com/2013/09/thoughts-on-avr-adc-accuracy/

  #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 
  #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB1286__)
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  ADCSRB &= ~_BV(MUX5);   // Without this the function always returns -1 on the ATmega2560 http://openenergymonitor.org/emon/node/2253#comment-11432
  #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  ADMUX = _BV(MUX5) | _BV(MUX0);
  #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  ADMUX = _BV(MUX3) | _BV(MUX2);

  #if defined(__AVR__)
  delay(2);                                        // Wait for Vref to settle
  ADCSRA |= _BV(ADSC);                             // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = READVCC_CALIBRATION_CONST / result;  //1100mV*1024 ADC steps http://openenergymonitor.org/emon/node/1186
  return result;
 #elif defined(__arm__)
  return (3300);                                  //Arduino Due
  return (3300);                                  //Guess that other un-supported architectures will be running a 3.3V!

I'll work on it, but any help is really appreciated :)

This is one important step to make our project better, I collaborate on a opensource framework called Souliss, and on the last update we've the framework working over an ESP using your awesome work with the ARDUINO IDE-ESP.

Thanks in advance.


Re: Integrate EmonLib (energy monitor) library to ESP

PostPosted: Tue May 19, 2015 11:49 am
by gambituk
I would be interested in seeing the results of this.



Re: Integrate EmonLib (energy monitor) library to ESP

PostPosted: Tue May 19, 2015 12:48 pm
by juanpintom
I maded a first step, I modified the library to call getVcc(void); instead the function upside. This return a reference value to take a read from the ADC pin of ESP.
https://github.com/esp8266/Arduino/blob ... 8266/Esp.h

Now the next step is modify the voltage divider of emoncms to a 0-1v (scheme attached) I actually have 10k on RVD and 220ohm on burden. Now with the ESP the vIn is 3.3v. I use with this scheme and 5v Vin ofr Arduino and calibrate emonlib with this line:
Code: Select allemon1.current(Sensor2_pin, 10);       // Current: input pin, calibration.

I need to make some calcs but I think a R1 10k R2 4k7 can work.


Re: Integrate EmonLib (energy monitor) library to ESP

PostPosted: Wed May 20, 2015 7:18 am
by gambituk
ok, so 0-1v because the adc is 10bit and i think only available(broken-out) on esp-12 and esp-07 ? but why do you still need to use the arduino? are you using it just as a power supply or something more? Sorry if i'm a bit slow to understand, i haven't looked at energy meter for a few years..