Hub V2 Can Discover mDNS?

Hi
I’m develop edge driver with LAN device.
I added search-parameters.yaml and do mdns.discover(“_my-service-name._tcp”, “local”)
mdns:

  • service: “_my-service-name._tcp”

in top of the init.lua, I write log.info(“init!”)
After I run logcat, terminal shows connected.
But, no log shown when I try to scan devices in SmartThings APP.

My hub is V2, and FW version is 000.052.00021.

I’m unclear as to whether you have written your discovery handler yet.

You may find it easier to leave adding search-parameters.yaml until you’ve got things working reliably without it.

1 Like

I declared discovery in Driver

disco.lua

function MyServiceDiscovery.discover(driver, _, should_continue)
    log.info("Okay, Start discover...")
    while should_continue() do
        local mdns_responses, err = mdns.discover(MyServiceDiscovery.ServiceType, MyServiceDiscovery.Domain)

        log.info("Start discover...")
        if err ~= nil then
            log.error("Error during service discovery: ", err)
            return
        end

        for _, info in ipairs(mdns_responses.found) do
            log.info(info)
        end
        socket.sleep(1.0)
    end
end

return MyServiceDiscovery

init.lua

local disco = Discovery.discover

local template = {
    discovery = disco,
    lifecycle_handlers = {
        init = device_init,
    },
    supported_capabilities = {
        capabilities.refresh,
    },
}


device_init = function(driver, device)
    log.info("device init")
end

local drvier = Driver("MyService", template)

log.info("Starting MyService driver")
drvier:run()
log.warn("MyService driver exiting")

Additionaly, log.info(“Starting MyService driver”) this not appears too. :frowning:
No mDNS discovery, no init.

PS. I can find my service with mDNS discovery APP which can be found in Google PlayStore.

Hi, @ranni

I asked the engineering team about your case and they mentioned you should check the following:

  1. If it works if you delete the search parameters YAML file entirely because if it’s malformed in any way, or has a tiny typo, then it’ll prevent the driver from starting during discovery.
  2. If you have the discovery permission in their config.yml. For example:
name: 'Temperature'
packageKey: 'Temperature'
description: 'Temperature'
vendorSupportInformation: '...'
permissions:
  lan: {}
  discovery: {}
1 Like

I was expecting to see:

local disco = require "disco"

and then in the template

discovery = disco.discover

Do you have something like

local Discovery = require "disco"

earlier in the init.lua file instead?

You wouldn’t expect to see the device.init log even if your discovery routine runs because it hasn’t created a device. So there is nothing for the driver to initialise.

Hi @nayelyz
I follow your answer

  1. Delete search parameteres YAML entirely → not works, log nothing.
  2. I already have ‘discovery’ permission in config.yaml
name: "MyServiceDriver"
packageKey: "myservice.local"
description: "MyService local driver"
vendorSupportInformation: "MyService"
permission:
  lan: {}
  discovery: {}

@ranni It looks like you have not included local log = require "log" anywhere in your init.lua code. That means that your driver will crash immediately before it can call :run() when it tries to log the starting message.

Hi. @doug.stephen
this is really whole code of mine
I just started write code, it’s so simple.
4 files.

src/disco.lua

local log = require "log"
-- local old_log = require "log"
-- old_log.error = function(...)
--     old_log.error_with({ hub_logs = true }, ...)
-- end

-- old_log.warn = function(...)
--     old_log.warn_with({ hub_logs = true }, ...)
-- end

-- old_log.info = function(...)
--     old_log.info_with({ hub_logs = true }, ...)
-- end

-- local log = {}

-- log.warn = old_log.warn
-- log.error = old_log.error
-- log.debug = old_log.debug
-- log.trace = old_log.trace
-- log.info = old_log.info

local socket = require "cosock.socket"

local mdns = require "st.mdns"
local net_utils = require "st.net_utils"
local st_utils = require "st.utils"

local SERVICE_TYPE = "_worlty._tcp"
local DOMAIN = "local"

local WorltyDiscovery = {
    ServiceType = SERVICE_TYPE,
    Domain = DOMAIN,
    device_state_cache = {},
    discovery_active = false
}

local WorltyTypeEnum = {
    "none",
    "binary_sensor",
    "climate",
    "fan",
    "event",
    "input",
    "light",
    "sensor",
    "switch"
}

function WorltyDiscovery.discover(driver, _, should_continue)
    log.info("Okay, Start discover...")
    while should_continue() do
        local mdns_responses, err = mdns.discover(WorltyDiscovery.ServiceType, WorltyDiscovery.Domain)

        log.info("Start discover...")
        if err ~= nil then
            log.error("Error during service discovery: ", err)
            return
        end

        for _, info in ipairs(mdns_responses.found) do
            log.info(info)
        end
        socket.sleep(1.0)
    end
end

return WorltyDiscovery

src/init.lua

local log = require "log"
-- local old_log = require "log"
-- old_log.error = function(...)
--     old_log.error_with({ hub_logs = true }, ...)
-- end

-- old_log.warn = function(...)
--     old_log.warn_with({ hub_logs = true }, ...)
-- end

-- old_log.info = function(...)
--     old_log.info_with({ hub_logs = true }, ...)
-- end

-- local log = {}

-- log.warn = old_log.warn
-- log.error = old_log.error
-- log.debug = old_log.debug
-- log.trace = old_log.trace
-- log.info = old_log.info

log.info("init!")

local capabilities = require "st.capabilities"
local Driver = require "st.driver"

local Discovery = require "disco"

local disco = Discovery.discover

local device_init

local template = {
    discovery = disco,
    lifecycle_handlers = {
        init = device_init,
    },
    supported_capabilities = {
        capabilities.refresh,
    },
}


device_init = function(driver, device)
    log.info("device init")
end

local drvier = Driver("Worlty", template)

log.info("Starting Worlty driver")
drvier:run()
log.warn("Worlty driver exiting")

config.yaml

name: "WorltyDriver"
packageKey: "worlty.local"
description: "Worlty local driver"
vendorSupportInformation: "Worlty"
permission:
  lan: {}
  discovery: {}

profiles/worlty-pad.yaml

name: worlty-pad
components:
  - id: main
    capabilities:
      - id: refresh
        version: 1

There’s a typo here, it should be permissions, once I modified that, the logs started showing.

Note: You need to delete the package completely and re-create it to update this section, otherwise you’ll get an error when trying to package the driver again.

1 Like

Oh my eyes…what a shame
Sorry for that
Thanks a lot

1 Like