Skip to content
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

Change the value of hostname #470

Closed
ederle opened this issue Sep 29, 2020 · 8 comments
Closed

Change the value of hostname #470

ederle opened this issue Sep 29, 2020 · 8 comments
Labels
enhancement fixed - please verify Issue has been fixed. Please verify and close.

Comments

@ederle
Copy link

ederle commented Sep 29, 2020

Hi, how to change the value of hostname for ESP32 & 8266 ? The default value is espressif. Is there an API for that ?

Thanks

@phoddie
Copy link
Collaborator

phoddie commented Sep 29, 2020

Hello. I'm not sure what you are referring to by "hostname". Would you be more precise?

FWIW - The Moddable SDK does not assign a hostname to the device. How are you observing that a the hostname is "espressif"?

@ederle
Copy link
Author

ederle commented Sep 30, 2020

hi, thank you for your reply :-)

When i connect my esp32 device that run my moddable app to my dsl box, i see in the devices list connected the device name associated with ip address (DNS) for my eps32 device the name is "espressif".
And when I do a "ping espressif", I have an answer from my device.

I searched for the word "espressif" in the ~ / esp32 / esp-idf folder and found in some C files:

netif-> hostname = "espressif";

But I did not find how to change the value of "hostname ".

Do you know if it's possible to change this value?

Thanks :-)

@phoddie
Copy link
Collaborator

phoddie commented Sep 30, 2020

Thank you for the details.

I never tried to set that hostname on the Espressif devices. I did some investigation and found it can be done using tcpip_adapter_set_hostname. It is necessary to wait for the Wi-Fi interface to come up, which does not happen immediately after calling esp_wifi_start, but it succeeds after receiving the SYSTEM_EVENT_STA_START event.

I've made a small change to the Moddable SDK Wi-Fi code to support setting the hostname in the project manifest's defines section. Here's a trivial example;

"defines": {
	"wifi": {
		"hostname": "#tada"
	}
},

We'll push the change to this repository a little later today.

On a somewhat related note, the Moddable SDK implements mDNS so you can claim a hostname that way (and automatically resolve name conflicts). Here's a basic example:

let hostName = "example"; // the hostName to claim. updated by callback to actual name claimed.
new MDNS({hostName}, function(message, value) {
if (MDNS.hostName === message)
hostName = value;
});

mkellner pushed a commit that referenced this issue Sep 30, 2020
@phoddie phoddie added the fixed - please verify Issue has been fixed. Please verify and close. label Sep 30, 2020
@ederle
Copy link
Author

ederle commented Oct 1, 2020

Thank you very much. I test as soon as possible and I inform you of the result.
Have a nice day :-)

@ederle
Copy link
Author

ederle commented Oct 1, 2020

Some weeks ago, i've tried this code (below) immediately after the wifi connection was ready. It seems working fine but my esp32 device had no hostname on my network.

_claimHostname() {
    if( this._hostname.length>0 ) {
        const mdns = new MDNS({hostName: this._hostname}, (message, value) => {
            switch( message ) {
                case 1:
                    if( this.debug ) {
                        trace(`${Date()}: MDNS - claimed hostname is "${value}"\n`);
                    }
                    break;
                case 2:
                    if( this.debug ) {
                        trace(`${Date()}: MDNS - failed to claim "${value}", try next\n`);
                    }
                    break;
                default:
                    if( message<0 ) {
                        if( this.debug ) {
                            trace("${Date()}: MDNS - failed to claim, give up\n");
                        }
                    }
                    break;
            }
        });
    }
}

I've copied this piece of code from moddable documentation: network / class MDNS.


... and congratulations: this is the second personal project I have done with Moddable SDK and it works really well!

@phoddie
Copy link
Collaborator

phoddie commented Oct 1, 2020

Glad to hear you are having good success with the Moddable SDK. If you decide to make any of your personal projects available, please let me know. It is always interesting to see how people are using the SDK.

Regarding mDNS, I adapted your code slightly and added it at the end of $MODDABLE/examples/network/http/httpget/main.js:

import MDNS from "mdns"
const _hostname = "test";
const debug = true;

const mdns = new MDNS({hostName: _hostname}, (message, value) => {
	switch( message ) {
		case 1:
			if( debug ) {
				trace(`${Date()}: MDNS - claimed hostname is "${value}"\n`);
			}
			break;
		case 2:
			if( debug ) {
				trace(`${Date()}: MDNS - failed to claim "${value}", try next\n`);
			}
			break;
		default:
			if( message<0 ) {
				if( debug ) {
					trace("${Date()}: MDNS - failed to claim, give up\n");
				}
			}
			break;
	}
});

That worked as expected for me. The hostname with mDNS is always in the ".local" domain, so I did this to check it:

> ping test.local
PING test.local (10.0.1.16): 56 data bytes
64 bytes from 10.0.1.16: icmp_seq=0 ttl=128 time=327.895 ms
64 bytes from 10.0.1.16: icmp_seq=1 ttl=128 time=9.557 ms

Not all computers resolve ".local" addresses -- macOS definitely does. For Windows, I believe it requires additional configuration.

@ederle
Copy link
Author

ederle commented Oct 2, 2020

It was my mistake, I did not add .local to the hostname in my pings. I will test again this weekend.
Thanks :-) Have a nice day .

@ederle
Copy link
Author

ederle commented Oct 9, 2020

All works fine (macOS) !
Thanks a lot :-)

@ederle ederle closed this as completed Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement fixed - please verify Issue has been fixed. Please verify and close.
Projects
None yet
Development

No branches or pull requests

2 participants