<p>Having some troubles to recover a "Volkszähler" system (server doesn't exists any more), I decided to build my own Smart Meter system,  user friendly and simple to maintenance.</p>
<p><strong>The concept:</strong> S0 Impulses are counted via interrupt on a microcontroller (ESP32) and sent to a Web-server running on a Rasperry Pi. Every time, after 60 seconds, three counter values (for each phase of the power lines) are written into a Round Robin Database (RRD) file. The graph function of RRDtool is used to present this data to a human viewer via Web-Browser.</p>
<p><img class="alignnone size-full wp-image-754" src="https://johann.langhofer.net/wp-content/uploads/2021/04/Screenshot-2021-04-03-232926.jpg" alt="" width="1109" height="458" /></p>
<p><strong>The hardware configuration: </strong>ESP32 -&gt;(WLAN)-&gt;RPI2</p>
<p>Software requirements (RPI): apache2, RRDtool, php7.3, php7.3-gd</p>

<!-- wp:paragraph -->
<p>Skipping the microcontroller part (data is sent with a simple http-request http://&lt;address-of-rpi>/s0.php?data=&lt;counter1>:&lt;counter2>:&lt;counter3>)  here are the software parts of the smartmeter:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>STEP 1:</strong> Defining the database (RRD):</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>rrdtool create power.rrd \<br>
   --start now-2h --step 60s \<br>
   DS:R:GAUGE:5m:0:24000 \<br>
   DS:S:GAUGE:5m:0:24000 \<br>
   DS:T:GAUGE:5m:0:24000 \<br>
   RRA:AVERAGE:0.5:1m:10d \<br>
   RRA:AVERAGE:0.5:1h:18M \<br>
   RRA:AVERAGE:0.5:1d:10y</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Each minute the powerline counter values are written into the database, Format: 'R:S:T'<br>There are three archives in this database: one for the original data (minutes)  holding the values for 10 days, one for reduced hour-values holding 18 months, and one for reduced daily-values holding 10 years.</p>
<!-- /wp:paragraph -->

<p><br /><!--StartFragment--></p>

<!-- wp:paragraph -->
<p><strong>STEP 1:</strong> Defining the database (RRD):</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>rrdtool create power.rrd \<br>
   --start now-2h --step 60s \<br>
   DS:R:GAUGE:5m:0:24000 \<br>
   DS:S:GAUGE:5m:0:24000 \<br>
   DS:T:GAUGE:5m:0:24000 \<br>
   RRA:AVERAGE:0.5:1m:10d \<br>
   RRA:AVERAGE:0.5:1h:18M \<br>
   RRA:AVERAGE:0.5:1d:10y</p>
<!-- /wp:paragraph -->

<p><!--EndFragment--><strong>Step 2</strong>: Script to permanently update the database:</p>

<!-- wp:html -->
<?php
    shell_exec("rrdtool update power.rrd N:".$_GET['data']);
   ?>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p><strong>Step 3</strong>: Script to visualize the data: Call this script with three optional query parameters: start, end, step.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>start: point where to begin viewing data i.e -1d -2M -4h, default 4h (4 hours)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>stop:  point where to stop viewing data (default now)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>step: resolution in seconds (default 60)</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<?php
$pars=<<<EOT
 DEF:R=power.rrd:R:AVERAGE \
 CDEF:RH=R,60,* AREA:RH#f06151:R \
 DEF:S=power.rrd:S:AVERAGE \
 CDEF:SH=S,60,* AREA:SH#f3d24f:S:STACK \
 DEF:T=power.rrd:T:AVERAGE \
 CDEF:TH=T,60,*  AREA:TH#55c0d2:T:STACK\
 CDEF:RS=RH,SH,+ CDEF:RST=RS,TH,+ \
 GPRINT:RST:AVERAGE:"average %2.1lf Wh" \
 GPRINT:RST:LAST:"last %2.1lf Wh"
EOT;
$start=isset($_GET['start'])?"-s ".$_GET['start']." ":" -s -4h ";
$end=isset($_GET['end'])?" -e ".$_GET['end']." ":" ";
$step=isset($_GET['step'])?" -S ".$_GET['step']." ":" ";
shell_exec("rrdtool graph _data.png -w 1200 -h 400 ".$start.$end.$step.$pars);
$im = imagecreatefrompng("_data.png");
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>

<!-- /wp:html -->