We have moved at community.getvera.com

Author Topic: LUA and UDP  (Read 6780 times)

Offline nixterrimus

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
LUA and UDP
« on: March 28, 2010, 01:27:25 am »
Hey, I'm working on a device for Vera that needs to listen to a UDP broadcast and react based on the message that is received.  I haven't been able to find any information on LUA and UDP and so I'm looking for help and code samples.

Thanks!
-Nick

guest4690

  • Guest
Re: LUA and UDP
« Reply #1 on: March 28, 2010, 05:36:47 am »
Vera includes the LuaSocket library (http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/.  In current firmware, it's missing some high-level functions (already fixed for next versions); but it does include all the UDP parts (http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/udp.html).

I haven't used it; but the code should be something like this:

Code: [Select]
require "socket"
local udpport = assert (socket.udp())
assert (udpport:setsockname ('*', yourlocalport))
udpport:settimeout (nil)                  -- wait indefinitely
local message = assert (udpport:receive())


Offline nixterrimus

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
Re: LUA and UDP
« Reply #2 on: March 28, 2010, 04:38:11 pm »
Fantastic!  Thank you, Javier, here is the code I'm working with, it listens on my UDP port and prints registers the events.  I'm just trying to bootstrap my way into doing something cool, XBMC integration.

Code: [Select]
-- Utility functions provided by MCV, here for local testing
function SetUPnPState(variable_name, variable_value)
    -- Would in reality register the variables
    print("Registered " .. variable_name .. " as " .. variable_value)
end


-- My Script
require "socket"

-- Opens the UDP port
--   Should be trying to receive from the non-forwarding IP 255.255.255.255 but * seems to work
--   Should open the port 8278 which is XBMC's default UDP port
function startup()
    udpport = assert (socket.udp())
    assert (udpport:setsockname ('*', 8278))
    udpport:settimeout (nil)                  -- wait indefinitely
    local message, host = assert (udpport:receivefrom(1024))
    --print("Message " .. message)
    print("Host " .. host)
    incomingData(message)
end

function incomingData(message)
    if (string.find(message, "OnPlayBackStarted") ~= nil) then
        SetUPnPState("PlaybackStatus", "Playing")
    elseif (string.find(message, "OnPlayBackPaused") ~= nil) then
        SetUPnPState("PlaybackStatus", "Paused")
    else
        print("Message " .. message)
    end
end   

startup()

Something I don't understand is where does the

Code: [Select]
local message, host = assert (udpport:receivefrom(1024))

go?  It doesn't seem to belong in the startup block.  I need to wait and receive these message indefinitely and pass the result to the incomingData function.  Also what does the incomingData function actually receive?  I need to grab the IP address and the message as this device is really the top level device for sub devices.

Thanks for any help or tips.

Sincerely,
Nick

Offline bfromdruten

  • Jr. Member
  • **
  • Posts: 64
  • Karma: +0/-0
Re: LUA and UDP
« Reply #3 on: August 14, 2011, 03:48:36 pm »
Any progress with this plugin?