The blog is read most by people that is not from Spain so … although I don’t like too much the idea of beginning to write in English (because my English is incredibly awesome, you will discover it) … I think it is a ‘must do’ … so how’s your English then?? xD
Last time I wrote about power consumption management system in my house I started manipulating files, arrays of characters, editing configuration files, and so on… and then I decided that I was wasting my time because I was programing a data/files management system without having any data adquiring system or even data!
Well, I have been programming since then a system with which I could store power data from everyday in a little bit organised style.
First of all I made a little modification to the program that gives all the data when capturing current (A) waveform. This program normally gave me all this info:
The thing is that, for adquiring information about the power consumption I don’t need the graph, the number of samples, time, current peak, … Indeed, I only need ‘Potencia’ (power) value.
The modification I made to have only the power value, was to insert input parameters into the C program I had made before:
As you can appreciate in the screen capture I ‘filtered’ the additional information with ‘argc’ input parameter in ‘main’. In addition to that, if the parameter introduced when we make the call to the program is “pg”, I give only power value and also I generate the graphic of current.
I also made a little help screen so as to remember how to work with this program. We get to this information by typing the parameter ‘?’ on the program call. It returns this:
- Parameter ‘p’ gives only the actual power value in W.
- Parameter ‘pg’ gives actual power value in W and generates current graphic.
- No parameters gives all information and generates current graphic
OK then, this was the first step to start generating power information data files, now I would need to make another program that catches data every X seconds and stores all this information in files.
The initial idea was to create files with the actual day information on its name like this:
(The format is in Spanish DD/MM/YYYY). I was tempted to do it in Basque as it is the normal format I use (YYYY/MM/DD) but day-month-year format I think is easier for this context.
So, inside that .txt file the information would be stored in columns:
First column would be time of day and second column is the Wh at that moment. For example, 16.13 with 45.62 Wh means that from 15.13 to 16.13 we have a power consumption of 45.6 W.
This would be OK but I don’t like it too much because my idea was to store this data in a file with the format:
… and also the information on every specific time of day like this:
Probably your are thinking that storing the data with that filename is not complex, and indeed you’re right, complexity = 0… but, what would happen when you have to access to that specific hour/minute of the day to show the informatio? How could you know if you have to write in ‘HHMM’ 1613, 1713, 2134, …?
So the solution is easy: force MM to be ALWAYS ’00’:
I created this function that generates a string with the specific path and name of the data file. Parameters ‘crear_ruta’ and ‘crear_ruta_hora’ will indicate this function if the returning string has in the name the actua day and also the hour or if it has only the actual day. The comment there ‘Para que se cree el fichero ….’ means: This is to force the data file to has ceros in the minute field in all cases’.
So after all this I have only created a file ‘pathing’.Let’s start with the rest of the program.
I created in the beginning of the program a ‘samples’ (muestra) tag. In my case this is 360, and it is configurable. For example, if you set 3600 this will store data every second, with 360 it will be every 10 seconds, and so on … The next screen capture shows this constant value in the beginning of the program:
(360 samples means 1 sample every 10 seconds in 1 hour; 3600 samples would be 1 sample every second in 1 hour).
Now we go to main program structure.
Here you can see the tags declaration. There are timing variables, buffering strings, double data arrays for storing power reads. Remember that ‘muestra’ means ‘sample’ in Spanish.
We create the file that will contain Wh every hour in the beginning as you see in the previous capture. We open with “a” so as not to overwrite if the file exists and append the information in the end of it.
And finally we start with the main loop.
As you see it is infinite loop that is catching the time data the whole time (first paragraph). Second paragraph named ‘CHEQUEAMOS EL DIA….’ means ‘we check that the actual day has not finished’. With this we detect a flank if the day is increasing so as to increase the path file for the new day.
I got the previous piece of code from the internet as I did not really know how to get the output data of an executable file to come into another program. I thought that it coul be done by ‘./lecturaA p > potencia.dat‘ and then read ‘potencia.dat’ file but I thought there must be another more elegant method … so that is what I found and it works!.
‘lecturaA’ is the executable file I modified in the beginning. With ‘p’ parameter I get only the actual power read.
Finally this bunch of code. Lots of letters, but don’t worry, most of them are comments :P. What I do here (I remind you we are inside the while loop yet) is checking if the counter is equal to the constant defined value ‘samples’ (muestras).
Normally we will be storing power data and timing data so if we pay attention to what is under ‘most-left-else’ we see an ‘IF’ that is checking that we read correctly the power value. If the reading is good it assigns ‘potencia’ (power) value we catched before. If not read a valid number it assigns 44.0 W by defect. (Need to check this last thing as my current sensor does not read less than 44 W On low currents the information is not accurate).
The sprintf stores the time information in the 2D array I created in the beginning and counter (contador) increases by 1.
When ‘contador’ gets to ‘muestras-1’ then we store the information in 2 different files. The first one is the one I created before with the data ‘dat_DD_MM_YYYY.txt’ and what I make is a sum of all power data (360 values) and divide them by ‘muestras’ => this value will be the ‘Wh‘ in the just passed hour. We store this with the hour information with ’00’ in the minute (as told before, I don’t really care about the minute as I could only get troubles with it).
Next I store ALL samples taken (360 in my case) in a file ‘dat_DD_MM_YYYY_HH00.txt’ I create with the function I described before (fc_crear_ruta….). I create, then record all data and then close the file so as to restart the process again.
As you see I set ‘contador’ to 0 and store the actual power read as indeed a power has been read but I have been storing the previous ones but I need to do this for first power data within the next hour.
And YES, the code that is under ‘contador = 0’ should have been written before the ‘sleep’ as it is common for both xDDD. I will make this modification as it is very easy right now, don’t get angry…
Ok, I think I have written too much today … Next time I will talk about plotting all this information and get a monthly report of power consumption.