Skip to content

Conversation

blazoncek
Copy link
Collaborator

@blazoncek blazoncek commented Jul 12, 2025

Summary by CodeRabbit

  • New Features

    • Improved mDNS support for MQTT server resolution on ESP32 devices.
  • Improvements

    • Unified and streamlined MQTT topic formatting for consistent and reliable topic generation.
    • Enhanced reliability of online status reporting via MQTT by centralizing Last Will and Testament message setup.
  • Documentation

    • Added and clarified comments for better understanding of MQTT payload parsing and logic.

- fixes #4671
- reduce some topic string parsing
- moves LWT into onConnect
Copy link
Contributor

coderabbitai bot commented Jul 12, 2025

"""

Walkthrough

The update refactors MQTT topic string construction in wled00/mqtt.cpp to use a centralized format string and snprintf_P for consistency. It also adds mDNS resolution support for the MQTT broker on ESP32 platforms and centralizes the setup of the MQTT Last Will and Testament (LWT) message. No changes were made to public interfaces.

Changes

Files / File Groups Change Summary
wled00/mqtt.cpp Refactored topic string construction to use a constant format string and snprintf_P; improved mDNS support for MQTT broker resolution on ESP32; centralized LWT message setup; added/clarified comments; minor formatting changes.

Assessment against linked issues

Objective (Issue #) Addressed Explanation
Resolve mDNS address for MQTT broker (#4671)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Possibly related PRs

  • Change to set LWT only once #4697: Both PRs modify the handling of the MQTT "online" status message and its publishing location within onMqttConnect and publishMqtt, focusing on centralizing and improving the LWT and status topic setup.
    """

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 07e303b and ecc3eae.

📒 Files selected for processing (1)
  • wled00/mqtt.cpp (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • wled00/mqtt.cpp
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: wled_build / Gather Environments
  • GitHub Check: wled_build / Test cdata.js
  • GitHub Check: wled_build / Build Environments (esp32s3dev_16MB_opi)
  • GitHub Check: wled_build / Build Environments (usermods)
  • GitHub Check: wled_build / Build Environments (esp32_wrover)
  • GitHub Check: wled_build / Build Environments (esp32s3_4M_qspi)
  • GitHub Check: wled_build / Build Environments (lolin_s2_mini)
  • GitHub Check: wled_build / Build Environments (esp32s3dev_8MB_opi)
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@schildbach
Copy link

So far, I had no luck with this PR. I installed WLED_0.16.0-alpha_ESP32.bin from this PR to my Athom LS8P, replaced Sync Settings → MQTT → Broker with "assistant.local" and rebooted. It does not seem to connect to my MQTT server.

There is no such problem if I use a regular DNS name or IP address. And a variety of ESPHome devices can connect using the mDNS name without problem as well.

@blazoncek
Copy link
Collaborator Author

You don't need to add .local to host name.

@schildbach
Copy link

You don't need to add .local to host name.

How do you mean? That's what my mDNS domain is called, and broadcasted via avahi-publish.

@blazoncek
Copy link
Collaborator Author

.local is needed for your host computer to know it needs to do mDNS. querying mDNS directly does not need it.

@schildbach
Copy link

.local is needed for your host computer to know it needs to do mDNS. querying mDNS directly does not need it.

Yes exactly. How does WLED know I want to resolve an mDNS address if I don't add the correct .local domain? Domains without .local are plain DNS so those would try to get resolved via my DNS server – and fail, right?

@blazoncek
Copy link
Collaborator Author

Just enter the hostname. What else? Similar as, if you are old enough to remember it, WINS.
Will add stripping of .local

@schildbach
Copy link

Just enter the hostname. What else? Similar as, if you are old enough to remember it, WINS. Will add stripping of .local

Again, how will it know it's an mDNS domain if I strip the .local?

@blazoncek
Copy link
Collaborator Author

See the code.

@schildbach
Copy link

schildbach commented Jul 15, 2025

I see changes to MQTT topic parsing which seem irrelevant to this PR. However I'm a Java dev with limited understanding of CPP code so I can't say for sure.

Can you describe in words?

Typically, in these kind of hostname fields there are these cases:

  • formatted as IPv4 → use IP
  • formatted as IPv6 → use IP
  • ending with .local → resolve IP via mDNS
  • just host, no domain → add DNS search domain, resove IP via DNS
  • host.domain → resolve IP via DNS

wled00/mqtt.cpp Outdated
Comment on lines 218 to 219
mqttMDNS.replace(F(".local"), ""); // remove .local if present
if (strlen(cmDNS) > 0 && mqttMDNS.length() > 0 && mqttMDNS.indexOf('.') < 0) { // if mDNS is enabled and server does not have domain

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is .replace() limited to the end of strings? Otherwise it would strip in the middle of DNS names too which is probably not wanted.

In general, I'd recommend explicitly checking for .local at the end, and only if present resolve via mDNS (stripping the domain if it isn't done by the mDNS component anyway). It seems the above code just checks for the presence of dots, which is imho not correct. Plain hostnames (without dots) are typically expected to be resolved via DNS (using the search domain).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the impl from this PR would fail for those who use dots in their mDNS name. Yes, some people do this and I understand this is bending the mDNS spec a bit, but it would be nice to support these cases anyway.

Copy link

@schildbach schildbach Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more comment about .replace(): is it case-insensitive? Both DNS and mDNS names/domains are case-insensitive.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are welcome to add on to this at a later time. For now just test if it fulfills your needs with .local domain.
You have to understand that I'm doing that in my spare time and I have no need for this feature and limited resources in ESP may need a few compromises.

FYI resolving mDNS host only requires hostname, no domain. Domain is only there to determine if regular DNS is needed or not.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, and thanks for your time investment. I was just reviewing this PR as you asked me to do.

@schildbach
Copy link

I retested the 24f2306 binary and assistant.local now works for me.

@schildbach
Copy link

However, assistant.LOCAL doesn't work although I think it should.

ASSISTANT.local works.

Copy link

@schildbach schildbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure "removing anything behind .local" is the correct fix?

A user could have a perfectly conformant DNS name "server1.localservers.example.com" and this code would strip it to just "server1" and interpret it as an mDNS name.

I think a more correct fix would make sure the .local part needs to appear at the end of the string to be considered as a mDNS name.

@blazoncek
Copy link
Collaborator Author

.local part needs to appear at the end of the string

Agreed, but that is a bit more complex. However, that stripping is only done for mDNS resolution. If host is not found, regular DNS query is made. See else.

@blazoncek blazoncek added enhancement connectivity Issue regarding protocols, WiFi connection or availability of interfaces labels Jul 26, 2025
@blazoncek blazoncek merged commit 93e011d into main Aug 2, 2025
42 checks passed
@blazoncek blazoncek deleted the fix-4671 branch August 2, 2025 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
connectivity Issue regarding protocols, WiFi connection or availability of interfaces enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Resolve mDNS address for MQTT broker
2 participants