Ethernet Hostname: Ethernet 2.

Hello I'm setting up multiple arduino's with Ethernet shields using the Ethernet2 library. Everything is working great. However I'm using Advance ip scanner so i can scan my network for the arduino devices. And what comes up under every device is Atmel Corporation on it. I can not tell what device goes to what. Because I'm using DHCP to get a address. Everything i found so far doesn't work in changing the hostname on the arduino network. Is it possible to change it. So i can name each server in the hostname?

Joseph

You haven't posted your code, but you should be able to put the hostname in the begin call.
From Ethernet library: added ability to set/get hostname by mykh · Pull Request #5701 · arduino/Arduino · GitHub

while (Ethernet.begin(mac, hostname) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    delay(10000);
    Serial.println("Reconnecting...");
  }

Hello Sorry for the late reply. I'm just using the normal arduino Ethernet webserver hello world. Only thing i added to it is the dht22 temperature sensor. Sketch is below. That i have found online a one time.

 http://www.bajdi.com
 Web Server showing the values from a DHT22 sensor
*/

#include <SPI.h>
#include <Ethernet.h>
#include <dht.h>

dht DHT;

#define DHTTYPE DHT22 // Sensor type
#define DHT22_PIN 4 // Data pin of DHT22 sensor

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 153 };

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
Server server(80);

void setup()
{
 
 // start the Ethernet connection and the server:
 Ethernet.begin(mac, ip);
 server.begin();
}

void loop()
{
 int chk = DHT.read22(DHT22_PIN);
 // listen for incoming clients
 Client client = server.available();
 if (client) {
 // an http request ends with a blank line
 boolean currentLineIsBlank = true;
 while (client.connected()) {
 if (client.available()) {
 char c = client.read();
 // if you've gotten to the end of the line (received a newline
 // character) and the line is blank, the http request has ended,
 // so you can send a reply
 if (c == '\n' && currentLineIsBlank) {
 // send a standard http response header
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println();

 // output the value of the DHT22 sensor
 client.print("Arduino powered webserver");
 client.println("
");
 client.print("Serving temperature and humidity values from a DHT22 sensor");
 client.println("
");
 client.print("Temperature (oC): ");
 client.print(DHT.temperature);
 client.println("
");
 client.print("Humidity (%): ");
 client.print(DHT.humidity); 
 client.println("
"); 
 
 break;
 }
 if (c == '\n') {
 // you're starting a new line
 currentLineIsBlank = true;
 } 
 else if (c != '\r') {
 // you've gotten a character on the current line
 currentLineIsBlank = false;
 }
 }
 }
 // give the web browser time to receive the data
 delay(1);
 // close the connection:
 client.stop();
 }
}

If you want to stay with the standard library that appears to set the hostname to WizNetxxx where xxx is the last 3 digits of the MAC address you will have set accept the xxx. If you define your own host name then it will still append the last 3 digits of the mac address. So, for example,
#define HOST_NAME "Neptune"
the hostname you see should be Neptunexxx
The #define will need to come after the #include Ethernet.H but before any executable code.

If you want anything more specific you will have to alter Dhcp.cpp in your ethernet library
or use a different library.

I can't actualy test this at the moment since I have misplaced my Uno with Ethernet shield. I am slightly concerned by your comment that they all appear as "Atmel Corporation" as that suggests that the HOST_NAME is ignored, or possibly you have scanned the network before the sketch has started on the Arduino.

Hello that is the original code before a change a few things. But mostly the sketch is the same.

When i put Ethernet.begin(mac,ip, hostname); i get this error 'hostname' was not declared in this scope. I did look at that site link and saw const char* hostname = "myarduino"; so i have added that and i still get that error message. I have tired adding the #define HOST_NAME "Neptune" and i still ge tthe same eror. But i change the Ethernet.begin(mac,ip, hostname); to Ethernet.begin(mac,ip, HOST_NAME); and i get this error exit status 1
conversion from 'const char [8]' to 'IPAddress' is ambiguous.

I thought you will need
const char[] hostname = "myarduino";

Is this still with the default library, or the one from the site I linked to?

I suspect the last erro message is because it is calling Ethernet.begin(map, IP, DNS IP) by passing "Neptune" as a const char when it is expecting an IP address.

1 Like

Hello, In the past I posted about changing hostnames from whats showing up when I do a ip scan. Because I have a bunch of the same arduinos with the same Ethernet shields they show up all the same. This is from the old post which is closed now and i can not reply back to it https://forum.arduino.cc/t/ethernet-hostname-ethernet-2/671076/4. I tried countrypaul what said to do add const char[] hostname = "myarduino"; to it and I got an error.

Ethernet-ping:5:11: error: decomposition declaration cannot be declared with type 'const char'
 const char[] hostname = "myarduino";

I also added in the setup hostname in the ethernet.begins mac, hostname. I'm not sure what i can do now. I really need help from the community. Does anyone know the correct way to change the hostname so when I scan my network I can see the hostnames? I'm just running the example ethernet sketch with a arduino uno board and a wiznet 5200.

/*
  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi
 
 */

#include <SPI.h>
#include <Ethernet.h>
const char[] hostname = "myarduino";
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // 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("Ethernet WebServer Example");

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, hostname);

  // 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.");
  }

  // start the server
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

Joseph

try:

const char hostname[] = "myarduino";

Or, perhaps

const char *hostname = "myarduino";

I have tried both of them And I get in serial monitor. Failed to obtaining an IP address I'm using DHCP.

That fixes the compiler error you posted in the opening post.

failing to obtain an IP address by DHCP is a different problem - it shows that your code is at least building now, and running on the target.

Do you have a DHCP server in the network that the Arduino is on?

How about set the hostname in the DHCP server....

1 Like

You want fixed IP or dynamic? The above is conflicting.

1 Like

I disable the static ip information. Only let it grab a dhcp. If I disabled the hostname That I added it will grab an Ip address.

only DHCP is coming from the router from my local ISP that is all. I disabled the static ip information. to let it grap a DHCP. If I don't include the hostname it will get an ip address.

Ok, and that's not working. There are some checks in the Ethernet example, could you put those lines in your sketch and rerun?

Edit: I'll insert the code here:

 Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    } else if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }

Which check would they need to be. I mean to comment out and change to make the hostname change and ip work?

Edit: sorry I did not see your edit. I will try that thank you.

I have never done networking and Arduino, but a lot of networking otherwise.

I believe the first check is for a MAC address, if your shield doesn't have one, you have to provide one in the sketch.

I did some skimming through this thread, and:

IP doesn't work that way, that you set the hostname in the device itself. It might work that the device recon itself by the set name, but not the rest of the network. What you need it a local DNS server, that knows about the connection between address and hostname, that will serve that info to a client asking about the hostname.

So the code I suggested is probably redundant, since DHCP in this case works without a set hostname.