Send scene event via socket

Hi,

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: http://forum.micasaverde.com/index.php?topic=6489.msg40643#msg40643

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

local t = os.date(‘*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 = os.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 192.168.230.230 and my PC in 192.168.238.41 (we have different sub-networks in the company). So my java code is like:

[code]import java.io.;
import java.net.
;

public class listener {
public static void main(String[] args) {
final String servidor=“192.168.230.230”;
final int puerto=53007;
try{
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”);
salida.println(“respuesta”);
//recibimos la respuesta del servidor
System.out.println(entrada.readLine());
socket.close();
} catch (UnknownHostException e){
e.printStackTrace();
System.out.println(“Debes estar conectado para que esto funcione bien”);
} catch (IOException e)
{
e.printStackTrace();
}

}

}[/code]

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()
  tcp:settimeout(3)
  if (tcp:connect(host, port)) then
     if (tcp:send(Msg)) then
     else
	luup.log("Error Sending Msg")
     end
  else
     luup.log("Error Opening Socket")
  end
  tcp:close(socket)
end

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

Thanks RichardTSchaefer!

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