# UDP Broadcast Discovery

**URL:** <https://community.smartthings.com/t/udp-broadcast-discovery/272697>\
**Category:** Writing Edge Drivers\
**Tags:** support\
**Created:** [November 6, 2023, 2:43am UTC](https://community.smartthings.com/t/udp-broadcast-discovery/272697 "2023-11-06T02:43:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![damien1](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@damien1](https://community.smartthings.com/u/damien1)\
**Post date:** [November 6, 2023, 2:43am UTC](https://community.smartthings.com/t/udp-broadcast-discovery/272697/1 "2023-11-06T02:43:18Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![AlejandroPadilla](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/alejandropadilla/32/175701_2.png) [@AlejandroPadilla](https://community.smartthings.com/u/AlejandroPadilla)\
**Post date:** [November 6, 2023, 7:50pm UTC](https://community.smartthings.com/t/udp-broadcast-discovery/272697/2 "2023-11-06T19:50:57Z")

</div>

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

> <https://github.com/SmartThingsDevelopers/SampleDrivers/blob/main/lightbulb-lan-esp8266/driver/src/discovery.lua#L73>

---

<div class="post-metadata">

**Author:** ![damien1](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@damien1](https://community.smartthings.com/u/damien1)\
**Post date:** [November 6, 2023, 8:45pm UTC](https://community.smartthings.com/t/udp-broadcast-discovery/272697/3 "2023-11-06T20:45:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![AlejandroPadilla](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/alejandropadilla/32/175701_2.png) [@AlejandroPadilla](https://community.smartthings.com/u/AlejandroPadilla)\
**Post date:** [November 7, 2023, 5:53pm UTC](https://community.smartthings.com/t/udp-broadcast-discovery/272697/4 "2023-11-07T17:53:25Z")

</div>

> [@damien1](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![damien1](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@damien1](https://community.smartthings.com/u/damien1)\
**Post date:** [November 9, 2023, 6:29am UTC](https://community.smartthings.com/t/udp-broadcast-discovery/272697/5 "2023-11-09T06:29:42Z")

</div>

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](https://github.com/damienemmerson/ST-Edge-IntesisInterface).

And, the protocol specification for the device im trying to integrate is [here](https://www.intesis.com/docs/wmp-protocol-specs).

Really appreciate you helping me out!

---

<div class="post-metadata">

**Author:** ![AlejandroPadilla](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/alejandropadilla/32/175701_2.png) [@AlejandroPadilla](https://community.smartthings.com/u/AlejandroPadilla)\
**Post date:** [November 9, 2023, 6:42pm UTC](https://community.smartthings.com/t/udp-broadcast-discovery/272697/6 "2023-11-09T18:42:18Z")

</div>

> [@damien1](#):
>
> Destination IP/Port: 255.255.255.255 (3310)

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:

> <https://github.com/SmartThingsDevelopers/SampleDrivers/blob/main/lightbulb-lan-esp8266/app/src/upnp.lua>

---

<div class="post-metadata">

**Author:** ![damien1](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@damien1](https://community.smartthings.com/u/damien1)\
**Post date:** [November 10, 2023, 5:16am UTC](https://community.smartthings.com/t/udp-broadcast-discovery/272697/7 "2023-11-10T05:16:44Z")

</div>

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](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()`
