summaryrefslogtreecommitdiffstats
path: root/radiator-plc/mt300n-v2
diff options
context:
space:
mode:
authorfishsoupisgood <github@madingley.org>2020-09-09 11:53:37 +0100
committerfishsoupisgood <github@madingley.org>2020-09-09 11:53:37 +0100
commit9d87c925a9eaa4fc256be3173c14a20d1469472d (patch)
tree50d63f87a47a0eac3f5b8058850184bcd4e6ee95 /radiator-plc/mt300n-v2
parentdafd8cf2fdcdd637cc06f760d318cf8391b1a294 (diff)
downloadheating-9d87c925a9eaa4fc256be3173c14a20d1469472d.tar.gz
heating-9d87c925a9eaa4fc256be3173c14a20d1469472d.tar.bz2
heating-9d87c925a9eaa4fc256be3173c14a20d1469472d.zip
everything, mostly, working
Diffstat (limited to 'radiator-plc/mt300n-v2')
-rw-r--r--radiator-plc/mt300n-v2/.gitignore1
-rw-r--r--radiator-plc/mt300n-v2/Makefile6
-rwxr-xr-xradiator-plc/mt300n-v2/usr/bin/mqtt_if.pl76
-rwxr-xr-xradiator-plc/mt300n-v2/usr/bin/run_mqtt_if6
4 files changed, 89 insertions, 0 deletions
diff --git a/radiator-plc/mt300n-v2/.gitignore b/radiator-plc/mt300n-v2/.gitignore
new file mode 100644
index 0000000..859afb1
--- /dev/null
+++ b/radiator-plc/mt300n-v2/.gitignore
@@ -0,0 +1 @@
+stamp
diff --git a/radiator-plc/mt300n-v2/Makefile b/radiator-plc/mt300n-v2/Makefile
new file mode 100644
index 0000000..cdf9d24
--- /dev/null
+++ b/radiator-plc/mt300n-v2/Makefile
@@ -0,0 +1,6 @@
+STUFF=$(shell find usr \! -type d -print)
+
+stamp: ${STUFF}
+ tar cf - ${STUFF} | ssh radiator-plc0 "cd / && tar xvpf -"
+ #tar cf - ${STUFF} | ssh radiator-plc1 "cd / && tar xvpf -"
+ touch $@
diff --git a/radiator-plc/mt300n-v2/usr/bin/mqtt_if.pl b/radiator-plc/mt300n-v2/usr/bin/mqtt_if.pl
new file mode 100755
index 0000000..f81b6e0
--- /dev/null
+++ b/radiator-plc/mt300n-v2/usr/bin/mqtt_if.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/env perl
+
+use strict;
+
+use Device::SerialPort;
+use Net::MQTT::Simple;
+
+sub chomp_harder($) {
+ my $ret = shift;
+ my $irs = $/;
+ $/ = "\n";
+ chomp $ret;
+ $/ = "\r";
+ chomp $ret;
+ $/ = $irs;
+ return $ret;
+}
+
+sub get_line($$) {
+ my ( $port, $mqtt ) = @_;
+
+ my $ret = "";
+
+ while (1) {
+ my ( $count, $byte ) = $port->read(1);
+ $mqtt->tick();
+
+ if ( $count > 0 ) {
+ $ret .= $byte;
+ return chomp_harder($ret) if $byte eq "\n";
+ }
+ else {
+ $ret = chomp_harder($ret);
+ return $ret;
+ }
+ }
+}
+
+my $plc_port = "/dev/ttyS1";
+my $plc = new Device::SerialPort($plc_port) || die "can't open $plc_port\n";
+
+$plc->baudrate(38400);
+$plc->parity("none");
+$plc->databits(8);
+$plc->stopbits(1);
+$plc->stty_icanon(0);
+
+$plc->read_char_time(0);
+$plc->read_const_time(1000);
+
+my $mqtt = Net::MQTT::Simple->new("10.32.139.1");
+
+sub mqtt_msg($$) {
+ my ( $topic, $message ) = @_;
+
+ $plc->write("MQTT $topic $message\r\n");
+}
+
+$mqtt->subscribe( 'cmnd/+/var1', \&mqtt_msg );
+$mqtt->subscribe( 'cmnd/+/var2', \&mqtt_msg );
+$mqtt->subscribe( 'cmnd/+/var3', \&mqtt_msg );
+
+while (1) {
+ my $line = get_line( $plc, $mqtt );
+ next unless length($line) > 0;
+
+ if ( $line =~ /MQTT\s+([^\s]+)\s+(.+)/ ) {
+ $mqtt->publish( $1, $2 );
+ }
+
+ if ( $line =~ /MQTTR\s+([^\s]+)\s+(.+)/ ) {
+ $mqtt->retain( $1, $2 );
+ }
+
+}
+
diff --git a/radiator-plc/mt300n-v2/usr/bin/run_mqtt_if b/radiator-plc/mt300n-v2/usr/bin/run_mqtt_if
new file mode 100755
index 0000000..2c06f6b
--- /dev/null
+++ b/radiator-plc/mt300n-v2/usr/bin/run_mqtt_if
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+while true; do
+ /usr/local/bin/mqtt_if.pl
+ sleep 1
+done