Restoring network binding in OH 3.2 (was working on OH 2.5)

Hi all, in the last days I migrated from OpenHab 2.5 (running on NetBook with ATOM CPU) to OpenHab 3.2 (now running on a Raspberry Pi3)

My configuration involves sending from OH some TCP packets to localhost at given port, where an external TCP server (a QT application of mine running on same machine) listen to the OH client.

It was working perfectly, so I would like to restore the OH 3.2 “network” functionalities as I had in previous OH 2.5. (QT application is kept the same)

I work mainly with " File-based configuration", mainly because I have all ready (again, I want to perform a porting to new OH/Pi3, not a new development).

I used to achieve the TCP sending by some rules like that

rule "GF_LivingRoom_Door"
	when 
		Item GF_LivingRoom_Door received command
	then
		// some logics here...
		val outStr = String::format("$%1$02d_%2$d#",DOORWAY_SPOTLIGHT,strVal)		
		sendCommand(txCmdString, outStr)
end

where txCmdString is an item

String txCmdString "txCmdString" (All) {tcp=">[127.0.0.1:1234:JS(sendValue.js)]"}

Now, when I act on GF_LivingRoom_Door item I see in log, as expected:

*2021-12-28 23:07:11.005 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'txCmdString' received command 
*2021-12-28 23:07:11.010 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'txCmdString' changed from...to...

but nothing reaches TCP server.

Sadly I don’t remember all the setup I did in OH 2.5, what am I missing? Some hints:

  • I installed the Network binding
  • weirdly, I didn’t find the network.cfg file under /etc/openhab/services…should I create it manually?
  • unfortunately, I don’t remember why I added JS(sendValue.js)] to my item, maybe am I missing some other binding/transformation, like JS?
  • I have the backup of old OH 2.5, in case I need to retrieve some info from previous setup

If you have any suggestions how to send data TCP data (TCP server expects just strings) to given host:port, please tell me.

My only constraint is I want to keep the previous setup, so please do not come saying “try MQTT/CURL/other mechanism…” as I definitely prefer to keep the same configuration was working properly with OH 2.5

Thanks,
Enzo

This Item was linked to the TCP version 1 binding.

No version 1 bindings are available in OH3.
Unfortunately the version 3 TCP binding is still experimental. Details here-

This has nothing to do with OH3 Network binding

That can be done using the linux commands nc resp. netcat by using executeCommandLine resp. the exec binding.

That can be done using the linux commands nc resp. netcat by using executeCommandLine resp. the exec binding.

Ok, sadly what you wrote it is quite cryptic and is not familiar to me. Can you please address me to a tutorial page which explains what you mean?
I need an example which drives me in sending TCP (even UDP) packets from OH to a generic TCP server.

Thanks a lot!

I am using rules to e.g. turn on (1) / off (0) outdoor lights by using a switch that undertands UDP commands:

executeCommandLine( Duration.ofSeconds(5), "/etc/openhab/scripts/outdoorlights.sh", "OUT4", "0" )

OUT4 is the port that is being used.
0 stands for turn off.

The command in the shell script itself then is:

#!/bin/bash
ip="IP_or_DNS_name_here"

switch=$1
OnOffStatus=$2

echo -n "$switch $OnOffStatus" | nc -4u -q1 $ip 30303

echo -n is required to not use return/newline at the end.
$switch contains the first argument which is OUT4.
$OnOffStatus contains the 0
nc is the command ( netcat )
-4u means UDP ( IPv4 )
-q1 wait one second to finish the command
$ip is set to the IP of the switch
30303 is the port that is being used

Further information can be found in the manual page: man nc
Intro e.g. at Introduction to NetCat | Engineering Education (EngEd) Program | Section

1 Like

Thx Wolfgang_S, I tried your suggestion and it works, I can send data from OH to TCP server via netcat (NC)

I have 2 questions:

  1. I noticed every time the rule is engaged, hence the script is run, OH performs a connection via NC to TCP server, then data transfer, then a disconnection. Upon new rule engagement, same trip again.
    Is there any way in OH to start a single NC connection to the server on init, then just send data on demand?
  2. It would be possible to receive also data from TCP server? I can still use NC somehow in OH?

Thanks for your support, again with easy words :wink:

Of course everyone is welcomed!

  1. I think that is not possible. Nevertheless sending data “on demand” together with the connection is what nc does
  2. receiving data should be possible. I have not tried that yet from within a OH rule. Will data be sent as a response to data that was sent via a nc channel or would this mean listen and wait until … somewhen data is sent to nc ?

Will data be sent as a response to data that was sent via a nc channel or
would this mean listen and wait until somewhen data is sent to nc ?

The latter (that is, OH waits forever for any incoming data over TCP), as a general purpose asynchronous way for sending data from external TCP server to OH.
It would be nice to understand if it would be possible, indeed!

For the moment, I will manage:

  • data from OH to TCP server: usage of nc (the only precaution I will adopt is to close the link on server side once data has been received, to avoid multiple/memory leaking connection
  • data from TCP server to OH: usage of curl, to directly act on OH items from TCP server. Eg:
    curl -X POST --header “Content-Type: text/plain” --header “Accept: application/json” -d “OFF” “http://127.0.0.1:8080/rest/items/G_Porch_SystemSts”, to switch OFF from TCP server the item called G_Porch_SystemSts

Thank you very much and happy new year

As long as it is possible to add and run curl from within your TCP server I would use that.
In case only would be able to send specific key words to a specific port on a specific server you could use e.g. a python script that listens on that port for e.g. specific words and translates them to a web request ( like in your curl example ).
This may help to get an idea about it:
https://pymotw.com/2/socket/tcp.html

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.