[SOLVED] Optoma beamer via RS232 over TCP/IP connector

I try to get my projector (Optoma HD25) connected to OpenHAB. It has a RS232 serial port. As the projector is mounted to the ceiling and far away from my OpenHAB server, I want to use a RS232-to-Ethernet converter (I use a USR-TCP232-302). It is set up as a TCP server now.
I can connect via putty (9600bps 8N1) and it works well. I can send commands on the telnet and recieve the answer. Also if switched on/off by the remote I get the string in the telnet.
Now I look for a binding to use with openHAB. What I need are two items to switch it on/off and to get the status as a string (which I will interpret by a rule):

String Projector_Status "Beamer Status [%s]" (gTV) { ??? }
Switch Projector_OnOff "Beamer OnOff [%s]" (gTV) { ??? }

I tried to get it work with the TCP-UDP binding (without a tcp.cfg file) like

tcp="<[192.168.X.XX:YYYY:'~01150 1']"
tcp=">[ON:192.168.X.XX:YYYY:'~0100 1'], >[OFF:192.168.X.XX:YYYY:'~0100 2']"

for the ???. But this does not work. Any idea whatā€™s wrong here and what I have to put for ???

I think the Exec Binding can solve your Problems ā€¦

rule "TV power"
	when 
	  Item TV received command ON
	then
      executeCommandLine("HERE THE COMMAND TO SWITCH ON THE BEAMER")
end

here is a Example for telnet in bash script

My Way is a Raspberry Pi Zero W with USB to RS232 Adapter and a little NodeJS App

Thank you. Thatā€™s not quite what I was looking for, but this gave me a new idea.
I redirected the TCP to a virtual COM port on my Openhab Sever with

sudo socat pty,link=/dev/vircom0,raw  tcp:192.168.X.XX:YYYY&
sudo chown openhab /dev/vircom0 

a local serial console to /dev/vircom0 works as before. Now I thought I can use the serial binding :slight_smile: But how to get this unusual port accessible in OH2 ? Where to add the

-Dgnu.io.rxtx.SerialPorts=/dev/vircom0

as described in the documentation ?

Sorry for delayed reply. I run openHAB2 from a manual installation while testing. The doc says there is no preconfigured configuration file (https://docs.openhab.org/installation/linux.html#file-locations). Unfortunately googling did not find anything useful and I have to ask again here. So how do I set up a configuration file for a manual installation?

sudo nano /etc/default/openhab2

???

unfortunately it is not that easy. I tried this and put the line

EXTRA_JAVA_OPTS="-Dgnu.io.rxtx.SerialPorts=/dev/vircom0"

into the file. But OH2 gives the error

Could not open serial port /dev/vircom0: Serial port '/dev/vircom0' could not be found. Available ports are:
/dev/ttyUSB1
/dev/ttyUSB0
/dev/ttyS0

as soon as it tries to open the serial port for my item.
The virtual port is there, and I can read/write via putty to it.

file settings are:

-rw-r--r-- 1 openhab root 57 Jan 24 21:14 /etc/default/openhab2
lrwxrwxrwx 1 root    root 11 Jan 21 12:57 /dev/vircom0 -> /dev/pts/22
crw--w---- 1 openhab tty 136, 22 Jan 21 13:01 /dev/pts/22

any other idea?

I gave up on the virtual COM port and tried the TCP binding from scratch. It works exactly the way I posted it in the first post. I just had to make sure that the data sent end with a ā€œ/r/nā€. This can be done either in the tcp.cfg or in the submitted string.

I was not really satisfied with the above solution and the TCP binding is unstable. So I did a totally other appraoch. I already have some MQTT items and why not let the projector chat with openHAB via MQTT?
My solution is a small python script that does the work. Just in case it would be handy for someone:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    __author__ = "JĆ¼rgen Gluch"
    """
     Copyright 2018 JĆ¼rgen Gluch
     Script:  beamer_tcp_mqtt.py
     Author:  JĆ¼rgen Gluch
     Purpose: Communicate with Projector RS232 over the TCP-RS232 bridge with MQTT messages
    """

    import socket
    import paho.mqtt.client as mqtt

    # define the TCP conection to the beamer
    TCP_IP = '192.168.XX.XX'
    TCP_PORT = 23
    BUFFER_SIZE = 128

    # define the MQTT connection for communication with openHAB
    broker = "192.168.XX.XX"
    username = "mqtt"
    password="XXXXXX"

    # send data to beamer
    def on_message_tobeamer(mosq, obj, msg):
      mqttmsg = str(msg.payload.decode("utf-8"))
      print("to beamer: " + mqttmsg )
      msgtcp = bytes(mqttmsg+"\r", 'utf-8')
      s.send(msgtcp)

    ### Main programm
    if __name__ == '__main__':
      try:
        # establish TCP connection
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((TCP_IP, TCP_PORT))
        # Add message callbacks that will only trigger on a specific subscription match.
        client = mqtt.Client()
        client.message_callback_add("groundfloor/living/projector/send", on_message_tobeamer)
        
        # Connect to MQTT and wait for messages
        client.on_message = on_message_mqtt
        client.username_pw_set(username, password)
        client.connect(broker, 1883, 60)
        client.subscribe("groundfloor/living/projector/send", 0)
        client.loop_start()
        
        # infinite loop ...
        print("Wait for signals:")
        while True:
          # receive data from beamer 
          data = str( s.recv(BUFFER_SIZE) )
          # cut the "b'" at front and "\r" at end
          data = data[2:len(data)-3] 
          print("Received data from beamer: " + data)
          client.publish("groundfloor/living/projector/get", data)

      except KeyboardInterrupt:
        print("Stopped by user")
      except:
        print("Exit by error")
      finally:
        s.close()
        print("The End.") 
2 Likes

Thanks JGluch, that sounds like a great idea, and nice and simple script. TCP binding seems to be majorly broken, it spams reconnect or not connected messages 2. sec initially, and then after a day ups it to around 10.000 a second. And it doesnā€™t reconnect for me.

I tried your script, I installed python3 and ā€˜pip install paho-mqttā€™.
I also added ā€˜raiseā€™ after print(ā€œExit by errorā€).

Iā€™m missing ā€˜on_message_mqttā€™.

2 Likes

Hi JĆ¼rgen,

please can you explain me, how I can start your script. I have also an Optoma beamer and I want to switch on and off the beamer with a rule.

Tanks,
Christoph

Sorry for the late reply. I start the python script in a console on my server and let it run there. Like this:

python3 beamer_tcp_mqtt.py

Hi

I just found this post and I would like to use your script to drive my projector but I donā€™t know how to declare my thing in openhab. can you help me.
FYI Iā€™m a beginner in everything :wink: