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
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.
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.
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.
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.
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.
Thanks for the clarification, looks like I need to do some more reading. I would imagine this could be something implemented in firmware, maybe someone very familiar with zeroconf / bonjour already has some code that does this?
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.
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).