How to print IP to TFT LCD

Hi I am using Testdhcp.ino

I can get result in serial output as bellow

[testDHCP]
MAC: 74:69:69:2D:30:31
Setting up DHCP
My IP: 192.168.1.12
Netmask: 255.255.255.0
GW IP: 192.168.1.1
DNS IP: 192.168.1.1

I want to print IP, Netmask ,GW and DNS to TFT

Hear is original source to print to serial.

#include <EtherCard.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];

void setup () {
  Serial.begin(57600);
  Serial.println(F("\n[testDHCP]"));

  Serial.print("MAC: ");
  for (byte i = 0; i < 6; ++i) {
    Serial.print(mymac[i], HEX);
    if (i < 5)
      Serial.print(':');
  }
  Serial.println();
  
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println(F("Failed to access Ethernet controller"));

  Serial.println(F("Setting up DHCP"));
  if (!ether.dhcpSetup())
    Serial.println(F("DHCP failed"));
  
  ether.printIp("My IP: ", ether.myip);
  ether.printIp("Netmask: ", ether.netmask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);
}

void loop () {}

I can not figure it out to print IP.
original it like " ether.printIp("My IP: “, ether.myip);”
i tried like
myGLCD.print("My IP: ", 12, 75);
myGLCD.print(ether.myip,32, 30);

doesn’t work gives me error.
Pl let me know how can i do it???
Thanks in advance

The IP address is very much like the MAC address but 4 bytes instead of 6. You already have code to display the MAC address:

  Serial.print("MAC: ");
  for (byte i = 0; i < 6; ++i) {
    Serial.print(mymac[i], HEX);
    if (i < 5)
      Serial.print(':');
  }
  Serial.println();

Perhaps you can modify that code to display an IP address. I’d start by trying to display the MAC address on the GLCD. Then change it to display only the first four bytes. Then give it a pointer to the IP address instead of the MAC address.

MAC address is static so you can print straight , check my code…

#include <UTFT.h>
#include <EtherCard.h>
extern uint8_t BigFont[];
extern uint8_t SmallFont[];
UTFT    myGLCD(ILI9325D_16,38,39,40,41);
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
int ipAdr  ;
byte Ethernet::buffer[500];

void setup () {
  Serial.begin(57600);
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setContrast(84);
  myGLCD.setFont(BigFont);
  myGLCD.print("Testing DHCP Server", CENTER, 2);
  
  myGLCD.setFont(SmallFont);
  myGLCD.print("MAC : ", 12, 60);
  myGLCD.print("74:69:69:2D:30:31",60, 60);
  myGLCD.setFont(BigFont);
  

if (ether.begin(sizeof Ethernet::buffer, mymac,53) == 0)
       myGLCD.print("NIC Failed", CENTER, 25);
else 
      myGLCD.print("NIC Found", CENTER, 25);
delay(2000);
if (!ether.dhcpSetup())
    myGLCD.print("DHCP Failed", CENTER, 40);
else 
  myGLCD.print("DHCP Found", CENTER, 40);
delay(2000);
  myGLCD.setFont(SmallFont);
  myGLCD.print("My IP: ", 12, 75);
 
 // myGLCD.print(myip,32, 30); //Gives error in all

  myGLCD.print("Netmask: ", 12,90);

//  myGLCD.print(ether.netmask,42, 30);

  myGLCD.print("GW IP: ", 12, 105);

//  myGLCD.print(ether.gwip,52, 30);

  myGLCD.print("DNS IP: ", 12, 120);

//  myGLCD.print(ether.dnsip,62, 30);

  ether.printIp("My IP: ", ether.myip);
  ether.printIp("Netmask: ", ether.netmask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);
  
}

void loop () {
  

}

IP, GW, Subnet, DNS are dynamic , can not print like MAC (sting)

is there any way i can save “ether.myip” to sting and then i can print ???

You COULD use sprintf() to convert the data to a string, and then print the string to the LCD. It helps to be smarter than the problem.

Thanks PaulS

I try that here is my code

 myTemplate = "My IP : %s ";
   sprintf(result, myTemplate, ether.myip );
   myGLCD.print(result,12, 75);

Gives me junk characters on LCD

If you can explain me with example i really appreciate it. I am not from programming background. Thanks in advance

Gives me junk characters on LCD

Well, that's junk you posted, so there is no surprise there. I won't guess what types myTemplate or result are. I won't guess how big result is.

I can't imagine how you think ether.myip, which is an array of bytes, is going to be handled properly as a string.

You need 4 format specifiers and 4 values in the sprintf() call AND you need to post ALL of your code.

here is complete code…

#include <UTFT.h>
#include <EtherCard.h>
extern uint8_t BigFont[];
extern uint8_t SmallFont[];
UTFT    myGLCD(ILI9325D_16,38,39,40,41);
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
char* myTemplate = "";
char result[64] = {};

byte Ethernet::buffer[500];

void setup () {
  
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myGLCD.setFont(BigFont);
  myGLCD.print("Testing DHCP Server", CENTER, 2);
  
  myGLCD.setFont(SmallFont);
  myGLCD.print("MAC : ", 12, 60);
  myGLCD.print("74:69:69:2D:30:31",60, 60);
  myGLCD.setFont(BigFont);
  

if (ether.begin(sizeof Ethernet::buffer, mymac,53) == 0)
       myGLCD.print("NIC Failed", CENTER, 25);
else 
      myGLCD.print("NIC Found", CENTER, 25);
delay(2000);
if (!ether.dhcpSetup())
    myGLCD.print("DHCP Failed", CENTER, 40);
else 
  myGLCD.print("DHCP Found", CENTER, 40);
delay(2000);
  myGLCD.setFont(SmallFont);

   myTemplate = "My IP : %s ";
   sprintf(result, myTemplate, ether.myip );
   myGLCD.print(result,12, 75);

  
   myTemplate = "Netmask : %s ";
   sprintf(result, myTemplate, ether.netmask );
   myGLCD.print (myip,12, 90);
    
  
     myTemplate = "GW IP:  %s ";
   sprintf(result, myTemplate, ether.gwip );
   myGLCD.print(result,12, 105);
  
   
     myTemplate = "DNS IP: %s ";
   sprintf(result, myTemplate, ether.dnsip );
   myGLCD.print(result,12, 120); 

}

void loop () {
  

}
   myTemplate = "My IP : %s ";
   sprintf(result, myTemplate, ether.myip );

The format specifier needs to match the type of the argument to be converted to a string. ether.myip is not a string. There is no format specifier for an array.

If ether.myip is a byte array then this should work:

   myTemplate = "My IP : %d:%d:%d:%d";
   sprintf(result, myTemplate, ether.myip[0],  ether.myip[1],  ether.myip[2],  ether.myip[3]);

If ether.myip is a long then this should work:

   myTemplate = "My IP : %d:%d:%d:%d";
   sprintf(result, myTemplate, (ether.myip>>24)&0x000000FF, (ether.myip>>16)&0x000000FF, (ether.myip>>8)&0x000000FF, ether.myip&0x000000FF);

Thanks a lot

Its working great ..

Thanks again for your help