TCP connection with an Arduino

Hi,

I want to replace my existing HomeAutomation Server with OpenHAB.
At the heart of my system, there are sevral Arduino’s with Ethernet connection, to control the following functions: Shutters, Weather Station, Heating System, SmartMeter and a Network of OneWire Temerature Sensors.

However, the Arduino’s are all over the House, and it can happen sometimes that due to a mains power failure, they disconnect from the Network shortly to be available agin moments later.

Therefore, I need to make sure, that I have an error tolreant and reliable comminicatuiion with my Arduinos.
But that seems to be not that easy with OpenHAB…

I created an ordinariy TCP binding:
String Arduino “Arduino [%s]” (arduino) {tcp=">[192.168.1.123:23:‘REGEX((.*))’]"}
Switch Arduino_Sw1 “Arduino Schalter”

And a very simple rule, to test the connection:
rule “Arduino_Send”
when
Item Arduino_Sw1 received command
then
if(receivedCommand==ON) {
sendCommand (Arduino , “Lights=ON”)
} else {
sendCommand (Arduino , “Lights=OFF”)
}
end

Everything is running fine, if the Arduino is on the Network, and when I start up OpenHAB. I can talk to it, using my switch. Here is the Startup Script, and the operation of the Switch.
The receiveing end at the Arduino is also working well.

Launching the openHAB runtime…
osgi> 2015-09-07 03:49:58.494 [INFO ] [.o.core.internal.CoreActivator] - openHAB runtime has been started (v1.7.1).
… more …
2015-09-07 03:50:16.458 [INFO ] [t.AbstractSocketChannelBinding] - The maximum buffer will be set to the default value of 1024
2015-09-07 03:50:16.464 [INFO ] [t.AbstractSocketChannelBinding] - The interval to retry connection setups will be set to the default value of 5
2015-09-07 03:50:16.465 [INFO ] [t.AbstractSocketChannelBinding] - The cron job to reset connections will be set to the default value of 0 0 0 * * ?
2015-09-07 03:50:16.466 [INFO ] [t.AbstractSocketChannelBinding] - The setting to queue write operation until a channel gets connected will be set to the defaul
t value of true
2015-09-07 03:50:16.466 [INFO ] [t.AbstractSocketChannelBinding] - The port to listen for incoming connections will be set to the default value of 0
2015-09-07 03:50:16.466 [INFO ] [t.AbstractSocketChannelBinding] - The setting to share channels within an Item will be set to the default value of true
2015-09-07 03:50:16.467 [INFO ] [t.AbstractSocketChannelBinding] - The setting to share channels between the items with the same direction will be set to the de
fault value of true
2015-09-07 03:50:16.467 [INFO ] [t.AbstractSocketChannelBinding] - The setting to share channels between directions will be set to the default value of true
2015-09-07 03:50:16.467 [INFO ] [t.AbstractSocketChannelBinding] - The setting to use address masks for incoming connections will be set to the default value of
true
2015-09-07 03:50:16.468 [INFO ] [t.AbstractSocketChannelBinding] - The refresh interval of the worker thread will be set to the default value of 250
2015-09-07 03:50:16.469 [INFO ] [t.protocol.internal.TCPBinding] - The maximum time out for blocking write operations will be set to the default vaulue of 3000
2015-09-07 03:50:16.470 [INFO ] [t.protocol.internal.TCPBinding] - The blocking nature of read/write operations will be set to the default vaulue of false
2015-09-07 03:50:16.471 [INFO ] [t.protocol.internal.TCPBinding] - The preamble for all write operations will be set to the default vaulue of
2015-09-07 03:50:16.472 [INFO ] [t.protocol.internal.TCPBinding] - The postamble for all write operations will be set to the default vaulue of
2015-09-07 03:50:16.472 [INFO ] [t.protocol.internal.TCPBinding] - Updating states with returned values will be set to the default vaulue of true
2015-09-07 03:50:16.471 [INFO ] [.service.AbstractActiveService] - TCP Refresh Service has been started
2015-09-07 03:50:16.475 [INFO ] [t.AbstractSocketChannelBinding] - Connecting the channel Channel [item=Arduino, command=0, direction=OUT, remote=/192.168.1.123:23, buffer=, isBlocking=false, isReconnecting=false, channel=, host=192.168.1.123, port=23]
2015-09-07 03:50:16.497 [INFO ] [.service.AbstractActiveService] - HTTP Refresh Service has been started
2015-09-07 03:50:16.728 [INFO ] [t.AbstractSocketChannelBinding] - The channel for /192.168.1.123:23 is now connected
2015-09-07 03:50:16.754 [INFO ] [.service.AbstractActiveService] - NTP Refresh Service has been started
… more …

2015-09-07 03:50:40.561 [INFO ] [runtime.busevents ] - Arduino_Sw1 received command ON
2015-09-07 03:50:40.636 [INFO ] [runtime.busevents ] - Arduino received command Lights=ON
2015-09-07 03:50:40.649 [INFO ] [runtime.busevents ] - Arduino state updated to Lights=ON
2015-09-07 03:50:40.698 [INFO ] [runtime.busevents ] - Arduino_Function state updated to Lights=ON

2015-09-07 03:50:43.425 [INFO ] [runtime.busevents ] - Arduino_Sw1 received command OFF
2015-09-07 03:50:43.456 [INFO ] [runtime.busevents ] - Arduino received command Lights=OFF
2015-09-07 03:50:43.458 [INFO ] [runtime.busevents ] - Arduino state updated to Lights=OFF
2015-09-07 03:50:43.469 [INFO ] [runtime.busevents ] - Arduino_Function state updated to Lights=OFF

However, once I reset the Ardunino Board, I can not recover from the error… the log is running full with Warnings, but I never get a connection again… What am I doing wrong? Or have overlooked.

2015-09-07 03:53:47.640 [WARN ] [t.AbstractSocketChannelBinding] - The channel for /192.168.1.123:23 has encountered an unknown IO Exception: An existing connection was forcibly closed by the remote host
2015-09-07 03:53:47.892 [WARN ] [t.AbstractSocketChannelBinding] - The channel for /192.168.1.123:23 has encountered an unknown IO Exception: An existing connection was forcibly closed by the remote host
2015-09-07 03:53:48.143 [WARN ] [t.AbstractSocketChannelBinding] - The channel for /192.168.1.123:23 has encountered an unknown IO Exception: An existing connection was forcibly closed by the remote host
2015-09-07 03:53:48.394 [WARN ] [t.AbstractSocketChannelBinding] - The channel for /192.168.1.123:23 has encountered an unknown IO Exception: An existing connection was forcibly closed by the remote host

I am greateful for any tip.

Can’t really help with your problem directly, but have you considered using a different protocol to communicate with your Arduino nodes? If they are all on Ethernet then it might be worth trying out MQTT (Google the PubSubClient Arduino library).

I have a number of Arduinos using this library which all connect to a central MQTT broker on my LAN. Then openHAB connects to the same broker using the MQTT binding and I can send/receive data to/from the Arduino nodes with very high reliability.

If a node goes down you will get a LWT (last will and testament) from the broker indicating the Arduino is no longer connected to the broker, which you can easily monitor in openHAB.

Check out https://github.com/sumnerboy12/hardwicke/blob/master/mqtt_security2/mqtt_security2.ino for an example of one of my sketches.

1 Like

Thank you very much, MQTT might be my last resort… However, I have hacked my complete Vissmann (something that was not even designed to be hooked up to a Network) and remotely control every parameter of it with Failsafe, bells and whistles. All with a Telnet interface.

I rather wish not to rewrite everything. Same is true for my SmartMeter Interface, or the One-WireServer.

If I can’t get the basics to work myself, I rather leave the hands off… I am not really happy with the Home Automation Server I am currently using… but TCP connection that can fall back on it’s legs after a disconnect is essential.

Is there not someone knowing the inner workings and parameters of that TCP-Binding?
Thank you very much in advance for your post.

@kgoderis any idea?

The exception stems from the underlying NIO library. It is a long time I had a look at the TCP binding code, but it could be very much true that it is not able to recover from a disconnect initiated by the remote end. Honestly, I can’t remember right now how it should behave.

To be clear:
The Remote end has just disconnected once, because of a reset, or a power glitch. When I contact it for example with a telnet, it is there with its welcome message and ready to reply.
I would do whatever is needed, to get this sorted, as it is one of the most basic connections.

The TCP binding always has been a kind of swiss knife in which we tried to accomplish a lot of things at the same time. It is more of a showcase than anything else, and if the protocol/interaction with the remote device is getting very specific, then a proper binding is the best solution. If you are fluent with java then i would suggest to step through the code in the IDE debugger. I quickly looked at the code (https://github.com/openhab/openhab/blob/master/bundles/binding/org.openhab.binding.tcp/src/main/java/org/openhab/binding/tcp/AbstractSocketChannelBinding.java#L1478), and when the error occurs, it should reconnect to your device after the interval specified in the configuration file. I have no clue why this is not happening in your case

Thanks, unfortunately I have not done anything in Java yet. 
I get around with a bit of C, Pascal, DotNet, and a few assembler languages.
Thanks for the tip I will invest some more time here.
You know, I have a power failure a few times per year. There is no problem, with the Server, as it has a UPS, but all the other devices can reset at any time. The Home Automation system has to be able to fall back on its feet, regardless how the boot order is. Things have to be failsafe!
By the way, I am a bit frustrated with the documentation. OpenHAB is a system from programmers for programmers. I am missing good foundations, tutorials some step-by-step instructions and descriptions. Having a few PPT’s, a Demo Script, and a Community is not doing the trick for a wider adoption.

Hauke

Life will be better under openHAB 2 . Most of our effort is currently going to that release, which OH 1 being maintained “as is”. It explains our limited involvement on OH1. in OH2, there will be an easy GUI driven way to configure devices and so forth.

But, if you get around with C or .NET, then java will not be a problem :wink: hey, i used to do Ada and Cobol in the past, and now I dream java at night :wink:

hi,

@newbee, did you have some details for your “vissmann hack”?

regards

@ben_jones12: just checked some of your sketches and they are awesome, thank you for sharing.

1 Like