Arduino connected to RPi3 serial port

I’m trying to create my own 433MHz receiver with a Arduino mini connected to the serial port of my Openhabian RPi3.

I have installed the RCSwitch sketch on the Arduino and changed it so it only sends the code received.
also I have configured Openhabian to enable the serial port and installed the serial binding.

So far so good.

When I set up the serial item as a string item like:
String keyfobs "Håndsender s" { serial="/dev/ttyS0" }
I get entery’s in the eventlog like:
2018-08-21 00:13:51.288 [vent.ItemStateChangedEvent] - keyfobs changed from 3982408^M to 3982404

But if I change the item to a switch like:
Switch keyfob "Håndsender 1" { serial="/dev/ttyS0,OFF(3982404\r),ON(3982402\r)" }

The item only change state on first received transmission, the following changes nothing no matter what code is being transmitted.

I have also tried to use regex like:
Switch keyfob "Håndsender 1" { serial="/dev/ttyS0,REGEX(s/3982402/ON),REGEX(s/3982404/OFF)" }

Still no dice.

I have tried to change the termination charecter sent by the Arduino. linefeed(newline), carriage return or both, no difference.

Am I doing something wrong or did I hit a bug in the serial binding?

I’m using Openhab 2.3.0 release btw.

I don’t know if it is relevant but in the binding README, they are listed in ON/OFF order. You have that reversed. Order usually matters in OH so perhaps it matters here as well.

Switch keyfob "Håndsender 1" { serial="/dev/ttyS0,ON(3982402\r),OFF(3982404\r)" }

Are you certain you are only getting one of these commands at a time and not two in a row as one message?

Maybe you need to add the /g to the REGEX to match globally? I’m pretty sure you would need to add the \r in your regular expression to match the full message for the substitution to work as expected.

Thanks Rich,

Maybe I should have been more clear about that.
I switched ON and OFF because when it item is first initialized (refreshed by openhab), it does actually switch form NULL to ON. But after that I can’t get it to switch from ON to OFF.
If I switch it “manually” through the sitemap, it does switch to OFF and I am one again able to switch it to ON.

When I swap ON and OFF in the item configuration, it works the other way around: it can go OFF but not ON. (regardless of the codes, if I put the OFF code in the first definition it reacts to the OFF button/code).

I tried to add the \r in the regex, also added the /g global. But the regex does not work at all. I even tried to wildcards before and after, but I can’t get the regex to work at all (not even switching ON):
Switch keyfob "Håndsender 1" { serial="/dev/ttyS0,REGEX(s/.*10701698.*/ON/g),REGEX(s/.*10701700.*/OFF/g)" }

Yes am pretty sure I only get one command. I have looked at it using a serial terminal (screen /dev/serial0), and I have added a delay in the arduino sketch so it will wait 200mS before receiving again.

What termination character does the serial binding expect? CR, LF or both?
Or is it not expecting a termination at all? Just reacting the the first match?

I can make the Arduino output anything I want, but I would prefer it to send the remote codes (not just ON and OFF), since I wouldn’t have to reprogram it again should I add more remotes later.

I really don’t understand what I’m doing wrong here. It should be so simple.
Could it be the “funny” serial port on the RPi3?
Do you think it would be worth trying with at USB2SERIAL adapter?

Btw. I have a UZB – Z-Wave.Me connected to the same RPi3. (using USB).

PS:
If I change the item type to “String” I get the following in the event.log
2018-08-27 23:43:44.084 [vent.ItemStateChangedEvent] - keyfobs changed from NULL to 10701698

2018-08-27 23:43:50.029 [vent.ItemStateChangedEvent] - keyfobs changed from 10701698^M to 10701700

2018-08-27 23:43:52.734 [vent.ItemStateChangedEvent] - keyfobs changed from 10701700^M to 10701698

2018-08-27 23:43:56.023 [vent.ItemStateChangedEvent] - keyfobs changed from 10701698^M to 10701700

I don’t use this binding but I do know it is one of the trickiest to work with. All of the low level bindings are. I’ve no direct experience with the binding so I have no specific advice to offer unfortunately.

Since it appears to reasonably work as a String you can link the String Item to the Switch with a Rule. Beyond that, unless someone else comes in all I can offer is to experiment until you find something that works or give up.

Thanks.

Yes it’s properly easiest just to implement it using a rule.
I just thought that sine I have control of both ends: both sender (Arduino) and receiver (OpenHAB) it would be a walk in the park…

But yes I will try a rule. Otherwise I must rethink the setup, and maybe implement it with MQTT or something. I would just hate to add more complicity.

In my experience, MQTT is far simpler and easier to deal with MQTT than serial or direct TCP/UDP type interfaces. It is well worth setting up the broker and bindings and libraries to use it. It will also give you the flexibility to add a wireless or ethernet shield to the arduinos in the future so they don’t have to be wired up to your server.