diff options
author | fishsoupisgood <github@madingley.org> | 2021-01-24 10:12:38 +0000 |
---|---|---|
committer | fishsoupisgood <github@madingley.org> | 2021-01-24 10:12:38 +0000 |
commit | 25fbb8a03c72f473a2d7f6cfb5961a1c43b1a6a2 (patch) | |
tree | d7292ae8122c45672d9e7080d246cb8b8563602a /piano-alarm/mt300-v2/usr/bin/sensor.pl | |
parent | 77f347fb4e37666e2d687cee99dfa4cb4e441b8a (diff) | |
download | heating-25fbb8a03c72f473a2d7f6cfb5961a1c43b1a6a2.tar.gz heating-25fbb8a03c72f473a2d7f6cfb5961a1c43b1a6a2.tar.bz2 heating-25fbb8a03c72f473a2d7f6cfb5961a1c43b1a6a2.zip |
add piano alarm
Diffstat (limited to 'piano-alarm/mt300-v2/usr/bin/sensor.pl')
-rwxr-xr-x | piano-alarm/mt300-v2/usr/bin/sensor.pl | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/piano-alarm/mt300-v2/usr/bin/sensor.pl b/piano-alarm/mt300-v2/usr/bin/sensor.pl new file mode 100755 index 0000000..9a5501d --- /dev/null +++ b/piano-alarm/mt300-v2/usr/bin/sensor.pl @@ -0,0 +1,93 @@ +#!/usr/bin/env perl + +use strict; + +use Device::SerialPort; + +sub get_line($) { + my $port = shift; + + my $ret = ""; + + while (1) { + my ( $count, $byte ) = $port->read(1); + + if ( $count > 0 ) { + $ret .= $byte; + return $ret if $byte eq "\n"; + } + else { + return $ret; + } + } +} + +sub display($$$$$$) { + my ( $t, $h, $show_t, $show_h, $int, $mrtg ) = @_; + + if ( $int or $mrtg ) { + $t = int( $t * 100 ); + $h = int( $h * 10 ); + } + + if ($mrtg) { + if ($show_t) { + print "$t\n"; + print "0\n"; + print "long time\n"; + print "temperature\n"; + } + + if ($show_h) { + print "$t\n"; + print "0\n"; + print "long time\n"; + print "humidity\n"; + } + } + else { + print "$t\n" if $show_t; + print "$h\n" if $show_h; + } +} + +my $keypad_port = "/dev/ttyS1"; +my $keypad = new Device::SerialPort($keypad_port) + || die "can't open $keypad_port\n"; + +$keypad->baudrate(115200); +$keypad->parity("none"); +$keypad->databits(8); +$keypad->stopbits(1); +$keypad->stty_icanon(0); + +$keypad->read_char_time(0); +$keypad->read_const_time(1000); + +my $mrtg = 0; +my $temp = 0; +my $humid = 0; +my $int = 0; + +for my $opt (@ARGV) { + $mrtg = 1 if $opt eq '-m'; + $temp = 1 if $opt eq '-t'; + $humid = 1 if $opt eq '-h'; + $int = 1 if $opt eq '-i'; +} + +for ( my $tries = 0 ; $tries < 4 ; ++$tries ) { + my $line = get_line($keypad); + + chomp $line; + chomp $line; + + if ( $line =~ /\$SNTHD,([-.0-9]+),([-.0-9]+)/ ) { + + display( $1, $2, $temp, $humid, $int, $mrtg ); + exit 0; + } +} + +exit 1; + |