Impulse counting - overview
26/03/2017
Whether for the visualization of water consumption in real time or that of instantaneous electrical power, the basic principle is the same:

- There is first a sensor that provides the sought information in the form of electronic impulses.
- A process activated by software interruption reads these impulses as fast as possible.
- Another process quietly stores these impulse readings in a database.
- A web server animates a mini internet site that recovers the data and transforms them into information presented in a browser.
The impulses are fleeting; it is important not to miss them and to be able to memorize the instant of their occurrence as precisely as possible.
A simplistic solution consists of recording in a file the total number of impulses counted and the date and time of the last impulse.
However, it's long, relative to the brevity of an impulse. It is indeed necessary: to open the file, read the last value of the counter, recreate a new version of the file with the new value of the counter and the date and time of the last impulse. If the impulses are too close, we could miss some. Moreover, a shift, of a more or less random value, is introduced between the impulse that triggers the treatment and the moment when the time is read.
In addition, it's poor: we only keep the total number of impulses and the date of the last occurrence.
It's for that that it is preferable to do the treatment of the impulses in two steps: a reading as fast as possible (step 2) then, independently and without time execution constraint, the storage of each impulse in a database (step 3). This treatment is done at the initiative of step 4 and, in parallel, it is also done at regular intervals. This allows having the most recent possible information to display and avoiding that a too large number of impulses accumulate before being stored in database if the display is not solicited often enough.
In addition, the storage of all the impulses in a database opens the way to numerous treatments and analyses.