We have moved at community.getvera.com

Author Topic: Sending mulicast messages with Vera  (Read 282 times)

Offline GreatGazoo

  • Sr. Newbie
  • *
  • Posts: 31
  • Karma: +0/-0
Sending mulicast messages with Vera
« on: March 12, 2019, 08:34:19 am »
Hallo,

for discovering devices with specific portnumber I need to send "multicast" message to my

local network. The adress "255.255.255.255" is functioning well with several (not-lua) devices

on my network. When I try this with my Vera3, in lua , I get no respond, doesn't matter what I

do or try, nothing works. I tried several other "multicast" adresses like "127.0.0.0" with no result.

The function I use works because I can sent messages to an device with a response message that I can

read. (LUACodeTest)

Is this possible with lua, and what do I have to change to get it working ?
This is my code:

socket = require("socket")
local s = socket.udp
s:settimeout(2)
bytes, ErrorCode = s:sendto(discoverDevice, "255.255.255.255", 5500)
      
function read_some(s)
  local msg, err, part = s:receive(15)

  if (not msg) and (err ~= 'timeout')  then
    return nil, err
  end
  msg = msg or part

  if not msg then return nil, 'timeout' end
  return msg
end

print(read_some(s))
s:close()


Thanks in advance,

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +291/-70
  • "Less is more"
Re: Sending mulicast messages with Vera
« Reply #1 on: March 12, 2019, 08:52:14 am »
Where/how does this fail?

I use UDP all the time in several applications, although not for multicast, so I use the setpeername() function.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline GreatGazoo

  • Sr. Newbie
  • *
  • Posts: 31
  • Karma: +0/-0
Re: Sending mulicast messages with Vera
« Reply #2 on: March 15, 2019, 04:21:52 am »
Hello,

thanks for your answer.
I tried to use setpeername() but got an error:

"string "ALTUI - LuaRunHandler"]:16: calling 'send' on bad self (udp{connected} expected, got userdata)"

Doesn't the command setpeername() set a connection with a single device ?
It definately doesn't work for adress "255.255.255.255", multicast.

regards,

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +291/-70
  • "Less is more"
Re: Sending mulicast messages with Vera
« Reply #3 on: March 15, 2019, 04:48:26 am »
Doesn't the command setpeername() set a connection with a single device ?
It definately doesn't work for adress "255.255.255.255", multicast.

Yes, that seems to be the case.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline dgb

  • Newbie
  • *
  • Posts: 7
  • Karma: +4/-0
Re: Sending mulicast messages with Vera
« Reply #4 on: March 15, 2019, 06:25:41 am »
It definately doesn't work for adress "255.255.255.255", multicast.

The IP address 255.255.255.255 is **NOT** a multicast address... It is the zero network BROADCAST address... and it is limited... Packets sent to this address are *NOT* forwarded by routers to other networks. (This means, if you have a device connected to a range extender, the packet sent to the zero network broadcast address from that device will never reach devices connected to your main router. Conversely, zero network broadcast packets sent from a device connected to your main router would never reach devices connected to the range extender). As all Vera units are essentially enhanced routers, packets sent to the zero network broadcast address are never forwarded outside of the Vera (except to devices that conected to and are using the Vera as a gateway). This is why you are receiving no response...

Multicast addresses are in the range 224.0.0.0 to 239.255.255.255. For discovery, the assigned multicast address is 239.255.255.250, which encompasses SSDP (Simple Service Discovery Protocol).

Discovering devices on the network is NOT as simple as sending a multicast packet.... Many device manufactures use custom discovery addresses, custom discovery ports or special protocols (requiring a specially formatted UDP data).


If you want to send broadcast packets to your "private network", you need to use the network broadcast address... IE: If your network is 192.168.34.0/24, the broadcast address would be 192.168.34.255.

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
Re: Sending mulicast messages with Vera
« Reply #5 on: March 15, 2019, 06:51:57 pm »
I use a version of the code below which works for me. I do note @dgb's comment above about the  '255.255.255.255' address. and agree with
Quote
Discovering devices on the network is NOT as simple as sending a multicast packet

I tried using multicast but failed in getting it to work. I therefore confess I'm not an expert in networking but my device of interest does communicate through a router. In my case the code below will get multiple replies and they all need to be handled and the device of interest checked for. Code has been extracted and may need a little massaging to suit. Interested in any feedback.

Code: [Select]
    local function debug(debugMessage)
        -- blah blah
    end

    local function addToPhysicalDevicesList(var1, var2)
        debug(var1)
        debug(var2)
    end

local function broadcastDiscoverDevicesMsg()
    local BROADCAST_IP = '255.255.255.255'
    local UDP_IP_PORT = 5500
    local txMsg = 'Here is a message'

    local ok = false
    local udp = socket.udp()
    udp:settimeout(MSG_TIMEOUT)

    local BROADCAST_IP = '255.255.255.255'
    -- HACK local MULTICAST_IP = '224.0.0.251'

    -- asterisk represents all the local interfaces on Vera eg Lan, WiFi, etc
    local setOK, failMsg = udp:setsockname('*', UDP_IP_PORT)
    if (setOK == nil) then
        debug('Set socket name failed: '..failMsg,50)
        udp:close()
        return ok
    end

   udp:setoption('broadcast', true)
    debug('Broadcasting discovery message')
    local resultTX, errorMsg = udp:sendto(txMsg, BROADCAST_IP, UDP_IP_PORT)
    -- HACK local resultTX, errorMsg = udp:sendto(txMsg, MULTICAST_IP, UDP_IP_PORT)
    if (resultTX == nil) then debug('Broadcast TX failed: '..errorMsg) udp:close() return ok end

    local rxMsg = nil
    local ipOrErrorMsg = ''
    -- repeat until the queue of all the device responses has been processed
    repeat
        -- allow for a msg length of 512. The receivefrom() function will block until timeout
        rxMsg, ipOrErrorMsg, _ = udp:receivefrom(512)
        if (rxMsg) then
            debug(ipOrErrorMsg)

            -- as the responses to the broadcast are rx'ed, add the devices to the list
            addToPhysicalDevicesList(rxMsg, ipOrErrorMsg)
        end
    until (not rxMsg)

    udp:close()
end

Offline GreatGazoo

  • Sr. Newbie
  • *
  • Posts: 31
  • Karma: +0/-0
Re: Sending mulicast messages with Vera
« Reply #6 on: March 17, 2019, 08:20:46 am »
Hello,

thanks for your reply.
I tried your code, but with no luck.
I tried to sent a message to my windows PC, where "packet sender" is installed,
but the message is not arriving, even if I try to sent the message directly (not thru 255.255.255.255)
then I receive an " Result: false".

regards,