5 second intervals between EmonLib samples?

Hi guys,
Im thinking of sampling once every 5 seconds before transmitting the data from emonlib.
At the moment it samples roughly every 1-2 seconds. Is it possible to adjust the samping time as id rather samle for loger as opposed to delaying to make up the 5 seconds?

You can do that. The original parameters were chosen with battery operation in mind, so that taking a 200 ms sample and sleeping for 10 s prolonged battery life. You can set whatever sample duration and repetition rate you like (within reason - I haven’t calculated where the numbers overflow), but remember that the inputs are sampled sequentially, and emoncms.org will only accept data once every 10 s. Your own server can be set up however you wish.

The sampling period is set in the call to either calcIrms(Number of samples) or in the call to calcVI(crossings, timeout).
The inherent sample rate (when I last checked it) for calcIrms was approx 5588 samples per second. For calcVI, “crossings” is the number of mains zero crossings, they come at the rate of 100 per second in the 50 Hz world.
If you have an emonTx with 4 input channels in use , you can’t sample for much more than 1 s (100 crossings) per channel, because you must leave time to send the data.

Hi robert,
in the calcvi it is currently set at 20,2000 so that 20 number of half cycles with a 2000mS timeout.
Sorry I meant to say im just using the Emolib with a mega thats plugged into the USB.
Im not actually using a emonTX or the CMS. I am sending the data locally over serial with a timestamp from an RTC to hyperTerminal and recording it. Rather than delaying I would rather change the sampling to 5 seconds if possible using the calcVI function call.
Thanks for your support, its always appreciated.

In that case, if you use calcIrms( ), you’ll need to work out the sample rate for yourself. The way I do it is record the time before and after calling the function, then change the number of samples and record a new time. Then take the difference in the number of samples and the time difference to work out the rate (gets rid of overheads that way - assuming they don’t change significantly).

Hi Robert,
I read the archived document you replied to in the past about doing this.
I believe it was this code to work out the time which roughly worked out at 200ms.

unsigned long timethen = millis();
double Irms = emon1.calcIrms(1172);  // Calculate Irms only
Serial.print("Time ");Serial.print(millis() - timethen);Serial.print(" ");

But that example was for current only.
doesnt calcVI do both current and voltage like I need?
I am also calculating VA, PF and real power.
I am using the standard emon ct and vt code but with a few tweaks to do other things like transmit over MQTT.

void setup()
  emon1.voltage(2, 234.26, 1.7);  // Voltage: input pin, calibration, phase_shift
  emon1.current(1, 111.1);       // Current: input pin, calibration.

void loop()
  emon1.calcVI(20,2000);         // Calculate all. No.of half wavelengths (crossings), time-out
  emon1.serialprint();           // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)
  float realPower       = emon1.realPower;        //extract Real Power into variable
  float apparentPower   = emon1.apparentPower;    //extract Apparent Power into variable
  float powerFActor     = emon1.powerFactor;      //extract Power Factor into Variable
  float supplyVoltage   = emon1.Vrms;             //extract Vrms into Variable
  float Irms            = emon1.Irms;             //extract Irms into Variable


I think you’re missing an important distinction between the way calcVI( ) and calcIrms( ) work.

calcVI( ) looks at both current and voltage. The voltage wave is (relatively) an unchanging, predictable, well-shaped wave. It’s possible to detect the cycles and by counting the number of zero crossings, you can determine the sampling period to a reasonable degree of accuracy - to within one sample.
Using an Arduino Mega changes nothing - except the rate at which individual pairs of samples are taken, and that doesn’t matter to you. You still count the mains zero crossings and time your 5 seconds from that.

But in the beginning, you didn’t say which part of emonLib you were using, so I answered the question for calcIrms( ) as well, then offered the information on how to obtain an accurate timing for that. Others reading this in the future might want to know that. If you don’t want to use it, that’s OK.

1 Like

Thats perfect Robert, once again thank you!!