Schotten Tank Niveaumessung
Inhaltsverzeichnis
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=32760; //in Liter
$umrechner=1000; //liter in m3
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="5">
<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), (>
body {
//background: #ccc;
font-size: 36px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="centered-element">
<?php
echo "<h1>Niveaumessung Schottentank</h1>";
echo "Nutzinhalt - <b>".$tankinhalt / $umrechner." m3</b><br />";
//$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 = "data.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;
// Status aus File auslesen
$file2 = "quality.txt";
$quality = file($file2);
//echo $quality[0];
$quali = $quality[0];
//echo $quali;
// zeig da schäiss mau a
echo '<br />';
echo '<div>';
//echo date('d/m/Y == H:i:s');
echo 'Status Niveaumessung: ';
if ($quali != "True") {
echo '<font color="#ff0000">Aktiv - Störung</font>';
} else {
echo '<font color="#00b000">Aktiv - Fehlerfrei</font>';
}
echo '<br /><br />Aktuellester Niveau Messwert vom<br /><b>'.date("Y.m.d - H:i::s",>
echo '<br /><h2><font color="#ff0000">';
if (($tankinhalt - $messwertliter) / $umrechner > 0) {
echo ($tankinhalt - $messwertliter) / $umrechner;
} else {
echo "Tank ist voll!<br />0";
}
echo ' m3</font><br /> sind noch frei</h2><br />';
echo '</div>';
echo '<img src="graph.php" alt="Fehler beim anzeigen" />';
echo '<br />24h-Trend<br />';
//echo 'Status Niveaumessung: ';
//if ($quali != "True") {
// echo '<font color="#ff0000">Aktiv - Störung</font>';
//} else {
// echo '<font color="#00b000">Aktiv - Fehlerfrei</font>';
//}
echo '<br /><br /><br /><br />Support-Link:<br />';
echo '<a href="https://steuerungstechnik.ch/kontakt/" target="_blank"><img src="ste>
?>
</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);
?>
messen Script
/usr/share/scripts/get_data_cx8090.sh
#!/bin/sh
#
# /usr/share/scripts/get_data_cx8090.sh
# wartezeit in Sec bis wieder gemessen wird
interval=5
# Start Routing Script
cd /usr/share/scripts/
# Feedback vom Routäscript True/False
# python3 create_route_cx8090.py
# Holä vom Status im status.txt file
# route=$(head -n 1 status.txt)
# echo output: $route
route=false
python3 create_route_cx8090.py
route=$(head -n 1 /var/www/html/status.txt)
#now=$(date +%s)
nowhuman=$(date)
echo $nowhuman,$level,'set route to PLC - restart of the get_data Script'>>/var/www>
# Start script get_data in a loop
while true
do
if [ "$route" != "True" ]
then
python3 create_route_cx8090.py
route=$(head -n 1 /var/www/html/status.txt)
level=$(head -n 1 /var/www/html/value.txt)
now=$(date +%s)
nowhuman=$(date)
echo $nowhuman,$level,no PLC connection>>/var/www/html/datalog.txt
# echo output: $route
else
python3 get_data_from_cx8090.py
route=$(head -n 1 /var/www/html/status.txt)
level=$(head -n 1 /var/www/html/value.txt)
now=$(date +%s)
echo $now,$level
hour="$(date +"%H%M")"
reset=2359 #lösche alle daten im File data.txt um Uhrzeit 23:59
echo $hour
echo $reset
if [ "$hour" -eq "$reset" ]
then
#echo nice
echo "">/var/www/html/data.txt
else
#echo els
echo $now,$level>>/var/www/html/data.txt
fi
sleep $interval
fi
done
echo wird jtz beändet!
Service messen
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 messen
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