-
Notifications
You must be signed in to change notification settings - Fork 294
feat: Add mDNS support for network discovery and dynamic hostname updates #1240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
camalolo
wants to merge
4
commits into
bitaxeorg:master
Choose a base branch
from
camalolo:avahi+devboard
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d0dced6
feat: Add mDNS support for network discovery and dynamic hostname upd…
camalolo e5999d4
feat: Enhance swarm component to support hostname addresses and impro…
camalolo 0a58044
Initialize mDNS/Avahi after WiFi connection, advertise HTTP service w…
camalolo e3f3b5f
Refactor: Skip processing of swarm peer if system info fetch fails si…
camalolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,4 +13,5 @@ REQUIRES | |
| "esp_wifi" | ||
| "esp_event" | ||
| "stratum" | ||
| "espressif__mdns" | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| #include <string.h> | ||
| #include "esp_event.h" | ||
| #include "esp_log.h" | ||
| #include "mdns.h" | ||
| #include "esp_system.h" | ||
| #include "esp_wifi.h" | ||
| #include "freertos/FreeRTOS.h" | ||
|
|
@@ -389,6 +390,45 @@ void wifi_init(void * pvParameters) | |
| ESP_LOGI(TAG, "ESP_WIFI setting hostname to: %s", hostname); | ||
| } | ||
|
|
||
| /* Initialize mDNS */ | ||
| err = mdns_init(); | ||
| if (err != ESP_OK) { | ||
| ESP_LOGW(TAG, "mDNS/Avahi initialization failed: %s", esp_err_to_name(err)); | ||
| ESP_LOGW(TAG, "Device will not be discoverable via mDNS/Bonjour/Avahi"); | ||
| } else { | ||
| ESP_LOGI(TAG, "mDNS/Avahi initialized successfully - device discoverable on network"); | ||
|
|
||
| /* Set mDNS hostname */ | ||
| err = mdns_hostname_set(hostname); | ||
| if (err != ESP_OK) { | ||
| ESP_LOGW(TAG, "mDNS hostname setup failed: %s", esp_err_to_name(err)); | ||
| ESP_LOGW(TAG, "Device hostname not set for mDNS discovery"); | ||
| } else { | ||
| ESP_LOGI(TAG, "mDNS hostname set to: %s.local", hostname); | ||
| ESP_LOGI(TAG, "Access device at: http://%s.local", hostname); | ||
| } | ||
|
|
||
| /* Set mDNS instance name */ | ||
| err = mdns_instance_name_set("ESP-Miner Web Server"); | ||
|
||
| if (err != ESP_OK) { | ||
| ESP_LOGW(TAG, "mDNS instance name setup failed: %s", esp_err_to_name(err)); | ||
| } else { | ||
| ESP_LOGI(TAG, "mDNS instance: ESP-Miner Web Server"); | ||
| } | ||
|
|
||
| /* Add HTTP service */ | ||
| err = mdns_service_add("ESP32 WebServer", "_http", "_tcp", 80, NULL, 0); | ||
| if (err != ESP_OK) { | ||
| ESP_LOGW(TAG, "mDNS HTTP service registration failed: %s", esp_err_to_name(err)); | ||
| ESP_LOGW(TAG, "HTTP service not advertised via mDNS"); | ||
| } else { | ||
| ESP_LOGI(TAG, "mDNS HTTP service registered: _http._tcp port 80"); | ||
| ESP_LOGI(TAG, "Discover with: avahi-browse _http._tcp"); | ||
| } | ||
|
|
||
| ESP_LOGI(TAG, "mDNS/Avahi setup complete - device ready for network discovery"); | ||
| } | ||
|
|
||
| free(hostname); | ||
|
|
||
| ESP_LOGI(TAG, "wifi_init_sta finished."); | ||
|
|
@@ -466,6 +506,22 @@ static const wifi_reason_desc_t wifi_reasons[] = { | |
| {0, NULL}, | ||
| }; | ||
|
|
||
| esp_err_t update_mdns_hostname(const char *new_hostname) { | ||
| if (new_hostname == NULL || strlen(new_hostname) == 0) { | ||
| ESP_LOGW(TAG, "Invalid hostname provided for mDNS update"); | ||
| return ESP_ERR_INVALID_ARG; | ||
| } | ||
|
|
||
| esp_err_t err = mdns_hostname_set(new_hostname); | ||
| if (err != ESP_OK) { | ||
| ESP_LOGW(TAG, "Failed to update mDNS hostname to: %s, error: %s", new_hostname, esp_err_to_name(err)); | ||
| return err; | ||
| } | ||
|
|
||
| ESP_LOGI(TAG, "mDNS hostname updated to: %s", new_hostname); | ||
| return ESP_OK; | ||
| } | ||
|
|
||
| static const char *get_wifi_reason_string(int reason) { | ||
| for (int i = 0; wifi_reasons[i].reason != 0; i++) { | ||
| if (wifi_reasons[i].reason == reason) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.