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?