Schotten Tank Niveaumessung: Unterschied zwischen den Versionen

Aus Zovis Wikili
Wechseln zu: Navigation, Suche
(Argo Tunnel)
 
(5 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 7: Zeile 7:
 
* Auf dem Webserver läuft ein php Script das die Daten aus den Files ausliest und darstellt.
 
* 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.
 
* Der Argotunnel von Cloudflare leitet die Webseite ins www.
 +
 +
 +
= ToDo =
 +
== Add “Add to Home screen” Button to your website ==
 +
https://medium.com/@sam20gh/how-to-add-add-to-home-screen-to-your-website-4b07aee02676
 +
 +
== ssh via cloudflared ==
 +
https://developers.cloudflare.com/cloudflare-one/tutorials/ssh
  
 
= PHP =
 
= PHP =
Zeile 491: Zeile 499:
 
== Tunnel erstellen ==
 
== Tunnel erstellen ==
  
  cloudflared tunnel login
+
Instal Argo:
 +
  wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm64.deb && dpkg -i cloudflared-linux-arm64.deb
  
dann URL in einem Browser öffnen und bei cloudflare einlogen.
 
  
Certi für service kopieren
+
Stop the old cloudflared Tunnel
  cp /root/.cloudflared/cert.pem /etc/cloudflared/cert.pem
+
  sudo systemctl stop cloudflared
  
 +
Create a Tunnel:
 +
cloudflared tunnel create <TUNNEL-NAME>
 +
 +
 +
Route traffic to your tunnel to create routes that your tunnel will serve.
 +
cloudflared tunnel route dns <TUNNEL-NAME> tunnel.example.com
  
 
anschliessend config erstellen
 
anschliessend config erstellen
Zeile 504: Zeile 518:
 
== Config ==
 
== Config ==
  
/etc/cloudflared/config.yml
+
/etc/cloudflared/config.yml
 
 
hostname: sburi.stemoag.ch
 
 
  url: http://localhost:80
 
  url: http://localhost:80
  logfile: /var/log/cloudflared.log
+
  tunnel: <Tunnel-UUID>
 +
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json
  
== Zerti ==
 
/etc/cloudflared/cert.pem
 
  
 
== Service File ==
 
== Service File ==
Zeile 525: Zeile 536:
 
TimeoutStartSec=0
 
TimeoutStartSec=0
 
Type=notify
 
Type=notify
ExecStart=/usr/local/bin/cloudflared --config /etc/cloudflared/config.yml --origincert /etc/cloudflared/cert.pem --autoupdate-freq 24h
+
ExecStart=/usr/local/bin/cloudflared --autoupdate-freq 24h --config /etc/cloudflared/config.yml tunnel run
 
Restart=on-failure
 
Restart=on-failure
 
RestartSec=5s
 
RestartSec=5s
Zeile 534: Zeile 545:
 
</pre>
 
</pre>
  
== Statische IP ==
+
 
 +
Start the Service. Thats it
 +
sudo systemctl start cloudflared
 +
 
 +
= Statische IP =
 +
check IP
 
  ip adr
 
  ip adr
  
 +
Config anpassen in
 
  sudo nano /etc/netplan/50-cloudcfg.yaml
 
  sudo nano /etc/netplan/50-cloudcfg.yaml
  
 +
alles raus und folgendes rein:
 
<pre>
 
<pre>
 
network:
 
network:
Zeile 551: Zeile 569:
 
         addresses: [192.168.182.1,1.1.1.1]
 
         addresses: [192.168.182.1,1.1.1.1]
 
</pre>
 
</pre>
 
+
nun
 
  sudo netplan apply
 
  sudo netplan apply
 +
oder reboot
  
 +
ip check:
 
  ip addr
 
  ip addr

Aktuelle Version vom 1. April 2023, 15:56 Uhr

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.


ToDo

Add “Add to Home screen” Button to your website

https://medium.com/@sam20gh/how-to-add-add-to-home-screen-to-your-website-4b07aee02676

ssh via cloudflared

https://developers.cloudflare.com/cloudflare-one/tutorials/ssh

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>
                <a href="http://sburi.stemoag.ch/datalog.txt" style="color:#eee; style="color:#eee;font-size:5px;"">&nbsp</a>
                <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=40;  // in m3
$maxinhalt=40; // max Inhalt für Grafik 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=date("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=40;


# ---- 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-$ma>


# ------- 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 $maxinhalt
$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
count=0

# 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 - '================================='>>/var/www/html/datalog.txt
echo $nowhuman - 'Start of the get_data Script'>>/var/www/html/datalog.txt
echo $nowhuman - 'set route to PLC...'>>/var/www/html/datalog.txt


#  Start script get_data in a loop
echo $nowhuman - 'Start PLC connection ... may it need a few tries after a reboot.'>>/var/www/html/datalog.txt
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 - 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)
                nowhuman=$(date)
                echo $nowhuman - 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
                        rm /var/www/html/data.txt
                else
                        #echo els
                        echo $now,$level>>/var/www/html/data.txt
                fi
                echo $count
                if [ "$count" -eq "0" ]
                then
                        echo $nowhuman - PLC connection established - Level: $level>>/var/www/html/datalog.txt
                        echo $nowhuman - "=================================">>/var/www/html/datalog.txt
                        count=$((count+1))
                        echo $count
                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

Python Scripts

Auslesen der SPS Variablen

 /usr/share/scripts/get_data_from_cx8090.py
# Python3 Skript zur ADS Kommunikation mit Beckhoff TwinCat PLC
# Auslesen der SPS Variablen

# create some constants for connection
CLIENT_IP = "192.168.11.105"                # Client IP-Address
CLIENT_NETID = "192.168.11.105.1.1"         # Client AMS NetId
TARGET_IP = "192.168.4.101"                 # PLC IP-Address (Target)
TARGET_NETID = "192.168.4.101.1.1"          # PLC AMS NetId (Target)    Die AMS Net>
TARGET_USERNAME = "Administrator"           # PLC Username (Target)
TARGET_PASSWORD = ""                        # PLC Password (Target)
TARGET_ADS_PORT = 801                       # PLC ADS Port (TwinCat2 = 801 / TwinCa>
ROUTE_NAME = "RaspberryPI_Niveau"           # Route Description

import pyads

# initial write value to files
file = open("/var/www/html/value.txt", "w")
file.truncate(0)
file.write('0')
file.close()

file = open("/var/www/html/status.txt", "w")
file.truncate(0)
file.write('lost_connection')
file.close()

# connect to plc and open connection
# route is added automatically to client on Linux, on Windows use the TwinCAT router
plc = pyads.Connection(TARGET_NETID, TARGET_ADS_PORT)
plc.open()

# check the connection state
#plc.read_state()

# read int value by name
iValue = plc.read_by_name(".raspNiveauMolketank", pyads.PLCTYPE_UDINT)
bStatus = plc.read_by_name(".raspFaultMolketank", pyads.PLCTYPE_BOOL)

# close connection
plc.close()

# write value to file
file = open("/var/www/html/value.txt", "w")
file.truncate(0)
file.write(str(iValue))
file.close()

file = open("/var/www/html/status.txt", "w")
file.truncate(0)
file.write('True')
file.close()

file = open("/var/www/html/quality.txt", "w")
file.truncate(0)
if  bStatus == True:
    file.write('False')
elif    bStatus == False:
    file.write('True')
file.close()

exit()
# END

Erstellen der ADS-Routen zwischen den Zielgeräten

/usr/share/scripts/create_route_cx8090.py
# Python3 Skript zur ADS Kommunikation mit Beckhoff TwinCat PLC
# Erstellen der ADS-Routen zwischen den Zielgeräten

# create some constants for connection
CLIENT_IP = "192.168.11.105"                # Client IP-Address
CLIENT_NETID = "192.168.11.105.1.1"         # Client AMS NetId
TARGET_IP = "192.168.4.101"                 # PLC IP-Address (Target)
TARGET_NETID = "192.168.4.101.1.1"          # PLC AMS NetId (Target)    Die AMS Net>
TARGET_USERNAME = "Administrator"           # PLC Username (Target)
TARGET_PASSWORD = ""                        # PLC Password (Target)
TARGET_ADS_PORT = 801                       # PLC ADS Port (TwinCat2 = 801 / TwinCa>
ROUTE_NAME = "RaspberryPI_Niveau"           # Route Description

import pyads

# write value to file
file = open("/var/www/html/status.txt", "w")
file.truncate(0)
file.write('False')
file.close()

# add a new route to the target plc
bStatus= pyads.add_route_to_plc(CLIENT_NETID, CLIENT_IP, TARGET_IP, TARGET_USERNAME>

# write value to file
file = open("/var/www/html/status.txt", "w")
file.truncate(0)
file.write(str(bStatus))
file.close()


exit()
# END


Argo Tunnel

Tunnel erstellen

Instal Argo:

wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm64.deb && dpkg -i cloudflared-linux-arm64.deb


Stop the old cloudflared Tunnel

sudo systemctl stop cloudflared

Create a Tunnel:

cloudflared tunnel create <TUNNEL-NAME>


Route traffic to your tunnel to create routes that your tunnel will serve.

cloudflared tunnel route dns <TUNNEL-NAME> tunnel.example.com

anschliessend config erstellen


Config

/etc/cloudflared/config.yml

url: http://localhost:80
tunnel: <Tunnel-UUID>
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json


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 --autoupdate-freq 24h --config /etc/cloudflared/config.yml tunnel run
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target


Start the Service. Thats it

sudo systemctl start cloudflared

Statische IP

check IP

ip adr

Config anpassen in

sudo nano /etc/netplan/50-cloudcfg.yaml

alles raus und folgendes rein:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses: [192.168.182./24]
      gateway4: 192.168.182.1
      nameservers:
        addresses: [192.168.182.1,1.1.1.1]

nun

sudo netplan apply

oder reboot

ip check:

ip addr