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):
a local serial console to /dev/vircom0 works as before. Now I thought I can use the serial binding But how to get this unusual port accessible in OH2 ? Where to add the
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?
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.")
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 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