Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung | |||
wetter:ws500:das_perl-script [08.03.2011 12:59. ] – django | wetter:ws500:das_perl-script [31.10.2023 18:33. ] (aktuell) – Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== Das Perl-Scriptchen ====== | ||
+ | |||
+ | Zum Auslesen der [[wetter: | ||
+ | |||
+ | <code perl| ws500.pl># | ||
+ | # | ||
+ | # ws500.pl: Ausleseprogramm fuer die WS500 von elv | ||
+ | # | ||
+ | # Copyright (C) 2006 Jochen, Basti, Django | ||
+ | # | ||
+ | # This program is free software; you can redistribute it and/or | ||
+ | # modify it under the terms of the GNU General Public License | ||
+ | # as published by the Free Software Foundation; either version 2 | ||
+ | # of the License, or (at your option) any later version. | ||
+ | # | ||
+ | # This program is distributed in the hope that it will be useful, | ||
+ | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
+ | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
+ | # GNU General Public License for more details. | ||
+ | # | ||
+ | # You should have received a copy of the GNU General Public License | ||
+ | # along with this program; if not, write to the Free Software | ||
+ | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
+ | # Or, point your browser to https:// | ||
+ | # | ||
+ | # The author can be reached at django[aett]mnet-mail[punkt]de | ||
+ | # | ||
+ | # The project' | ||
+ | # | ||
+ | |||
+ | use Device:: | ||
+ | use POSIX qw(locale_h); | ||
+ | use locale; | ||
+ | use DBI; | ||
+ | use DBD::mysql; | ||
+ | use IO::File; | ||
+ | |||
+ | use strict; | ||
+ | use warnings; | ||
+ | use diagnostics; | ||
+ | |||
+ | |||
+ | # --- Definition der Systemeinstellungen --- # | ||
+ | |||
+ | our $dev; | ||
+ | our $lastcount; | ||
+ | our $debug; | ||
+ | our $reporting; | ||
+ | our $mailinfo; | ||
+ | our $mailto; | ||
+ | our $mailfrom; | ||
+ | our $Sendmail_Prog; | ||
+ | our $logfile; | ||
+ | our $writeToFile; | ||
+ | our $recordFile; | ||
+ | our $statusFile; | ||
+ | our $server; | ||
+ | our $mysqlusage; | ||
+ | our $dbhost; | ||
+ | our $dbuser; | ||
+ | our $dbpasswd; | ||
+ | our $dbdatabase; | ||
+ | #our $sqllogger; | ||
+ | |||
+ | # --- Definition der Systemvariablen --- # | ||
+ | |||
+ | my @sensoren_status; | ||
+ | my @empfausfall; | ||
+ | my @RXBuffer; | ||
+ | my @TXBuffer; | ||
+ | my @feuchte; | ||
+ | my @temperatur; | ||
+ | my @csvzeile; | ||
+ | my @sensor_db; | ||
+ | |||
+ | |||
+ | my $FTDI; | ||
+ | my $absdruck; | ||
+ | my $charakter; | ||
+ | my $commandstring; | ||
+ | my $druck; | ||
+ | my $errorcount; | ||
+ | my $firmware = 2.50; # test | ||
+ | my $firstrun; | ||
+ | my $framelaenge = 0; | ||
+ | my $hexcharakter; | ||
+ | my $hoehe; | ||
+ | my $i; | ||
+ | my $input; | ||
+ | my $intervall; | ||
+ | my $is_offline; | ||
+ | my $is_raining; | ||
+ | my $korrhoehe = " | ||
+ | my $meterbytehigh; | ||
+ | my $meterbytelow; | ||
+ | my $newmysqltime; | ||
+ | my $offset = 0; | ||
+ | my $oldIntervall; | ||
+ | my $oldmysqltime; | ||
+ | my $online; | ||
+ | my $paniccount; | ||
+ | my $recordtime; | ||
+ | my $sensor_db; | ||
+ | my ( $regen, | ||
+ | my ( $sonne, | ||
+ | my ($sth, | ||
+ | my $time_next_record; | ||
+ | my $time_next_developstatus; | ||
+ | my $time_nr; | ||
+ | my $tmp; | ||
+ | my $tmpstring; | ||
+ | my $type; | ||
+ | my $Version = " | ||
+ | my $wettervorhersage; | ||
+ | my ( $wind_geschwindigkeit, | ||
+ | my $wippe; | ||
+ | my $wippebytehigh; | ||
+ | my $wippebytelow; | ||
+ | my $writeString; | ||
+ | |||
+ | |||
+ | require '/ | ||
+ | |||
+ | ########################################################################################################################################################## | ||
+ | # *** START *** Hauptprogramm | ||
+ | ########################################################################################################################################################## | ||
+ | |||
+ | write_Log (" | ||
+ | print STDERR " | ||
+ | get_lastcount(); | ||
+ | connect_MySQL () if ($mysqlusage == 1); # MySQL-Datenbankanbindung herstellen, sofern gewuenscht | ||
+ | |||
+ | $time_next_developstatus = time(); | ||
+ | $time_next_record = time(); | ||
+ | $intervall = 0; | ||
+ | $is_offline = 0; | ||
+ | $firstrun = 1; | ||
+ | |||
+ | while ( 1 ) { # " | ||
+ | my $theMessage = shift; | ||
+ | |||
+ | if ($time_next_developstatus <= time()) { | ||
+ | $paniccount = 0; | ||
+ | $oldIntervall = $intervall; | ||
+ | init_USB(); | ||
+ | write_Log (" | ||
+ | $tmp = get_DEVELOP_STATUS(); | ||
+ | while (!$tmp && ($paniccount < 5)) { # 5x Versuchen den Status der Station abzufragen | ||
+ | $paniccount++; | ||
+ | $tmp = get_DEVELOP_STATUS(); | ||
+ | } | ||
+ | close_USB(); | ||
+ | if ($paniccount >= 5){ # sind die 5 Versuche Fehlgeschlagen, | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | write_Log (" | ||
+ | $theMessage = " | ||
+ | send_Mail($theMessage); | ||
+ | |||
+ | $is_offline = 1; | ||
+ | if ($firstrun == 1) { | ||
+ | sleep 60; # beim ersten Mal müssen erst die kritischen Variablen befüllt werden! | ||
+ | |||
+ | #### | ||
+ | init_USB(); | ||
+ | write_Log ("2nd try to get_DEVEOLOP_STATUS()" | ||
+ | $tmp = get_DEVELOP_STATUS(); | ||
+ | while (!$tmp && ($paniccount < 5)) { # 5x Versuchen den Status der Station abzufragen | ||
+ | $paniccount++; | ||
+ | $tmp = get_DEVEOLOP_STATUS(); | ||
+ | } | ||
+ | close_USB(); | ||
+ | #### | ||
+ | |||
+ | } | ||
+ | else { | ||
+ | $time_next_developstatus = time() + 60;# einfach in einer Minute noch mal versuchen. | ||
+ | } | ||
+ | $paniccount = 0; | ||
+ | } | ||
+ | |||
+ | if (($tmp == 1) && ($is_offline == 1)) { | ||
+ | $is_offline = 0; | ||
+ | write_Log (" | ||
+ | $theMessage = " | ||
+ | send_Mail($theMessage) | ||
+ | } | ||
+ | |||
+ | if ($is_offline == 0) { | ||
+ | if ($oldIntervall != $intervall) { | ||
+ | $firstrun = 0; | ||
+ | $time_next_record = time(); | ||
+ | write_Log (" | ||
+ | } | ||
+ | |||
+ | write_Status_To_File(); | ||
+ | $time_next_developstatus = time() + $intervall*60 if ($is_offline == 0); | ||
+ | write_Log (" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if ($time_next_record <= time()) { | ||
+ | write_Log (" | ||
+ | init_USB(); | ||
+ | if (get_CURRENT_RECORD()) { | ||
+ | if ($is_offline == 1) { | ||
+ | $is_offline = 0; | ||
+ | write_Log (" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $errorcount = 0; | ||
+ | |||
+ | do { #solange noch Daten gespeichert sind | ||
+ | write_Log (" | ||
+ | |||
+ | if (get_NEXT_RECORD() == 1) { | ||
+ | write_Log (" | ||
+ | if ($is_offline == 1) { | ||
+ | $is_offline = 0; | ||
+ | write_Log (" | ||
+ | } | ||
+ | prozess_DATA(); | ||
+ | } | ||
+ | |||
+ | if ($framelaenge == 0) { | ||
+ | $paniccount++; | ||
+ | } | ||
+ | if ($framelaenge < 5) { | ||
+ | $errorcount++; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | $errorcount = 0; | ||
+ | $paniccount = 0; | ||
+ | } | ||
+ | } until (($framelaenge < 5) && ($errorcount > 5) ); | ||
+ | close_USB(); | ||
+ | if ($paniccount >= 5){ | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | write_Log (" | ||
+ | $theMessage = " | ||
+ | send_Mail($theMessage); | ||
+ | | ||
+ | $paniccount = 0; | ||
+ | $time_next_record = time() + 60; #in 60 sekunden noch mal nachschauen | ||
+ | $is_offline = 1; | ||
+ | } | ||
+ | $time_next_record = time() + $intervall*60 if ($is_offline == 0); | ||
+ | write_Log (" | ||
+ | } | ||
+ | |||
+ | sleep 1; | ||
+ | } | ||
+ | |||
+ | ########################################################################################################################################################## | ||
+ | # *** ENDE *** Hauptprogramm | ||
+ | ########################################################################################################################################################## | ||
+ | |||
+ | |||
+ | ############################################################################## | ||
+ | ### ab hier sind die ueberarbeiteten Unterprogramme alphabetisch abgelegt: ### | ||
+ | ############################################################################## | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub byte2Int { | ||
+ | my $highbyte = shift; | ||
+ | my $lowbyte | ||
+ | |||
+ | # if ($highbyte > 127) { | ||
+ | # | ||
+ | # | ||
+ | # } | ||
+ | if ($highbyte == 255) { # wenn Highbyte gleich " | ||
+ | # | ||
+ | # | ||
+ | return(($lowbyte)-256); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | return($highbyte*255+$lowbyte); | ||
+ | } | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub close_USB { | ||
+ | write_Log (" | ||
+ | # my $rts=$FTDI-> | ||
+ | # | ||
+ | |||
+ | $FTDI-> | ||
+ | $online = 0; | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub connect_MySQL { | ||
+ | my $theMessage = shift; | ||
+ | $dbh = DBI -> connect (" | ||
+ | { | ||
+ | PrintError => 0, | ||
+ | } | ||
+ | ); | ||
+ | unless ( $dbh ) { | ||
+ | write_Log (" | ||
+ | $theMessage = " | ||
+ | send_Mail($theMessage); | ||
+ | die (" | ||
+ | } | ||
+ | write_Log (" | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub convert_Time { | ||
+ | my $oldtime = shift; | ||
+ | |||
+ | my $date=gmtime($oldtime); | ||
+ | my @date=gmtime($oldtime); | ||
+ | my $sekunde=sprintf(" | ||
+ | my $minute=sprintf(" | ||
+ | my $stunde=sprintf(" | ||
+ | my $tag=sprintf(" | ||
+ | my $mon=sprintf(" | ||
+ | my $jahr=sprintf(" | ||
+ | my $minus = " | ||
+ | my $leer = " "; | ||
+ | my $doppelpunkt = ":"; | ||
+ | |||
+ | $newmysqltime = $jahr.$minus.$mon.$minus.$tag.$leer.$stunde.$doppelpunkt.$minute.$doppelpunkt.$sekunde; | ||
+ | |||
+ | # | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub escapeByte { | ||
+ | my $addbyte = shift; | ||
+ | if (is_Escape_Chr($addbyte)) { | ||
+ | write_Log ("byte escaped!: " | ||
+ | $commandstring .= " | ||
+ | } | ||
+ | else { | ||
+ | $commandstring .= chr($addbyte); | ||
+ | } | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub get_CURRENT_RECORD { | ||
+ | write_ws500(" | ||
+ | read_ws500(); | ||
+ | write_Array(); | ||
+ | if ($framelaenge >= 40) { | ||
+ | read_RECORD_Frame(); | ||
+ | |||
+ | if ($debug == 1) { | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | for ($i=0; $i < 10; $i++) { | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | } | ||
+ | print " | ||
+ | } | ||
+ | } | ||
+ | return ($framelaenge >= 40); | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub get_DEVELOP_STATUS { | ||
+ | my $theMessage = shift; | ||
+ | get_FIRMWARE(); | ||
+ | # | ||
+ | write_ws500(" | ||
+ | read_ws500(); | ||
+ | write_Array(); | ||
+ | |||
+ | # Config auslesen | ||
+ | if ($framelaenge >= 17) { | ||
+ | if ($RXBuffer[1] == 50) # config record | ||
+ | { | ||
+ | $offset = 2; | ||
+ | |||
+ | for ($i=1; $i < 10; $i++) { | ||
+ | $sensoren_status[$i] = unescapeByte() - 16; | ||
+ | |||
+ | if ($sensoren_status[$i] < 0) { | ||
+ | $sensor_db[$i] = " | ||
+ | } | ||
+ | else { | ||
+ | $sensor_db[$i] = " | ||
+ | } | ||
+ | |||
+ | if ($sensoren_status[$i] > 0 ) { | ||
+ | $theMessage = "Die Kommunikation der WS500 mit dem Fuehler $i ist unterbrochen! Es sind $sensoren_status[$i] Funkausfaelle zu verzeichnen!\n"; | ||
+ | send_Mail($theMessage); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | $sensor_db[0] = " | ||
+ | |||
+ | $intervall = unescapeByte(); | ||
+ | $hoehe = unescapeByte()*255 + unescapeByte(); | ||
+ | $wippe = unescapeByte()*255 + unescapeByte(); | ||
+ | |||
+ | for ($i=1; $i < 10; $i++) { | ||
+ | |||
+ | if ($debug == 1) | ||
+ | { | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | |||
+ | $sensor_db[0] = " | ||
+ | |||
+ | for ($i=1; $i < 10; $i++) { | ||
+ | if ($sensoren_status[$i] < 0) { | ||
+ | print " | ||
+ | } | ||
+ | else { | ||
+ | print " | ||
+ | } | ||
+ | } | ||
+ | print " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | return ($framelaenge >= 17); | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub get_FIRMWARE { | ||
+ | write_ws500(" | ||
+ | read_ws500(); | ||
+ | |||
+ | if ($framelaenge >= 4) { | ||
+ | $firmware = (sprintf(" | ||
+ | } | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub get_lastcount { | ||
+ | if(!open(BACKUPFILE, | ||
+ | $reporting = 0; | ||
+ | print " | ||
+ | } | ||
+ | print BACKUPFILE ""; | ||
+ | close(BACKUPFILE) || warn "close failed"; | ||
+ | |||
+ | $sonne_alt = 0; # Sonnenwert zuruecksetzen | ||
+ | $regen_alt = 0; # Regenwert zuruecksetzen | ||
+ | |||
+ | open (BACKUPFILE, | ||
+ | |||
+ | while (< | ||
+ | if (m/^\s*#/o) {next; | ||
+ | @csvzeile = split /;/, | ||
+ | $regen_alt = $csvzeile [0]; # letzter Regenwert aus Feld/Spalte " | ||
+ | $sonne_alt = $csvzeile [1]; # letzter Sonnenwert aus Feld/Spalte " | ||
+ | |||
+ | } | ||
+ | write_Log (" | ||
+ | close(BACKUPFILE); | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub get_NEXT_RECORD { | ||
+ | write_ws500(" | ||
+ | read_ws500(); | ||
+ | if ($framelaenge >= 40) { | ||
+ | write_Array(@RXBuffer); | ||
+ | read_RECORD_Frame(); | ||
+ | |||
+ | if ($debug == 1){ | ||
+ | print " | ||
+ | print "|\t Datensatz - Nummer " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | for ($i=0; $i < 10; $i++) { | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | } | ||
+ | print " | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | write_Log (" | ||
+ | } | ||
+ | return ($framelaenge >= 40); # gibt true zurueck, falls erfolgreich ein Datensatz ausgelesen wurde | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub init_USB { | ||
+ | $FTDI-> | ||
+ | $FTDI = tie(*FTD, ' | ||
+ | $FTDI-> | ||
+ | $FTDI-> | ||
+ | my $baud = $FTDI-> | ||
+ | my $dbits | ||
+ | my $parity | ||
+ | my $sbits | ||
+ | # my $handshake = $FTDI-> | ||
+ | my $handshake = $FTDI-> | ||
+ | my $rts=$FTDI-> | ||
+ | $FTDI-> | ||
+ | if ($debug == 1) { | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | } | ||
+ | $online = 1; | ||
+ | write_Log (" | ||
+ | select(undef, | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub is_Escape_Chr { | ||
+ | my $theChr = shift; | ||
+ | return (($theChr == 0xF8 || $theChr == 0xF8 || $theChr == 0xF8) ); | ||
+ | } | ||
+ | # *** START *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub prozess_DATA { | ||
+ | |||
+ | write_lastcount(); | ||
+ | write_MySQL() if ($mysqlusage == 1); # MySQL-Datenbank " | ||
+ | write_Record_to_File() if ($writeToFile == 1); # Wetterdaten in CSV-Datei schreiben, falls gewuenscht | ||
+ | $regen_alt = $regen; | ||
+ | $sonne_alt = $sonne; | ||
+ | print " | ||
+ | |||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub read_RECORD_Frame { | ||
+ | if ((($RXBuffer[1] == 49) || ($RXBuffer[1] == 51)) && ($framelaenge >= 40)) | ||
+ | { | ||
+ | if ($RXBuffer[1] == 51) { # aktueller Datensatz | ||
+ | $offset | ||
+ | $time_nr | ||
+ | $type = " | ||
+ | } | ||
+ | else { # naechster Datensatz | ||
+ | $offset | ||
+ | $time_nr | ||
+ | $type = " | ||
+ | } | ||
+ | |||
+ | $recordtime = time()-($time_nr*60); | ||
+ | $oldmysqltime = ( gmtime($recordtime) ); | ||
+ | convert_Time ( $recordtime ); | ||
+ | |||
+ | undef @temperatur; | ||
+ | undef @feuchte; | ||
+ | for (my $i=1; $i < 10; $i++) { | ||
+ | $temperatur[$i]= (byte2Int(unescapeByte(), | ||
+ | $feuchte[$i]= unescapeByte(); | ||
+ | } | ||
+ | |||
+ | $regen = (unescapeByte()*255 + unescapeByte()); | ||
+ | $regen_delta = $regen - $regen_alt; | ||
+ | $regen_menge = $regen_delta * $wippe; | ||
+ | if( $regen_delta eq " | ||
+ | $regen_db = " | ||
+ | } | ||
+ | else { | ||
+ | | ||
+ | } | ||
+ | $wind_geschwindigkeit = (unescapeByte()*255 + unescapeByte()) / 10; | ||
+ | $wind_richtung = (unescapeByte() * 5); | ||
+ | $wind_schwankung = (unescapeByte() * 5); | ||
+ | $sonne = (unescapeByte()*255 + unescapeByte()); | ||
+ | $sonne_delta = $sonne - $sonne_alt; | ||
+ | if( $sonne_delta eq " | ||
+ | $sonne_ok = 0; | ||
+ | $sonne_db = " | ||
+ | } | ||
+ | else { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | $temperatur[0] = ( byte2Int(unescapeByte(), | ||
+ | $feuchte[0] = unescapeByte(); | ||
+ | $druck = (unescapeByte()*255 + unescapeByte()); | ||
+ | $absdruck = $druck; | ||
+ | $druck = $druck * ( 2.718 ** ( ( ( $hoehe + $korrhoehe ) * 9.8066 ) / ( 287.05 * ( $temperatur[0] + 273.15 ) ) ) ) ; | ||
+ | $druck = sprintf (" | ||
+ | |||
+ | if ($type eq " | ||
+ | $wettervorhersage = unescapeByte(); | ||
+ | # 3 = schwuel, | ||
+ | $is_raining = ($wettervorhersage > 120); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub read_ws500 { | ||
+ | my $i = 0; | ||
+ | $input = $FTDI-> | ||
+ | $framelaenge = length($input); | ||
+ | undef @RXBuffer; | ||
+ | foreach $charakter (split(//, | ||
+ | $RXBuffer[$i] = ord($charakter); | ||
+ | $i++; | ||
+ | } | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub send_Mail { | ||
+ | my $theMessage = shift; | ||
+ | if ($mailinfo == 1) { | ||
+ | open(MAIL," | ||
+ | print MAIL "From: $mailfrom\n"; | ||
+ | print MAIL "To: $mailto\n"; | ||
+ | print MAIL " | ||
+ | print MAIL " | ||
+ | close(MAIL); | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub sqlquery { | ||
+ | $query=shift(); | ||
+ | $sth=$dbh-> | ||
+ | $sth-> | ||
+ | $sqlstate = $dbh-> | ||
+ | unless ($sqlstate) {$sqlstate=' | ||
+ | $sqllogger .= " | ||
+ | sysopen(FHNEW," | ||
+ | syswrite(FHNEW, | ||
+ | close(FHNEW); | ||
+ | $sqllogger = ''; | ||
+ | } | ||
+ | # *** START *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub unescapeByte { | ||
+ | my $tmp = 0; | ||
+ | if (is_Escape_Chr($RXBuffer[$offset])) { | ||
+ | $offset++; | ||
+ | $tmp = $RXBuffer[$offset] - 1; | ||
+ | } | ||
+ | else { | ||
+ | |||
+ | $tmp = $RXBuffer[$offset]; | ||
+ | } | ||
+ | $offset++; | ||
+ | return( $tmp ); | ||
+ | } | ||
+ | # *** STOP *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub write_Array { | ||
+ | if ($debug == 1){ | ||
+ | print "Der empfangene Datensatz lautet: | ||
+ | foreach $charakter (@RXBuffer) { | ||
+ | $hexcharakter = sprintf(" | ||
+ | print $hexcharakter.", | ||
+ | } | ||
+ | print " | ||
+ | foreach $charakter (@RXBuffer) { | ||
+ | print $charakter.", | ||
+ | } | ||
+ | print " | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub write_Config { | ||
+ | my $zeit = shift; | ||
+ | my $meter = shift; | ||
+ | my $mmwippe = shift; | ||
+ | |||
+ | if (($zeit >= 5) && ($zeit < 100) && ($meter >= 0) && ($meter < 2000) && ($mmwippe >= 200) && ($mmwippe < 400)) { | ||
+ | $meterbytehigh = int($meter / 255); | ||
+ | $meterbytelow = $meter % 255; | ||
+ | $wippebytehigh = int($mmwippe / 255); | ||
+ | $wippebytelow = $mmwippe % 255; | ||
+ | write_Log (" | ||
+ | $commandstring = " | ||
+ | escapeByte($meterbytehigh); | ||
+ | escapeByte($meterbytelow); | ||
+ | escapeByte($wippebytehigh); | ||
+ | escapeByte($wippebytelow); | ||
+ | $commandstring .= " | ||
+ | #achtung: | ||
+ | # | ||
+ | if ($reporting == 1) { | ||
+ | $tmpstring = ""; | ||
+ | foreach $charakter (split (//, | ||
+ | $tmpstring .= ord($charakter).", | ||
+ | } | ||
+ | write_Log (" | ||
+ | } | ||
+ | write_Log (" | ||
+ | my $laststatus = $online; | ||
+ | init_USB() if(!$laststatus); | ||
+ | ##### | ||
+ | close_USB() if(!$laststatus); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub write_dev_2_MySQL { | ||
+ | $sql=" | ||
+ | sqlquery($sql); | ||
+ | |||
+ | write_Log (" | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub write_lastcount { | ||
+ | if(!open(BACKUPFILE, | ||
+ | $writeToFile = 0; | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | print BACKUPFILE $regen.";"; | ||
+ | print BACKUPFILE $sonne.";"; | ||
+ | close(BACKUPFILE) || warn "close failed"; | ||
+ | |||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub write_Log { | ||
+ | my $theMessage = shift; | ||
+ | my $newtime; | ||
+ | if ( $reporting == 1 ){ | ||
+ | if(!open(LOGFILE, | ||
+ | print " | ||
+ | } | ||
+ | print LOGFILE localtime(time()).": | ||
+ | close(LOGFILE) || warn "close failed"; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub write_MySQL { | ||
+ | |||
+ | # Luftdruck in der Datenbank ablegen | ||
+ | $sql=" | ||
+ | sqlquery($sql); | ||
+ | |||
+ | # Windwerte in der Datenbank ablegen | ||
+ | $sql=" | ||
+ | sqlquery($sql); | ||
+ | |||
+ | # Regenwerte in der Datenbank ablegen | ||
+ | if( $regen_db == 1 ) { | ||
+ | $sql=" | ||
+ | sqlquery($sql); | ||
+ | } | ||
+ | |||
+ | # Sonnenscheinwerte in der Datenbank ablegen | ||
+ | if( $sonne_db == 1 ) { | ||
+ | $sql=" | ||
+ | sqlquery($sql); | ||
+ | } | ||
+ | |||
+ | # Temperatur + Feuchtewerte der Sensoren 1 bis 9 in der Datenbank ablegen | ||
+ | for ($i=1; $i < 10; $i++) { | ||
+ | if( $sensor_db[$i] == 1 ) { | ||
+ | $sql=" | ||
+ | sqlquery($sql); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # Temperatur + Feuchtewerte des Innensensors in der Datenbank ablegen | ||
+ | if( $sensor_db[0] == 1 ) { | ||
+ | $sql=" | ||
+ | sqlquery($sql); | ||
+ | } | ||
+ | |||
+ | write_Log (" | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub write_Record_to_File { | ||
+ | if(!open(RECORDFILE, | ||
+ | $writeToFile = 0; | ||
+ | print " | ||
+ | } | ||
+ | # " | ||
+ | print RECORDFILE $newmysqltime.";"; | ||
+ | print RECORDFILE $regen_alt.";"; | ||
+ | print RECORDFILE $regen.";"; | ||
+ | print RECORDFILE $regen_delta.";"; | ||
+ | print RECORDFILE $regen_menge.";"; | ||
+ | print RECORDFILE $wind_geschwindigkeit.";"; | ||
+ | print RECORDFILE $wind_richtung.";"; | ||
+ | print RECORDFILE $wind_schwankung.";"; | ||
+ | print RECORDFILE $druck.";"; | ||
+ | print RECORDFILE $absdruck.";"; | ||
+ | print RECORDFILE $sonne_alt.";"; | ||
+ | print RECORDFILE $sonne.";"; | ||
+ | print RECORDFILE $sonne_delta.";"; | ||
+ | print RECORDFILE $sonne_ok.";"; | ||
+ | print RECORDFILE $wettervorhersage.";"; | ||
+ | for ($i=1; $i < 10; $i++) { | ||
+ | if( $sensor_db[$i] == 1 ) { | ||
+ | print RECORDFILE $temperatur[$i].";"; | ||
+ | print RECORDFILE $feuchte[$i].";"; | ||
+ | } | ||
+ | } | ||
+ | print RECORDFILE $temperatur[0].";"; | ||
+ | print RECORDFILE $feuchte[0].";"; | ||
+ | print RECORDFILE " | ||
+ | close(RECORDFILE) || warn "close failed"; | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub write_ws500 | ||
+ | { | ||
+ | my $command = shift; | ||
+ | if ($command ne "" | ||
+ | $FTDI-> | ||
+ | syswrite FTD, $command, length($command), | ||
+ | select(undef, | ||
+ | $FTDI-> | ||
+ | } | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | |||
+ | # *** START *** Unterprogramm " | ||
+ | sub write_Status_To_File { | ||
+ | my $theMessage = shift; | ||
+ | if(!open(STATUSFILE, | ||
+ | print " | ||
+ | } | ||
+ | else { | ||
+ | print STATUSFILE " | ||
+ | print STATUSFILE " | ||
+ | print STATUSFILE " | ||
+ | print STATUSFILE " | ||
+ | for ($i=1; $i < 10; $i++) { | ||
+ | if ($sensoren_status[$i] < 0) { | ||
+ | print STATUSFILE " | ||
+ | } | ||
+ | else { | ||
+ | print STATUSFILE " | ||
+ | } | ||
+ | } | ||
+ | close (STATUSFILE); | ||
+ | } | ||
+ | } | ||
+ | # *** ENDE *** Unterprogramm " | ||
+ | </ | ||
+ | [[wetter: | ||