Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comfort to MQTT
#1
Hi, i would like to share with the community my simple driver implementation of comfort to MQTT.

I have this running for a couple of months and never registered any failure with the communication. MQTT is amazingly light and mature. And my driver seems to be stable enough for the job, had some issues with strange USB resets, but now it gracefully handles every USB quirks i found, including reconnecting the UCM.

This approach provides a simple and flexible way to interact with comfort from/to multiple sources. Personally i have comfort ULTRA, individual power monitoring, solar inverter statistics all feeding data to a MQTT broker. Then I process some of that data in NodeRED and present/control everything in OpenHAB. It works really well.

I do not use the alarm section of comfort, only home automation with push buttons, relays, infrared and some KNX stuff.


Used hardware:
- UCM USB
- A x86 server with Debian Jessie installed (could be a raspberry Pi)



(1)
Connect the UCM/USB to the server and make the USB connection deterministic:

(1.1)
Find the Device / ID / Serial Number of the device:

Code:
root@debian:/# lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 002: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

root@debian:/# lsusb -d 0403:6001 -v | grep iSerial
  iSerial                 3 A7007iyv
 
 

(1.2)
From the output above we get:
- idVendor: 0403
- idProduct: 6001
- Serial Number: A7007iyv

Your data will probabily be the same, except for the unique serial number of your UCM.
Next create a file like the one below and add the right info:

Code:
nano /etc/udev/rules.d/99-usb-serial.rules
Code:
SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"0403\", ATTRS{idProduct}==\"6001\", ATTRS{serial}==\"A7007iyv\", SYMLINK+=\"comfort\"

Now everytime the USB UCM is connected th the server it will be uniquely available at /dev/comfort, regardless of other USB peripherals in the system.

(2)
Install MQTT broker and clients


http://mosquitto.org/2013/01/mosquitto-d...epository/

for the clients:
sudo apt-get install mosquitto-clients

anoher good reference with some testing:
https://pallavichaurasia94.wordpress.com...in-debian/



(3)
the script to establish a bridge between Comfort and MQTT:

(3.1)
First install the folloing perl modules:

Code:
cpan Net::MQTT::Simple
cpan IO::Select
cpan Time::HiRes

(3.2)
Find a appropriate directory, for example /opt and copy the contents of the script to a new file, for example:

Code:
nano simple_mqtt_serial_select.pl
Code:
#!/usr/bin/perl

use Net::MQTT::Simple;
use IO::Select;
use Time::HiRes qw(time);
use strict;

#############################################################
###                                                by:APR ###
###                                                       ###
### Perform the following system commands:                ###
### lsusb                                                 ###
### lsusb -d 0403:6001 -v | grep iSerial                  ###
### nano /etc/udev/rules.d/99-usb-serial.rules            ###
### SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"0403\", ATTRS{idProduct}==\"6001\", ATTRS{serial}==\"A7007iyv\", SYMLINK+=\"comfort\"
###                                                       ###
### Change the following lines to suit your configuration ###
###                                                       ###
#############################################################

my $comfort_port = \'/dev/comfort\';
my $mqtt_hostname = \'localhost\';
my $mqtt_to_comfort = \"comfort/to\";
my $mqtt_from_comfort = \"comfort/from\";

#############################################################

my $serial_location = \"off\";
my $serie =  IO::Select->new();

my $mqtt = Net::MQTT::Simple->new($mqtt_hostname);
$mqtt->subscribe($mqtt_to_comfort,\\&callback);

my $ts = time();

while(1) {

  if ( time() - $ts > 10 ) { # every 10 secs
    $ts = time();
    my $SL = `ls -al /dev/comfort 2>&1`;
    if( $SL =~ /(comfort -> .+)/ ) { # isconnected
      $SL = $1;
      if ( $serial_location eq \"off\" ) { # was plugged
        print localtime(time) . \" $SL\
\";
        $serial_location = $SL;
        open(SERIAL,  \"+<$comfort_port\") or die \"Can\'t open serial port: $!\
\";
        $serie->add(\\*SERIAL);
      }
      elsif ( $SL ne $serial_location) { # was restarted
        print localtime(time) . \" $SL\
\";
        $serial_location = $SL;
        $serie->remove(\\*SERIAL);
        close(SERIAL);

        open(SERIAL,  \"+<$comfort_port\") or die \"Can\'t open serial port: $!\
\";
        $serie->add(\\*SERIAL);
      }
    }
    else {  # is disconnected
      if($serial_location ne \"off\") { # was removed
        print localtime(time) . \"UCM disconnected\
\";
        $serial_location = \"off\";
        $serie->remove(\\*SERIAL);
        close(SERIAL);
      }
    }
  }
  $mqtt->tick(0.1);
  if($serial_location ne \"off\") {
    if ($serie->can_read(.01)) {
      my $recv = <SERIAL>;
      $recv =~ tr/\\x03\\x0D\\x0A//d;
      $mqtt->publish($mqtt_from_comfort => $recv);
    }
  }
}

sub callback {
  my ($topic, $message) = @_;
  if($serial_location  ne \"off\") { print SERIAL chr(0x03) . \"$message\\
\"; }
  else { $mqtt->publish($mqtt_from_comfort => \"OFFLINE\"); }
}

To test:
WINDOW_1: run the script

Code:
root@debian:/# perl simple_mqtt_serial_select.pl


WINDOW_2: listen for MQTT events

Code:
root@debian:/# mosquitto_sub -v -t comfort/#

WINDOW_3: post something to comfort topic MQTT

Code:
root@debian:/# mosquitto_pub -t comfort/to -m \'LI1234\'

On the WINDOW_2 you should see something like:

Code:
comfort/to LI1234
comfort/from LU01

That´s it! Now you can subscribe (listen) or post commands to the MQTT broker and interact with comfort.

Make the script boot at startup by editing the file just before \"exit 0\"

Code:
root@debian:/# nano /etc/rc.local
Code:
/usr/bin/perl /opt/simple_mqtt_serial_select.pl &

And now comfort can easily be integrated with other bits of amazing software, like openHAB, NodeRED, etc.


If there is any interest in this i can share some of my NodeRED and OpenHAB integration configuration.


Have fun!
Reply
#2
yes pleasecan this work with UCM/ETh03?
How can this allow working with openhab  etc
Reply
#3
Hi, i\'m not sure how the UCM/ETH03 works, since i don\'t have one, but i suppose everything is possible Smile.

I\'ll gather some info and post here later a working example of OpenHAB controlling comfort.
Reply
#4
So the next step after having a MQTT broker interacting with comfort is to install NodeRED
http://nodered.org/

It is really easy to set up stuff in logical blocks, however some javascript is needed for the functions.
This is my processing of comfort stuff:


The first \"flow\" updates comfort\'s time periodically. Every 10 minutes a timestamp is generated, passed through the \"Build Date Command\" and sent to the MQTT topic comfort is listening to. The function is something like this:



At the bottom of the first image, every group of received messages from Comfort are handled. For instance, i have temperature sensors, one KNX and another from a SCS that are transformed to MQTT also and sent to the broker. This way i can later read the sensors and display them on openHab. 

The \"split function is configured as shown:



and the Sensor function decodes comfort sensor messages like this:
Code:
var string = msg.payload;
var re = /^s[r\\?](..)(..)(..)/; // regex to extract LSB, MSB
var sensor = msg.payload.replace(re, \"$1\"); //get sensor id
var hex = msg.payload.replace(re, \"$3$2\"); //Store MSB LSB
var dec = parseInt(hex,16); // convert to Decimal

if ((dec & 0x8000) > 0) { // handle negative numbers   
  dec = dec - 0x10000;
}
switch(sensor) {   
  case \"00\":       
    msg.topic = \"house/floor2/livingroom/temperature\";       
    break;   
  case \"09\":       
    msg.topic = \"house/floor2/bedroom/temperature\";       
    break;   
  default:       
    msg.topic = \"unspecified\";
}

msg.payload = dec;return msg;

All processing can be done here. Imagination is the limit, everything can be done with small bits of code and simple \"flows\".

Later i will try to show some of my OpenHab setup that takes advantage of all this data.

Fell free to ask any questions.


Reply
#5
Hi guys sorry for the late update, i didn\'t want to show the openhab examples in portuguese so i kept delaying a update. Now i have changed my dashboards to show you what is the final result.

for setting up openhab to control comfort you just need to interact with the mqtt broker, still it is a bit slow learning curve but it is totally worth it!




and for example inside the TV control you can find the actual control:




The buttons listen for updates, so if i turn off my living room TV locally, i am actually pressing a KNX button that sends a telegram to the BUS that is captured by the UCM and passed on to comfort that updates a counter that is sent though serial connection that is captured by the perl driver and sent to MQTT, that broadcasts it to any listening device, where openhab is waiting, and then applies some rules to update the button status that is binded to the TV, so next time i toggle the TV i will perform the right action! Easy!  Big Grin


Reply
#6
Thats nice. Its good to see Comfort with Openhab.
We will try to test it
Reply
#7
How does the Comfort program go into this setup?
Reply
#8
Hi not sure i understand your question. The comfort software requires direct and exclusive access to the UCM, so they cannot exist in a system at the same time.

In order to use comfort apps or to upload new configuration, i normally shutdown this \"driver\" and use the socat approach described here:

http://www.comfortforums.com/view_topic.php?id=3249&forum_id=83&jump_to=13896#p13896
Reply
#9
Hi

Is there any chance you could post more of your Node-Red javascript?

I need to do exactly this as I want to start using my old comfort as essentially an i/o block for openHab.

The more reference code I can find the better.

As an aside - any thoughts on how one could go about accessing Comfort voicemail recordings from openHab?

Many thanks

Peter
Reply
#10
I\'m so pleased that I\'ve seen this thread :-)

I have just started with openHAB and am just about to have a play with MQTT (for other reasons). I\'ve been wondering for ages how I could connect Comfort to openHAB and wondered myself about MQTT as a sensible option.

I have a UCM/ETH02 which I intend to keep for Comfigurator.

I also have a UCM/232 which I plan to connect to my Raspberry Pi 3 (which is running openHAB and will also be running MQTT when I install it). I\'ve got a tiny USB to serial converter so I should be able to link my UCM/232 to the Raspberry Pi that way.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)