TCP - Activate and Send Respone through Rule

Hey,

i want to communicate with my Raspberry Pi to OpenHAB 2 (running on the pi) through TCP and want to receive some item-specific data like the name and state, preferably in a JSON format. I want do send a Request from the Pi to OH2 and OH2 should send a Response back with the json string containing all information i need.
But i don’t understand how to setup the TCP-Binding correctly.

The documentation says something about:

tcp=">[ON:192.168.0.1:3000:'MAP(my.device.map)')], >[OFF:192.168.0.1:3000:'MAP(my.device.map)']"

But…

My first Question:
Which IP do I have to use? My OpenHAB runs on localhost on Port 8080.

My second Question:
How can i send the TCP Request to a rule, which sends a String back as Respone?

My third Question:
Does the TCP-Binding only works with items like switch, slider… or works it also with an placeholder-item like a String?

I’m a beginner in TCP and OH2, so please explain my Questions with examples.

Is there a reason the built in REST API won’t work for you?

Those two examples are outgoing TCP binding configs. So the IP address is the address of the machine you are trying to connect to from OH. For an incoming binding config, as it says in the docs

< for inbound-triggered communication, whereby the openHAB runtime will act as a server and listen for incoming connections from the specified <ip address>:<port>

Set up an Item with an inbound TCP binding config to receive the incoming request. Write a Rule that triggers when the Item receives and Update. In the Rule sendCommands with the data you want to return to an Item bound to outgoing TCP binding configs to communicate back.

NOTE: The communication will be over two separate sockets, not a back and forth on the same socket. OH does not support that sort of connection.

It works with any type of Item so long as the received data is valid for that Item type (e.g. you can use a Number Item only if the received data is parsable into a number).

I’d recommend some other approach. OH is hard. Working with raw TCP is hard. If you are a beginner in both this is going to be very difficult for you. The OH REST API is built for this sort of thing.

Thank you very much for your answer,

so if i want to use an Placeholder-Item for an inbound TCP, is there any need of an IP or how can i handle this?
And so the same: the outbound TCP which IP should i use there? The same IP as of the inbound TCP-Item ?

I guess the “machine” i want to address is only the OpenHAB server itself, so does the localhost IP does the job?

And to answer your Question: I want to learn to use socket-connections instead of connections over http-protocols.

Then I suggest that you learn them outside of OH first. Seriously, this stuff is hard and trying to learn both at the same time is going to make it really really hard. And OH does not provide a standard TCP set of capabilities which is going to make it even more difficult.

Learn them both independently and then tackle trying to use them together. Even seasoned experienced developers often fail to get the TCPO binding to work the way they need it to in OH.

What do you mean by this statement? What is a “Placeholder-Item”?

As it says in the docs (quoting again):

< for inbound-triggered communication, whereby the openHAB runtime will act as a server and listen for incoming connections from the specified <ip address>:<port>

So yes, the IP address and port are required and they are the address or port of the machine that will be connecting to OH. The remote machine’s address and port from OH’s perspective.

> for outbound-triggered communication, whereby the openHAB runtime will act as a client and establish an outbound connection to the specified <ip address>:<port>

So you should use the address and port of the machine OH needs to connect to.

Probably. But you need to realize the implication of this for your other program. It needs to set itself up as a server and a client. It will send a message to OH on one socket and get the response on a different socket. I’ve coded this sort of thing before. It is very difficult to get right because there are so many error cases and limitations and the need to deal with multithreaded code and such.

Again I strongly recommend learning the basics of TCP and socket programming independently from OH first. There are tons of internet tutorials for the programming languages of your choice. Then the OH part will make a lot more sense and you can make intelligent decisions on how to approach it, and the docs will make a whole lot more sense.

Only if you want OH to talk to itself or the program OH is talking to is on the same machine.

Thank you really much again for your answer,

What is a “Placeholder-Item”?

With the term “Placeholder-Item” I meant an item without a real connection to a device or some hardware component so e.g. a switch just to trigger the rule.

Only if you want OH to talk to itself or the program OH is talking to is on the same machine.

Thats right, I want to communicate between OH and a java-based programm on the same machine.

So my Question again: is the required IP for the switch oder target-item without a connection to any hardware in this case my localhost-IP?

And another last Question: Can I choose any Port I want or is it limited on the port 8080 where I’ve installed OpenHAB on it.

OK, we use “Virtual Item” or “Unbound Item” for that. Sometimes you will see it called a “Dummy Item.”

OK, in that case, no, it wouldn’t be an unbound Item because it will need to be bound to the incoming TCP connection.

Without quoting the docs yet again, which answer you question…

  • Yes the IP and Port are required for both incoming and outgoing binding configs
  • For incoming configurations, the IP address needs to be the IP address and port the connection is coming from. Since you are on the same machine the IP address will be localhost. The port will be what you configure your program to use.
  • Four outgoing configurations, the IP address need to the the IP address and port the connection is going to. Since you are on the same machine the IP address will be localhost. The port will be whatever you configure your Java program to listen on.

This question and the fact that you don’t seem to understand the TCP binding docs makes me once again strongly recommend learning how to use TCP independently of OH. Your chances of success trying to figure this out with OH is pretty close to zero and if you are successful it will take you much more effort and take much more time then if you learn each separately and then apply what you learn to combine the two.

1 Like

Sorry, just putting a thought out there… Why TCP?
MQTT? Simple, reliable, infinitely flexible…

try this simple…Java Socket Programming