Script TinyTX Sensoren Anbindung

    • Script TinyTX Sensoren Anbindung

      Hallo Zusammen,

      ich habe das script von "meigrafd" erweitert, damit es die Empfangenen Daten von TinyTX Sendern mit folgenden Sensoren DS18x20, DHT11/22 und BMP085/180, an das SHC Übermittelt.

      Getestet habe ich das mit meinen DS18x20 Sensoren, da ich noch keine anderen in Benutzung habe. Es kann sein, das bei dem DHT11/22 und BMP085/180 die übermittelten Werte noch angepasst werden müssen. Es sind auch bestimmt noch fehler vorhanden, aber es funktioniert schon mal.

      Bei den DS18x20 gibt es eine FakeID Sensor ID, die sich aus einer FakeID und der TinyTX Node ID zusammensetzt. Bei den anderen Sensoren wird nur die TinyTX Node ID als Sensor ID übermittelt.



      Hier mal das Script


      Perl Source Code

      1. #!/usr/bin/perl -w
      2. #
      3. # sensor.pl
      4. # Based on meigrafd
      5. # Modified by Michael Harperscheidt for Work with "agent4788" SHC_Framework
      6. #
      7. # DS18x20, DHT11/22 and BMP085/180 without Altitude, Altitude is set fix to 0
      8. # Testet only with DS18x20, it is possible that the values at the DHT11/22 and BMP085/180
      9. # have to be adjusted (divide or something else)
      10. #
      11. # Reads data from serial port and posts to PHP and to SHC
      12. #
      13. # Run: sudo apt-get install libdevice-serialport-perl
      14. # if you get "Can't locate device/SerialPort.pm in @INC (@INC includes ..."
      15. #
      16. # use lib '/usr/lib/perl5/Device'
      17. # sudo apt-get install libwww-mechanize-perl
      18. #To install Proc:Daemon
      19. #perl -MCPAN -e 'install Proc::Daemon' OR sudo apt-get install libproc-daemon-perl
      20. #### CONFIG - START
      21. # Config for meigrafd PHP Sample
      22. # Secret Key for PHP
      23. my $KEY = '23338d373027ce83b1f81b9e9563b629';
      24. # set url to add.php for PHP
      25. my $url1 = "http://192.168.100.254/sensoren/add.php?key=". $KEY ."&node=";
      26. # Config for SHC_Framework
      27. # set Sensor Point ID
      28. my $spid = '2';
      29. # set Fake DS18x20 Serial
      30. my $ds18 = '28-aaaa0000';
      31. # set url to SHC_Framework
      32. my $url2 = "http://192.168.100.254/shc/index.php?app=shc&a&ajax=pushsensorvalues&spid=";
      33. # Config Global
      34. # set UART baudrate
      35. my $baudrate = 9600;
      36. # set Port
      37. my $PORT = "/dev/ttyUSB0";
      38. #### CONFIG - END
      39. # Declare the subroutines
      40. sub trim($);
      41. BEGIN {
      42.        push @INC,"/usr/lib/perl5/";
      43. }
      44. use strict;
      45. use Device::SerialPort qw( :PARAM :STAT 0.07 );
      46. use WWW::Mechanize;
      47. use Time::localtime;
      48. use Scalar::Util 'looks_like_number';
      49. use Proc::Daemon;
      50. print "Serial to PHP gateway for RaspberryPi with RFM12B\r\n";
      51. my $ob = Device::SerialPort->new($PORT);
      52. $ob->baudrate($baudrate);
      53. $ob->parity("none");
      54. $ob->databits(8);
      55. $ob->stopbits(1);
      56. #$ob->handshake("xoff");
      57. $ob->write_settings;
      58. open(SERIAL, "+>$PORT");
      59. my $continue = 1;
      60. $SIG{TERM} = sub { $continue = 0 };
      61. while ($continue) {
      62.        my $line = trim(<SERIAL>);
      63.        print $line; print "\r\n";
      64.        my @values = split(' ', $line);
      65.        if(looks_like_number($values[0]) && $values[0] >=1) {
      66.                post2php($values[0],$values[1]);
      67.                post2shc($values[0],$values[1]);
      68.                sleep(2);
      69.        }
      70. }
      71. sub post2php {
      72. # Send Values to the meigrafd PHP Sample
      73.        my $ua = WWW::Mechanize->new();
      74.        my $URL = $url1 . $_[0] ."&" . $_[1];
      75.                print $URL; print "\r\n";
      76.                my $response = $ua->get($URL);
      77.                if ($response->is_success) {
      78.                                my $c = $ua->content;
      79.                                print ("$c");
      80.                } else {
      81.                                print "Failed to open url!";
      82.                }
      83. }
      84. sub post2shc {
      85. # Send Values to the SHC_Framework
      86.        my $ua = WWW::Mechanize->new();
      87.        my $sid = $ds18 . $_[0];
      88.        my $URL;
      89.        my @humpre;
      90.        my @werte = split('&', $_[1]);
      91.        my $anzahl = @werte ;
      92.        my @volt =  split('=', $werte[0]);
      93.        my @temp =  split('=', $werte[1]);
      94.        print $anzahl; print "\r\n";
      95.        if ( $anzahl eq "3" ) {
      96.                @humpre =  split('=', $werte[2]);
      97.                }
      98.        else {
      99.                @humpre = ("a", "1111");
      100.                }
      101.        my $temp = $temp[1] / 100; # Correct temperature value
      102.        if ( $humpre[0] eq "h" ) {
      103.                        $URL = $url2  . $spid . "&sid=". $_[0] . "&type=2&v1=". $temp ."&v2=". $humpre[1];
      104.                        }
      105.        elsif ( $humpre[0] eq "p" ) {
      106.                        $URL = $url2  . $spid . "&sid=". $_[0] . "&type=3&v1=". $temp ."&v2=". $humpre[1] . "&v3=0";
      107.                        }
      108.        else {
      109.                        $URL = $url2  . $spid . "&sid=". $sid . "&type=1&v1=". $temp;
      110.                        }
      111.        print $URL; print "\r\n";
      112.                my $response = $ua->get($URL);
      113.                if ($response->is_success) {
      114.                                my $c = $ua->content;
      115.                        if ( $c eq "1" ) {
      116.                                print "Data saved successfully to SHC_Framework\n";
      117.                                }
      118.                        elsif ( $c eq "2" ) {
      119.                                 print "missing parameter for SHC_Framework\n";
      120.                                }
      121.                        elsif ( $c eq "3" ) {
      122.                                 print "Save Failed to SHC_Framework\n";
      123.                                }
      124.                        elsif ( $c eq "4" ) {
      125.                                 print "Invalid Sensor Type for SHC_Framework\n";
      126.                                }
      127.                        else {
      128.                                print "Reply unknown\n";
      129.                                }
      130.        } else {
      131.                print "Failed to open url!";
      132.        }
      133. }
      134. # Perl trim function to remove whitespace from the start and end of the string
      135. sub trim($) {
      136.        my $string = shift;
      137.        $string =~ s/^\s+//;
      138.        $string =~ s/\s+$//;
      139.        return $string;
      140. }
      141. #
      Display All





      Gruß
      Harpi

      The post was edited 1 time, last by harpi ().