Recording of Kwh

 In Emon, I have found this code;

//if (millis()-wtime>10000) {realPower = realPower + 100.0; wtime = millis();}
//delay(1000);
lwhtime = whtime;
whtime = millis();
whInc = realPower * ((whtime-lwhtime)/3600000.0);

Presumably its to calculate power usage, but the first part of the code is commented out - //

Could this be used to calculate power over a 24hr period?

 

 

 

TrystanLea's picture

Re: Recording of Kwh

Hmm not sure why I left that in there, I think its a bit of code to test the whInc calculation, it increase realpower by a 100 every 10s.

A rolling 24 hours period would be quite hard to do on the arduino due to memory size, however its fairly straightforward to do a kwh today, yesterday, day before type calculation.

1) You need to sum whInc:

kwh_today = kwh_today + (whInc/1000);

2) reset kwh_today every 24 hours without a realtime clock we can set this by reseting the arduino around 12 pm

in setup: start_time = millis();

if ((millis() - start_time) > (3600000 * 24)){

  kwh_yesterday = kwh_today;

  kwh_daybefore = kwh_yesterday;

  kwh_today = 0; // RESET

}

 

 

 

Paul Reed's picture

Re: Recording of Kwh

 Hi Trystan,

I can follow most of the code, but not sure how this code deals with the second 24hrs, how does it start again once millis >(3600000*24)??

Please be patient...!!   I'm new to this..

TrystanLea's picture

Re: Recording of Kwh

a yes I see, I forgot one crucial line

after:

if ((millis() - start_time) > (3600000 * 24)){

start_time = millis(); // this line is needed to reset start_time

Is that the bit that did not make sense?

 

Paul Reed's picture

Re: Recording of Kwh

 I get it now thanks.

So start_time is reset to the new millis(), and the process repeats itself.

This will be handy to see what the daily solar power generation is, and now I have a simple menu system to control the LCD, I can call this up at any time, or leave it on permenant display. Great stuff!

Paul Reed's picture

Re: Recording of Kwh

 Hi, have given some thought to what happens after day 49 when millis overflows and is reset, and re-written this section to try and handle it in the background.

 //Power measurement
  t_now = millis();
    if ( t_now > t_last ) {          //check to see if millis has overflowed back to zero (after approx 49 days)
      t_diff = t_now - t_last; }  //if not, the time each emon cycle takes
        else {  //if millis has reset...
          t_diff = 4127; }            //t_diff = (the amount of time each average emon cycle takes), again dealing with the millis reset.
  t_culm = t_culm + t_diff;
  t_last = t_now;

  if ( t_culm > (3600000 * 24)){      //check to see if 24hrs has elapsed
  Solarkwh = 0; // RESET
  Mainskwh = 0; // RESET
  t_culm = 0;   // RESET

It's a bit clunky, and am sure that somebody more skilfull could come up with more elequent code, but it seems to work OK!!

Also added a line in setup, to advance the t_culm by 12hrs on first run - so can start the module at lunchtime, instead of waiting till midnight.

 t_culm = (3600000 * 12); //On first run, advances the 24hr timer by 12hrs (so can reset at midday!)

Guest's picture

Re: Recording of Kwh

OEM n00b here, going on third week of learning Arduino. So far, everything I've tried works! Yay!! Like paulreed, I'm building a controller to strip off excess solar power before it reaches my utility meter (I get charged by the kWh for putting it back into the grid). The system is based on an optical mouse reading an old rotating-disc watthour meter movement, then sending control signals to a 20 ampere motor-driven autotransformer (Variac). This works very well using the (modified) "basicSketchWCal.pde" sketch . Will soon begin prototyping a triac controller, more on that later.

Right now, I'm toying with LCD displays to indicate the amount of power being dumped into the water heater. I'd like to have a daily total of the kWh sent to the heater using the calculated watts. So far, I'm not able to grasp what's necessary to integrate the parts of the emon code into setup() and loop() to do this. Perhaps someone could point me to a sketch with the relevant bits in it so I could copy-paste and attempt to get a working Kwh display? It doesn't have to run for more than 12 hours before the Arduino is reset at the end of the day, so resetting millis() isn't important.

Thanks for any advice, I can reward you with a build thread once I get it working.

Guest's picture

Re: Recording of Kwh

I guess it makes sense to read the form fields before submitting, I thought that the "subject" field was "name". Is this an intellegence test?

Paul Reed's picture

Re: Recording of Kwh

 You need 2 sections adding to the loop, 1) to calculate the time that has passed, and 2) to multiply that time by power to give kwh, so the sketch would be something like;

 

 //Time Calcs
  t_now = millis();
    if ( t_now > t_last ) {   //check to see if millis has overflowed back to zero (after approx 49 days)
      t_diff = t_now - t_last; } //if not, the time each emon cycle takes
        else {  //if millis has reset...
          t_diff = 4127; }   //t_diff = (the amount of time each average emon cycle takes), again dealing with the millis reset.
  t_culm += t_diff;
  t_last = t_now;
 
if ( t_culm > (3600000 * 24)){ //check to see if 24hrs has elapsed and if so reset the kwh to zero
  kwh = 0; // RESET
  t_culm = 0;   // RESET
  }

 

  //Calculate number of kwh generated.
 kwh += ((ch1.realPower)* t_diff) / 3600000 ;

 

You will note that the code will reset the kwh's every day (24hrs after starting the Arduino).
kwh is the variable which tots up the  kw's. 

Guest's picture

Re: Recording of Kwh

After doing quite a lot of clicking around on this site, I eventually found the Emon sketch which I was able to look through and see what I needed to add into my sketch to allow me to use the variables in your example. Since I'm not using the actual emon sketch, but a modified version of the basic sketch, the variables such as t_now, t_diff, etc. will need to be declared before it will compile. I wasn't exactly sure what I'd need unitl I looked through emon.

I'll code things up tonight and see if it works.

(I guess I might comment in the "disoranized" topic thread, that using the Drupal menu structure more effectively might help a lot in navigating and finding things on this site, as would a site map.)

-Sharkey

Mr. Sharkey's picture

Re: Recording of Kwh

I snatched some code from the Emon sketch, pasted parts of it into my prototype program, changed up some of the values (Wh instead of kWh), and put it to the test. With a simulated load of 600 watts (AC adapter, voltage divider, 1k pot), I was able to accumulate 601.4 Wh in an hour, accurate to within .24%. It was a royal pain keeping the current simulation steady, the AC line voltage out here in the sticks must wander all over the place.

Thanks for the attention, like I said previously, everything I try with the Arduino seems to work, I wish all my projects were so easy.

I'll be putting together a build thread in my blog in case anyone is interested in details.

Paul Reed's picture

Re: Recording of Kwh

 Great news, look forward to your build thread.

glyn.hudson's picture

Re: Recording of Kwh

Sounds like good work! Could you post a link to your blog so that we can follow your progress. It's possible to add your blog and twitter to your openenergymonitor account profile.

Looking forward to seeing your progress.

All the best, Glyn.  

Mr. Sharkey's picture

Re: Recording of Kwh

Yep, did that already.

Comment viewing options

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