Contributed by WildboarG, organized by Ai-Thinker
[WB2] Auto-Discovering Devices on the LAN
mDNS Browser (mDNS_Browser)
An mDNS browser is a tool used to discover and view mDNS (Multicast DNS) devices and services on the LAN.
By listening to the mDNS broadcast packets on the LAN, it automatically lists the hostnames published on the current network (such as xxx.local) and their corresponding service types (such as HTTP, SSH, etc.), without scanning IP addresses or relying on a centralized DNS server.
Main uses:
- View mDNS-capable devices on the LAN
- Verify whether a device or service publishes mDNS information correctly
- Quickly locate devices when the IP address is unknown
Features:
- Works within the LAN
- Based on UDP 5353 multicast communication
- Passive listening, does not actively scan the network
Implementing an mDNS Browser on WB2
I wrapped a library that lets you quickly implement an mdns browser
https://github.com/WildboarG/mdns_browser
Give it a star if you're interested 🌟
Usage
- Pull the library files to the current directory
- Only
mdns_bor.candmdns_bro.hmatter - Copy the two files above to your project root directory
- After obtaining the wifi IP, create
新建任务to implement service discovery - You need to implement the callback function yourself, and call the start
- Example demo
Click to expand full code
/* mdns_bro_demo.c
* 简单演示如何使用 mdns_bro 库:
* - 注册回调接收新增/更新/删除事件
* - 设置要监听的服务类型(示例:_http._tcp.local)
* - 启动/停止 mdns 浏览器
*
* 将 `mdns_bro_demo_init()` 在设备获得 IP 后调用(例如在 wifi GOT_IP 回调中)。
*/
#
include
<stdio.h>
#
include
<string.h>
#
include
"mdns_bro.h"
#
include
<lwip/ip_addr.h>
#
include
<lwip/inet.h>
static
void
mdns_demo_cb
(
const
struct
mdns_service_info *info,
mdns_event_t
ev,
void
*user_data)
{
(
void
)user_data;
const
char
*evs = (ev == MDNS_EVENT_ADDED) ?
"ADDED"
: (ev == MDNS_EVENT_UPDATED) ?
"UPDATED"
:
"REMOVED"
;
char
ipbuf[
64
] = {
0
};
if
(ipaddr_ntoa_r(&info->ip, ipbuf,
sizeof
(ipbuf)) ==
NULL
) {
strncpy
(ipbuf,
"0.0.0.0"
,
sizeof
(ipbuf)
-1
);
}
printf
(
"[mdns_demo] %s: instance='%s' host='%s' ip=%s port=%u txt='%s' expires_ms=%u\n"
,
evs,
info->instance[
0
] ? info->instance :
"-"
,
info->hostname[
0
] ? info->hostname :
"-"
,
ipbuf,
(
unsigned
)info->port,
info->txt[
0
] ? info->txt :
"-"
,
(
unsigned
)info->expires_ms);
}
/* Call this after network is up (GOT_IP). */
void
mdns_bro_demo_init
(
void
)
{
mdns_bro_init();
mdns_bro_register_callback(mdns_demo_cb,
NULL
);
/* 默认监听 HTTP 服务;如果需要其他服务可调用 mdns_bro_set_service_type() */
mdns_bro_set_service_type(
"_http._tcp.local"
);
mdns_bro_set_ttl_cap(
300
);
/* 可选:限制 TTL 最大值 */
mdns_bro_set_update_debounce(
30
*
1000
);
mdns_bro_start();
}
/* 可选:在关闭网络或停止发现时调用 */
void
mdns_bro_demo_stop
(
void
)
{
mdns_bro_stop();
}Result
I have two WB2s on my LAN: one is the http simulated lamp (mywb2light.local) that automatically publishes records, and the other flashes the code above — the
browserdemo
After flashing and booting, the browser discovers the mylight service on the LAN and parses out its api from the txt.


