Sending a simple text string to a third party device over ethernet

Being new to Lua I was hoping someone could give me some hints on how to do this. I’m working on integrating the Vera with a 3rd party automation system. I can write code in the 3rd party system to communicate with Lua using the http interface. Using this paradigm the 3rd party system needs to periodically poll the Vera for any status changes such as someone unlocking a Schlage door lock that is connected to the Vera.

What I would like to do is to have an event in the Vera that, when the door is unlocked, sends a text message using a predefined Ethernet port to the 3rd party automation system.

Is there a way in Lua to open an Ethernet port, send some text, and the close the port?

Thanks in advance for the help

Jay

If by “Ethernet port” you mean a TCP connection, then absolutely. Vera includes the LuaSocket package. The simple way could be something like this:

require "socket"

local function just_send (addr, port, data)
	local s = assert (socket.tcp())
	assert (s:connect (addr, port))
	assert (s:send(data))
	s:close()
end

Another possibility is to define a ‘serial port’ device on a TCP connection to your other system, and use the usual [tt]luup.io.write()[/tt] function. That also allows Luup to invoke your Lua code on any incoming data.

i would like to send several tcp comamnds but i can only seem to get one to work.

I have been able to get a single command to work using the following

local socket = require(“socket”)
tcp = assert(socket.connect(“192.168.1.28”, 8102))
tcp:send(“MZ” … “\r\n”)
luup.sleep(50)
tcp:close()

but i would like to builder a macro that will turn on my amp then change the input on the amp, how do i add extra commands to the above.

i also would like to send a hex string \xaa\x05\x05\x02\x4a but i am not able to get this to work.

I worked out how to send a hex string via tcp

local hex = {
“aa”, “05”, “05”, “01”, “4b”
}
local binary = “”;
for i, v in ipairs(hex) do
binary = binary … string.char(tonumber(v, 16))
end
local socket = require(“socket”)
tcp = assert(socket.connect(“192.168.30.100”, 4999))
tcp:send(binary … “\r\n”)
luup.sleep(100)
tcp:close()

i now just would like to know how to send multiple tcp commands

Have you tried just pasting the code into the end? It’s probably that simple.

I would like to also be able to send some http strings to change some status on the multswitch plugin
can any one show me an example og what it sould look like.

This is one of the string i would like to send.

http://192.168.1.48:3480/data_request?id=lu_variableset&DeviceNum=36&serviceId=urn%3Adcineco-com%3AserviceId%3AMSwitch1&Variable=Status5&Value=On&rand=0.8281729044392705