UDP Broadcast Discovery

Hi Everyone.

I’m attempting to write a discovery for my edge driver.

The device requires a message (‘DISCOVER\r\n’) to be braodcast over UDP on port 3310. The device will then reply by broadcasting a message containing its IP address.

My code below successfully send the discovery message. I can see it in Wireshark. Likewise, the device broadcasts a message response which contains its IP address. I can see that in wireshark too. But for some reason the hub never receives the response from the device.

Here’s the code.

local function find_device()
  -- UDP socket initialization
  local udp = socket.udp()
  udp:setsockname('*', 0)
  udp:setoption('broadcast', true)
  udp:settimeout(2)

  -- broadcasting request
  log.info('===== SCANNING NETWORK...')
  udp:sendto('DISCOVER\r\n', '255.255.255.255', 3310)

  -- Socket will wait 2 seconds to receive a response back.
  local res = udp:receive()
  
  -- close udp socket
  udp:close()

  if res ~= nil then
    return res
  end
  return nil
end

local disco = {}
function disco.start(driver, opts, cons)
  while true do
    local device_res = find_device()

    if device_res ~= nil then
      
      log.info('===== DEVICE FOUND IN NETWORK...')
      
    end
    log.error('===== DEVICE NOT FOUND IN NETWORK')
  end
end

return disco

Any help would be appreciated.

Hi @damien1

Maybe the problem is in local res = udp:receive() if you try local res = udp:receivefrom() what happens? , I was checking the tutorial and the function is different

Hi @AlejandroPadilla

Thanks for getting back to me with your suggestion. Unfortunetly, I got the same result.
I also tried both local socket = require 'cosock.socket' and local socket = require 'socket' with no success.

Any other ideas?

Mmm, let me do some tests, Could you share the response?

That would be great, @AlejandroPadilla. Thanks!

This is the response from the device as captured in wireshark:
Source IP/Port: 192.168.0.16 (3310)
Destination IP/Port: 255.255.255.255 (3310)
Data: DISCOVER:PA-RC2-WMP-1,CC3F1D04D392,192.168.0.16,ASCII,v1.3.3,-38,WMP_04D392,N,3

If it helps, my code can be found here.

And, the protocol specification for the device im trying to integrate is here.

Really appreciate you helping me out!

1 Like

Mmm I think the hub has not received the response because the device is sending the response to the broadcast address, so what happens If you change the destination?

Maybe this example can help you get the hub IP:

Unfortunately the manufacturer of the device has specififed how the discovery works as per the devices protocol specifications: https://www.intesis.com/docs/wmp-protocol-specs

The device will respond on the broadcast address and that cant be changed.

What would the code look like for the edge driver to listen for broadcast messages on port 3310?

I cant see any options in the documentation for udp:receive()