-
Notifications
You must be signed in to change notification settings - Fork 270
Create option for setting and getting the hostname #223
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Set Host Name | ||
|
||
This example shows you how to set the host name with the Ethernet library. | ||
|
||
Circuit: | ||
* Ethernet shield attached to pins 10, 11, 12, 13 | ||
|
||
created 28 May 2023 | ||
by Attila Herczog | ||
*/ | ||
|
||
#include <SPI.h> | ||
#include <Ethernet.h> | ||
|
||
// Enter a MAC address for your controller below. | ||
// Newer Ethernet shields have a MAC address printed on a sticker on the shield | ||
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | ||
|
||
// Host name to use | ||
char hostName[] = "ExampleHostName"; | ||
|
||
void setup() | ||
{ | ||
// Open serial communications and wait for port to open: | ||
Serial.begin(9600); | ||
while (!Serial) | ||
{ | ||
; // wait for serial port to connect. Needed for native USB port only | ||
} | ||
Serial.println("Host Name Example"); | ||
|
||
// Set the Host Name | ||
// Call this function before Ethernet.begin() to set your host name. | ||
Ethernet.setHostName(hostName); | ||
|
||
// Start the Ethernet connection and the server: | ||
Ethernet.begin(mac); | ||
|
||
// Check for Ethernet hardware present | ||
if (Ethernet.hardwareStatus() == EthernetNoHardware) | ||
{ | ||
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); | ||
while (true) | ||
{ | ||
delay(1); // do nothing, no point running without Ethernet hardware | ||
} | ||
} | ||
if (Ethernet.linkStatus() == LinkOFF) | ||
{ | ||
Serial.println("Ethernet cable is not connected."); | ||
} | ||
|
||
Serial.print("My IP is: "); | ||
Serial.println(Ethernet.localIP()); | ||
Serial.print("My host name is: "); | ||
Serial.println(Ethernet.getHostName()); | ||
Serial.println("You can now check your router's DHCP table to see the assigned host name."); | ||
} | ||
|
||
void loop() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name=Ethernet | ||
version=2.0.2 | ||
version=2.0.3 | ||
author=Various (see AUTHORS file for details) | ||
maintainer=Arduino <[email protected]> | ||
sentence=Enables network connection (local and Internet) using the Arduino Ethernet Board or Shield. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ | |
|
||
IPAddress EthernetClass::_dnsServerAddress; | ||
DhcpClass* EthernetClass::_dhcp = NULL; | ||
bool EthernetClass::_manualHostName = false; | ||
char EthernetClass::_hostName[HOST_NAME_MAX_LEN] = ""; | ||
|
||
int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) | ||
{ | ||
|
@@ -38,8 +40,13 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp | |
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); | ||
SPI.endTransaction(); | ||
|
||
// Generate a default host name based on the MAC address if not already set by user | ||
if(!_manualHostName) { | ||
generateDefaultHostName(mac); | ||
} | ||
|
||
// Now try to get our config info from a DHCP server | ||
int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout); | ||
int ret = _dhcp->beginWithDHCP(mac, _hostName, timeout, responseTimeout); | ||
if (ret == 1) { | ||
// We've successfully found a DHCP server and got our configuration | ||
// info, so set things accordingly | ||
|
@@ -224,6 +231,32 @@ void EthernetClass::setRetransmissionCount(uint8_t num) | |
SPI.endTransaction(); | ||
} | ||
|
||
void EthernetClass::generateDefaultHostName(uint8_t *mac) { | ||
// Copy the default host name base | ||
strcpy(_hostName, DEFAULT_HOST_NAME); | ||
|
||
// Append the last 3 bytes of the MAC (HEX'd) | ||
char macAddrStr[3]; | ||
sprintf(macAddrStr, "%02X", mac[3]); | ||
strcat(_hostName, macAddrStr); | ||
sprintf(macAddrStr, "%02X", mac[4]); | ||
strcat(_hostName, macAddrStr); | ||
sprintf(macAddrStr, "%02X", mac[5]); | ||
strcat(_hostName, macAddrStr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be reduced to one line?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. I changed a bit on your suggestion to include the default hostname as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The report from the Github bot shows change in Before:
After:
A very welcome change :) Now I wonder what is adding over 1KB of memory? 🤔 It seems like a lot. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the 1KB increase be explained by the compiler deciding to inline some functions on its own? I've seen that before when LTO is enabled (I'm not sure if that's the case in the default Arduino builds). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I refactored this again. This time I removed the usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks correct to me. 👍 |
||
} | ||
|
||
void EthernetClass::setHostName(const char *dhcpHost) { | ||
// Copy the host name and ensure it is null terminated | ||
strncpy(_hostName, dhcpHost, HOST_NAME_MAX_LEN); | ||
_hostName[HOST_NAME_MAX_LEN - 1] = '\0'; | ||
|
||
// Indicate that a host name has been set manually | ||
_manualHostName = true; | ||
} | ||
|
||
char* EthernetClass::getHostName() { | ||
return _hostName; | ||
} | ||
|
||
|
||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.