telling when energy is being exported

I am using the code below but only get positive number whether I am importing or exporting power to the grid. Any advice on what i am doing wrong, probably trivial as I am rather a begiiner in this area

 

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

emon2.current(3, 111.1); 

....

double Irms = emon1.calcIrms(1480);  // Calculate Irms only
emon1.calcVI(20,2000);          // Calculate all. No.of crossings, time-out

double Irmsfeed = emon2.calcIrms(1480);  // Calculate Irms only

emontx.powersolar=Irms*230;
emontx.powerfeed=emon1.realPower;

Robert Wall's picture

Re: telling when energy is being exported

Power direction depends on the relative phases of the current and the voltage - read about it in Building Blocks - An introduction to AC Power

Your sketch has no knowledge of the voltage, hence it cannot know the relationship between current and voltage. You need to look at one of the CT123_voltage example sketches - and measure the voltage as well as the current.

Da01769's picture

Re: telling when energy is being exported

Robert,

many thanks for the suggestion, I have read both the intro and CT123 but am still confused. In my script I have the line

emon1.voltage(2, 234.26, 1.7);

does this not do what is required? Sorry if I am being stupid here but I am struggling with how the libraries are invoked. I assumed the the calc in calcirms invoked what I needed. I have run the CT123 demo script but do not get meaningful, at least to me, output.  What have I missed here?

 

Dave

 

Robert Wall's picture

Re: telling when energy is being exported

Take a look at emonTx_CT123_Voltage which you can get from Github

In the emonLib library, in the EnergyMonitor there are two principal methods that do the donkey work:

    void calcVI(int wavelengths, int timeout, long refCal=1126400L);
    double calcIrms(int NUMBER_OF_SAMPLES, long refCal=1126400L);

and some supporting methods that set up the variables:

    void voltage(int _inPinV, double _VCAL, double _PHASECAL);
    void current(int _inPinI, double _ICAL);

    void voltageTX(double _VCAL, double _PHASECAL);
    void currentTX(int _channel, double _ICAL);

    void voltageTX3(double _VCAL, double _PHASECAL);
    void currentTX3(int _channel, double _ICAL);

You are using calcIrms, the one that calculates rms current. If you read through the code in EmonLib.cpp, you'll see that nowhere is the voltage used.

Now look at calcVI(....), and you'll see that - like the graphs in the BB article, each sample of current is multiplied by the corresponding voltage sample to give you power. Hence if voltage and current are the same sign, power is positive and if they are the opposite sign, power is negative.

You do have an ac-ac voltage adapter connected to your voltage input, don't you?

Da01769's picture

Re: telling when energy is being exported

I do have a ac-ac voltage adapter connected and thanks for the explanation of why calcirms does not work. Now with calcirms I assign the answer in the invocation ( irms = calcirms ...) but for ClacVI i just invoke the routine, how do I reference the results, in particular the realpower with its associated direction indication. This is where I think my main problem in grasping how things for together lies, too many years since I programmed seriously.

 

Dave

Robert Wall's picture

Re: telling when energy is being exported

Look at emonTx_CT123_Voltage.ino and the line immediately after calcVI(...)

realPower is a property of the EnergyMonitor class - calcVI gives it a value and there it sits until it's changed. That line fishes it out and assigns the value to the left-hand side. You could equally well do
  Serial.print([whatever].realPower);
or indeed any of the other properties listed as public in emonLib.h, apparentPower, powerFactor, Vrms, Irms.
[whatever] is the name you gave to the class instance, emon1 or emon2 in your case. You almost had this in the last line of your first post.

[Edit: Robin - don't confuse the poor guy at this stage, he's fumbling with getting his first sketch to work.]

 

calypso_rae's picture

Re: telling when energy is being exported

Once you have a working voltage sensor and current sensor, you may find it helpful to run some of the simple sketches which are listed near the top of my Summary Page.  They may seem trivial, but they can really show you what's going on at a low level. 

Using MinAndMaxValues, you are actually seeing the raw sample values from your ADC.  The later version, MinMaxAndRangeChecker, repeatedly displays these values for each of the first four analogue channels.  Your V and I sensors are likely to be using two of these channels, but the other two channels will be floating so beware of spurious values from them.

RawSamplesTool can provide a simple graphical display on your Serial Monitor of your voltage and current waveforms.  This is a really useful tool to run on any new front-end hardware.   If you don't have EmonLib handy (which it doesn't actually use), the later version RawSamplesTool_4ss_2 may be better.  I used that one myself only yesterday.

Any of my Mk2 PV Router sketches will allow you to measure continuously the flow of energy.  Mk2_PV_Router_mini_3 might be a good one to try first.  Every second, it displays how many Joules have been recorded.  It should work fine with just the input hardware, but it does require the AC voltage source to be present, as do most of my sketches.

Another useful sketch which I'd almost forgotten about is DigitalMeterEmulator.  This gives a 40 mS pulse every 3600 Joules which is what most digital electricity meters do.  If you happen to have a spare 1000 pulses-per-kWh meter which you can connect in series, calibration then becomes very straightforward.  When both LEDs are flashing at the same rate, your calibration is perfect :D

Edit - Many people have told me that the simple tools that I've posted have been really helpful to get their systems up and running.  I look forward to seeing how this thread develops ...

Da01769's picture

Re: telling when energy is being exported

many thanks for the help with my sketch, I think I now have direction of current and am going to let it run for a while to collect some data. I will then try some of the suggested stuff to both calibrate and refine what i am collecting. I think I am starting to get the hang of how things link together, fairly simple once grasped but really very confusing until you see the linkages between the setup section, the loop section and the libraries. 

Dave

 

Comment viewing options

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