Send scene event via socket


I’m pretty new in using the Vera Lite and of course I’ve never programmed with luup or lua…

The question is that I have an external java app that needs to know when a motion sensor has detected a presence and when a door sensor has detecte the door is open.

I was working with the motion sensor and I created a scene with a trigger when the sensor is tripped. Now, I guessed I could write a luup code to be executed when the trigger is triggered, so a message is sent through a socket.

This is the code I wrote, based on the code in this thread:

[code]local socket = require “socket”
local udp = socket.try(socket.udp())
broadcast_ip = ‘’
port = 53007

local t =‘*t’)

local message = ‘MotionSensor’+t

socket.try(udp:sendto(message, broadcast_ip, port))[/code]

I have tested it in the Test Luup Code section and I get “Code failed” but no reason why.

Any idea how can I do this easy?

It is best to log into vera via ssh and run the lua code from the command prompt. When logged into Vera via ssh, create a file with your lua code and issue the lua file_name to run the code. Your better off doing this for testing than to use the luup test section of vera. The only issue is that you will not be able to run any Vera specific apis e.g. luup code.

  • Garrett

Thanks garrettwp, that was helpful.

Nos I have a code tha’ts supposed to work (I mean, I wrote the code in a file in through ssh and executed it successfully)

local host, port = "localhost",53007 local socket = require("socket") local ip = assert(socket.dns.toip(host)) local udp = assert(socket.udp()) local date ="MotionSensor %c") assert(udp:sendto(date, ip, port))

Now, I have that code in the luup event of a trigger of a scene. In order to test it, I wrote a java code to listen to the 53007 port, but when I execute it I get “Connection refused”. I’m I supposed to open the port before or the problem should be another one?

My Vera Lite is in the IP and my PC in (we have different sub-networks in the company). So my java code is like:


public class listener {
public static void main(String[] args) {
final String servidor=“”;
final int puerto=53007;
Socket socket= new Socket (servidor,puerto);
//conseguimos el canal de entrada
BufferedReader entrada = new BufferedReader (new InputStreamReader(socket.getInputStream()));
//conseguimos el canal de salida
PrintWriter salida=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
salida.println(“Hola servidor”);
//recibimos la respuesta del servidor
} catch (UnknownHostException e){
System.out.println(“Debes estar conectado para que esto funcione bien”);
} catch (IOException e)



I’ve executed the code agains port 80 and port 22 and I don’t get the “Connection refused” exception…

The Lua code is sending a packet to Port 53007 on localhost (i.e., the Vera). Nowhere does it mention the address or hostname of your PC with the Java code.

Edit: Not to mention that your Java code looks to be a TCP client, not a UDP server.

It looks like you are trying to learn networking at the same time as you learn Lua. Not something I’d recommend.

Thanks! You’re rigth, not a good idea.

As I said, pretty new on these fields.

Anyone knows an easy way to do what I want? I just need to monitor from a java app if the sensor detects any motion…It seems it should be something easy to do, but I have not clue how to…

Your on the right path … but you have to get all of the details correct.
Here is some LUA code I used to send a TCP message from Vera:

function VeraAlert(host, port, Msg) 
  local socket = require("socket")
  local tcp = socket.tcp()
  if (tcp:connect(host, port)) then
     if (tcp:send(Msg)) then
	luup.log("Error Sending Msg")
     luup.log("Error Opening Socket")

VeraAlert("", 50123, "Hello!")

Thanks RichardTSchaefer!

I’ll try with that and I’ll let all you know how it goes :wink: