Give Spark Core a Host Name?

When I look at my router DHCP table the only way to identify the Spark Core is by the MAC address because it does not supply the router with a host name.
I am using UDP communications and since the IP address of the core changes from time to time I’d like to connect by host name using the python code …

name = 'mySparkOne'
host = socket.gethostbyname(name)

As you can see in the attached DHCP table the Spark, which for the moment I’ve given the static address 192.168.1.100, has a blank field for Host Name.
Is there anything I can do to give the Spark Core a host name?
Thanks in anticipation

2 Likes

Hi @phec,

You can totally do this! The core advertises itself during listening mode as “CC3000”, if you wanted to set your own name and advertise occasionally in your code, you could do something like:

char my_device_name[] = "SparkyBadger";
void setup() {
	unsigned char idx = 0;
	while (idx < 3)
	{
		mdnsAdvertiser(1,my_device_name,strlen(my_device_name));
		idx++;
	}
}

I’m not 100% sure what your router is looking for here, it might be Netbios. The code I provided will do mDNS, or “Bonjour” which most modern computers can hear and deal with. You’d just want to send out an advertisement packet every few minutes or so, so computers just waking up will hear it.

:smiley:

Thanks,
David

1 Like

Thanks @Dave for the quick response. I’m afraid I didn’t make any progress with the suggestion. I get a compile error when I paste your code because device_name is already in use. (Whether I compile on line or do a clean and compile locally.)

obj/src/spark_wlan.o:(.data+0x3): multiple definition of `device_name'
the_user_app.o:(.data+0x0): first defined here
/opt/gcc_arm/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/bin/ld: Warning: size of symbol `device_name' changed from 13 in the_user_app.o to 7 in obj/src/spark_wlan.o
collect2: error: ld returned 1 exit status
make: *** [fdfcdfd9f15e9bd3d9df7891a5c41350d92b9107f9aa558145422a9b2864.elf] Error 1 ```

In any case my router doesn't pick up the default name CC3000 whether flashing blue, green or breathing cyan so changing the name wouldn't help. It isn't router specific - I've tried 3. 

With Linux if I change the name in /etc/hosts and /etc/hostname or in Windows if I change it in Computer/Properties/Change Settings/Description, the router picks it up.

Anyway it is a minor point - I can stick with fixed IP addresses. Otherwise I continue to be delighted with my Spark and have ordered two more.

Peter

Hi @phec,

Ah sorry about that, you’ll want to change “device_name” to something else. Glad it’s working well for you. :smile:

Thanks,
David

Thanks again @Dave
Yes that runs fine - but as expected it doesn’t seem to help the router or other computers trying to find the host by name with socket.connect(host,port). Never mind. In the meantime my Spark enegy monitor is moving from breadboard to Veroboard tomorrow.
Peter

1 Like

On your Linux machine you need to install an mDNS package. On Windows you can install the Bonjour Printer Wizard from Apple. On Macs everything you need is already in place.

Once you’ve got these going you can find your Core as device_name.local! This should work in Python or the CLI as it supplements the DNS resolver for hosts on the mDNS domain, .local.

mDNS is a zeroconf service and host discovery protocol. Let me know if this works for you. :smile:

I think the data shown by the router is supplied by the device during the DHCP exchange.

The DHCP implementation is baked into the CC3000, so we could only supply a device name if the CC3000 supports that, I will check and report back.

I have only ever seen a core send 3 mDNS packets right after startup, which is only marginally useful as a general case.

However, most routers will let you assign a fixed IP address (still using DHCP) - if you specify the MAC address of the core and the IP address you wish the DHCP server to assign to it every time.

Thanks @timb and @AndyW and @Dave,
I’d not come across mDNS before. It is quite neat for the Raspberry Pi but not needed for that as the router (and C++ and Python) can get at its host name.
I had no joy with getting any response from a ping to the Spark with CC3000.local or the name I gave it in David’s software snippet (repeated every few milliseconds).
As you say AndyW I will stick with the fixed IP addresses which work fine. They just aren’t as memorable as a name.

I’ve just noticed that I can flash code wirelessly even though the core is busy UDPing. That is a very useful improvement. Thank you very much to whoever made that change.

Let me give the mDNS a try!

@Dave Yeah, this isn’t working for me either. I’m on a Mac and it’s not showing up for me.

Sadly, there is no way to supply a device name or identifier to the DHCP code baked into the CC3000.

Right, but the mDNSAdvisor function should send out a zeroconf broadcast and it’s not.

It didn’t work for me either.

There is a lot of discussion over on the TI Support pages from Jan 2014 about mDNS for the CC3000 and how it is only half-baked. Apparently the TI sample code “works” but according to some folks, it does not work in the correct way. I didn’t see anyone saying that mDNS worked great for them, only the opposite.

http://e2e.ti.com/support/low_power_rf/f/851/t/306413.aspx

http://e2e.ti.com/support/low_power_rf/f/851/t/314404.aspx

http://e2e.ti.com/support/low_power_rf/f/851/t/315589.aspx

It’s good reading if you are up for another challenge!

Thanks for the clarification, looks like I need to do some more reading. :slight_smile: I would imagine this could be something implemented in firmware, maybe someone very familiar with zeroconf / bonjour already has some code that does this?

Hi @Dave

Adafruit took a shot at this a few months back:

This would be cool to have, but it seems like much lower priority than lots of other stuff to me.

2 Likes

OK so I read the RFC on mDNS and the nice Adafruit code and I learned a few things.

The protocol requires a UDP port open to listen for mDNS messages at all times. You are supposed to probe for your own name and if someone else is using it, change your name. Only then do you announce your name and IP address over mDNS. If anyone else probes for your name, you have to answer that you have that name already to prevent conflicts. There are some 5 minute back-offs built in for conflicts.

What the TI sample code seems to do is just announce blindly that they have this name and close the connection.

I think for Spark, using up a precious UDP socket on naming via mDNS should not be the default behavior. Maybe this could be an option for some people.

1 Like

Thanks everyone. I agree this is something that is relatively unimportant best left for another day.

Hi All,

After reading this thread, last weekend I decided to work on MDNS sample. I took the Adafruit CC3000_MDNS library and changed two method. 1. Changed MDNSResponder::begin method to accept the IP address as a parameter and 2. Added MDNSResponder::ip2int to convert IP to int. I checked on my Mac and it is working for me. I didn’t check Windows with Bonjour, but I suppose it will work (but not sure).

3 Likes