IP Address Printer

Newbie here experimenting with the DHCP Address Printer sketch example. I'm using an UNO coupled with the W5500 Ethernet shield 2. I connected up a 16x2 LCD w/o the I2C, because of this I thought I'd use UNO pins 6 and 7 instead of the recommended 12 and 11, respectively; as in all the examples I can find, due to the fact that the Ethernet shield uses pins 10, 11, 12 and 13.

I modified the original dhcpAddressPrinter sketch to make it report the IP address in both the COM window and LCD.

 */

// include library codes
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>

int var = 0;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0xA8, 0x61, 0x0A, 0xAE, 0x72, 0x48
};

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 6, en = 7, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(10);  // Most Arduino shields
  
  // 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
  }
  
  {
    lcd.begin(16, 2);                     // Set up the LCD's number of columns and rows
    lcd.clear();                          // Clear the LCD
    lcd.setCursor(0, 0);                  // Set the LCD cursor to column 0, line 0 
    lcd.print("My IP address:");          // Send message to the LCD
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP");
  
  {
    Serial.print("My Local IP Address is");  // Send message to the COM monitor
    {
    var = 0;
    while (var < 3) {
      Serial.print(".");
      delay(500);
      var++;
    }
    Serial.println(".");
    }
  }
  
  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.");
    }
    // no point in carrying on, so do nothing forevermore:
    while (true) {
      delay(1);
    }
    
  }

  {
    Serial.println(Ethernet.localIP());     // Send message to the COM monitor
    lcd.setCursor(0, 1);
    lcd.print(Ethernet.localIP());
  }
}

void loop() {
  switch (Ethernet.maintain()) {
    case 1:
      //renewed fail
      Serial.println("Error: renewed fail");
      break;

    case 2:
      //renewed success
      Serial.println("Renewed success");
      //print your local IP address:
      Serial.print("My IP address: ");
      Serial.println(Ethernet.localIP());
      break;

    case 3:
      //rebind fail
      Serial.println("Error: rebind fail");
      break;

    case 4:
      //rebind success
      Serial.println("Rebind success");
      //print your local IP address:
      Serial.print("My IP address: ");
      Serial.println(Ethernet.localIP());
      break;

    default:
      //nothing happened
      break;
  }
}

The result... Displayed in the COM window:
Initialize Ethernet with DHCP
My Local IP Address is....
192.168.1.4

Displayed in the LCD:
My IP Address:
0.0.0.0

Upon troubleshooting/experimenting... I can only get the IP address to display correctly on the LCD if its the only thing I send it. lcd.setCursor command seems to be causing errors. I found that if I comment out the lcd.setCursor(0, 1); line above I get the following on the LCD:

My IP Address:19 //runs out of room on the display to show the whole IP address.

I am out of things to try... any suggestions?

Update... if I do this:

lcd.setCursor(0, 1);
lcd.print("192.168.1.4");

Displayed on LCD:
My IP Address:
192.168.1.4

Again, not sure whats going on...

Nobody has any ideas to try here? No matter what I've tried... I cannot get the IP address to correctly appear on the second line of the 16x2 LCD unless I explicitly tell it what to print as shown above. Always shows up as 0.0.0.0

Update - Once I print the Ethernet.localIP() to the LCD (192.168.1.4 in my case) and then Serial.print(Ethernet.localIP()) it shows up as 0.0.0.0 None of this is making any sense! Could there be a bug in the LiquidCrystal.h library?

BTW - this is without the I2C adapter.

Hi,
I don't know what is causing your problem.
Yesterday I analyzed your code and found no errors.
Today, due to you informing that you did not receive any help, I decided to set up your project.
I didn't change anything in your assembly or your code and it worked correctly for me.
Follow image.

Thank you so much!! That is weird... the only difference I see in that picture is that I have a different Ethernet shield. Maybe I'll see about getting that version and try again. This issue is making me go crazy because it does not make any sense :slight_smile:

OMG... pulled my hair out for a day on this!! Turns out the W5500 Ethernet Shield 2 I was trying to use did not work correctly. Swapped out with a W5100 and works as expected!!

Could it be a bug in the Ethernet library?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.