This page last changed on Nov 24, 2014 by rberg.

Hi,

I have an Arduino mega that I setup to send UDP broadcast every 250ms with a string like this:

23:on:24:off:25:on:26:on:27on:28:off.........53:off
Where the numbers are the pin numbers and on|off the status of that pin.

So I can have all 30 port status updated every 0.25s, all in one string, sending to one UDP port.

It works for one Sensor (one udplistener). Actually it works up to 4 sensors (4 udplisteners, same UDP port), I don't know why. I guess it should give Concurrent exception for the second on?? (see below).

With this regex filter in the UDPListener:
(?<=49(on|off)
I will have the status from the arduino digital port (pin) 49. It works.. Sensor type switch.

The question is How can I have several sensors sharing the same UDPListener?

This is the error when add 5 udplisteners with the same port:
2014-11-24 01:17:49,191 ERROR [UDPListener Port: 7006]: Error in UDPListenerThread
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:894)
at java.util.HashMap$EntryIterator.next(HashMap.java:934)
at java.util.HashMap$EntryIterator.next(HashMap.java:932)
at org.openremote.controller.protocol.socket.UDPListenerCommand$UDPListenerThread.run(Unknown Source)

OR Pro 1.0.14

Thanks.

I am working on that for RaZberry. See this thread

Draft description here: (you must be logged in the forum to read that)

Posted by pz1 at Nov 24, 2014 07:35

I saw it,
The problem I have with your solution is to verify all the options (port 23, 24, ....) in just one event call.

something like this:

rule "UDPListener"
when
  Event(source=="Arduino1-UDP-Status", $v: value!="")
then
  java.util.regex.Pattern regexPattern = java.util.regex.Pattern.compile("(?<=49:\)(on|off)"); // understand :\) as : ) otherwise you would see a smiley :)
  java.util.regex.Matcher matcher = regexPattern.matcher($v.toString());
  if (matcher.find()) {
    execute.command("BTN_49, matcher.group(2));
  }

 ...... repeat for every port ....

  java.util.regex.Pattern regexPattern = java.util.regex.Pattern.compile("(?<=22:\)(on|off)"); // understand :\) as : ) otherwise you would see a smiley :)
  java.util.regex.Matcher matcher = regexPattern.matcher($v.toString());
  if (matcher.find()) {
    execute.command("BTN_22, matcher.group(2));
  }
end

Will give me error. Something like duplicated matcher, I can't remember now.

I will try to change the arduino string to AR122:on:AR123:off....AR153:off
So I can use commands AR123 ... AR153 on|off through matcher.group(1), matcher.group(2)

Posted by rberg at Nov 24, 2014 07:53

Include your code in {code}...{code} tags

Posted by pz1 at Nov 24, 2014 08:15

Done, Thanks.

Posted by rberg at Nov 24, 2014 08:27

As far as I understand it, UDPListener only reads one port? How many of these ports do you have? If there aren't to many you can consider making one listener for each. Each of those channels then gets a rule.
I assume you can not tweak the Arduino side to push it all to one receiver?

Posted by pz1 at Nov 24, 2014 08:41

I'll need a bunch. this project with the Arduinos for a full home automation, I'll use Arduinos for every thing, light switches, dimmers, window blinds, swimming pool, presence sensors, doors, windows, power monitor and whatever is possible with Analog/Digital ports.

I've done one UDP port for each Arduino port. It works like a charm, but it is not elegant at all to have hundreds of UDP packs flying at same time like a solar storm.

If there is no way to do with one string/UDP port for each Arduino
then
I'll go with one UDP port for each Analog/Digital.

Posted by rberg at Nov 24, 2014 09:18

I got it working.
Every call to matcher.find() to will move to the next match in the same string, so while() will do it.

when
  CustomState (source=="AR01-UDP-7001", $v:value!="")  
then
  java.util.regex.Pattern regexPattern = java.util.regex.Pattern.compile("([0-9][0-9]):(on|off)");
  java.util.regex.Matcher matcher = regexPattern.matcher($v.toString());
  while (matcher.find()) {
	execute.command("AR01-" + matcher.group(1),matcher.group(2));
  }
end


Here you can see it working. http://youtu.be/9kSBTUN_2as
I've tested changing state at all ports at same time, it is almost real time response.

Posted by rberg at Nov 25, 2014 05:31

Good news, congrats! One question remaining:

The problem I have with your solution is to verify all the options (port 23, 24, ....)

How did you handle all these different UDP ports?

Posted by pz1 at Nov 25, 2014 08:25

It is just one UDPListener for each Arduino. The Arduino Mega has 54 Digital + 16 Analog. Total of 70 ports.
Then I can connect 70 devices in just one Arduino and send one only string like this... almost like that. To make it more interesting I'll open 3 UDP ports to separate Digital/PWM/Analog status... but let's concentrate in the Digital ports for now.

In the Arduino end it will read the ports every 250ms and broadcast through UDP port 7001 the string with all digital ports states

22:on,23:off,24:on....

In the OR, I have one UDPListener command at port 7001 with regex (.*), the whole string.
Have a custom sensor attached to the Listener command, so the port is open by the OR and receives the whole string with all ports:status separated by comma or whatever.

In the rules, like you did but using while() I catch with regex

 [0-9][0-9]:(on|off) 

what will return 22 on for group(1) and group(2) in the first find() call

Then I run the virtual state command "AR01-" group(1), group(2) that will be AR01-22 on
the While() will call the find() making it to jump to the second match

Then it runs the command call again, at this time group(1) and group(2) will be 23 off, so the command will be AR01-23 off

Every else you know, I have AR01-22 to AR01-53 virtual state commands, AR01-22 to AR01-53 custom sensors connected to the virtual commands and AR01-22 to AR01-53 switches connected to the sensors.

In this specific case you see in the video, I have hacked that switch installing few components inside to accept a pulse to switch on/off. to turn the switch on/off the arduino sends a 50ms pulse in the port.

Posted by rberg at Nov 25, 2014 08:54

Thanks for the explanation. This will help others to follow your path!

Posted by pz1 at Nov 25, 2014 09:42

Hi R Berg,
I see the video, very compliments for the result.
Beautiful tablet, this is a Samsung tablet?

I want apply your UDP Listener for my automation system.
Please, Can you explayn your final configuration of rules, status sensor and sensor of switch? I tried it but i have problem.
What is the version of your OR?
Thanks for all

Posted by gesatecnico at Dec 08, 2014 18:33
Document generated by Confluence on Jun 05, 2016 09:37