Hello all,
I'm programming on IDE since a while but I'm not a programmer rather an electronic.
I'm blocked by a programming problem I cannot solve by myselfe in a good way.
I have an Arduino mega reading the house digital electricity meter. I can extract the value of the instant powwer and the total power consumption.
I have the NTP time data available via WiFi connection by using the NTPClient.h library of a serial connected esp8266.
So far so good.
Then I had the idea to implement a new function.
Based on the available data, how can I calculate the consumption of the last actual 24 hours? This value shall be continuously (every minute) updated to reflect the the last 24 hours at any given moment.
Is there a simple function or math I can use for such a calculation?
Thank you for helping.
To measure daily total consumption, save the value of the total power consumption at the beginning of the 24 hour interval, and subtract that saved value from the total power consumption at the end of the interval.
Thank you for your answer. That was easy.
The problem is that I want to have a dynamic 24 hours consumption. This means that based on your suggestion, I should save 1440 values (1 every minute) in 1440 variables.
I want to have the last 24hours consumption at any moment.
If now the CET time is 18:15, I want to display the consumption between yesterday 18:15 and today 18:15 (24houras)
Later, e.g. at 8pm, I want to display again the 24hours consumption between yesterday 8pm and today 8pm.
I hope I was more clear now.
For a rolling total, the simplest approach is to save all the intermediate values and sum them when required. A circular buffer is ideal for that.
You can optimize the calculation by realizing that you need only subtract from the total the earliest (soon to be discarded) value, and add in and store the most recent value.
total -= reading [index];
total += reading [index++] = newReading;
if (index >= 1440)
index = 0;
First of all thank you because the use of the correct terminology is the key for an appropriate search and I was missing both of them.
I don't know what a circular buffer is and how it works but at least I now know what i have to look for:
Rolling total and circular buffer
Thank you
Thank you too.
First I will try to understand this very simple code (without understanding there is no fun) then I will adapt it and implement it.
P.S. I was getting mad since days and to see how simple the solution is, it is quite frustrating
Don't let the name confuse you. It is neither circular nor a buffer. IT is an array that you add and remove individual entries to. You keep track of the index for the next entry to be added to the array. You can also remove the oldest entry and keep track of the index for the next oldest so it can also be removed. When the next to be removed index is equal to the next to be added, the array is empty. There is much more that can be done, but that will take away the mystery.
It absolutely is a buffer.
Yep, a buffer, and access to its contents is usually "circular", in either direction (last in, first out or first in, last out).
Buffer is not a key word in "C". But array is.
No, it isn't.
Nice try.
(Someone may well mark this post as "spam", and I'll get an anodyne mail from @system informing me that the post has been deleted, and that I should read the community guidelines (I have), but I believe in the Reithian principles of "educate, inform and entertain", unlike some here.)
I tried it but unfortunately this is not what I exactly need or may be I didn't uderstand it properly. This code is making the sum of the values I read providing a rolling sum of all the values I have read in the given time period (eg 24h for 1440 values read every minute).
I don't need the sum of them. I read an electricity meter which provides me the total consumption, not the instant consumption.
I need to find a way where I read the last value xxx kWh and I subtract the yyy kWh I had 24 hours before and so on every minute to have the delta between the read out now and the read out of 24h before.
Anyway, I understood how to use an array and I guess I can now solve my problem.
So, modify it to your needs...
I think the code is a little obscure for newbies as it does multiple things on one line. This is equivalent(I hope):
total -= reading[index];
reading[index] = newReading;
total += reading[index];
index++;
if (index >= 1440) index = 0;
Maybe this is easier to understand...
Basically you can simply subtract the number that is about to leave the buffer from your newest number (as your numbers are already totals...).
The total consumption over the last 24 h is the sum of the last 1440 "instant consumptions", read once per minute. Technically, a scale factor may be needed to convert to kWh (one minute is 1/60 of a hour).
If what you get from the meter is the average power consumed over the last minute, then the code is correct. If not, what is it that you get from the meter?
This is what it is hard to explain. My electricity meter gives me already the accumulated total consumption. It is a classic house digital meter provided by the energy supplier.
Yes, I can read the instant consumption, but I can also read the total accumulated consumption since it was installed. With total accumulated consumption I mean those numbers we have to report annually to our energy provider. I want therefore make a calculation based on the total consumption as appearing and constatly increasing on the meter.
I have probably understood how to do i with the array.
I register in the array the meter value every minute. After 24 hours the array is full then I make the following math.
reading[index] = newReading //I store the current read in the array with the current index value
24h consumption = reading[index] - reading[index+1]
I subtract to the reading[index] the oldest value in the array which is index+1
In this way I calculate the delta between the newest and the oldest value in the array and I obtain the difference in 24h.
then
index++;
if (index >= 1440) index = 0;
I'm testing it now and I hope it will work.
reading[index+1]
will be nonsense, if you run off the end of the declared array.
you are right. Altought it will be nonsense for 1 read only, I can make it in this way.
STROM_24h = (reading[index] - reading[index + 1] + arraySize) % arraySize;
(index+1)%arraySize
automatically wraps around. Just be careful!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.