SOLVED: Bogus data localIP

All,

I have a small problem writing my localIP to teh LCD display. When I write my localIP to the serial output I get the IP address that's configured on my Arduino but if i write it to my LCD i get bogus.

Anyone encountered this before? Thanks...

test code I used:

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
LiquidCrystal lcd(4,5,6,7,8,9);
EthernetClient client;

IPAddress ip (192,168,0,200);
IPAddress gw (192,168,0,1);
IPAddress sm (255,255,255,0);

void setup() {
// put your setup code here, to run once:
Ethernet.begin(mac,ip,sm,gw);
Serial.begin(9600);
lcd.begin(16,2);
delay(1000);
}

void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,1);
lcd.print(Ethernet.localIP());
Serial.println(Ethernet.localIP());
delay(1000);
}

Bogus in what way?

Since Serial and LiquidCrystal both inherit their printing capability from Print they should both act the same. Unless the LiquidCrystal library you are using is not the current Arduino library. Do you have a third-party LiquidCrystal library?

Since Serial and LiquidCrystal both inherit their printing capability from Print they should both act the same.

Yes - but the LCD does not respond to println the same way as the computer monitor.

The println() function appends the ASCII 'control codes' for a carriage return (0x0D) and line feed (0x0A) to a text string. The computer monitor responds as expected but the LCD just displays the characters stored at those locations in it's memory space. Those locations happen to be foldback (redundant) addresses for two of the custom characters, the ones at locations 4 and 1 if I did the calculations properly. Since most users have not programmed any custom characters the result will be two random characters after each string displayed on the LCD.

Don

So if I understand correctly I need to strip the extra chars? But localIP exists of 4 bytes which you can extract individually... So is there a way to parse them to a string so the extra chars are not added?

Or are my brains making wrong turns here? :slight_smile:

Rich

Don,

Thanks for your info it works if you print byte by byte.
Thanks Don..

This is the code that works:

void loop() {
// put your main code here, to run repeatedly:
lcd.clear();
lcd.setCursor(0,0);
for (byte ipbyte = 0; ipbyte <4; ipbyte++)
{
lcd.print(Ethernet.localIP()[ipbyte],DEC);
if (ipbyte < 3)
{
lcd.print(".");
}
}
Serial.println(Ethernet.localIP());
delay(1000);

}

So if I understand correctly I need to strip the extra chars?

If you use lcd.print instead of lcd.println then you shouldn't have to do any testing or stripping.

Also - you really should avoid using lcd.clear if you can, especially in loop(). You usually can accomplish what you need to do by repositioning the cursor and overwriting your old data with the new.

If you do use lcd.clear then you do not have to also position the cursor at (0,0) - that is done as part of the clear operation.

Don

Thanks for the tip Don... I removed it from the code.