RESOLVED - EmonTX voltage error

I have had the Stontronics AC adaptor connected to my emontx for about 6 months with the calibration value of 238.56, and all had been fine.

A few days ago I changed the adaptor for a Mascot 9580 with value 234.26 as I needed another AC adaptor to use elsewhere, and I thought I'd fit the latest OEM supplied adaptor to my emonTX as I believe it's supposed to be slightly better(?).

Since I have done this the system is indicating 163.5 V (Serial Print)  instead of a correct 245 V.  But when I change everything back to the original Stontronics adaptor it is also now indicating 163.5 V - and I only changed the calibration value and adaptor.

Just incase I inadvertently altered something else I have just downloaded a fresh copy of the latest emonTX from Github and it is the same, 163.5V.  I also updated the emonlib.

 

Does anyone have any suggestions on what I may have done wrong?

 

Thanks,  Dennis

edllew's picture

Re: RESOLVED - EmonTX voltage error

I just had a similar problem (yesterday), and it turned out that EnergyMonitor::readVcc() was not building correctly for me.

It is a #define problem.  __AVR_ATmega328__ seems not to be is not properly #defined, so the snippet:

  #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__)
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 
  #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #endif

is not getting compiled at all I think.  There really should be an additional #elif for that potential compile-time error case.

I just hardwired it to build correctly for me.  I meant to mention this on the forums here but just got around to it now when this post reminded me.

This may be particular to my system (linux-Ubuntu 12.04, Arduino 1.0.1).  I am not familiar with the Arduino build environment internals, and I have not tried to track down the problem yet.  I do have board set correctly in the IDE, I believe.

-Ed

logic's picture

Re: RESOLVED - EmonTX voltage error

Although I don't fully understand all of whats going on with those files, I have had a look at them and see where it is, and that there is no #define in the sketch.

I've tried adding a few #define __AVR_ATmega328__ and variations of it, but as I don't really know what I'd doing then nothing seems to work.

Thanks for your help

Further to my initial post I remembered I had a 2nd emonTX which I have tried to rule out a hardware problem.  It was the same.

 

Dennis

 

edllew's picture

Re: RESOLVED - EmonTX voltage error

Sorry , I should have suggested replacing the code I showed above with the following (adding the #else and #error):

  #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__)
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 
  #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #else
  #error "missing __AVR__ type define in readVcc()"
  #endif

Then if the compile fails, you know you have the same problem I do, otherwise not.

Good luck.

glyn.hudson's picture

Re: RESOLVED - EmonTX voltage error

Sorry guys, I think I've fixed the problem in emonLib. Could you confirm that the latest version of emonlib from GitHub works well for you. 

@logic the Mascot adapter is no 'better' than the Stontronics one, it's just different. There is a good chance that it could be marginally worse since it's a smaller adapter. However we had the Mascot adapter extensively tested (thanks to Robert Wall) and it's performance is adequate. We are unable to source the Stontronics adapter anymore. There shouldn't be a noticeable difference between the two adapters in use. 

logic's picture

Re: RESOLVED - EmonTX voltage error

Thanks

Unfortunately the updated emonlib has not corrected the voltage reading.  Thanks for the info on the adaptors, which I now realise was incidental to the program going wrong as I must have updated the libraries all at the same time.

edllew, thanks for that info, but I think I'd be better leaving all alone in there as I'm not really too sure what I'm doing with that define stuff.

 

Dennis

ianjm's picture

Re: RESOLVED - EmonTX voltage error

I have the same problem as Dennis

My EmonTx had died (builders killed it with dust!). 
Just got round to replacing it with a new one yesterday - took the opportunity to download all the latest libraries at the same time and the latest emonTX.
I am using emonTx_CT123_Voltage - currently have ct3 disabled and set to use RF12_868MHZ
Not changed anything else.

Powers are showing about half what they should be and voltage is showing around 162.00

Any ideas why this is happening

Ian

glyn.hudson's picture

Re: RESOLVED - EmonTX voltage error

Oh dear, I can't see why this isnt working. The changes I made were to add support for different ATmega micro controllers to enabling the reading of the internal supply voltage of the chip, this voltage is used in the power calculation to account for running the emonTx off batteries which get slowly depleted.

 

I changed

long EnergyMonitor::readVcc() {
  long result;
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2);
  ADCSRA |= _BV(ADSC);
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result;
  return result;
}

to

long EnergyMonitor::readVcc() {
  long result;
  #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__)
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #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);
  #endif
  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;   //1100mV*1024 ADC steps http://openenergymonitor.org/emon/node/1186
  return result;
}

 

In EmonLib.cpp. I have attached the old EmonLib.cpp file. Try using this instead, you will need to replace the EmonLib.cpp file in your emonlib arduino library folder with this one:

ianjm's picture

Re: RESOLVED - EmonTX voltage error

Hi All

I have replaced the new emonlib with one I downloaded in september that does not have the new code in

Voltages are now back to normal 238 

Ian

glyn.hudson's picture

Re: RESOLVED - EmonTX voltage error

Ah, I think Robert Wall has just alerted me in an email. I missed the 'P' off the ATmega328p

This should have fixed the problem:

#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__)
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #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);
  #endif

The changes have been pushed to github. Could someone please confirm if this iw working for you. 

Cheers, 

ianjm's picture

Re: RESOLVED - EmonTX voltage error

Hi All

Have downloaded the latest emonlib and it has resolved the problem with the voltage

Ian

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.