Schotten Tank Niveaumessung

Aus Zovis Wikili
Version vom 19. April 2021, 01:00 Uhr von Zovi (Diskussion | Beiträge) (messung Script Service)
Wechseln zu: Navigation, Suche

Was

Raspi, dass werte mit Python abholt und via Apache2 und php anzeigt. Webseite mit einem Argotunnel ins www stellen.

  • PythonScript holt stündlich den Messwert vom Messgerät und speichert ihn mit einem UnixTimestamp in das /var/www/html/data.txt (1616127492,22178). Es werden nicht mehr als 23 Datensätze gepeichert.
  • Ein anderes PythonScript holt den Messwert alle 10 sek vom Messgerät und überschreibt ihn mit einem UnixTimestamp in das /var/www/html/dataact.txt (1616124562,15178)
  • Auf dem Webserver läuft ein php Script das die Daten aus den Files ausliest und darstellt.
  • Der Argotunnel von Cloudflare leitet die Webseite ins www.

PHP

index.php

/var/www/html/index.php

<?php
$tankinhalt=24000; //in Liter
$umrechner=1000; //liter in m3
?>
<!DOCTYPE html>
<html lang="en">
        <head>
                <meta charset="UTF-8" />
                <<meta http-equiv="refresh" content="10">
                <title>Niveaumessung Schottentank</title>
                <style>
                        body {
                        background-color: #eee;
                        font-family: sans-serif;
                        }
                        .container {
                                background-color: #D3E8FA;
                                display: felx;
                                text-algin: center;
                                justify-content: center;
                                align-items: center;
                                height: 100%;
                                width: 100%;
                        }
                        .centered-element {
                                text-align: center;
                                //width: min(50vw, 4000px);
                                //height: min(50vw, 4000px);
                                border-radius: 5px;
                                background-color: #eee;
                        }
                        @media all and (max-width: 699px) and (min-width: 520px), (max-width: 1151px) {
                                body {
                                        //background: #ccc;
                                        font-size: 36px;
                                }
                        }
                </style>
        </head>
        <body>
                <div class="container">
                        <div class="centered-element">
<?php
echo "<h1>Niveaumessung Schottentank ".$tankinhalt / $umrechner."m3</H1>";

//$pollids  = "data.txt";
//$contents = file_get_contents($pollids);
//$pollfields = explode(',', $contents);



//echo $pollfields[0] .  '<br />'; // Prints the value in first "cell"
//echo $pollfields[1] .  '<br />'; // The second
//echo $pollfields[2] .  '<br />'; // And so on

//echo  '<br /> <br /> <br />0000000000000 <br /> <br /> <br />';

# altes auslesen zeile für zeile und zusammen schnurpfen
/*
$file1 = "data.txt";
$lines = file($file1);
foreach($lines as $line_num => $line)
{
        $linesep = explode(',', $line);
        //echo '<b>Messzeit:</b> '.gmdate("Y.m.d H:i:s ", $linesep[0]);
        if ($linesep[1] == true)
        {
                //echo ' - <b>Wert:</b> '.$linesep[1];
        }
        if ($linesep[2] == true)
        {
                //echo ' - <b>Info:</b> '.$linesep[2];
        }
        //echo "<br>";
}
*/

//# Usläsä us Feil u dividier durch 1000

$file1 = "dataact.txt";
$lines = file($file1);
//echo $lines[0];
//echo '<br />';
foreach($lines as $line_num => $line)
{
        $linesep = explode(',', $line);
}
//echo "linesep: ".$linesep[1];
$messwertliter=$linesep[1];
$messwert=$messwertliter / 1000;


// zeig da schäiss mau a
echo '<br />';
echo '<div>';
echo 'Aktuellester Niveau Messwert vom<br /><b>'.gmdate("Y.m.d - H:i::s", $linesep[0]).'<br /><h1>'.$messwert.' m3</b></h1>';
echo '<br />';
echo ($tankinhalt - $messwertliter) / $umrechner;
echo ' m3 sind noch frei<br /><br />';
echo '</div>';

echo '<img src="graph.php" alt="Fehler beim anzeigen" />';

?>


                        </div>
                </div>
        </body>
</html>



Grafik Modul

/var/www/html/graph.php
<?php
$tankinhalt=24;  // in m3
# File usläsä

$file1 = "data.txt";
$lines = file($file1);
//$valuesold=array('0' => 0);
foreach($lines as $line_num => $line)
{
        $linesep = explode(',', $line);
        $date=gmdate("H", $linesep[0]);
        $linesepcalc= $linesep[1] / 1000;
//      echo $date;
        $values[$date] = $linesepcalc;
//      echo '<b>Messzeit:</b> '.gmdate("Y.m.d H:i:s ", $linesep[0]);
//      echo "<br>";
}

//      print_r($values);


# ------- The graph values in the form of associative array
//$values=array(
//    "Jan" => 110,
//    "Feb" => 130,
//    "Dec" => 196
//);


$img_width=800;
$img_height=400;
$margins=20;


# ---- Find the size of graph by substracting the size of borders
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2;
$img=imagecreate($img_width,$img_height);


$bar_width=8;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);


# -------  Define Colors ----------------
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220);

# ------ Create the border around the graph ------

imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);



# ------- Max value is required to adjust the scale -------
//$max_value=max($values);
$max_value=$tankinhalt; // ersetzt mit $tankinhalt

$ratio= $graph_height/$max_value;


# -------- Create scale and draw horizontal lines  --------
$horizontal_lines=$tankinhalt; // 20 ersetzt $tankinhalt
$horizontal_gap=$graph_height/$horizontal_lines;

for($i=1;$i<=$horizontal_lines;$i++){
    $y=$img_height - $margins - $horizontal_gap * $i ;
    imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
    $v=intval($horizontal_gap * $i /$ratio);
    imagestring($img,0,5,$y-5,$v,$bar_color);

}


# ----------- Draw the bars here ------
for($i=0;$i< $total_bars; $i++){
    # ------ Extract key and value pair from the current pointer position
    list($key,$value)=each($values);
    $x1= $margins + $gap + $i * ($gap+$bar_width) ;
    $x2= $x1 + $bar_width;
    $y1=$margins +$graph_height- intval($value * $ratio) ;
    $y2=$img_height-$margins;
    imagestring($img,0,$x1-8,$y1-10,$value,$bar_color);
    imagestring($img,0,$x1+0,$img_height-15,$key,$bar_color);
    imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}
header("Content-type:image/png");
imagepng($img);

?>



messung Script Service

Service File erstellen

/etc/systemd/system/messen.service

Inhalt:

[Unit]
Description=Messen startup script by STEMOAG

[Service]
ExecStart=/usr/share/scripts/get_data_cx8090.sh start

[Install]
WantedBy=multi-user.target


Service enablen

systemctl enable my-service

Argo Tunnel

Config

/etc/cloudflared/config.yml
hostname: niveau.naranja.li
url: http://localhost:80
logfile: /var/log/cloudflared.log

Zerti

/etc/cloudflared/cert.pem

Service File

/etc/systemd/system/cloudflared.service
[Unit]
Description=Argo Tunnel (%I)
After=network.target

[Service]
TimeoutStartSec=0
Type=notify
ExecStart=/usr/local/bin/cloudflared --config /etc/cloudflared/config.yml --origincert /etc/cloudflared/cert.pem --autoupdate-freq 24h
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target