From 9a2230b60eb7409d085e60048f3d84c5eed31094 Mon Sep 17 00:00:00 2001
From: Li Hardy Extreme
Date: Fri, 6 Nov 2026 00:10:59 +0100
Subject: [PATCH] feat: mDNS ESP32 - enable local network discovery over mDNS
---
examples/companion_radio/main.cpp | 32 +++++++++++++++++++++++++++++++
variants/heltec_v4/platformio.ini | 2 ++
2 files changed, 34 insertions(+)
diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp
index 839d49583..1ff744702 100644
--- a/examples/companion_radio/main.cpp
+++ b/examples/companion_radio/main.cpp
@@ -2,6 +2,11 @@
#include
#include "MyMesh.h"
+#if defined(ENABLE_WIFI_INTERFACE) && defined(ENABLE_ESP_MDNS)
+#define HAS_ESP_MDNS
+#include
+#endif
+
// Believe it or not, this std C function is busted on some platforms!
static uint32_t _atoi(const char* sp) {
uint32_t n = 0;
@@ -266,6 +271,33 @@ void setup() {
interface_manager.addInterface(InterfaceType::Ethernet, ðernet_interface);
#endif
+#ifdef HAS_ESP_MDNS
+ String nodeName = the_mesh.getNodeName();
+ String sanitizedNodeName;
+ sanitizedNodeName.reserve(nodeName.length());
+ for (size_t i = 0; i < nodeName.length(); ++i) {
+ const char c = nodeName[i];
+ if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
+ (c >= '0' && c <= '9') || c == '-' || c == '_') {
+ sanitizedNodeName += c;
+ }
+ }
+
+ String mdnsName = "meshcore_";
+ mdnsName += sanitizedNodeName;
+ if (!MDNS.begin(mdnsName.c_str())) {
+ WIFI_DEBUG_PRINTLN("mDNS failed to start!");
+ } else {
+ MDNS.addService("meshcore", "tcp", TCP_PORT);
+ MDNS.addServiceTxt("meshcore", "tcp", "node-name", the_mesh.getNodeName());
+ MDNS.addServiceTxt("meshcore", "tcp", "firmware-version", FIRMWARE_VERSION);
+ String protocol; protocol+=FIRMWARE_VER_CODE;
+ MDNS.addServiceTxt("meshcore", "tcp", "protocol-version", protocol.c_str());
+ MDNS.addServiceTxt("meshcore", "tcp", "build-date", __DATE__);
+ MDNS.addServiceTxt("meshcore", "tcp", "build-time", __TIME__);
+ }
+#endif
+
// add hardware serial interface
#if defined(SERIAL_RX)
companion_serial.setPins(SERIAL_RX, SERIAL_TX);
diff --git a/variants/heltec_v4/platformio.ini b/variants/heltec_v4/platformio.ini
index c91e31a80..69ac9ad89 100644
--- a/variants/heltec_v4/platformio.ini
+++ b/variants/heltec_v4/platformio.ini
@@ -212,6 +212,7 @@ build_flags =
-D BLE_DEBUG_LOGGING=1
-D OFFLINE_QUEUE_SIZE=256
-D ENABLE_WIFI_INTERFACE
+ -D ENABLE_ESP_MDNS
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
build_src_filter = ${heltec_v4_oled.build_src_filter}
@@ -231,6 +232,7 @@ build_flags =
${heltec_v4_oled.build_flags}
-I examples/companion_radio/ui-new
-D ENABLE_WIFI_INTERFACE
+ -D ENABLE_ESP_MDNS
-D MAX_CONTACTS=350
-D MAX_GROUP_CHANNELS=40
-D OFFLINE_QUEUE_SIZE=256