Help monitoring 2 phases of AC current only ADS1115 & NodeMCU

If you get back to this by 4pm your time, I’ll probably still be around. Otherwise, it’s tomorrow.

Meanwhile, I’ll try to point out and explain the things you got wrong, and the things I’d do differently.

1. You wrote that you wanted to use emonLib. The way this works is it samples a channel (both voltage and current if you want power, or just the current if you only want current) for a fixed period of time. In the case of power, voltage is available so it can count mains half-cycles and start and stop as close as it can to a zero crossing. In the case of current only, you have to calculate (or measure) and give it the number of samples to record so that you’re as close as you can be to a whole number of mains cycles, otherwise, the rms average will be wrong because of the extra part-cycle – and you can’t define the starting or ending points, so the error is essentially random. Having sampled one channel it switches to the next, and so on. When it’s done all, it dispatches the data to wherever it’s needed. Then the sketch waits (the way we used it) for 9 s or so and then tells it to get the samples for all the channels again.

2. Now, looking at loop(...), you think you’ve got two functions to get the current. In fact you haven’t, because you’ve both declared and defined only one – double calcIrms2(unsigned int Number_of_Samples) { ... }. You declared double calcIrms1(unsigned int Number_of_Samples); but that’s all, there is no definition of what it does – there is no pair of curly brackets with code between them that defines how it does anything.
This is what "undefined reference to _Z9calcIrms1j "is trying to tell you (but the name has got mangled).

3. Inside calcIrms2(...), you’re trying to sample both samples sequentially. This would be OK, but (a) it’s not how emonLib works and more seriously, calcIrms2(...) is defined as returning a double – A double, one, not two, so

  return Irms1;
  return Irms2;

is illegal because you can never return two separate things from a function. To do this, you’d have to either use global variables (these are values that exist for all time all over the sketch, which is messy and generally not recommended) or you’d need to use an array or pointers, and that’s probably too advanced for you at the moment.

The other, and best, reason for not doing both legs in parallel is your sample rate from the ADS1115 is slow, 860 samples per second, to you that’s 14.33 samples per mains cycle. If you’re doing two channels, that’s only 7.167 samples per cycle. You’ll be in trouble if there’s anything above the third harmonic in your current wave. If you have a current wave like this spiky current waveform (Washing machine brushless d.c. motor on spin by @dBC

Phase measurement and correction in IoTaWatt - #17 by dBC)

or this most distorted channel made up of a boat load of CFLs:

(from The importance of continuous sampling Red is the current, also thanks to @dBC)
Just consider how much of the wave you’ll sample, or not sample, with only 14 - let alone just 7 sampling points - on each cycle.

1 Like